How to draw many rectangles by sliding window?

2 ビュー (過去 30 日間)
Aneta Ch
Aneta Ch 2019 年 1 月 13 日
コメント済み: Rena Berman 2019 年 3 月 5 日
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 件のコメント
Image Analyst
Image Analyst 2019 年 2 月 6 日
Note: aneta, for some reason, got rid of the original question and replaced it with one where the answers below no longer make sense.
Rena Berman
Rena Berman 2019 年 3 月 5 日
(Answers Dev) Restored edit

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2019 年 1 月 13 日
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?

Community Treasure Hunt

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

Start Hunting!

Translated by