1 | initial version |
First, don't use functions starting with cvSomething. Those functions belong to C interface of OpenCV 1.0 and will be deprecated in future (maybe even near future). Use C++ interface of those functions. In your case it will be:
rectangle(targetMat, rectangle, Scalar(255,0,0), CV_FILLED);
Second, it is weird that you are getting red or white colors. I checked the function and got blue rectangle, as it is supposed to be (don't forget that OpenCV works with BGR color scheme so Scalar(255,0,0) means blue color). Are there any other draw functions you apply to the same image?
2 | No.2 Revision |
First, don't use functions starting with cvSomething. Those functions belong to C interface of OpenCV 1.0 and will be deprecated in future (maybe even near future). Use C++ interface of those functions. In your case it will be:
rectangle(targetMat, rectangle, yourRect, Scalar(255,0,0), CV_FILLED);
Second, it is weird that you are getting red or white colors. I checked the function and got blue rectangle, as it is supposed to be (don't forget that OpenCV works with BGR color scheme so Scalar(255,0,0) means blue color). Are there any other draw functions you apply to the same image?
3 | No.3 Revision |
First, don't use functions starting with cvSomething. Those functions belong to C interface of OpenCV 1.0 and will be deprecated in future (maybe even near future). Use C++ interface of those functions. In your case it will be:
rectangle(targetMat, yourRect, Scalar(255,0,0), CV_FILLED);
Second, it is weird that you are getting red or white colors. I checked the function and got blue rectangle, as it is supposed to be (don't forget that OpenCV works with BGR color scheme so Scalar(255,0,0) means blue color). Are there any other draw functions you apply to the same image?
Edit (to answer the comments):
I afraid there some sort of problem with almost every line of your code. Lets discuss them one by one:
Are you completely sure that you really have/need alpha channel? Working with 3 channels (RGB) is more common.
You are using Canny edge detection with equal lower and higher thresholds. It will work but this is sort of waste of capabilities of Canny.
Output of Canny is binary image. Value 255 for edges and value 0 for non-edges. Applying Gaussian smoothing is legal (you can apply it to any image) but it is not "correct". Maybe you want to apply smoothing prior to edge detection, are you? findContours that you use after smoothing treats every non-zero pixel as edge so the effect of Gaussian smoothing in this case is dilation of polygons. If dilation was indeed your intention you should use dilate function instead.
Are you sure that one of the contours you extracted by findContours is whole rectangle? Consider for example that one of the pixels along the edge of rectangle wasn't considered "edge" by Canny because its gradient was too low, i.e. edges are not continuous. Than the object that will be found by findContours will be thin long "snake" along the edge of rectangle. If you will try to draw it by drawContours, it will draw thin red object along the boundary of rectangle. I guess this may be the source of your problem. If yes, you may consider using convexHull function on output of findContours.
You don't need to copy contours to other vectors (temp_contour) in order to find their area. Just run over original vector:
for(int i=0; i < contours.size(); i++) if (contourArea(contour) > maxArea) // do something
You are trying to draw on image that is not empty to begin with (it is copy of original image). Instead allocate clean image (of appropriate size and type of course) and draw the polygon on it. This will let you see where the problem lies.