Vectorize loop for image manipulation

1 Ansicht (letzte 30 Tage)
Clay Swackhamer
Clay Swackhamer am 11 Mär. 2017
Kommentiert: Clay Swackhamer am 11 Mär. 2017
I have a black and white image (Ibw), and I want to take a circular region from it, and place it on a white background. I made a mask with a one in each position where I want to keep the pixel in the image (inside the circular region).
The code below works... but I want to vectorize it if possible. Right now I step across the image pixels one at a time, and if the mask has a one, I put the value in the black and white image onto the white field (Ifield).
Ifield = ones(size(Ibw));
for i = 1:1:rows
for j = 1:1:cols
if mask(i,j) > 0
Ifield(i,j) = Ibw(i,j);
else
%Let Ifield keep its 1 (white)
end
end
end

Akzeptierte Antwort

Image Analyst
Image Analyst am 11 Mär. 2017
Try this vectorized way:
Ifield = ones(size(Ibw));
Ifield(mask) = Ibw(mask);
  1 Kommentar
Clay Swackhamer
Clay Swackhamer am 11 Mär. 2017
It works- and it's much easier on the eyes. Thanks ImageAnalyst!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Create Block Masks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by