How to draw a rectangle around a blob?

조회 수: 1 (최근 30일)
Sachin Dalvi
Sachin Dalvi 2017년 12월 13일
답변: Sachin Dalvi 2018년 2월 6일
Hello, I am very new to MATLAB programming
I have a binary blob which changes its position with each frame. I have managed to get lower and higher bounds for each frame (Horizontal and Vertical).
vertprofile = any(binaryImage>0,1);
bbox_x_low = find(vertprofile, 1, 'first');
box_x_high = find(vertprofile, 1, 'last');
horzprofile = any(binaryImage>0,2);
bbox_y_low = find(horzprofile, 1, 'first');
bbox_y_high = find(horzprofile, 1, 'last');
My question is how can I draw a rectangle within these bounds?
also,
rectangle('Position',[1 2 5 6])
in this command how can I replace numbers with variables.
for example I have variables as, a=1;b=2;c=5;d=6 which changes with every frame.... and using the above command how can I draw rectangle by putting these variables instead numbers.
Thank you

답변 (2개)

Harish Ramachandran
Harish Ramachandran 2017년 12월 19일
Hi Sachin,
1. Using Rectangle: You can use variable names instead of values in the 'rectangle' command. You have the values for the following:
  • x_low, y_low - First two arguments for the rectangle command
  • (x_high - x_low), (y_high - y_low) - Final two arguments for the rectangle command
x_low = 1;
y_low = 2;
x_high = 6;
y_high = 8;
axis([0 10 0 10]);
rectangle('Position',[x_low y_low (x_high-x_low) (y_high-y_low)])
is equivalent to
axis([0 10 0 10]);
rectangle('Position',[1 2 5 6]);
2. Using plot: You can refer to this link for an alternate way.
  댓글 수: 4
Sachin Dalvi
Sachin Dalvi 2018년 1월 22일
Hi Harish, I have a video of cat walking which i am processing frame by frame, so the values of x_low,x_high,y_low,y_high are different for each frame. binary image(720 x 1280) of last frame is as this
and the corresponding values for bounds are :
Now i want to draw a rectangle within these bounds and crop out this part,but the function rectangle is somehow not working with this. I get error saying 'the value must be 4 element vector' but when I comment out the rectangle function, program runs and the scalar values of bounds can be seen in workspace.
rectangle('Position',[x_low y_low (x_high-x_low) (y_high-y_low)]);
I also tried performing arithmatic operation outside the function and rewrite the function just with single variables as
rectangle('Position',[x_low y_low a b);
but I am getting the same error.
Harish Ramachandran
Harish Ramachandran 2018년 2월 1일
Sorry for the delayed response. I am not sure what the issue is. Based on the values you provided -
x_high = 724;
y_high = 569;
x_low = 265;
y_low = 1;
rectangle('Position',[x_low y_low (x_high-x_low) (y_high-y_low)]);
I am able to draw a rectangle using those values. Can you provide a copy of the code / input? I will try my best to help you out.

댓글을 달려면 로그인하십시오.


Sachin Dalvi
Sachin Dalvi 2018년 2월 6일
Hello Harish, Below is my code and attached is the video file.
clc;
close all;
imtool close all;
clear;
workspace;
fontSize = 22;
% Looking for video file
folder = fileparts(which('cat1.mp4'));
movieFullFileName = fullfile(folder, 'cat1.mp4');
if ~exist(movieFullFileName, 'file')
strErrorMessage = sprintf('File not found:\n%s\nYou can choose a new one, or cancel', movieFullFileName);
response = questdlg(strErrorMessage, 'File not found', 'OK - choose a new movie.', 'Cancel', 'OK - choose a new movie.');
if strcmpi(response, 'OK - choose a new movie.')
[baseFileName, folderName, FilterIndex] = uigetfile('*.mp4');
if ~isequal(baseFileName, 0)
movieFullFileName = fullfile(folderName, baseFileName);
else
return;
end
elsearduino
return;
end
end
try
%--------------Making video object
videoObject = VideoReader(movieFullFileName);
numberOfFrames = 85;
numberOfFramesWritten = 0;
%-------------Reading frames
for frame = 1 : numberOfFrames
thisFrame = read(videoObject, frame);
subplot(1, 2, 1);
imshow(thisFrame);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow;
%-------------Differencing
alpha = 0.4;
if frame == 1
Background = thisFrame;
else
Background = (1-alpha)* thisFrame + alpha * Background;
end
differenceImage = thisFrame - uint8(Background);
grayImage = rgb2gray(differenceImage);
%--------------Get threshold.
thresholdLevel = graythresh(grayImage);
%-------------Do the binarization
binaryImage = imbinarize(grayImage, thresholdLevel);
%----getting vertical and horizontal bounds
vertprofile = any(binaryImage>0,1);
horzprofile = any(binaryImage>0,2);
x_low = find(vertprofile, 1, 'first');
x_high = find(vertprofile, 1, 'last');
y_low = find(horzprofile, 1, 'first');
y_high = find(horzprofile, 1, 'last');
%------Plot the binary image.
subplot(1, 2, 2);
imshow(binaryImage);
hold on;
rectangle('Position',[x_low y_low (x_high-x_low) (y_high-y_low)]);
title('Binarized Difference Image', 'FontSize', fontSize);
end
catch ME
%--------Some error happened if you get here.
strErrorMessage = sprintf('Error extracting movie frames from:\n\n%s\n\nError: %s\n\n)', movieFullFileName, ME.message);
uiwait(msgbox(strErrorMessage));
end

카테고리

Help CenterFile Exchange에서 Red에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by