How to draw many rectangles by sliding window?
2 views (last 30 days)
Show older comments
I have a sliding window on a picture, which counts numbers of pixels from two groups and computes them. It gives pic in return for every sliding window. If pic is greater than 0.666 it turns red but as it moves I don't get an image with red rectangles in places with pic>0.666. And that's what I want to achieve.
Any ideas?
This is my code:
I = imread('image.tif');
%image size
imH = size(I, 1);
imW = size(I, 2);
%sliding window size
windowWidth = 525;
windowHeight = 525;
%step
step = 525;
for r = 1:step:imH - windowHeight + 1
for c = 1:step:imW - windowWidth + 1
%sliding window value
W = I(r:r + windowHeight - 1, c:c + windowWidth - 1, :);
pos = [c r windowHeight windowWidth];
R = W(:,:,1);
G = W(:,:,2);
B = W(:,:,3);
% RGB intervals for every sliding window
matchNh = (R > 45 & R < 180)&...
(G > 50 & G < 185)&...
(B > 160 & B < 215);
matchNd = (R > 40 & R < 115)&...
(G > 6 & G < 80)&...
(B > 10 & B < 75);
Nh = nnz(matchNh);
Nd = nnz(matchNd);
pic = Nd / (Nh + Nd);
if pic > 0.666
subplot(121); imshow(I); title 'Image';
hold on;
rectangle('Position', pos, 'FaceColor', '(1 0 0)');
% here I get rectangle when pic>0.666How to draw the rectangle
% to get an image with all rectangles in the places where pic is
% greater than 0.666? Is there a command to keep the rectangle and move?
end
end
2 Comments
Image Analyst
on 6 Feb 2019
Note: aneta, for some reason, got rid of the original question and replaced it with one where the answers below no longer make sense.
Answers (1)
Image Analyst
on 13 Jan 2019
You forgot to attach your image.
Why not simply use the Color Thresholder app on the Apps tab of the tool ribbon?
Or else vectorize your code?
1 Comment
Image Analyst
on 13 Jan 2019
Well it might work but it would be easier if you used the Color Thresholder.
Also, see these links:
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!