how to get the region after segmentaion? - matlab

just as the picture above shows ,I have get the boundarys of one image after
segmentaion . But I just get the logical boudarys and I want to label each
region that splited by segmentaion. function bwlabel doesn't work ,It can only
label connected region. so ,how can I label these two hundred regions ???

If your original image is called img and your current bw segmented image is called I, I would suggest:
I = (I==0); % invert the image
cc = bwconncomp(I,8); % could use 4-connected neighborhood also
s = regionprops(cc,img,'all');
The last line will provide all the available properties from the original image for each segmented region (if you want it). You could use bwlabel as you mentioned, but I think bwconncomp may be faster/more efficient.
An alternative to bwlabel for labeling is:
L = labelmatrix(cc);
Then I like using label2rgb to view the segments:
RGB_label = label2rgb(L, #jet, 'k', 'shuffle');
imshow(RGB_label);
This will randomly color each segment according to the colormap provided (jet, in this case).

Related

Image Segmentation Matlab

I have this BW image:
And using the function RegionProps, it shows that some objetcs are connected:
So I used morphological operations like imerode to separte the objects to get their centroids:
Now I have all the centroids of each object separated, but to that I lost a lot of information when eroding the region, like you can see in picture 3 in comparison with picture 1.
So I was thinking if is there anyway to "dilate" the picture 3 till get closer to picture 1 but without connecting again the objects.
You might want to take a look at bwmorph(). With the 'thicken', inf name-value pair it will thicken the labels until they would overlap. It's a neat tool for segmentation. We can use it to create segmentation borders for the original image.
bw is the original image.
labels is the image of the eroded labels.
lines = bwmorph(labels, 'thicken', inf);
segmented_bw = bw & lines
You could also skip a few phases and achieve similiar results with a marker based watershed. Or even better, as the morphological seesaw has destroyed some information as seen on the poorly segmented cluster on the lower right.
You can assign each white pixel in the mask to the closest centroid and work with the resulting label map:
[y x]= find(bw); % get coordinates of mask pixels
D = pdist2([x(:), y(:)], [cx(:), cy(:)]); % assuming cx, cy are centers' coordinates
[~, lb] = min(D, [], 2); % find index of closest center
lb_map = 0*bw;
lb_map(bw) = lb; % should give you the map.
See pdist2 for more information.

Superimposing Multiple Images + Adding Colormap

I'm an undergrad student working in a cell biology lab with a basic background in matlab. I'm working on a project of tracking cell trajectory (time lapse) on a petri dish. Below are two example images that i used the watershed feature to separate from the background. The original pictures had neon green cells, now this is all in black and white/
Let's say i have 20 pictures like this, how might I superimpose one on top of another so they all of equal transparency?
Then, how can i add a colormap that represents time? (The bottom most picture is one end of the colormap and the most recent picture is the opposite end) <- this is extremely challenging as it often things the background is black and not NaN
The Basic Idea
Probably the easiest way to do this, is to take the binary image for each layer, and multiply the image by the time at which it was acquired (or it's index in time). Then you can concatenate all images along the third dimension (using cat). You can compute the maximum value along the third dimension using max. This will make the newer time points appear to be "on top" of the older time points. You can then display the resulting flattened matrix using imagesc and it will automatically map to the colormap for the current figure. Typically we would refer to this as a maximum intensity projection.
Creating Some Data
First since you've only provided two images, I'm going to create some shifted versions of the first image you've provided for the demonstration.
% Create some pseudo-data in a cell array that represents the image over time
im = imread('http://i.imgur.com/xTurvfO.jpg');
im = im(:,:,1);
ims = cell(1, 5);
% Create some shifted versions of im1
shifts = round(linspace(0,1000,5));
for k = 1:numel(shifts)
ims{k} = circshift(im > 100, shifts([k k]));
end
Implementing the Method
Now for the application of the method I discussed
% For each image, multiply the binary mask by the time
for k = 1:numel(ims)
ims{k} = ims{k} * k;
end
% Concatenate all images along the third dimension
IMS = cat(3, ims{:});
% Flatten by taking the maximum value along the third dimension
MIP = max(IMS, [], 3);
% Display the resulting flattened image using imagesc
imagesc(MIP);
% Create a custom colormap with black at the end to create our black background
colormap(cat(1, [0 0 0], parula))
The Result
I have used imfuse to create composite images, which is similar to combining multiple channels on a fluorescent microscope. The Mathworks documentation is http://www.mathworks.com/help/images/ref/imfuse.html.
The tricky part is choosing the vector for color channels. For example, [2,1,2] means choosing B(lue) for image 1, R(ed) and G(reen) for image 2. [2,1,2] is the scheme recommended for colorblind people and gives figure on the left of this image. Using [1,0,2] for red/blue gives the the figure on the right.
fig1 = imread([basepath filesep 'fig.jpg']); %white --> black
fig2 = imread([basepath filesep 'fig2.jpg']);
fig_overlay = imfuse(fig1, fig2,'falsecolor','Scaling','joint', 'ColorChannels', [1,0,2]);
imshow(fig_overlay)

Matlab: extract portions of an image (without its background)

I'm working with leaves' images in matlab. I will compare portions of those leaves through some similarity functions (euclidean for example), but first I need to extract portions of each leave and then save them. So, this is my problem now: how do I select those portions and draw a rectangle that shows me what will be cut? I already got the centroid and a boundingBox by using regionprops function (you can see those in red in the image firstResultsMatlab.png). However, I'm struggling by trying to draw and extract portions like those that are in blue (same image). I don't want to get parts from the black background, only portions from the leave.
I've also added an images of leaf as an example of what I've been working on and the code I used to get a boundingBox and the centroid. Any ideas are welcomed!
Thank you very much in advance.
I = imread('C:\Users\IBM_ADMIN\Desktop\Mestrado\Imagens_Final\IMG1_N1_1.png');
L = bwlabel(I);
s = regionprops(L,'BoundingBox');
stat = regionprops(L,'centroid');
hold on;
colors = hsv(numel(s));
for k = 1:numel(s)
him = imshow(I);
hold on;
rectangle('Position', s(k).BoundingBox, 'EdgeColor', colors(k,:));
plot(stat(k).Centroid(1),stat(k).Centroid(2),'rx');
end
matlab-results
leaf-1

how to identify an object appears homogenously white

I have a segmented image. i need to verify the intensity variation of the ellipse like structure present in the image. I need to check whether that ellipse is homogeneously white
original image
ellipse like structure is inside the rectangle
my segmented image is
i want to compare the original image (which is homogeneous white) with the segmented region.
regionprops is perfect for this sort of task. You can pass it your segmented binary image, and your original image to retrieve a list of the pixels in each region (presuming each region is not connected, as shown in your sample image). These will be in the form of a n x 1 vector for each region, returned as a struct array.
stats = regionprops(BW, I, 'PixelValues');
(You may want to retrieve other values returned by regionprops, like BoundingBox or Centroid, to help identify which set of pixels belongs to which region more easily. Consult the documentation to see what options are available.).
You can then define some statistical function to show the variation within each region, for example, to calculate the variance and standard deviation for each:
for n = 1:length(stats)
stats(n).var = var(stats(n).PixelValues);
stats(n).std = std(stats(n).PixelValues);
end
If you have some other specific definition of "intensity variation" in mind, then you need to develop some function that calculates it, then just call that instead of a built in like var or std.

Creating intensity band across image border using matlab

I have this image (8 bit, pseudo-colored, gray-scale):
And I want to create an intensity band of a specific measure around it's border.
I tried erosion and other mathematical operations, including filtering to achieve the desired band but the actual image intensity changes as soon as I use erosion to cut part of the border.
My code so far looks like:
clear all
clc
x=imread('8-BIT COPY OF EGFP001.tif');
imshow(x);
y = imerode(x,strel('disk',2));
y1=imerode(y,strel('disk',7));
z=y-y1;
figure
z(z<30)=0
imshow(z)
The main problem I am encountering using this is that it somewhat changes the intensity of the original images as follows:
So my question is, how do I create such a band across image border without changing any other attribute of the original image?
Going with what beaker was talking about and what you would like done, I would personally convert your image into binary where false represents the background and true represents the foreground. When you're done, you then erode this image using a good structuring element that preserves the roundness of the contours of your objects (disk in your example).
The output of this would be the interior of the large object that is in the image. What you can do is use this mask and set these locations in the image to black so that you can preserve the outer band. As such, try doing something like this:
%// Read in image (directly from StackOverflow) and pseudo-colour the image
[im,map] = imread('http://i.stack.imgur.com/OxFwB.png');
out = ind2rgb(im, map);
%// Threshold the grayscale version
im_b = im > 10;
%// Create structuring element that removes border
se = strel('disk',7);
%// Erode thresholded image to get final mask
erode_b = imerode(im_b, se);
%// Duplicate mask in 3D
mask_3D = cat(3, erode_b, erode_b, erode_b);
%// Find indices that are true and black out result
final = out;
final(mask_3D) = 0;
figure;
imshow(final);
Let's go through the code slowly. The first two lines take your PNG image, which contains a grayscale image and a colour map and we read both of these into MATLAB. Next, we use ind2rgb to convert the image into its pseudo-coloured version. Once we do this, we use the grayscale image and threshold the image so that we capture all of the object pixels. I threshold the image with a value of 10 to escape some quantization noise that is seen in the image. This binary image is what we will operate on to determine those pixels we want to set to 0 to get the outer border.
Next, we declare a structuring element that is a disk of a radius of 7, then erode the mask. Once I'm done, I duplicate this mask in 3D so that it has the same number of channels as the pseudo-coloured image, then use the locations of the mask to set the values that are internal to the object to 0. The result would be the original image, but having the outer contours of all of the objects remain.
The result I get is: