How can I change the red pixel values that are greater than green and blue bands?
1 次查看(过去 30 天)
显示 更早的评论
I have an RGB image, sized (128,128,3). I need to write a for loop and change the pixel values whose red band is larger than their green or blue channels, with white pixels. But I am new to programming and couldn't figure out how to write the for loop. i just have a basic structure;
image2 =ones(128)*255 % a matrix with all white pixel values(255)
for image(128,128,3);
if image(:,:,1) > image(:,:,2:3);
then image(:,:,1) = image2(:,:,1);
end
end
can you help me please?
0 个评论
采纳的回答
Guillaume
2015-3-15
Going through the introduction tutorial in matlab's documentation would have told you the syntax of for loops as well as the syntax for if branching.
There's so many things wrong with your code that I'm not going to explain it. Read the doc. If you were going to use a loop, this would work:
for row = 1:size(image, 1) %iterate over the rows
for col = 1:size(image, 2) %iterate over the columns
if all(image(row, col, 1) > image(row, col, 2:3))
image(row, col, :) = 1;
end
end
end
However, using a loop to do this is extremely inneficient. The following will do exactly the same in less line of codes and less time:
isgreater = image(:, :, 1) > image(:, :, 2) & image(:, :, 1) > image(:, :, 3);
image(repmat(isgreater, 1, 1, 3)) = 1; %replicate isgreater across all three colour channels
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!