Minimal Rectangle Bounding box around a region if an image using matlab - matlab

Hi i am working with matlab and trying to select a region using a bounding box. The code is as shown below
BW=bwconncomp(I1);
STATS = regionprops(BW, 'FilledArea','BoundingBox','Image');
The result is as shown below
I am trying to obtain an output as shown below. Is it possible?

I found these two codes for the generation of oriented bounding boxes on the File Exchange:
orientedBox in geom2d by David Legland:
OBOX = orientedBox(PTS) Computes the oriented bounding box of a set of points.
imOrientedBox in Feret diameter and oriented box also by David Legland
OBB = imOrientedBox(IMG) Computes the minimum area oriented
bounding box of labels in image IMG.
You will probably get what you are looking for from imOrientedBox.

Related

How to label separate lines in the image (image of a text) separately using "bwlabel" in matlab?

I have recognized and labeled objects in my image that is fully consists of texts. you can see the objects are labeled as red color in the attached image. so, I want to separate the objects in the second line (or more lines) from the first line and give them different colors (each line would has a different colors) but I can't do that. do you have any idea? thanks for all answers.
this is part of my matlab code that does the labeling:
%% Label connected components
[L, Ne]=bwlabel(imagen);
%% Measure properties of image regions
propied=regionprops(L,'BoundingBox');
hold on
%% Plot Bounding Box
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox,'EdgeColor','r','LineWidth',2)
end
and this is labeled image that all the objects in different lines have the same label (same color=red).
I think the following methods should work if the lines are not too curvy.
Find the centroids of the bounding boxes, or get the centroids from the regionprops itself, then cluster their y coordinates using kmeans with k = 2.
The result is not perfect, but fine. May be you can then fit a curve to the clustered points, with outlier removal (e.g. RANSAC)
OR
Prepare a new image by filling in the bounding boxes.
Prepare a rectangular structuring element whose height is 1 and width is the width of the widest bounding box.
Perform a morphological closing of the filled image using this structuring element. This will connect the regions horizontally. Now you get a mask separating the two regions.
The resulting images were obtained using opencv (I'm not posting the code because it's too untidy. Hope the instructions are clear enough).

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

Area of Bounding Box of an object matlab

I have objets have different shapes and my aim is to find ratio between object area and its Bounding Box area. There is no problem to find object area but I did not find a way to get Bounding Box area.
Is there any way or any exist function in matlab to calculate Bounding Box area?
But, you know the bounding box? If you dont, use regionprops(Imgbw,'BoundingBox')
And you will get it.
Once you have it, its kinda easy. Is just computing the area of a square. Regionprops will give you [x y] and [x_width y_width]. Im sure you are capable of calculating the area of a square with the size of its sides.

How to identify boundaries of a binary image to crop in matlab?

How to identify boundaries of a binary image to crop in matlab?
ie. the input binary image has no noises. only has one black object in white background.
You can use the edge command in MATLAB.
E = edge(I);
I would be an input grayscale or binary image. This will return a binary image with only the edges.
This can provide further assistance:
http://www.mathworks.com/help/images/ref/edge.html
If your image is just black-and-white and has a single object, you can likely make use of the Flood fill algorithm, for which Matlab has built-in support!
Try the imfill function (ref).
This should give you the extents of the object, which would allow you to crop at will.
You can also invert the image, then do regionprops to extract all of the properties for separate objects. You need to invert the image as regionprops assumes that the objects are white while the background is black. A good thing about this approach is that it generalizes for multiple objects and you only need about a few lines of code to do it.
As an example, let's artificially create a circle in the centre of an image that is black on a white background as you have suggested. Let's assume this is also a binary image.
im = true(200, 200);
[X,Y] = meshgrid(1:200, 1:200);
ind = (X-100).^2 + (Y-100).^2 <= 1000;
im(ind) = false;
imshow(im);
This is what your circle will look like:
Now let's go ahead and invert this so that it's a white circle on black background:
imInvert = ~im;
imshow(imInvert);
This is what your inverted circle will look like:
Now, invoke regionprops to find properties of all of the objects in our image. In this case, there should only be one.
s = regionProps(imInvert, 'BoundingBox');
As such, s contains a structure that is 1 element long, and has a single field called BoundingBox. This field is a 4 element array that is structured in the following way:
[x y w h]
x denotes the column/vertical co-ordinate while y denotes the row/horizontal co-ordinate of the top-left corner of the bounding box. w,h are the width and height of the rectangle. Our output of the above code is:
s =
BoundingBox: [68.5000 68.5000 63 63]
This means that the top-left corner of our bounding box is located at (x,y) = (68.5,68.5), and has a width and height of 63 each. Therefore, the span of our bounding box goes from rows (68.5,131.5) and columns (68.5,131.5). To make sure that we have the right bounding box, you can draw a rectangle around our shape by using the rectangle command.
imshow(im);
rectangle('Position', s.BoundingBox);
This is what your image will look like with a rectangle drawn around the object. As you can see, the bounding box given from regionprops is the minimum spanning bounding box required to fully encapsulate the object.
If you wish to crop the object, you can do the following:
imCrop = imcrop(imInvert, s.BoundingBox);
This should give you the cropped image that is defined by the bounding box that we talked about earlier.
Hope this is what you're looking for. Good luck!

Matlab/OpenCV create angle image

I am following the instructions of the following paper (basically just page 2):
http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=05649943
As described in the paper, I want to do the following:
I have an image --> Extract face rectangles
Create edge images (using Canny)
Create distance images (using bwdist in MatLab)
Create angle image
The process looks like the following:
And described here:
And I am stuck at step 4: creating the angle image.
I am using Matlab to create the angle image:
im = imread(['face_images/faces/' ims(i).name]);
I = rgb2gray(im);
[Gmag, Gdir]=imgradient(I);
GdirI=(Gdir+180)*(255/360);
imwrite(GdirI, ims(i).name);
end
But I am not getting the images that are presented in the last row in the paper. Do I have to give the edge images as input or the original images? Because I am using the original images right now...
When the gradients magnitude is small the edge angles are poorly defined. I would normally set a threshold on the magnitude and only calculate the angles for the pixels above the threshold. In your case it maybe that you only calculate the angles at the pixels that the cannny filter finds an edge.