How to prevent inaccurate segmentation of enclosed background regions in Watershed Algorithm? - matlab

I'm using the watershed algorithm to segment bright spots on a dark background. The code is provided below, along with some images it generates.
In the second image, I've marked with red crosses the areas of enclosed background which are segmented as 'cells' (they're not biological cells, just using the word) - this is incorrect, they're part of the background, just enclosed by 'cells'. I see that this creates a false minimum, any help on how to prevent this?
% Improve contrast, binarize
RFP_adjust = imadjust(RFP_blur, stretchlim(RFP_blur, 0.001));
figure, imshow(RFP_adjust), title('Contrast adjust');
RFP_binarized = imbinarize(RFP_adjust);
RFP_perimeters = bwperim(RFP_binarized);
% figure, imshow(RFP_binarized), title('Otsu thresholding');
%2B - SEGMENTATION BY WATERSHED METHOD
% Discover putative cell centroids and process
RFP_maxs = imextendedmax(RFP_adjust, 3000);
RFP_maxs = imclose(RFP_maxs, strel('disk',5));
RFP_maxs = imfill(RFP_maxs, 'holes');
RFP_maxs = bwareaopen(RFP_maxs, 5);
RFP_max_overlay = imoverlay(RFP_adjust, RFP_perimeters | RFP_maxs, [1 .3 .3]);
figure, imshow(RFP_max_overlay), title('Maxima');
% Obtain complement - maxima become low-points (required for watershed)
RFP_comp = imcomplement(RFP_adjust);
RFP_imposemin = imimposemin(RFP_comp, ~RFP_binarized | RFP_maxs);
figure, imshow(RFP_imposemin), title('Inverted Maxima');
% Apply watershed
RFP_watershed = watershed(RFP_imposemin);
mask = im2bw(RFP_watershed, 1);
overlay3 = imoverlay(RFP_adjust, mask, [1 .3 .3]);
figure, imshow(overlay3), title('Segmented cells');
% Segment
RFP_cc = bwconncomp(RFP_watershed);
RFP_label_matrix = labelmatrix(RFP_cc);
whos labeled;
RFP_label = label2rgb(RFP_label_matrix, #spring, 'c', 'shuffle');
figure, imshow(RFP_label), title('Cells segmented');
Image 0 - result for image titled 'Maxima' (i.e. adjusted original image with maxima and outlines overlaid).
Image 1 - the result for image titled 'inverted maxima'
Image 2 - the result for image titled 'Cells segmented'

I would suggest something like what is done in the example included for the watershed function: use the background mask to set those pixels to Inf, perform the watershed operation, then set the background pixels in the result to 0. I believe you could change the watershed section of your code like so to achieve this:
% Apply watershed
RFP_watershed = RFP_imposemin; % Added
RFP_watershed(~RFP_binarized) = Inf; % Added
RFP_watershed = watershed(RFP_watershed); % Modified
RFP_watershed(~RFP_binarized) = 0; % Added
mask = im2bw(RFP_watershed, 1);
overlay3 = imoverlay(RFP_adjust, mask, [1 .3 .3]);
figure, imshow(overlay3), title('Segmented cells');

There no magic bullet, but a few things you can try.
One is to filter the image with a very large circular disc, creating a blurry image that looks like the background. Then subtract it from the original image. That will tend to force the actual background to zero.
Another is to Otsu threshold to separate foreground from background. That creates a binary image. Then do a morphological open operation using a mask designed to look like the actual cells.

Related

MATLAB Plot contrast enhancement?

I have a plot that needs to be contrast enhanced before another plot is superimposed over it.
figure
plot(something);
** contrast enhancement **
hold on
plot(something_else);
hold off
Is there a way to do this in the ** contrast enhancement ** line above? I have looked into imadjust function but it takes direct image input (tif/jpeg etc.).
Thank you.
Edit: Example code -
figure
plot(ebsd,ebsd.prop.bc);
mtexColorMap black2white
** contrast enhancement **
hold on
plot(ebsd('Forsterite'),ebsd('Forsterite').orientations.angle./degree);
hold off
Grab the image from the axes (if it is an image!)
im=getimage(rgb2gary(gca)); %it should already be gray, but matlab returns RGB anyway
and automatically adjust the contrast
im2=imadjust(im);
Set it again
imshow(im2,'Parent',gca); % or whatever other method you are using for display.
Of what I see in the example, you want area with saturated colors on a background of "faint", milky colors. Try this solution of highlight by increased saturation.
rgb = imread('peppers.png');
% make under-saturated image
hsv = rgb2hsv(rgb);
hsv(:, :, 2) = hsv(:, :, 2)*0.2;
hsv(hsv > 1) = 1; % Limit values
rgbFaint = uint8(255*hsv2rgb(hsv));
% make a mask of area to highlight
mask = false(size(rgb,1),size(rgb,2));
h = fspecial('disk',60) > 0;
mask(200:200+size(h,1)-1,200:200+size(h,2)-1) = h;
mask = repmat(mask,1,1,3);
% create image with highlight area
rgbHighlight = rgbFaint;
rgbHighlight(mask) = rgb(mask);
figure;
imshow(rgbHighlight)

Extracting Feature from an Image - suggestions for best approach?

I am currently learning computer vision and I would like to extract the onion from the image. What would be the best approach to do this?
I attempted a thresholding approach to detect white colour by breaking down the image into its R,G,B channels, but that also detects light reflections on other parts of the image. How could I clean up this image to obtain a mask that approximately represents the onion?
onionRGB = imread('onion.png');
onionGRAY = rgb2gray(onionRGB);
figure, imshow(onionRGB);
% split channels
rOnion = onionRGB(:, :, 1); % red channel
gOnion = onionRGB(:, :, 2); % green channel
bOnion = onionRGB(:, :, 3); % blue channel
whiteThresh = 160*3;
% detect white onion
onionDetection = double(rOnion) + double(gOnion) + double(bOnion);
% apply thresholding to segment the foreground
maskOnion = onionDetection > whiteThresh;
figure, imshow(maskOnion);
The following code, placed after splitting into channels, works well.
onionHSV = rgb2hsv(onionRGB);
saturationOnion = onionHSV(:,:,2);
figure;
imagesc(saturationOnion);
title('Saturation');
figure;
imagesc(rOnion+bOnion); title('purple');
%apply threshold to saturation and purple brightness levels
maskOnion = and((saturationOnion < 0.645), (rOnion+bOnion >=155));
%filter out all but the largest object
maskOnion = bwareafilt(maskOnion,1);
figure, imshow(maskOnion);
The first trick is using saturation from the HSV representation of the colors for filtering purposes.
The second trick is thresholding on more than one channel.
The third trick is filtering out all but the largest object.

Finding similar corners in matlab

I need to find similar corners in image (for example: 4 corners of a rectangle, same corners, just different orientation?).
I have this code:
% read the image into MATLAB and convert it to grayscale
I = imread('s2.jpg');
Igray = rgb2gray(I);
figure, imshow(I);
% We can see that the image is noisy. We will clean it up with a few
% morphological operations
Ibw = im2bw(Igray,graythresh(Igray));
se = strel('line',3,90);
cleanI = imdilate(~Ibw,se);
figure, imshow(cleanI);
% Perform a Hough Transform on the image
% The Hough Transform identifies lines in an image
[H,theta,rho] = hough(cleanI);
peaks = houghpeaks(H,10);
lines = houghlines(Ibw,theta,rho,peaks);
figure, imshow(cleanI)
% Highlight (by changing color) the lines found by MATLAB
hold on
After running this code I convert my starting image into a binary image with:
binary = im2bw(I);
after this I get a product from those 2 binary images and I think I get corners..
product = binary .* cleanI;
now I imfuse this picture with grayscale starting picture and get this:
I dont know what to do to get only those 4 corners!
Ok second try. Below a code that does not finally do the job but it might help.
Edge identifies the contours and with regionprops you get the characteristica of each identified element. As soon as you know what characteristics your desired object has you can filter it and plot it. I went through the Areas in shapedata.Area and the 6th largest was the one you were searching for. If you combine Area with some of the other charateristica you might get the one you want. As I said not ideal and final but perhaps a start ...
clear all
close all
source = imread('Mobile Phone.jpg');
im = rgb2gray(source);
bw = edge(im ,'canny',[],sqrt(2));
shapedata=regionprops (bwlabel(bw,8),'all');
%index = find([shapedata.Area]== max([shapedata.Area]));
index = 213;
data = shapedata(index).PixelList;
figure
imshow(im)
hold on
plot(data(:,1),data(:,2),'ro');

Removing background and measuring features of an image in MATLAB

I'm trying to measure the areas of each particle shown in this image:
I managed to get the general shape of each particle using MSER shown here:
but I'm having trouble removing the background. I tried using MATLAB's imfill, but it doesn't fill all the particles because some are cut off at the edges. Any tips on how to get rid of the background or find the areas of the particles some other way?
Cheers.
Edit: This is what imfill looks like:
Edit 2: Here is the code used to get the outline. I used this for the MSER.
%Compute region seeds and elliptial frames.
%MinDiversity = how similar to its parent MSER the region is
%MaxVariation = stability of the region
%BrightOnDark is used as the void is primarily dark. It also prevents dark
%patches in the void being detected.
[r,f] = vl_mser(I,'MinDiversity',0.7,...
'MaxVariation',0.2,...
'Delta',10,...
'BrightOnDark',1,'DarkOnBright',0) ;
%Plot region frames, but not used right now
%f = vl_ertr(f) ;
%vl_plotframe(f) ;
%Plot MSERs
M = zeros(size(I)) ; %M = no of overlapping extremal regions
for x=r'
s = vl_erfill(I,x) ;
M(s) = M(s) + 1;
end
%Display region boundaries
figure(1) ;
clf ; imagesc(I) ; hold on ; axis equal off; colormap gray ;
%Create contour plot using the values
%0:max(M(:))+.5 is the no of contour levels. Only level 0 is needed so
%[0 0] is used.
[c,h]=contour(M,[0 0]) ;;
set(h,'color','r','linewidth',1) ;
%Retrieve the image data from the contour image
f = getframe;
I2 = f.cdata;
%Convert the image into binary; the red outlines are while while the rest
%is black.
I2 = all(bsxfun(#eq,I2,reshape([255 0 0],[1 1 3])),3);
I2 = imcrop(I2,[20 1 395 343]);
imshow(~I2);
Proposed solution / trick and code
It seems you can work with M here. One trick that you can employ here would be to pad zeros all across the boundaries of the image M and then fill its holes. This would take care of filling the blobs that were touching the boundaries before, as now there won't be any blob touching the boundaries because of the zeros padding.
Thus, after you have M, you can add this code -
%// Get a binary version of M
M_bw = im2bw(M);
%// Pad zeros all across the grayscale image
padlen = 2; %// length of zeros padding
M_pad = padarray(M_bw,[padlen padlen],0);
%// Fill the holes
M_pad_filled = imfill(M_pad,'holes');
%// Get the background mask after the holes are gone
background_mask = ~M_pad_filled(padlen+1:end-padlen,padlen+1:end-padlen);
%// Overlay the background mask on the original image to show that you have
%// a working background mask for use
I(background_mask) = 0;
figure,imshow(I)
Results
Input image -
Foreground mask (this would be ~background_mask) -
Output image -

matlab: how to plot multidimensional array

Let's say I have 9 MxN black and white images that are in some way related to one another (i.e. time lapse of some event). What is a way that I can display all of these images on one surface plot?
Assume the MxN matrices only contain 0's and 1's. Assume the images simply contain white lines on a black background (i.e. pixel value == 1 if that pixel is part of a line, 0 otherwise). Assume images are ordered in such a way as to suggest movement progression of line(s) in subsequent images. I want to be able to see a "side-view" (or volumetric representation) of these images which will show the surface that a particular line "carves out" in its movement across the images.
Coding is done in MATLAB. I have looked at plot (but it only does 2D plots) and surf, which does 3D plots but doesn't work for my MxNx9 matrix of images. I have also tried to experiment with contourslice, but not sure what parameters to pass it.
Thanks!
Mariya
Are these images black and white with simple features on a "blank" field, or greyscale, with more dense information?
I can see a couple of approaches.
You can use movie() to display a sequence of images as an animation.
For a static view of sparse, simple data, you could plot each image as a separate layer in a single figure, giving each layer a different color for the foreground, and using AlphaData to make the background transparent so all the steps in the sequenc show through. The gradient of colors corresponds to position in the image sequence. Here's an example.
function plotImageSequence
% Made-up test data
nLayers = 9;
x = zeros(100,100,nLayers);
for i = 1:nLayers
x(20+(3*i),:,i) = 1;
end
% Plot each image as a "layer", indicated by color
figure;
hold on;
for i = 1:nLayers
layerData = x(:,:,i);
alphaMask = layerData == 1;
layerData(logical(layerData)) = i; % So each layer gets its own color
image('CData',layerData,...
'AlphaData',alphaMask,...
'CDataMapping','scaled');
end
hold off
Directly showing the path of movement a "line" carves out is hard with raster data, because Matlab won't know which "moved" pixels in two subsequent images are associated with each other. Don't suppose you have underlying vector data for the geometric features in the images? Plot3() might allow you to show their movement, with time as the z axis. Or you could use the regular plot() and some manual fiddling to plot the paths of all the control points or vertexes in the geometric features.
EDIT: Here's a variation that uses patch() to draw each pixel as a little polygon floating in space at the Z level of its index in the image sequence. I think this will look more like the "surface" style plots you are asking for. You could fiddle with the FaceAlpha property to make dense plots more legible.
function plotImageSequencePatch
% Made-up test data
nLayers = 6;
sz = [50 50];
img = zeros(sz(1),sz(2),nLayers);
for i = 1:nLayers
img(20+(3*i),:,i) = 1;
end
% Plot each image as a "layer", indicated by color
% With each "pixel" as a separate patch
figure;
set(gca, 'XLim', [0 sz(1)]);
set(gca, 'YLim', [0 sz(2)]);
hold on;
for i = 1:nLayers
layerData = img(:,:,i);
[x,y] = find(layerData); % X,Y of all pixels
% Reshape in to patch outline
x = x';
y = y';
patch_x = [x; x+1; x+1; x];
patch_y = [y; y; y+1; y+1];
patch_z = repmat(i, size(patch_x));
patch(patch_x, patch_y, patch_z, i);
end
hold off