How can i convert an Indexed Image To a Binary Image ?

3 visualizaciones (últimos 30 días)
Mallouli Marwa
Mallouli Marwa el 20 de Jul. de 2017
Comentada: Walter Roberson el 15 de Jun. de 2025
Hello
How can i convert the blue into white and the other colours into black ?
The image is attached.

Respuestas (1)

DGM
DGM el 15 de Jun. de 2025
The attached image is a JPG; as such, it is not an indexed image. So we now have two interpretations of the question:
"The image is actually an indexed image, and I want one specific color to be the foreground"
[inpict,CT] = imread('op1.png'); % an actual indexed image
imshow(inpict,CT)
% select the medium purple regions
fgcolor = 2; % the index of the chosen color
mask = inpict == fgcolor; % an exact selection
imshow(mask)
"The image is a lossy pseudocolor image, and I want one specific color to be the foreground"
inpict = imread('op1.jpg'); % a low-quality JPG of the same image
imshow(inpict)
% an exact selection won't work with all the error
fgcolor = [152 58 143]; % the chosen color
mask = all(inpict == permute(fgcolor,[1 3 2]),3); % an exact selection
imshow(mask)
% try using a tolerance
tol = 0.1; % pick some tolerance
mask = all(abs(im2double(inpict) - permute(fgcolor/255,[1 3 2])) < tol,3);
imshow(mask)
  1 comentario
Walter Roberson
Walter Roberson el 15 de Jun. de 2025
As the original poster asks about converting "blue" into white, my interpretation is that they want all of the shades of blue to be turned into white. It is not immediately clear whether they want the intensity of the white to be equal to the intensity of the corresponding blue, or if they want all shades of blue to come out as the same shade of white.
In order to convert all shades of blue, I would suggest converting the RGB version of the image into HSV, and then look for locations where the hue is in the range 0.667 +/- 0.0625 (or so). The exact hue range to select is a bit uncertain -- how close to cyan do you want to allow, and how close to purple do you want to allow?
If you did want the shade of white to be the same as the shade of blue, then I would construct an HSV array with Hue and Saturation all 0, and with Value the same as corresponding "blue" pixels. Indeed if you were to do that and also set the Value for the unselected pixels to 0, then hsv2rgb would give the final result.
TheRGBImage = imread('ductile.JPG');
HSV = rgb2hsv(TheRGBImage);
H = HSV(:,:,1);
mask = (2/3 - 1/16) <= H & H <= (2/3 + 1/16);
S = HSV(:,:,3);
NewS = zeros(size(S),'like',S);
NewS(mask) = S(mask);
NewHSV = cat(3, zeros(size(HSV,1),size(HSV,2),2), NewS);
newRGB = hsv2rgb(NewHSV);
subplot(2,1,1); imshow(TheRGBImage);
subplot(2,1,2); imshow(newRGB);

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by