Starting from an image, I would like to shift its content upward of 10 pixels, without changing size and filling in black the sub image (width x 10px
) on the bottom.
For instance, the original:
And the shifted:
Is there any function to perform this operation with OpenCV?
You can simply use affine transformation translation matrix (which is for shifting points basically). cv::warpAffine() with proper transformation matrix will do the trick. where: tx is shift in the image x axis, ty is shift in the image y axis, Every single pixel in the image will be shifted like that.
You can simply use affine transformation translation matrix (which is for shifting points basically). cv::warpAffine()
with proper transformation matrix will do the trick.
where: tx is shift in the image x axis, ty is shift in the image y axis, Every single pixel in the image will be shifted like that.
You can use this function which returns the translation matrix. (That is probably unnecessary for you) But it will shift the image based on offsetx
and offsety
parameters.
Mat translateImg(Mat &img, int offsetx, int offsety){ Mat trans_mat = (Mat_<double>(2,3) << 1, 0, offsetx, 0, 1, offsety); warpAffine(img,img,trans_mat,img.size()); return img; }
In your case - you want to shift image 10 pixels up, you call:
translateImg(image,0,-10);
And then your image will be shifted as you desire.
Is there a function to perform directly this operation with OpenCV?
https://github.com/opencv/opencv/issues/4413 (previously http://web.archive.org/web/20170615214220/http://code.opencv.org/issues/2299)
or you would do this
cv::Mat out = cv::Mat::zeros(frame.size(), frame.type()); frame(cv::Rect(0,10, frame.cols,frame.rows-10)).copyTo(out(cv::Rect(0,0,frame.cols,frame.rows-10)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With