# how to turn contours into vectors?

Hi

I have a simple irregular shape. I have a broken edges of this shape in black and white after pre-processing it. I extracted the contours from this image.

I want to convert this contour to a simple vector(least number of points with which I can reconstruct the edges again).

Any help as to how I can do this?????

Example: Image of a stone

This is the edge extracted image I have got after pre processing

Here's the code I'm using to access the contours

ret, thresh = cv2.threshold(dnoise,127,255,0)
dnoise, contours, hieararchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

print(contours)
for ar_i, arr in enumerate(contours):
for row_i, row in enumerate(arr):
if (row_i==0):
for el in row:
print("\n",format(el))


I don't want to use inbuilt ellipse function in cv!

edit retag close merge delete

Why do you mean by vector ? math vector ? C++ vector (contour is already vector<point>?

( 2017-08-24 05:05:20 -0600 )edit

I want to find the best fit points that can describe the shape with minimum number of points. From all the detected contours. I am using python here, not C++.

( 2017-08-24 05:13:30 -0600 )edit

( 2017-08-24 05:16:32 -0600 )edit
1

Storing the first element of each and every contour that is found in the image. Connecting the stored elements.

( 2017-08-24 05:47:02 -0600 )edit
1
( 2017-08-24 05:52:08 -0600 )edit
1

@Santhosh1 what both @LBerger and @sturkmen actually want to point out is that a contour is already in a vector format. However, your question is soo poorly formulated, I think they miss the point. Probably you have some black and white edges indicating a contour, but not the contour itself right? Update your question, add sample images, a line of code, ... and people will help you much more concrete.

( 2017-08-24 07:27:00 -0600 )edit
2

i would like to help but i did not understand what do you want to do

( 2017-08-24 12:28:28 -0600 )edit

In the example above we have got the outer edges of the stone.

At pixel level even these edges which look thin to our eye would be a collection of more than a single pixel.

I want to select as much less pixels(points) as possible in this image, so that I can get a rough shape of the image.

( 2017-08-25 00:38:01 -0600 )edit

Blur original image

( 2017-08-25 03:15:57 -0600 )edit

please post the code you used so far. let's solve the point you stuck.

( 2017-08-29 14:28:02 -0600 )edit

Sort by ยป oldest newest most voted

i tried some simple code and get this image

another test image and result:

....

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])

{
Mat image, gray,dst_img;
String image_file_name = "15035807843241733.jpg";//argv[1];

if (image.empty())
{
cout << "\nunable to open " << image_file_name << "\nprogram terminated!\n";
return 1;
}

imshow("source", image);

cvtColor(image, gray, COLOR_BGR2GRAY);
Canny(gray, dst_img, 50, 150);
blur(dst_img, dst_img, Size(5, 5));
threshold(dst_img, dst_img, 0, 255, THRESH_OTSU);

vector<Point> pts;
vector<Point> hull;
findNonZero(dst_img, pts);
convexHull(pts, hull, true);
polylines(image, hull, true, Scalar(0, 0, 255), 2);

imshow("dest", image);
waitKey();

return 0;
}

more

Nice trace ๐

( 2017-09-02 02:11:21 -0600 )edit

Official site

GitHub

Wiki

Documentation