How to use graphs cuts to segment objects in grayscale 2D images?
I need to extract some unwanted objects from chest radiographs using the Shai Bagon Matlab Wrapper for Graph Cuts as suggested by @rayryeng in this post .
I read Boykov 's article and got some insight into how Graph Cuts work. I also downloaded Sha Bagon Matlab Warpper for Graph Cuts and compiled the required mex files. To get started, I downloaded a simple example of image segmentation. But I am confused as to how I might use
[gch ...] = GraphCut(mode, ...);
to segment unwanted objects in 2D grayscale images.
Any help is appreciated as always. Thank.
source to share
The basics themselves:
img = imread('http:%//i.stack.imgur.com/nntda.png'); %// read the image
img = img(:,:,1); %// use only one channel as it is a gray scale image
Observing that the object is generally brighter than 110, while the rest of the lungs are darker than this value, you can define the per-pixel data term:
Dc(:,:,1) = img > 110; %// cost of 1-st label (bg): penalize pixels brighter than 110
Dc(:,:,2) = img < 110; %// cost of 2-nd label (fg): penalize pixels darker than 110
lambda = 11; %// relative weight of smoothness cost vs. data cost
Sc = [0 1; 1 0]; %// give 0 cost for bg-bg or fg-fg transitions, and 1 cost for fg-bg transitions
Optimize with a wrapper GraphCut
:
gch = GraphCut( 'open', Dc, lambda * Sc ); %// define the graph
[gch L] = GraphCut('expand', gch ); %//optimize and get the labeling L
gch=GraphCut('close',gch); %// clean up the mess
What can you do next? (Besides supporting and accepting this extraordinary answer)
- Use a more complex data cost - use L1 or L2 "distance" from the 110 threshold.
- Use "smooth" smoothness - make the smoothness costs dependent on the edges of the image.
- Use an 8-connected grid plot rather than the default 4-connected graph - this will reduce the "ladder" shape of the segmentation boundary.
- Use the best foreground / background model - create an appearance model based on image patches, not a single pixel.
I'll leave that to you to figure out more interesting ways to improve this method ...
source to share