Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 0dbb042

Browse files
committed
Added cropping of the input picture
1 parent ea4c1a6 commit 0dbb042

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

test/v4l2_to_png/V4L2Grabber.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ static void yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t & r, uint8_t & g, u
3737
}
3838

3939

40-
V4L2Grabber::V4L2Grabber(const std::string &device, int input, VideoStandard videoStandard, int frameDecimation, int pixelDecimation) :
40+
V4L2Grabber::V4L2Grabber(const std::string &device, int input, VideoStandard videoStandard, int cropHorizontal, int cropVertical, int frameDecimation, int pixelDecimation) :
4141
_deviceName(device),
4242
_ioMethod(IO_METHOD_MMAP),
4343
_fileDescriptor(-1),
4444
_buffers(),
4545
_width(0),
4646
_height(0),
47+
_cropWidth(cropHorizontal),
48+
_cropHeight(cropVertical),
4749
_frameDecimation(std::max(1, frameDecimation)),
4850
_pixelDecimation(std::max(1, pixelDecimation)),
4951
_currentFrame(0)
@@ -590,14 +592,14 @@ void V4L2Grabber::process_image(const uint8_t * data)
590592
{
591593
std::cout << "process image" << std::endl;
592594

593-
int width = (_width + _pixelDecimation/2) / _pixelDecimation;
594-
int height = (_height + _pixelDecimation/2) / _pixelDecimation;
595+
int width = (_width - 2 * _cropWidth + _pixelDecimation/2) / _pixelDecimation;
596+
int height = (_height - 2 * _cropHeight + _pixelDecimation/2) / _pixelDecimation;
595597

596598
Image<ColorRgb> image(width, height);
597599

598-
for (int ySource = _pixelDecimation/2, yDest = 0; ySource < _height; ySource += _pixelDecimation, ++yDest)
600+
for (int ySource = _cropHeight + _pixelDecimation/2, yDest = 0; ySource < _height - _cropHeight; ySource += _pixelDecimation, ++yDest)
599601
{
600-
for (int xSource = _pixelDecimation/2, xDest = 0; xSource < _width; xSource += _pixelDecimation, ++xDest)
602+
for (int xSource = _cropWidth + _pixelDecimation/2, xDest = 0; xSource < _width - _cropWidth; xSource += _pixelDecimation, ++xDest)
601603
{
602604
int index = (_width * ySource + xSource) * 2;
603605
uint8_t y = data[index+1];

test/v4l2_to_png/V4L2Grabber.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class V4L2Grabber
1919
};
2020

2121
public:
22-
V4L2Grabber(const std::string & device, int input, VideoStandard videoStandard, int frameDecimation, int pixelDecimation);
22+
V4L2Grabber(const std::string & device, int input, VideoStandard videoStandard, int cropHorizontal, int cropVertical, int frameDecimation, int pixelDecimation);
2323
virtual ~V4L2Grabber();
2424

2525
void start();
@@ -77,6 +77,8 @@ class V4L2Grabber
7777

7878
int _width;
7979
int _height;
80+
const int _cropWidth;
81+
const int _cropHeight;
8082
const int _frameDecimation;
8183
const int _pixelDecimation;
8284

test/v4l2_to_png/v4l2_to_png.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,17 @@ int main(int argc, char** argv)
5757
StringParameter & argDevice = parameters.add<StringParameter> ('d', "device", "The device to use [default=/dev/video0]");
5858
VideoStandardParameter & argVideoStandard = parameters.add<VideoStandardParameter>('v', "video-standard", "The used video standard. Valid values are PAL. NYSC, or NO-CHANGE [default=PAL]");
5959
IntParameter & argInput = parameters.add<IntParameter> ('i', "input", "Input channel [default=0]");
60+
IntParameter & argCropWidth = parameters.add<IntParameter> (0x0, "crop-width", "Number of pixels to crop from the left and right sides in the picture before decimation [default=0]");
61+
IntParameter & argCropHeight = parameters.add<IntParameter> (0x0, "crop-height", "Number of pixels to crop from the top and the bottom in the picture before decimation [default=0]");
6062
IntParameter & argSizeDecimation = parameters.add<IntParameter> ('s', "size-decimator", "Decimation factor for the output size [default=1]");
6163
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<> > ('h', "help", "Show this help message and exit");
6264

6365
// set defaults
6466
argDevice.setDefault("/dev/video0");
6567
argVideoStandard.setDefault(V4L2Grabber::PAL);
6668
argInput.setDefault(0);
69+
argCropWidth.setDefault(0);
70+
argCropHeight.setDefault(0);
6771
argSizeDecimation.setDefault(1);
6872

6973
// parse all options
@@ -80,6 +84,8 @@ int main(int argc, char** argv)
8084
argDevice.getValue(),
8185
argInput.getValue(),
8286
argVideoStandard.getValue(),
87+
std::max(0, argCropWidth.getValue()),
88+
std::max(0, argCropHeight.getValue()),
8389
1,
8490
argSizeDecimation.getValue());
8591

0 commit comments

Comments
 (0)