![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1824538/image.png)
How to use index an image using color inequality?
6 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (1 件)
Ayush Aniket
2025 年 1 月 29 日 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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1824538/image.png)
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.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!