how to include a dark border to bright segmented image? - matlab

I have an image like this.note that the regions are not perfectly shaped.it is rectangular like region and ellipse like region. I have segmented the ellipse like region using some algorithm.segmented region is bright one.the border (red rectangle) is dark one
finally i must get red rectangular like region
can you suggest any algorithm to perform this

I see that you have done some real progress on your segmentation. Because you already have an idea of the location of elements you want to segment, you should use a watershed with constraints/markers:
Your actual segmentation represents the inner markers.
You dilate it with a big structuring element (bugger than the inter disk space).
You take the contour of the dilation, and that's your outer markers.
You compute the gradient of the original image.
You apply the watershed on the gradient image, using the markers you have just computed.
[EDIT] As the segmentation you provided does not match with the original image (different dimensions), I had to simulate roughly a simple segmentation, using this image (the red lines being the the segmentation you already have). And I got this result.

Related

How to measure these black regions in Matlab

The image above has been processed to remove its background and increase contrast with im2bw. I want to now identify and measure the two elongated black regions at the top and bottom centre of the image. This is the result:
If I use imfill(I,'holes'), one of them does not get identified.
I would also like to identify the boundaries, so that I can measure the area of these regions and find their respective "weighted centroid".
What I want to achieve is something that allows me to measure an angle between the orientation of the elongated black regions in different frames, as pictured in the sketch below (the red line indicates the position of the top black region in a previous frame).
In this answer, I'll be using DIPimage 3, an image analysis toolbox for MATLAB (disclosure: I'm an author). However, the filters applied are quite simple, it should be no problem implementing this using other toolboxes instead.
The original image is very noisy. Simply thresholding that image leads to a noisy binary image that is very difficult to work with. I'm suggesting you filter the original image to highlight the structures of interest first, before thresholding and measuring.
Because we're interested in detecting lines, we'll use the Laplace of Gaussian filter. It is important to tune the sigma parameter to match the width of the lines to be detected. After applying the Laplace filter, dark lines will appear bright, and bright lines will appear dark. The bright dot in the middle of the image will also be enhanced, but appear dark.
img = readim('https://i.stack.imgur.com/0LzF3m.png');
img = img{1}; % all three channels of PNG file are identical, take one
out = laplace(img,10);
This image is straight-forward to threshold.
out = out > 0.25;
Finally, we'll measure the orientation of these two lines as the angle under which the projection is largest.
msr = measure(out,[],'feret');
angle = msr.Feret(:,4)
Output (angle in radian, 0 is to the right, pi/2 is down):
angle =
-1.7575
-1.7714

Matlab : ROI substraction

I'm learning about statistical feature of an image.A quote that I'm reading is
For the first method which is statistical features of texture, after
the image is loaded, it is converted to gray scale image. Then the
background is subtracted from the original image. This is done by
subtract the any blue intensity pixels for the image. Finally, the ROI
is obtained by finding the pixels which are not zero value.
The implementation :
% PREPROCESSING segments the Region of Interest (ROI) for
% statistical features extraction.
% Convert RGB image to grayscale image
g=rgb2gray(I);
% Obtain blue layer from original image
b=I(:,:,3);
% Subtract blue background from grayscale image
r=g-b;
% Find the ROI by finding non-zero pixels.
x=find(r~=0);
f=g(x);
My interpretation :
The purpose of substracting the blue channel here is related to the fact that the ROI is non blue background? Like :
But in the real world imaging like for example an object but surrounded with more than one colors? What is the best way to extract ROI in that case?
like for example (assuming only 2 colors on all parts of the bird which are green and black, & geometri shaped is ignored):
what would I do in that case? Also the picture will be transformed to gray scale right? while there's a black part of the ROI (bird) itself.
I mean in the bird case how can I extract only green & black parts? and remove the rest colors (which are considered as background ) of it?
Background removal in an image is a large and potentielly complicated subject in a general case but what I understand is that you want to take advantage of a color information that you already have about your background (correct me if I'm wrong).
If you know the colour to remove, you can for instance:
switch from RGB to Lab color space (Wiki link).
after converting your image, compute the Euclidean from the background color (say orange), to all the pixels in your image
define a threshold under which the pixels are background
In other words, if coordinates of a pixel in Lab are close to orange coordinates in Lab, this pixel is background. The advantage of using Lab is that Euclidean distance between points relates to human perception of colours.
I think this should work, please give it a shot or let me know if I misunderstood the question.

Selecting Overlapping / Touching regions (Binary Images, Matlab)

I'm trying to select overlapping/ touching images from 2 binary images. The first image (blue circles) has the main body and I want to find any green triangles attached to it.
1st Binary Image here (colored for identification)
2nd Binary Image here
FinalImage = BinaryImage1 | BinaryImage2;
**Apply Filter**
Expected result:
Note how the unattached Green triangles are removed, & all blue circles are retained.
Is there a way to do this trick?
This calls for a morphological reconstruction! In morphological reconstruction, you specify a marker and start reconstructing the original image from that marker point using a morphological dilation.
Luckily for us, MATLAB already has a function for that, called imreconstruct in the Image Processing Toolbox, which is called by imreconstruct(marker,image)
To recognize touching, but non-overlapping figures, we can just do a dilation on one of the input image to make touching figures overlap. As structuring element we can e.g. use a 3x3 square, so we also recognize 8-connected figures. After that we use the overlapping points as markers and do the morphological reconstruction using the combined image.
dilatedImage1 = imdilate(binaryImage1, strel('square',3));
finalImage = imreconstruct(dilatedImage1&binaryImage2, dilatedImage1|binaryImage2);
As you write that all circles, i.e. all parts from binaryImage1 should be retained, we can just add binaryImage1 to the result using
finalImage = finalImage | binaryImage1;
For your two example images, this results in:
The circle on the upper right is not connected to any triangle. I don't know how that triangle appeared in your expected result image, but I suppose this is only for demonstration purposes.
Note: I imported the .jpg example images in MATLAB, which lead to ugly borders, so I did a morphological opening on the images first. The borders are still not optimal, but it doesn't look that bad.

How Do I Find The Bounding Box For All Regions?

I'm using the MNIST digit images for a machine learning experiment, and I'm trying to center each image based on position, rather than the center of mass that they are centered on by default.
I'm using the regionprops class, BoundingBox method to extract the images. I create a B&W copy of the greyscale, use this to determine the BoundingBox properties (regionprops works only B&W images) and then apply that on the greyscale original to extract the precise image rectangle. This works fine on ~98% of the images.
The problem I have is that the other ~2% of images has some kind of noise or errant pixel in the upper left corner, and I end up extracting only that pixel, with the rest of the image discarded.
How can I incorporate all elements of the image into a single rectangle?
EDIT: Further research has made me realise that I can summarise and rephrase this question as "How do I find the bounding box for all regions?". I've tried adjusting a label matrix so that all regions are the same label, to no avail.
You can use an erosion mask with the same size of that noise to make it totally disappear " using imerode followed by imdilate to inverse erosion ", or you can use median filter

Edge removal (in matlab)

I have a image which looks like this:
The (blue) background have the value zero, the the (red) ring has a "large" value (compared to the rest of the image). I want to plot only the orange part of the sample. However, due to the finite resolution of the image the edges still appear as show here:
As you can see specially the white regions (yes there are a few) above are hard to see due to all the noise from edges.
Is there a good algorithm (preferable in matlab) which can help me clean up these images?
Find the binary mask for the ring
Dilate the mask a bit using imdilate and strel
Use the inverted mask to 'and out' the ring and the region around it