How to use index an image using color inequality?

1 vue (au cours des 30 derniers jours)
Inderdeep Grewal
Inderdeep Grewal le 16 Juil 2018
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?

Réponses (1)

Ayush Aniket
Ayush Aniket le 29 Jan 2025
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.

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by