Search code examples
matlabimage-processingvideo-processingparking

Plot rectangle boxes on image using MATLAB


Here I am developed some code for plotting boxes on image but I am getting
boxes on different image. All the boxes should be in same image. Please help me in this.

output image 1

output image 2

video = VideoReader('parking video1.mp4');
I = read(video,1);
J = read(video,200);
a=104; b=73;
c=104; d=515;
count=0;count1=0;count2=0;
total=10;

for i=1:5

im1=imcrop(I,[a,b,283, 448]);

im3=imcrop(J, [a,b,283, 448]); 

Background1 =abs(im1 - im3);

grayImage1 = rgb2gray(Background1);
% Convert to gray level

 thresholdLevel1 = graythresh(grayImage1);
    % Get threshold.

binaryImage1 = im2bw( grayImage1, thresholdLevel1);
   % Do the binarization


binaryImage1 = bwareaopen(binaryImage1,1000);



ak=bwarea(binaryImage1);


figure, imshow(J);
hold on;  

   if ak>0


     rectangle('Position',[a,b,283, 448],'Edgecolor', 'r');
   else

     rectangle('Position',[a,b,283, 448],'Edgecolor', 'g');


   end
a=a+280;  
end

Solution

  • You are opening a new window every time your run the code. You should specify which figure window to use before showing it.

    So instead of this:

    figure, imshow(J);
    

    do this:

    figure(1), imshow(J);
    

    That should show the image in the same figure window (figure number 1) every time.