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

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.

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.

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.

face detection and make it blur

I am having trouble understanding the code about the Matlab
a = imread('Untitled2.png');
faceDetector = vision.CascadeObjectDetector;
bbox=step(faceDetector,a);
for j=1:size(bbox)
xbox=bbox(j,:);
subImage = imcrop(a, xbox);
H = fspecial('disk',10);
blurred = imfilter(subImage,H);
a(xbox(2):xbox(2)+xbox(4),xbox(1):xbox(1)+xbox(3),1:end) = blurred;
end
imshow(a);
Can anyone please kindly explain me that what is the for loop doing? I tried to use my own method to blur the face that I detected, but I just manage to crop out the face and blur the cropped image but I don't know how to put it back to original image. Then I tried to use the source code above I get from internet and the internet source code is able to blur the face and I can't understand the for loop logic. Kindly please explain to me, T^T.
Thanks.
As you can see there :
BBOX = step(detector,I) returns BBOX, an M-by-4 matrix defining M bounding boxes containing the detected objects. This method performs multiscale object detection on the input image, I. Each row of the output matrix, BBOX, contains a four-element vector, [x y width height], that specifies in pixels, the upper-left corner and size of a bounding box. The input image I, must be a grayscale or truecolor (RGB) image.
Are you sure that it's j=1:size(bbox) and not j=1:size(bbox,1) in the code?
Basically, The definition of BBox speaks by itself. The loop just iterates over all the boxes detected..
You then extract the informations about the jth box.
Then you extract the subImage given the position and size of xbox (xbox is a vector containing [x y width height]).
Then you define you filter.
Then you blur your subImage.
Then you override you image a with the blurred subimage using the informations in xbox.
EDIT : If you've already suceeded in blurring the cropped image, you just need to override you input image with your blurred image!

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!

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);