1

Hi I have code which puts a sliding window over an image in Matlab. If the pixels within the sliding window meet certain conditions then I need to highlight that sliding window on the original image by putting a rectangle around it.

Can anyone explain to me how to do this?

Thanks.

 if average>200
         N2=8;
         info2 = repmat(struct, ceil(size(Z, 1) / N2), ceil(size(Z, 2) / N2)); 
         for row1 = 1:N2:size(Z, 1)%loop through each pixel in the 8x8 window
             for col1 = 1:N2:size(Z, 2)
                 x = (row1 - 1) / N2 + 1;
                 y = (col1 - 1) / N2 + 1;

                 imgWindow2 = Z(row1:min(end,row1+N2-1), col1:min(end,col1+N2-1));
                 average2 = mean(imgWindow2(:)); %calculate mean intensity of pixels
                 window2(x,y).average=average2;
                % display(window2(x,y).average);


                % if the intensity of the 8x8 window is greater than
                % 210 then considered suspicious-
                 if average2>210
                 %%%% THEN HIGHLIGH THIS WINDOW ON THE ORG IMAGE (Z)
                 end 
             end
         end
user1853871
  • 249
  • 5
  • 21

1 Answers1

0

If you want to display this in a figure, you can use the imrect function. If you want to draw the rectangle into the image itself, and you have the Computer Vision System Toolbox, you can use insertShape or insertObjectAnnotation functions.

Dima
  • 38,860
  • 14
  • 75
  • 115
  • Thanks! How do I specify where on the original image to place this rectangle? I have updated my question so that you can see how I have created my sliding window. – user1853871 Apr 22 '14 at 15:00
  • Look at the doc for these functions. They all take the rectangle specified as [x, y, width, height], where [x,y] are the coordinates of the upper-left corner. – Dima Apr 22 '14 at 15:30
  • Tired this Z1 = insertShape(Z, 'circle', [x y 8], 'LineWidth', 5); using the x and y position for the window shown in the code above but I am getting an error : Error in BeastSeg2 (line 209) Z1 = insertShape(Z, 'circle', [150 280 35], 'LineWidth', 5); – user1853871 Apr 22 '14 at 15:40
  • What was the error? My guess is that you may have an older version of MATLAB, which doesn't support the 'LineWidth' parameter. – Dima Apr 22 '14 at 17:00
  • yes sorry forgot to show the error : Argument 'LineWidth' did not match any valid parameter of the parser. Is there another way of doing this? – user1853871 Apr 22 '14 at 17:30
  • Just don't use LineWidth. You will get a circle 1 pixel thick. – Dima Apr 22 '14 at 18:39
  • thanks. A rectangle now prints on the image but not in the correct position. I have posted a new question, I wonder if you could help me with this? – user1853871 Apr 23 '14 at 18:40
  • Check that you didn't switch x and y. As for your new question, a link would help. – Dima Apr 23 '14 at 21:25
  • http://stackoverflow.com/questions/23244167/matlab-put-rectangle-on-image, sorry forgot the link the first time. – user1853871 Apr 23 '14 at 22:23