How to use index an image using color inequality?

6 visualizaciones (últimos 30 días)
Inderdeep Grewal
Inderdeep Grewal el 16 de Jul. de 2018
Respondida: Ayush Aniket el 29 de En. de 2025 a las 11:44
I have an image that I would like to turn black or white depending on a user defined color RGB value associated with the scale in the image. How would I do this?

Respuestas (1)

Ayush Aniket
Ayush Aniket el 29 de En. de 2025 a las 11:44
One of the ways you can do this is by computing the Euclidean distance between each pixel's color and the target RGB, and then convert the image to black and white based on a threshold applied to the distance. Refer to the code snippet below:
% Step 1: Read the image
img = imread('5xx.jpg');
% Step 2: Define the target RGB color
targetColor = [0, 124, 255]; % Replace R, G, B with your target RGB values
% Step 3: Calculate the distance from the target color
distance = sqrt((double(img(:,:,1)) - targetColor(1)).^2 + ...
(double(img(:,:,2)) - targetColor(2)).^2 + ...
(double(img(:,:,3)) - targetColor(3)).^2);
% Step 4: Define a threshold
threshold = 50; % Adjust this value based on your needs
% Step 5: Create a binary (black and white) image
bwImage = distance < threshold;
% Display the result
imshow(bwImage);
This approach allows you to highlight areas of the image that are similar to a specific color by turning them white, while other areas become black.

Categorías

Más información sobre Images en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by