Ask Your Question
1

tracking boundary using opencv

asked 2015-05-04 23:59:00 -0500

NabeelKhan gravatar image

I am trying to track/ trace boundary points in sequence from the following binary image:image description

I am using OpenCV (python). I am using three ways:

  1. Apply Canny edge detection to detect edge. Problem is how to get the sequence of points? It's work fine but it's very hard to get the sequence of boundary points
  2. Ideal option is to detect contours on the binary image. Because contours return the boundary points in sequence. But openCV contour method is not detecting the boundary as shown in the results. Why is this happening?
  3. Detect contours on the Canny edge. Still some boundary is missed ?? image description

Can anyone help me what's going on with OpenCV contours? Why it is not tracking the complete boundary. I am detecting contours as follows:

contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE ,cv2.CHAIN_APPROX_SIMPLE)

where thresh1 is the binary image

edit retag flag offensive close merge delete

Comments

1

how did you print the points of contours in the above, second in the row image? I think you have done something wrong. If you could provide some more code, that might help :-).

theodore gravatar imagetheodore ( 2015-05-05 06:05:22 -0500 )edit

See related question here.

Haris gravatar imageHaris ( 2015-05-05 10:01:06 -0500 )edit

My code is :

seg= cv2.imread(eachImg) # reading binary image blurred= cv2.medianBlur(seg,3) contours, hierarchy = cv2.findContours(blurred,cv2.RETR_TREE ,cv2.CHAIN_APPROX_SIMPLE)

NabeelKhan gravatar imageNabeelKhan ( 2015-05-05 15:26:51 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-05-05 06:01:30 -0500

theodore gravatar image

updated 2015-05-05 17:20:42 -0500

why not use the findNonZero() function. The following code seems to give the sequence of the points from top-bottom, left-right:

Mat cn;
Canny(src, cn, 100, 255);
imshow("canny", cn);

vector<Point> pnts;
findNonZero(cn, pnts);

cout << "Total number of points: " << pnts.size() << endl;

for(size_t i = 0; i < pnts.size(); ++i)
    cout << pnts[i] << endl;

edit: update

then if you still want to use the contour properties you will need to use the CV_CHAIN_APPROX_NONE flag instead of the CV_CHAIN_APPROX_SIMPLE one that you are using. According to the documentation:

CV_CHAIN_APPROX_NONE stores absolutely all the contour points. That is, any 2 subsequent points (x1,y1) and (x2,y2) of the contour will be either horizontal, vertical or diagonal neighbors, that is, max(abs(x1-x2),abs(y2-y1))==1.

CV_CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, an up-right rectangular contour is encoded with 4 points.

So, due to that compression you were lacking some points, and a code snippet:

// Find contours
vector<Vec4i> hierarchy;
std::vector<std::vector<cv::Point> > contours;
cv::findContours(src, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);

Mat dst = Mat::zeros(src.size(), CV_8UC3);
for(size_t i = 0; i < contours[0].size(); ++i) // since we have only one contour
{
    cout << contours[0][i] << endl; // print the points
    circle(dst, contours[0][i], 0, Scalar(0, 0, 255));
}

imshow("dst", dst);
waitKey();

just bear in mind that the points are stored in an anti-clockwise sequence. But that's quite easy to address, just search the web how to do it. The result can be seen below:

image description

edit flag offensive delete link more

Comments

problem is that I need the boundary points in sequence. Non-zero will fail in some cases especially if object shape is weird such as two folds at any place. I need sequence starting from top to the bottom as boundary is traced. Due to some reason contour is failing ?? I want to know why this is happening ??

NabeelKhan gravatar imageNabeelKhan ( 2015-05-05 15:24:30 -0500 )edit

thanks a lot ... will give it a try

NabeelKhan gravatar imageNabeelKhan ( 2015-05-05 20:06:33 -0500 )edit

yes it works perfectly fine ... thanks a lot

NabeelKhan gravatar imageNabeelKhan ( 2015-05-06 20:40:43 -0500 )edit

glad to hear that ;-)

theodore gravatar imagetheodore ( 2015-05-07 05:22:43 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-05-04 23:59:00 -0500

Seen: 3,577 times

Last updated: May 05 '15