Ask Your Question
1

how to turn contours into vectors?

asked 2017-08-24 05:03:03 -0600

Santhosh1 gravatar image

updated 2017-08-29 15:49:18 -0600

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 Original Image

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

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 flag offensive close merge delete

Comments

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

LBerger gravatar imageLBerger ( 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++.

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

I don't know python but may be approxPolyDP can help you

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

How about this?

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

Santhosh1 gravatar imageSanthosh1 ( 2017-08-24 05:47:02 -0600 )edit
1
sturkmen gravatar imagesturkmen ( 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.

StevenPuttemans gravatar imageStevenPuttemans ( 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

sturkmen gravatar imagesturkmen ( 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.

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

Blur original image

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

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

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

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-08-29 15:26:30 -0600

updated 2017-08-29 15:34:16 -0600

i tried some simple code and get this image

image description

another test image and result:

image description ....image description

#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];

        image = imread(image_file_name);
        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;
}
edit flag offensive delete link more

Comments

Nice trace ๐Ÿ‘

Santhosh1 gravatar imageSanthosh1 ( 2017-09-02 02:11:21 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-08-24 05:03:03 -0600

Seen: 4,514 times

Last updated: Aug 29 '17