Trying to save the red positions of an image
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm trying to save all the position's of an image with red color.
im = imread('image.png');
locations = find(im(:,:,1) == 36)
This code is giving me an empty array. Why this is happening? How can I fix it?
0 个评论
采纳的回答
Image Analyst
2020-8-7
Oliver, this works:
rgbImage = imread('image.png');
subplot(1, 2, 1);
imshow(rgbImage);
impixelinfo; % Let's you mouse around and see rgb values.
% Split image into components:
[r, g, b] = imsplit(rgbImage);
% Red spots are r=237, g=28, b=36.
% Get red mask
redMask = r > 200 & g < 50 & b < 50;
subplot(1, 2, 2);
imshow(redMask);
% Get rows and columns where the red pixels are
[redRows, redColumns] = find(redMask)
redRows and redColumns are paired row,column coordinates for each pixel that is red in the image, in column-major order. Not sure what you're going to do with that, but there you have it.
2 个评论
Image Analyst
2020-8-7
You have an old version. Try this
r = rgbImage(:, :, 1);
g = rgbImage(:, :, 2);
b = rgbImage(:, :, 3);
更多回答(1 个)
Sudheer Bhimireddy
2020-8-7
Does your image has that Red value? Double-check it.
To test your code, follow this example.
A = imread('ngc6543a.jpg'); % Read MATLAB example figure
A_read = A(:,:,1); % Read Red channel
A_ind = find(A_read == 36); % Find non-zeros where the value is equal to 36
A_sz = size(A_read);
[A_row,A_col] = ind2sub(A_sz,A_ind); % Convert linear indices to matrix subscripts
If you change the above find statement to
A_ind = find(A_read == 3600);
you will get an empty A_ind array because there exists no such value inside A_read.
10 个评论
Sudheer Bhimireddy
2020-8-7
To understand that, you need to understand the way image color data is stored in MATLAB.
For an RGB image, if you are storing the pixel values in a [m,n,3] array; where m and n gives the size of the image. If you are storing them in unit8 format, they can varry from [0 255] and if you store them in double format they varry from [0 1].
Now, lets say pixel at index (10,10) in your image is filled with red alone, then the corresponding cell values will be (in unit8 format)
(10,10,1) = 255;
(10,10,2) = 0;
(10,10,3) = 0;
Similarly if it is filled with blue alone, they will have values
(10,10,1) = 0;
(10,10,2) = 0;
(10,10,3) = 255;
So, the first channel of the image contains the amount of Red color on a scale of [0 255] at each pixel.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!