Set MinimumBlobArea with width and height - MATLAB - matlab

I have a project to detect an object with background subtraction. But I can only set minimumblobarea with minimum pixel.. in my case, I need to set minimunblobarea to detect object in width and height to get specific object.. example : if there is an object bigger than width and height that I've set before, that object can't be detected.. so what should I do?
I Use this code
http://www.mathworks.com/help/vision/examples/motion-based-multiple-object-tracking.html

vision.BlobAnalysis returns bounding boxes of the detected blobs. The bounding box is a 4-element array of the form [x, y, width, height]. Once you have the bounding boxes you can easily check with ones have the width or the height that is too big, and exclude them.
Let's say you have N bounding boxes returned by vision.BlobAnalysys as an N-by-4 matrix called bboxes. You can use logical indexing to find boxes that are too big:
bigBoxesIdx = (bboxes(:, 3) > maxWidth) | (bboxes(:,4) > maxHeight);
bigBoxesIdx is now a logical array, where you have 1's for boxes that are too big, and 0's for the ones that are not. Note that you have to use the |, which stands for "element-wise or", rather than ||.
Now to throw away the boxes that are too big you simply do
bboxes(bigBoxesIdx, :) = [];

Related

Loop to change block position

I have a Matlab script that creates a Model Block for each element i found in a text file.
The problem is that all Models are created on each other in the window. So i'm trying to make a loop like:
for each element in text file
I add a Model block
I place right to the previous one
end
So it can look like this:
As you can see on the left, all models are on each other and I would like to place them like the one on the right.
I tried this:
m = mdlrefCountBlocks(diagrammeName)+500;
add_block('simulink/Ports & Subsystems/Model',[diagrammeName '/' component_NameValue]);
set_param(sprintf('%s/%s',diagrammeName,component_NameValue), 'ModelFile',component_NameValue);
size_blk = get_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position');
X = size_blk(1,1);
Y = size_blk(1,2);
Width = size_blk(1,3);
Height = size_blk(1,4);
set_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position',[X+m Y X+Width Y+Height]);
Inside the loop but it returns an error Invalid definition of rectangle. Width and height should be positive.
Thanks for helping!
The position property of a block does actually not contain its width and height, but the positions of the corners on the canvas (see Common Block Properties):
vector of coordinates, in pixels: [left top right bottom]
The origin is the upper-left corner of the Simulink Editor canvas before any canvas resizing. Supported coordinates are between -1073740824 and 1073740823, inclusive. Positive values are to the right of and down from the origin. Negative values are to the left of and up from the origin.
So change your code to e.g.:
size_blk = get_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position');
set_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position', size_blk + [m 0 0 0]);

Label text truncated after increasing font size

I followed the procedures in this question, and also tried setting individual text object with larger fonts. Here is my sample code:
hf = figure;
set(hf, 'DefaultAxesFontSize', 14)
hx = axes('Parent',hf);
[hx,hp1,hp2] = plotyy(hx, rand(10,1),rand(10,1),rand(10,1),rand(10,1),'scatter');
hlx = xlabel(hx(1), 'Only half of this line show up');
hl1 = ylabel(hx(1), 'Not usually truncated but less border');
hl2 = ylabel(hx(2), 'Only part of this line show up');
ht = title(hx(1), 'Too close to border');
As can be seen in the picture, the labels get truncated by the border of the figure. I have to drag the figure to very large - contrary to desired - in order to reveal all text.
How can I automatically set the text box according to the text font size, so that even for small graphs they don't get cut?
I know I can do it manually by setting Position of the axes but it's kind of manual and guess-and-try. Is there any automatic way to calculate the margins?
One thing that can be done is to calculate increased margin according to new text font size. Assume we know Matlab's default font size is 10, or otherwise get it by get(hf,'DefaultAxesFontSize').
Then get the relative position of the axes by get(hx, 'Position'), which gives four percentage values. First two define left and bottom margin. Since it's for the labels, increasing the font size from 10 to 14 means the text box should grow by 1.4 times. The next two numbers define the size of the axis. Since text boxes on both sides grow by 1.4 times, assuming original size being x, then new size is 1-[(1-x)*1.4] = 1.4x - 0.4.
Suggested workaround:
hf = figure;
set(hf, 'DefaultAxesFontSize', 14)
hx = axes('Parent',hf);
set(hx, 'Position', [1.4 1.4 1.4 1.4].*get(hx, 'Position')+ [0 0 -.4 -.4])
[hx,hp1,hp2] = plotyy(hx, rand(10,1),rand(10,1),rand(10,1),rand(10,1),'scatter');
hlx = xlabel(hx(1), 'Only half of this line show up');
hl1 = ylabel(hx(1), 'Not usually truncated but less border');
hl2 = ylabel(hx(2), 'Only part of this line show up');
ht = title(hx(1), 'Too close to border');
You may replace the manually entered number 1.4 with the ratio between newly assigned (bigger, hopefully) font size and the original size which is 10.

