How do i scan pixel from an rgb image and then if red value is higher than 200 show red only and if its lower = all 3 colors = 0

 採用された回答

David Fletcher
David Fletcher 2021 年 5 月 22 日
編集済み: David Fletcher 2021 年 5 月 22 日
May not be the best answer - I don't do much image manipulation work
%load and display image
imageData=imread('test.jpeg');
subplot(2,1,1)
imshow(imageData);
%Create mask for red colour data >200
redMask=imageData(:,:,1)>200;
%Apply mask to red channel
red=imageData(:,:,1);
red=red.*cast(redMask,'like',imageData);
%Clear colour data from all channels
imageData=imageData*0;
%Replace red channel with the masked data
imageData(:,:,1)=red;
%Show image with red channel mask mask
subplot(2,1,2)
imshow(imageData);

10 件のコメント

I need using for loops please .
David Fletcher
David Fletcher 2021 年 5 月 23 日
編集済み: David Fletcher 2021 年 5 月 23 日
It is fairly easy to convert this to a for loop solution (or even two for loops), but you should give it a go yourself - after all that is the whole purpose of homework. You will gain nothing by just being handed a solution. Set up two for loops to iterate through the pixels on a colour plane (pixels on the other two colour planes can be checked and adjusted at the same time). Check back if you have problems.
Moe Makki
Moe Makki 2021 年 5 月 23 日
編集済み: Moe Makki 2021 年 5 月 23 日
originalImage = imread('Colors.jpg');
[rows, columns, numberOfColorChannels] = size(originalImage);
if numberOfColorChannels == 3
for column = 1 : columns
for row = 1 : rows
redValue = originalImage(row, column, 1);
greenValue = originalImage(row, column, 2);
blueValue = originalImage(row, column, 3);
if redValue>200
redValue=255;
greenValue=0;
BlueValue=0;
else if redValue<200
redValue=0;
greenValue=0
BlueValue=0
end
end
end
end
end
imshow('Colors.jpg');
what is wrong in this code
David Fletcher
David Fletcher 2021 年 5 月 23 日
編集済み: David Fletcher 2021 年 5 月 23 日
Good try. Just a few things - there is no point in getting the value of pixels in the green and blue layers: they are of no interest, they do not need to be checked. The if statement for checking the red value doesn't need an extra else if clause: either the red value is over 200 or it isn't - there is only need for an if else to cover both situations. When you are assigning colour values, you need to apply them to the image pixels, not to the variables you have assigned to check the values of the pixels. Try this
originalImage = imread('Colors.jpg');
[rows, columns, numberOfColorChannels] = size(originalImage);
if numberOfColorChannels == 3
for column = 1 : columns
for row = 1 : rows
redValue = originalImage(row, column, 1);
if redValue>200
originalImage(row, column, 1)=255;
originalImage(row, column, 2:3)=0;
else
originalImage(row, column, 1:3)=0;
end
end
end
end
imshow(originalImage);
@David Fletcher Thanks for making it clearer
but the output is still the same image
blue and green colors are still there
is it because of the if statement ?
in the workspace i can see the redValue=35
which means the (else) statement should work = all colors = 0 = black image
it still shows the normal image
what do you think is the problem here
Is this still your code, or the code I put down above? When I run my amended code on the image I get the results shown above (which is I believe what you want).
oops my bad there was an error with imshow i replaced it with the same picture
Thanks for your help appreciated.

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

その他の回答 (1 件)

Try this:
% Find where red value is more than 200.
mask = rgbImage(:, :, 1) > 200;
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));

6 件のコメント

@Image Analyst it is possible to use two for loops and if yes how please ? thanks for your answers
originalImage = imread('Colors.jpg');
[rows, columns, numberOfColorChannels] = size(originalImage);
if numberOfColorChannels == 3
for column = 1 : columns
for row = 1 : rows
redValue = originalImage(row, column, 1);
greenValue = originalImage(row, column, 2);
blueValue = originalImage(row, column, 3);
if redValue>200
redValue=255;
greenValue=0;
BlueValue=0;
else if redValue<200
redValue=0;
greenValue=0
BlueValue=0
end
end
end
end
end
imshow('Colors.jpg');
@Moe Makki, Yes, but why would you want to do it that less efficient way? The best answer is the two line snippet I gave you.
@Image Analyst sadly the instructor asked to do it in double for loops . it's not my own choice
Image Analyst
Image Analyst 2021 年 5 月 24 日
編集済み: Image Analyst 2021 年 5 月 24 日
In the future, please "Tag" the post as homework if it's homework (I've done it for you now). That tells us not to post full working code, that, if you turn in as your own, may get you into trouble for plagiarism. We wouldn't want you to get into trouble.
@Image Analyst will do thanks for your lovely advice .
Have a great day sir.

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by