Ask Your Question
0

best way for detecting contour

asked 2013-09-30 20:42:05 -0600

nakano gravatar image

Hello Everyone,

I have some trouble for detecting correctly contour of a square image and fill it with a specific color.

I've started to explain a little in a previous post (http://answers.opencv.org/question/21603/fill-contour-with-drawcontours/) but it's better I explain in more detail in this specific post.

First, I prefer to tell you now that for me openCV is really a powerful framework but it's also really hard for me to use it. So far I've used and tried to adapt source code of other person which explain that my current source code isn't clean (which result of a strange behavior for what I'm trying to do) so please explain me in detail :)

What I'm trying to do is to used cvVideoCamera on iOS (I also have to do it on Android but I'll be able to do it by adapting the iOS version on my own) and detecting square image (my image is embedded in a black rectangle). once I've detected the square I would like to fill this rectangle (so in any case I have to use the function drawContours and use CV_FILLED parameter) in a specific color with an alpha for letting the user be able to still see the image behind my drawing rectangle.

The purpose of the function is to show a rectangle on a detected square until I have a match with a reference image.

I've already done the function for testing if I have a match with my reference image (I guess this function is not clean too...) but I don't understand what I can do in order to achieve the function explained above.

I've heard about dilate and convexHull function but I don't know/understand what I need and how to proceed to achieve it.

Could you please explain/ show me how to do it?

I really thank you for your help

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-10-03 03:52:34 -0600

updated 2013-10-03 03:53:32 -0600

// Only accept CV_8UC1
// binImage must be the image with your rectangle detected
if (binImage.channels() != 1 || frame.type() != CV_8U)
    return;

destframe = Mat::zeros(binImage.size(), binImage.type()); //create a mat to accumulate the results

// Find all contours

vector<vector<Point>> contours;
findContours(binImag.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //will find all 

for (int i = 0; i < contours.size(); i++)
{

    // Paint all contours
        drawContours(destframe, contours, i, CV_RGB(0,0,255), -1);
}
edit flag offensive delete link more

Comments

Hello,

Thank you for your comment :) I didn't tried to launch your code yet but as I can seen, you're drawing all rectangle in a black screen right? (due to CV_8U1). Isn't it possible to draw these rectangle in a mat image from the camera mat image? So far I can display a black screen with a colored rectangle followinf this url (http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html) but now I don't know how to show the colored rectangle in my camera mat image :S

In any case I'll study your source code ^-^ again thank you for your help :)

nakano gravatar imagenakano ( 2013-10-03 04:06:25 -0600 )edit

Easy

OriginalImage.setTo(0, binarizedImageWithRectangle) --> set all pixels common with the rectangle to 0. FinalImage = OriginalImage + ImageWithPaintedRectangle

Pedro Batista gravatar imagePedro Batista ( 2013-10-03 05:47:06 -0600 )edit

Or alternatively, OriginalImage.setTo(Scalar(0,255,0), binarizedImageWithRectangle) --> all pixels corresponding to the rectangle in the original image will change color to green

Pedro Batista gravatar imagePedro Batista ( 2013-10-03 05:53:24 -0600 )edit
0

answered 2013-10-02 11:48:04 -0600

Can I assume you already found the rectangle and only need to paint it some color?

If so, check this link, where you'll find code that does exactly what you want.

http://areshopencv.blogspot.pt/2011/12/blob-detection-connected-component-pure.html

edit flag offensive delete link more

Comments

Hello,

Thank you for your help

In fact I found code source for finding contour but this code isn't correct and clean. As you can in the link of my previous post where I post the source code I'm using one told me I had error on almost every line and that may be the reason I fail to fill correctly the contour of a square.

In any case I'll check the link you gave.

If it doesn't bother you, could you please explain me/ show me the good way to detect contour?

nakano gravatar imagenakano ( 2013-10-02 20:13:00 -0600 )edit

I've tested both original code and the code in the link but in the findBlobs function my blob size is always 0.

(I've converted my Mat image to a binary image using the answer in the post : http://answers.opencv.org/question/4423/how-to-create-a-binary-image-mat/ )

Here is the findBlob function:

blobs.clear();

cv::Mat label_image; binary.convertTo(label_image, CV_32SC1);

int label_count = 2;

for(int y=0; y < label_image.rows; y++) {

int row = (int)label_image.ptr(y);

for(int x=0; x < label_image.cols; x++) {

if(row[x] != 1) continue;

cv::Rect rect; cv::floodFill(label_image, cv::Point(x,y), label_count, &rect, 0, 0, 4);

[...]

}

}

when I called this function I never enter to the part cv::floodFill(); with the blobs.push_back(blob)

nakano gravatar imagenakano ( 2013-10-02 21:43:21 -0600 )edit

Question Tools

Stats

Asked: 2013-09-30 20:42:05 -0600

Seen: 1,942 times

Last updated: Oct 03 '13