Getting height and width of objects after connected component analysis

I want to find the width and height of the connected component objects using regionprops 'BoundingBox' property in matlab.I find and mark the rectangles but I don't know how to extract the height and width.So how can I extract it?My code is given below
cc=bwconncomp(im);
stats=regionprops(cc,'BoundingBox');
for n = 1 : length(stats)
thisBB = stats(n).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',1 )
end
You have already extracted the width and height of the rectangle. The output of 'BoundingBox' in the BW case is
[ corner_x corner_y, width, height]
thisBB(3) is therefore the width, and thisBB(4) is the height.
The rectangle function expects exactly this as input: [x,y,w,h].

i want to extract particular bounding box from so many bounding boxes using matlab

From so many bounding box in image,i want bounding box of particular height and width ratio. I have created bounding box around 32 different objects.
Firstly i have to label each bounding box.
Then I want to calculate values for a particular box and in output i want bounding boxes of that particular height and width ratio. How to do this.
My code is:
f=imread('G:\..pooja project\prjct nw\r.jpg');
f=imresize(f,[400 NaN]); % Resizing the image keeping aspect ratio same.
g=rgb2gray(f);
g=medfilt2(g,[3 3]);
se=strel('disk',1);
gi=imdilate(g,se);
ge=imerode(g,se);
gdiff=imsubtract(gi,ge);
gdiff=mat2gray(gdiff);
gdiff=conv2(gdiff,[1 1;1 1]);
gdiff=imadjust(gdiff,[0.5 0.7],[0 1],0.1); .
B=logical(gdiff);
er=imerode(B,strel('line',50,0));
out1=imsubtract(B,er);
F=imfill(out1,'holes');
figure(2) ,imshow(F)
H=bwmorph(F,'thin',1);
H=imerode(H,strel('line',3,90));
figure(3),imshow(H)
final=bwareaopen(H,100);
figure(4),imshow(final);
[Ilabel num]=bwlabel(final);
disp(num)
figure(7),imshow(final)
Iprops=regionprops(Ilabel);
stats = regionprops(final,'eccentricity','orientation','area')
%stats()
Ibox=[Iprops.BoundingBox];
Ibox=reshape(Ibox,[4 num]);
for cnt=1:num
rectangle('position',Ibox(:,cnt),'edgecolor','r');

Using rectangle in Matlab. Using Sum()

I have performed rgb2gray on an image and did a sobel edge detection on the image.
then did
faceEdges = faceNoNoise(:,:) > 50; %binary threshold
so it sets the outline of the image (a picture of a face), to black and white. Values 1 is white pixel, and 0 is black pixel. Someone said I could use this,
mouthsquare = rectangle('position',[recX-mouthBoxBuffer, recY-mouthBoxBuffer, recXDiff*2+mouthBoxBuffer/2, recYDiff*2+mouthBoxBuffer/2],... % see the change in coordinates
'edgecolor','r');
numWhite = sum(sum(mouthsquare));
He said to use two sum()'s because it gets the columns and rows of the contained pixels within the rectangle. numWhite always returns 178 and some decimal numbers.
If you have a 2D matrix M (this being -- for exmple -- an image), the way to count how many elements have the value 1 is:
count_1 = sum(M(:)==1)
or
count_1 = sum(reshape(M,1,[])==1)
If the target values are not exactly 1, but have a Δ-threshold of, let's say, +/- 0.02, then one should ask for:
count_1_pm02 = sum((M(:)>=0.98) & (M(:)<=1.02))
or the equivalent using reshape.