From 2cfbcc881b26cf40ed350adfa54f1d2e6888ac01 Mon Sep 17 00:00:00 2001 From: johan Date: Sun, 12 Jan 2014 20:27:19 +0100 Subject: [PATCH] Added cropping of the input picture Former-commit-id: 0dbb042c39dbdf841f6003391bf21f3f548433ee --- test/v4l2_to_png/V4L2Grabber.cpp | 12 +++++++----- test/v4l2_to_png/V4L2Grabber.h | 4 +++- test/v4l2_to_png/v4l2_to_png.cpp | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/test/v4l2_to_png/V4L2Grabber.cpp b/test/v4l2_to_png/V4L2Grabber.cpp index f278a432..af1a3e9f 100644 --- a/test/v4l2_to_png/V4L2Grabber.cpp +++ b/test/v4l2_to_png/V4L2Grabber.cpp @@ -37,13 +37,15 @@ static void yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t & r, uint8_t & g, u } -V4L2Grabber::V4L2Grabber(const std::string &device, int input, VideoStandard videoStandard, int frameDecimation, int pixelDecimation) : +V4L2Grabber::V4L2Grabber(const std::string &device, int input, VideoStandard videoStandard, int cropHorizontal, int cropVertical, int frameDecimation, int pixelDecimation) : _deviceName(device), _ioMethod(IO_METHOD_MMAP), _fileDescriptor(-1), _buffers(), _width(0), _height(0), + _cropWidth(cropHorizontal), + _cropHeight(cropVertical), _frameDecimation(std::max(1, frameDecimation)), _pixelDecimation(std::max(1, pixelDecimation)), _currentFrame(0) @@ -590,14 +592,14 @@ void V4L2Grabber::process_image(const uint8_t * data) { std::cout << "process image" << std::endl; - int width = (_width + _pixelDecimation/2) / _pixelDecimation; - int height = (_height + _pixelDecimation/2) / _pixelDecimation; + int width = (_width - 2 * _cropWidth + _pixelDecimation/2) / _pixelDecimation; + int height = (_height - 2 * _cropHeight + _pixelDecimation/2) / _pixelDecimation; Image image(width, height); - for (int ySource = _pixelDecimation/2, yDest = 0; ySource < _height; ySource += _pixelDecimation, ++yDest) + for (int ySource = _cropHeight + _pixelDecimation/2, yDest = 0; ySource < _height - _cropHeight; ySource += _pixelDecimation, ++yDest) { - for (int xSource = _pixelDecimation/2, xDest = 0; xSource < _width; xSource += _pixelDecimation, ++xDest) + for (int xSource = _cropWidth + _pixelDecimation/2, xDest = 0; xSource < _width - _cropWidth; xSource += _pixelDecimation, ++xDest) { int index = (_width * ySource + xSource) * 2; uint8_t y = data[index+1]; diff --git a/test/v4l2_to_png/V4L2Grabber.h b/test/v4l2_to_png/V4L2Grabber.h index 1b23e761..c028e745 100644 --- a/test/v4l2_to_png/V4L2Grabber.h +++ b/test/v4l2_to_png/V4L2Grabber.h @@ -19,7 +19,7 @@ public: }; public: - V4L2Grabber(const std::string & device, int input, VideoStandard videoStandard, int frameDecimation, int pixelDecimation); + V4L2Grabber(const std::string & device, int input, VideoStandard videoStandard, int cropHorizontal, int cropVertical, int frameDecimation, int pixelDecimation); virtual ~V4L2Grabber(); void start(); @@ -77,6 +77,8 @@ private: int _width; int _height; + const int _cropWidth; + const int _cropHeight; const int _frameDecimation; const int _pixelDecimation; diff --git a/test/v4l2_to_png/v4l2_to_png.cpp b/test/v4l2_to_png/v4l2_to_png.cpp index 29932578..7f418018 100644 --- a/test/v4l2_to_png/v4l2_to_png.cpp +++ b/test/v4l2_to_png/v4l2_to_png.cpp @@ -57,6 +57,8 @@ int main(int argc, char** argv) StringParameter & argDevice = parameters.add ('d', "device", "The device to use [default=/dev/video0]"); VideoStandardParameter & argVideoStandard = parameters.add('v', "video-standard", "The used video standard. Valid values are PAL. NYSC, or NO-CHANGE [default=PAL]"); IntParameter & argInput = parameters.add ('i', "input", "Input channel [default=0]"); + IntParameter & argCropWidth = parameters.add (0x0, "crop-width", "Number of pixels to crop from the left and right sides in the picture before decimation [default=0]"); + IntParameter & argCropHeight = parameters.add (0x0, "crop-height", "Number of pixels to crop from the top and the bottom in the picture before decimation [default=0]"); IntParameter & argSizeDecimation = parameters.add ('s', "size-decimator", "Decimation factor for the output size [default=1]"); SwitchParameter<> & argHelp = parameters.add > ('h', "help", "Show this help message and exit"); @@ -64,6 +66,8 @@ int main(int argc, char** argv) argDevice.setDefault("/dev/video0"); argVideoStandard.setDefault(V4L2Grabber::PAL); argInput.setDefault(0); + argCropWidth.setDefault(0); + argCropHeight.setDefault(0); argSizeDecimation.setDefault(1); // parse all options @@ -80,6 +84,8 @@ int main(int argc, char** argv) argDevice.getValue(), argInput.getValue(), argVideoStandard.getValue(), + std::max(0, argCropWidth.getValue()), + std::max(0, argCropHeight.getValue()), 1, argSizeDecimation.getValue());