Draw draggable/adjustable rectangle on image based on PREDEFINED COORDINATES in Matlab? - 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.

Related

Crop an image using multiple coordinates in Matlab

I used the "imfreehand" to crop an irregular shape and save its positions into a variable. This position variable is a 85*2 double matrix (85 points, X and Y coordinates). Now, I want to use the same position to crop another image (different layer of the image, but the location of the objects is the same). The functions I can find all requires rectangle positions (X1,X2,Y1,Y2). In my situation, I have 82 different (X,Y) coordinates, how can I use the position information to crop a new image?
From what I understand, you want to take the coordinates created by imfreehand(...) to create a cropable object on another image. You can use the function impoly(hparent,position) for this purpose.
The MathWorks page provides an example to guide you on its usage.

how to place a filled shape on a bbox to maks the moving object like placing rectangle using insertObjectAnnotation?

I would like to place a mask (image) on a moving object that is extracted using blob analysis.
frame = insertObjectAnnotation(frame, 'rectangle', bboxes, labels);
I found an example in matlab documentation. here bbox is the cordinates of moving object. its drawing rectangles around the moving objects in the video using inserObjectAnnotation() function.
like that I want to place a filled in shape in place of rectangle. how can I do that?
Please help
The insertShape function can draw filled shapes into images.

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!

drawing a regular polygon over an image and calculate the area

I want to draw a regular polygon over an image in matlab and then calculate the area of that polygon.
have you try area or polyarea?

Markers and the imagesc() function in matlab

How do you create a marker on a 2D image that is plotted using imagesc()? Is it even possible?
You simply want to draw something over your image? Then you can just use hold on.
For example, this will draw a circle at the 10,10 pixel of the image.
imagesc(magic(24));
hold on
scatter(10,10);