Ask Your Question
1

OpenCV: Masking Operation Does not Function Properly

asked 2015-10-27 06:21:23 -0600

frageDE gravatar image

updated 2015-10-27 10:57:36 -0600

I have an image which I want to crop, for this I am using masking operation with copyTo() function. Here is the code block that does the operation:

// ROI by creating mask for the trapezoid
Mat mask = Mat(frame.rows, frame.cols, CV_8UC1, Scalar(0));

// Create Polygon from vertices
approxPolyDP(pointsForTrapezoid, roiPolygonized, 1.0, true);

// Fill polygon white
fillConvexPoly(mask, &roiPolygonized[0], roiPolygonized.size(), 255, 8, 0);

// Create new image for result storage
Mat maskedImage = Mat(frame.rows, frame.cols, CV_8UC3);
frame.copyTo(maskedImage, mask);
return maskedImage;

However, there is something really weird with this. I get different outputs from each run. Sometimes it works and sometimes it does not. Let me explain with snapshots:

This is the correct mask which I generate:

enter image description here

This is the correctly applied mask, after the operation:

enter image description here

And these are the ridiculously applied masks, after the operations:

enter image description here enter image description here enter image description here

As you can see, sometimes the masking operation works, and sometimes it does not. I don't know what the hell is wrong with OpenCV, but this shouldn't happen. Same code with same input should not create different output on each run. I suspect that copyTo() function is messed up.

Any thoughts?

edit retag flag offensive close merge delete

Comments

1

I suppose it is normal: you are copying a 4 channel Mat to a 3 channel Mat. Be sure your frame has 3 channels: if (frame.channel() != 3) { std::cout << "not 3 channel" << std::endl; return cv::Mat; }

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-27 07:23:13 -0600 )edit
1

looks like leftover memory to me. again, it does not copy, where the mask is 0

try to put zeros into your maskedImage before

berak gravatar imageberak ( 2015-10-27 08:40:52 -0600 )edit
2

Mat::zero() is a static function, that returns a new image. (so, that had no effect)

try : Mat maskedImage = Mat(frame.rows, frame.cols, CV_8UC3, Scalar::all());

(easier to read/understand)

berak gravatar imageberak ( 2015-10-27 10:06:42 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-10-27 09:51:29 -0600

frageDE gravatar image

updated 2015-10-27 10:09:20 -0600

@berak You were right. I populated the matrix with zeros before feeding into the copyTo() function, and it worked. No ridiculous output anymore. Thanks.

Mat maskedImage = Mat::zeros(frame.rows, frame.cols, CV_8UC3);
frame.copyTo(maskedImage, mask);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-10-27 06:21:23 -0600

Seen: 956 times

Last updated: Oct 27 '15