Added cropping of the input picture

Former-commit-id: 0dbb042c39dbdf841f6003391bf21f3f548433ee
This commit is contained in:
johan 2014-01-12 20:27:19 +01:00
parent f90f076ca4
commit 2cfbcc881b
3 changed files with 16 additions and 6 deletions

View File

@ -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<ColorRgb> 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];

View File

@ -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;

View File

@ -57,6 +57,8 @@ int main(int argc, char** argv)
StringParameter & argDevice = parameters.add<StringParameter> ('d', "device", "The device to use [default=/dev/video0]");
VideoStandardParameter & argVideoStandard = parameters.add<VideoStandardParameter>('v', "video-standard", "The used video standard. Valid values are PAL. NYSC, or NO-CHANGE [default=PAL]");
IntParameter & argInput = parameters.add<IntParameter> ('i', "input", "Input channel [default=0]");
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]");
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]");
IntParameter & argSizeDecimation = parameters.add<IntParameter> ('s', "size-decimator", "Decimation factor for the output size [default=1]");
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<> > ('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());