How Do I Find The Bounding Box For All Regions? - matlab

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

Related

Finding the maximum area of a rectangular damaged Manuscript in Matlab

I have this image below. How can I find the original area of the manuscript? I used imfill and was able to find the area within the boundaries, but I need the maximum area of just the manuscript itself within the image
image: Damaged Manuscript
You can calculate the bounding box / bounding rectangle to approximate the original area.
Use regionprops to calculate the property BoundingBox.
ConvexHull might help as well but I guess the resulting area would tend to be to small.
http://de.mathworks.com/help/images/ref/regionprops.html

Finding the centers of overlapping circles in a low resolution grayscale image

I am currently taking my first steps in the field of computer vision and image processing.
One of the tasks I'm working on is finding the center coordinates of (overlapping and occluded) circles.
Here is a sample image:
Here is another sample image showing two overlapping circles:
Further information about the problem:
Always a monochrome, grayscale image
Rather low resolution images
Radii of the circles are unknown
Number of circles in a given image is unknown
Center of circle is to be determined, preferably with sub-pixel accuracy
Radii do not have to be determined
Relative low overhead of the algorithm is of importance; the processing is supposed to be carried out with real-time camera images
For the first sample image, it is relatively easy to calculate the center of the circle by finding the center of mass. Unfortunately, this is not going to work for the second image.
Things I tried are mainly based on the Circle Hough Transform and the Distance Transform.
The Circle Hough Transform seemed relatively computationally expensive due to the fact that I have no information about the radii and the range of possible radii is large. Furthermore, it seems hard to identify the (appropriate) pixels along the edge because of the low resolution of the image.
As for the Distance Transform, I have trouble identifying the centers of the circles and the fact that the image needs to be binarized implies a certain loss of information.
Now I am looking for viable alternatives to the aforementioned algorithms.
Some more sample images (images like the two samples above are extracted from images like the following):
Just thinking aloud to try and get the ball rolling for you... I would be thinking of a Blob, or Connected Component analysis to separate out your blobs.
Then I would start looking at each blob individually. First thing is to see how square the bounding box is for each blob. If it is pretty square AND the centroid of the blob is central within the square, then you have a single circle. If it is not square, or the centroid is not central, you have more than one circle.
Now I am going to start looking at where the white areas touch the edges of the bounding box for some clues as to where the centres are...

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.

3D reconstruction based on stereo rectified edge images

I have two closed curve stereo rectified edge images. Is it possible to find the disparity(along x-axis in image coordinates) between the edge images and do a 3D reconstruction since I know the camera matrix. I am using matlab for the process. And I will not be able to do a window based technique as it's a binary image since a window based technique requires texture. The question how will I compute the disparity between the edge images? The images are available in the following links. Left Edge image https://www.dropbox.com/s/g5g22f6b0vge9ct/edge_left.jpg?dl=0 Right Edge Image https://www.dropbox.com/s/wjmu3pugldzo2gw/edge_right.jpg?dl=0
For this type of images, you can easily map each edge pixel from the left image to its counterpart in the right image, and therefore calculate the disparity for those pixels as usual.
The mapping can be done in various ways, depending on how typical these images are. For example, using DTW like approach to match curvatures.
For all other pixels in the image, you just don't have any information.
#Photon: Thanks for the suggestion. I did what you suggested. I matched each edge pixel in the left and right image in a DTW like fashion. But there are some pixels whose y-pixel coordinate value differ by 1 or 2 pixels, albeit they are properly rectified. So I calculated the depth by averaging those differing(up to 2-pixel difference in y-axis) edge pixels using least squares method. But I ended getting this space curve (https://www.dropbox.com/s/xbg2q009fjji0qd/false_edge.jpg?dl=0) when they actually should have been like this (https://www.dropbox.com/s/0ib06yvzf3k9dny/true_edge.jpg?dl=0) which is obtained using RGB images.I couldn't think of any other reason why it would be the case since I compared by traversing along the 408 edge pixels.

Ignoring MSER components with bounding ellipse overlapping with a binary mask

This question seems a little basic, but I would like to have some inputs about an efficient way of doing this.
Suppose I have the following image :
I also have a binary mask image as follows :
I detect MSER features on this image and plot the corresponding bounding ellipses.
What I need is that I want all those MSER regions removed, whose bounded ellipses overlap with the mask image. My issue is that I have a number of such operations and have to process a large number of images. Thus, what is the most efficient and fast way of doing this, which requirest minimal memory usage ?
It depends how your ellipses are stored, and perhaps on the size of your image. If they are represented as masks then I would be tempted to superimpose all the ellipses first and then do an intersection operation with the rectangle. Then you have a mask which you can apply to the original image.
If your ellipses are stored in a symbolic form - like the output of regionprops - it might be more efficient to test them against the rectangle first and only if they intersect would you convert it into a mask and add it to the overall mask.