0

i have this image

orginal image

i want to create a tranigle mask to get only this zone

zone

but with the following code i get this result

result

Moments mu = moments(red,true);
Point center;
center.x = mu.m10 / mu.m00;
center.y = mu.m01 / mu.m00;
circle(red, center, 2, Scalar(0, 0, 255));
cv::Size sz = red.size();
int imageWidth = sz.width;
int imageHeight = sz.height;
Mat mask3(red.size(), CV_8UC1, Scalar::all(0));
// Create Polygon from vertices
vector<Point> ptmask3(3);
ptmask3.push_back(Point(imageHeight-1, imageWidth-1));
ptmask3.push_back(Point(center.x, center.y));
ptmask3.push_back(Point(0, red.rows - 1));

vector<Point> pt;

approxPolyDP(ptmask3, pt, 1.0, true);

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

// Create new image for result storage
Mat hide3(red.size(), CV_8UC3);


// Cut out ROI and store it in imageDest
red.copyTo(hide3, mask3);

imshow("mask3", hide3);
slawekwin
  • 6,270
  • 1
  • 44
  • 57
  • 1
    Please, provide a proper [mcve] (and remember this in the future, so we don't need to remind on every question). Look at it this way -- you're asking someone to help you for free, yet as first thing you expect them to waste time filling in all the blanks (that you could easily have provided), so they can even compile the code. Furthermore this often involves a lot of guessing, so we may not even end up with the same thing that you have -- getting a correct answer based on that is akin to shooting in the dark. – Dan Mašek Oct 02 '18 at 19:33
  • 1
    Obvious mistake: `std::vector ptmask3(3);` creates vector with 3 points of value `(0,0)`. Then you `push_back` 3 additional points, for the total of 6. Since you're talking about triangles, you probably just want the 3 points you pushed_back, not the default constructed ones (which in most cases will turn your triangle into a quadrilateral). – Dan Mašek Oct 02 '18 at 19:42
  • @DanMašek sorry but it's the first time i ask questions here , thank you so much , i fix it – Yasmine Mrd Oct 04 '18 at 05:24
  • No problem. Make sure to add the part of your code that creates loads the input and calculates `red`. That's the most significant bit that's missing (other than includes, `using namespace` statements -- even though I'd [rather not see those](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and beginning/end of `main`.) – Dan Mašek Oct 04 '18 at 08:59

1 Answers1

0

Updated Version (with the Help of Dan Mašek)

Your Triangle is wrong

This is because you're initializing the vector with size 3, then putting another three points into it, for a total of 6 points of which three have default values. Try this instead:

vector<Point> ptmask3;

Also, make sure that the coordinates of the points are correct. You'll want to have a point in the bottom left corner, but it doesn't seem like your current triangle has one like that.

Your image is gray

You need to initialize hide3 properly, like this:

cv::Mat hide3(img.size(), CV_8UC3, cv::Scalar(0));
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 1
    OP is calling `fillConvexPoly` on a single channel input image, drawing with intensity of `255` -- that's as white a rectangle as you can get, and given that it's meant to be a mask that selects the corresponding ROI, it's correct. | A `cv::Scalar` will be implicitly constructed from an integer (OP's code is equivalent of using `cv::Scalar(255,0,0,0)`, which is just what is needed here). – Dan Mašek Oct 02 '18 at 19:48
  • 1
    Part of the output image is gray simply because `hide3` hasn't had its values initialized to any specific value. A simple change to `cv::Mat hide3(img.size(), CV_8UC3, cv::Scalar(0));` solves that problem. Using a single channel mask with a multi-channel image is perfectly fine in this case. – Dan Mašek Oct 02 '18 at 19:57
  • 1
    I'm also pretty sure that the missing area is due to the extra points added to `ptmask3`, as per my second comment on the question. | Sorry to tear you answer apart like this :) Feel free to update it based on my comments, as I'm not writing anything up until there's a proper [mcve] available. – Dan Mašek Oct 02 '18 at 20:01
  • @DanMašek Thanks for you input. Looks like my OpenCV knowledge is more rusty than I though! I also don't have any installation of it accessible right now, but as you mentioned, there's no complete example to reproduce the issue anyway. Looks like I learned a thing or three today. :) – Blaze Oct 03 '18 at 06:49