# code for level sets segmentation

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

edit retag close merge delete

Sort by ยป oldest newest most voted

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:

https://github.com/sjhalayka/Marching...

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)
{

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);

waitKey(0);

destroyAllWindows();

return 0;
}

more

## Stats

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

Seen: 324 times

Last updated: Mar 30