MATLAB Answers

How do I extract the pixels of yellow pixels,purple pixels from the image and display it in a white image?

17 views (last 30 days)
Varun  Subramaniam
Varun Subramaniam on 18 Dec 2019
Answered: Gaurav Garg on 23 Dec 2019
Ive attached the code for extracting the R pixels for a particular range and displayed it in a black image
rgbimage = imread('sample1.jpeg');
m = size(rgbimage);
R = rgbimage(:,:,1);
G = rgbimage(:,:,2);
B = rgbimage(:,:,3);
imtool(rgbimage);
%redplane
[x1,y1]=find(and(R>=230,R<=250));
t = zeros ([977 1280 3]);
t = cast(t,'uint8');
a= size(x1);
b = size(y1);
for i=1:a
t(x1(i),y1(i),1) = rgbimage(x1(i),y1(i),1);
end
imtool(t);
figure();
imhist(t);

  0 Comments

Sign in to comment.

Answers (1)

Gaurav Garg
Gaurav Garg on 23 Dec 2019
Hi,
You can use ‘Color Thresholder’ app provided in the Image Acquisition Toolbox.
  1. After loading the image from a file/workspace, you can choose a color space for the image. Since you want to extract purple and yellow colours, I would recommend you to use YCbCr for purple colour and RGB space for the yellow colour.
  2. You would then see the image along with a set of controls for each colour component, depending on the colour space chosen. Adjust these controls to get the colour you want. And ‘Export Function’ for the resultant image.
  3. By exporting to function, you get Min and Max values for each channel for both the colours (yellow and Purple). You would also get a sliderBW matrix in each function. Use logical operator || to include both the matrices in your workspace and then set these pixels to white colour.
You can also refer to an example for purple colour from the documentation provided in the link.
Below is the code for an image file named ‘peppers.png’ and may need to be changed in some cases -
RGB = imread('peppers.png');
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 226.000;
channel1Max = 255.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 104.000;
channel2Max = 236.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 115.000;
% Create mask based on chosen histogram thresholds
sliderBW1 = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
I = rgb2ycbcr(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 50.000;
channel1Max = 71.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 68.000;
channel2Max = 204.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 88.000;
channel3Max = 164.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
res = sliderBW|sliderBW1;
maskedRGBImage = RGB;
maskedRGBImage(repmat(res,[1 1 3])) = 255;

  0 Comments

Sign in to comment.


Translated by