Ask Your Question

code for level sets segmentation

asked 2018-03-30 12:02:25 -0500

Nbb gravatar image

Does anyone here have a simple implementation of the level sets for image segmentation ? I am studying it and tried the code here but it does not work. The contour ignores the object and continues to shrink.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-03-30 13:36:11 -0500

sjhalayka gravatar image

updated 2018-03-30 16:41:07 -0500

If you want to do it yourself from the ground up, you can use the Marching Squares algorithm to create the level set contour(s). Here is some C++ code that I wrote some time ago:

The code uses the OpenGL/GLUT libraries to draw the image and the contour(s).

Best of luck. If you like this answer, please mark the answer as correct.

There is also the OpenCV threshold and findContours functions, which can be used to get the level set, if you find that Marching Squares is too complicated:

#pragma comment(lib, "opencv_world340.lib")

#include <opencv2/opencv.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(void)
    Mat cat_img = imread("cat.png", CV_LOAD_IMAGE_GRAYSCALE);

    threshold(cat_img, cat_img, 127, 255, THRESH_BINARY);

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours(cat_img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

    Mat cat_contours = Mat::zeros(cat_img.size(), CV_8UC3);

    for (size_t i = 0; i < contours.size(); i++)
        drawContours(cat_contours, contours, (int)i, Scalar(255, 0, 0), 2, 8, hierarchy, 0, Point());

    imshow("cat img", cat_contours);



    return 0;
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-03-30 12:02:25 -0500

Seen: 166 times

Last updated: Mar 30