How to detect and separate pixels in an image
5 views (last 30 days)
Show older comments
Jacob Lancianese
on 6 May 2021
Commented: Jacob Lancianese
on 6 May 2021
I want to take an image and detect pixels that are green, which I've accomplished. But I also want to keep their color and seprate them such that I can then take the rest of the image and convert it to greyscale. Here is the code I have currently that detects the green pixels:
function [ result ] = detectGreen( rgbimage )
%set the threshold of green in the hue channel
GREEN_THRESHOLD = [65,170]/360;
INTENSITY = .1;
%convert image to HSV color space
hsv = rgb2hsv(rgbimage);
%find pixels in the relevent area that are in the range in H and V channels
greenMask = hsv(:,:,1)>GREEN_THRESHOLD(1) & hsv(:,:,1)<GREEN_THRESHOLD(2) & hsv(:,:,3) > INTENSITY;
%return the green pixels
result = greenMask;
end
Currently, this takes the color image and processes it to be like the second image:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/608725/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/608730/image.png)
Is there a way that I can keep the green color of those detected pixels such that I can then convert the whole image to grey scale and overlay the detected white on top so that the only thing in color is the green of the field?
0 Comments
Accepted Answer
Subhadeep Koley
on 6 May 2021
rgbImg = imread('image.png');
grayImg = rgb2gray(rgbImg);
greenMask = detectGreen(rgbImg);
figure
imshow(rgbImg);
hold on
han = imshow(grayImg);
hold off
set(han, 'AlphaData', ~greenMask);
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!