Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image (color?) segmentation with opencv C++

http://i.imgur.com/iIv4gJa.png

As the graph showed, I'd like to input image and get several segments as a result like that.

It's just like cluster the closest color segment, so I think it's close to the concept of "meanshift"?

I've searched relevant questions here but still don't know how to start and construct the structure in opencv C++. I'm looking for some advises, and I'll be very appreciate if getting a piece of implementation code for me to reference! Thanks for any help!!

==================================================

Edit 5/19/2015

Let me add that one of my trying implementations is Watershed here:(http://blog.csdn.net/fdl19881/article/details/6749976).

enter image description here

It's not perfect but the result i want. In this implement, user needs to operate manually( draw the watershed lines ), so i'm looking for AUTOMATIC version of it. Sounds a little bit hard, but... i'll appreciate for some suggestion or piece of code to do it.

like image 278
Oasis Avatar asked May 18 '15 12:05

Oasis


People also ask

What color is represented by the tuple 255 255 255?

The RGBA value of white as a tuple is (255, 255, 255, 1) so it'd look something like this: caption_area = Image.

What is color segmentation?

It is the process of dividing an image into its constituent parts or objects. Common techniques include edge detection, boundary detection, thresholding, region based segmentation, among others. For this blog, let us focus on segmenting our images using Color Image Segmentation through the HSV color space.


2 Answers

Opencv Documentation: Link

Parameters: here

Sample code for Meanshift filtering:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

Mat img, res, element;

int main(int argc, char** argv)
{

   namedWindow( "Meanshift", 0 );
   img = imread( argv[1] );
// GaussianBlur(img, img, Size(5,5), 2, 2);
   pyrMeanShiftFiltering( img, res, 20, 45, 3);
   imwrite("meanshift.png", res);
   imshow( "Meanshift", res );

   waitKey();

   return 0;
}

This is the output with your image, you might need to use some pre-processing before or maybe find some better parameters:

output

EDIT: Output with some gaussian blur beforehand (comment in code)

here

like image 134
Aristu Avatar answered Oct 01 '22 07:10

Aristu


The problem with looking at existing segmentation approaches is that they are either implemented in Matlab (which nobody outside of Uni can use) or they are not automatic. An approach where the user needs to preprocess the picture by choosing objects of interest or levels that indicate how to split colors is not useful because it is not automatic. If you like, you can try my OpenCV based implementation of segmentation described in this blog post. It is not perfect, but it is automatic and does most of the job and you can actually download the source and try it out.

like image 34
MoDJ Avatar answered Oct 01 '22 06:10

MoDJ