Extracting Feature from an Image - suggestions for best approach? - matlab

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.

Related

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

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.

Matlab - Plot areas of interest onto an image

I'm working on an application and I'm at a stage where I'm comparing two images to see if they have any resemblance, with one another. I have managed to do this, an example you can find here.
From the image, it will display white spaces for pixels that are near the same for both images given. What I want to do next is get the coordinates of the white spaces and plot them onto the original image to highlight the strongest features about the coin. However, I'm unsure how to do this as I'm rather new to Matlab.
firstImage = sprintf('M:/Project/MatLab/Coin Image Processing/Image Processing/test-1.jpg');
secondImage = sprintf('M:/Project/MatLab/Coin Image Processing/Image Processing/test-99.jpg');
a = rgb2gray(imread(firstImage));
b = rgb2gray(imread(secondImage));
axes(handles.axes4);
imshow(a==b);
title('Scanning For Strongest Features', 'fontweight', 'bold')
From using disp(a==b), I can see which points of both pictures are the same. So my guess is that I need to do something where I get the coordinates of all the zeroes and then plot them onto the original image in a way that highlights it, similar to using a yellow highlighter, but I just don't know how.
If I got your question, I think you should use find to collect all the coordinates for which a==b:
[X, Y] = find(a == b); % Find coordinates for which the two images are equal
imshow(a), axis image; % Show first image
hold on
plot(Y, X, 'y.'); % Overlay those coordinates
hold off
You can use a transparent overlay to plot the region of interest.
figure
imshow(originalImage); % plot the original image
hold on
% generate a red overlay
overlay(:, :, 1) = ones(size(a)); % red channel
overlay(:, :, 2) = zeros(size(a)); % green channel
overlay(:, :, 3) = zeros(size(a)); % blue channel
h = imshow(overlay); % plot the overlay
set(h, 'AlphaData', (a == b) * 0.5); % set the transparency to 50% if a == b and 0% otherwise

How can I separate same colored connected object from an image?

I have an image shown below:
I am applying some sort of threshold like in the code. I could separate the blue objects like below:
However, now I have a problem separating these blue objects. I applied watershed (I don't know whether I made it right or wrong) but it didn't work out, so I need help to separate these connected objects.
The code I tried to use is shown below:
RGB=imread('testImage.jpg');
RGB = im2double(RGB);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
I = applycform(RGB,cform);
channel1Min = 12.099;
channel1Max = 36.044;
channel2Min = -9.048;
channel2Max = 48.547;
channel3Min = -53.996;
channel3Max = 15.471;
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
figure
imshow(maskedRGBImage)
In general, this type of segmentation is a serious research problem. In your case, you could do pretty well using a combination of morphology operations. These see widespread use in microscopy image processing.
First, clean up BW a bit by removing small blobs and filling holes,
BWopen = imopen(BW, strel('disk', 6));
BWclose = imclose(BWopen, strel('disk', 6));
(you may want to tune the structuring elements a bit, "6" is just a radius that seemed to work on your test image.)
Then you can use aggressive erosion to generate some seeds
seeds = imerode(BWclose, strel('disk', 35));
which you can use for watershed, or just assign each point in BW to its closest seed
labels = bwlabel(seeds);
[D, i] = bwdist(seeds);
closestLabels = labels(i);
originalLabels = BWopen .* closestLabels;
imshow(originalLabels, []);
I would try the following steps:
Convert the image to gray and then to a binary mask.
Apply morphological opening (imopen) to clean small noisy objects.
Apply Connected Component Analysis (CCA) using bwlabel. Each connected component contains at least 1 object.
These blue objects really look like stretched/distorted circles, so I would try Hough transform to detect cicles inside each labeled component. There is a built-in function (imfindcircles) or code available online (Hough transform for circles), depending on your Matlab version and available toolboxes.
Then, you need to take some decisions regarding the number of objects, N, inside each component (N>=1). I don't know in advance what the best criteria should be, but you could also apply these simple rules:
[i] An object needs to be of a minimum size.
[ii] Overlaping circles correspond to the same object (or not, depending on the overlap amount).
The circle centroids can then serve as seeds to complete the final object segmentation. Of course, if there is only one circle in each component, you just keep it directly as an object.
I didn't check all steps for validity in Matlab, but I quickly checked 1, 2, and 4 and they seemed to be quite promising. I show the result of circle detection for the most difficult component, in the center of the image:
The code I used to create this image is:
close all;clear all;clc;
addpath 'circle_hough'; % add path to code of [Hough transform for circles] link above
im = imread('im.jpg');
img = rgb2gray(im);
mask = img>30; mask = 255*mask; % create a binary mask
figure;imshow(mask)
% filter the image so that only the central part of 6 blue objects remains (for demo purposes only)
o = zeros(size(mask)); o(170:370, 220:320) = 1;
mask = mask.*o;
figure;imshow(mask);
se = strel('disk',3);
mask = imopen(mask,se); % apply morphological opening
figure;imshow(mask);
% check for circles using Hough transform (see also circle_houghdemo.m in [Hough transform for circles] link above)
radii = 15:5:40; % allowed circle radii
h = circle_hough(mask, radii, 'same', 'normalise');
% choose the 10 biggest circles
peaks = circle_houghpeaks(h, radii, 'nhoodxy', 15, 'nhoodr', 21, 'npeaks', 10);
% show result
figure;imshow(im);
for peak = peaks
[x, y] = circlepoints(peak(3));
hold on;plot(x+peak(1), y+peak(2), 'g-');
end
Some spontaneous thoughts. I assume the ultimate goal is to count the blue corpuscles. If so, I would search for a way to determine the total area of those objects and the average area of a corpuscle. As a first step convert to binary (black and White):
level = graythresh(RGB);
BW = im2bw(RGB,level);
Since morphological operations (open/close) will not preserve the areas, I would work with bwlabel to find the connected components. Looking at the picture I see 12 isolated corpuscles, two connected groups, some truncated corpuscles at the edge and some small noisy fragments. So first remove small objects and those touching edges. Then determine total area (bwarea) and the median size of objects as a proxy for the average area of one corpuscle.

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 -

Interpolation of pixel color in an image, matlab

I have a simple question. I have image A and I want to interpolate its rgb to subpixel level.
rgb = imread('ngc6543a.jpg');
red = rgb(:,:,1); % Red channel
green = rgb(:,:,2); % Green channel
blue = rgb(:,:,3); % Blue channel
One way to do it is to splitt it into three channels and then do interpolation for every channel.
Here I have confusuion. How can I assign rows and colums.I 'm using interp2.
Red_subpixel = interp2(X,Y,red,Xq,Yq)
What are values of X,Y. What is their expression in matlab code.
Is there any other function that interpolates all channels alltogether.
To get X and Y you can use meshgrid:
[X,Y] = meshgrid(1:size(red,1), 1:size(red,2))
To see what it does try [x,y] = meshgrid(1:3,1:3) in the command line and it should be fairly apparent.