ranger
ranger

Reputation: 496

Flipping an image to get mirror effect

I am working on a video processing project which needs some flipping of frame. I tried using cvFlip but doesnt seem to flip along y axis (x axis working...) and results in segmentation fault. Is there any other option??

cv::Mat dst=src;      //src= source image from cam
cv::flip(dst, dst, 1);     //segmentation fault shown

imshow("flipped",dst);

Upvotes: 16

Views: 54470

Answers (4)

AndrewBloom
AndrewBloom

Reputation: 2408

At least in openCV 4.8, you can flip in place (with both cv::Mat and cv::UMat).

A possible sample code:

// buffer is a pointer to data, for example:
// uint8_t* buffer = new uint8_t[w*h*4]
// CV_8UC4 means unsigned byte, 4 channel image (for example RGBA)
// UMat allows to use GPU acceleration, it may be useful or not depending
// on the scenario, or you can simply just use cv::Mat.
cv::UMat m = cv::Mat(h , w, CV_8UC4, buffer).getUMat(cv::ACCESS_READ);
// flip accepts a flag that can be 0, <0 or >0
// here I put zero cause if the image is taken in landscape mode, although // looks like it needs vertical mirroring, it is actually horizontal. So you // may want to try the various options if 1 doesn't work as expected
flip(m, m, 0);

Upvotes: 1

Eric
Eric

Reputation: 61

The key is to create the dst exactly like the src:

cv::Mat dst = cv::Mat(src.rows, src.cols, CV_8UC3);
cv::flip(src, dst, 1);

imshow("flipped", dst);

Upvotes: 6

juanchopanza
juanchopanza

Reputation: 227370

Use cv::flip and pass 1 as flipcode.

Looking at your edit with the sample code, you cannot flip in place. You need a separate destination cv::Mat:

cv::Mat dst;
cv::flip(src, dst, 1);
imshow("flipped",dst);

Upvotes: 9

berak
berak

Reputation: 39796

cv::Mat src=imload("bla.png");
cv::Mat dst;               // dst must be a different Mat
cv::flip(src, dst, 1);     // because you can't flip in-place (leads to segfault)

Upvotes: 16

Related Questions