Bounding box of an object in an image Matlab - matlab

I want to get the object's bounding box positions (x, y, width and height) in the image and save it to a text file. As shown in the below image. So if anyone could please advise.
The image can be found here

Assuming you have read the image using imread, this should work:
bwImage=~im2bw(img,0.98); %making gray pixels white and (almost) white pixels black
bndBox=regionprops(bwImage,'BoundingBox');
bndBox will have top-left corner of the bounding box and its width and height stored in the format [corner_x corner_y width height].

Related

How to get size of the drawn image in actual logical pixels?

I would like to know how to get the size of the drawn image in logical pixels. I know how to get image size in pixels, and I could convert that to logical pixels, but the problem is that those are not the actual pixels of the image drawn on the screen. I would like to know how to get how much logical pixels of the display do the image occupy when drawn on the screen.

Draw draggable/adjustable rectangle on image based on PREDEFINED COORDINATES in Matlab?

I calculate the rectangular bounding box coordinates for objects in my matlab code ([xmin ymin width height]). But the coordinates might not be precise. Then, I want to draw the box on the image and modify that by dragging the box and/or adjusting the borders. I tried to use imrect and imcrop, but those functions do not draw the draggable/adjustable rectangle on image, based on predefined coordinates. Is there any way to do that?
Thanks in advance for your time.
Take a look at imrect. It works much like the imcrop rectangle but you can set an initial position and get the current position by calling getPosition.

How to crop an image in matlab using interactive tool?

I want to crop out a window of size 250 X 250 pixels from an image at a specific place. How can I do that? I have seen imtool but I don't know how to control the size of cropping window.
Use imrect, set the size and make it non-resizable, then imcrop, something like:
figure, imshow(I);
h = imrect(gca, [10 10 250 250]);
setResizable(h,0)
rect = wait(h);
% now move to appropriate position
% command line blocked until rectangle is double clicked
I2 = imcrop(I, rect)
This can be done in many ways. For example you may have an image im, and use imtool(im) to find the coordinate (x,y) of the upper right corner of the window you want to crop down to. You can find this coordinate by holding you mouse curser over the pixel and looking in the parentheses in the lower left corner of the imtool-window.
Then you get the cropped image by cropim = im(y+(0:249),x+(0:249)); or cropim = im(y+(0:249),x+(0:249),:); depending on how the image is stored.

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!

Filling some region with colour, and the rest of the image as black

I have drawn some polygon on an image after using imshow and hold on, and filled it with white as follows:
fill(x(k),y(k),[1 1 1])
How can I make the rest of the image black while keeping the polygon white? In other words, how can I make a binary image, where the polygon is white, and the rest of the image is black? Provided that the polygon is a bit complex.
Thanks.
Use roipoly:
BW = roipoly( I, x(k), y(k) );
Where I is your input image (you only need it to get the desired output size of the binary maxk BW). y and x are the corners of your polygon.