Can I change certain colors within an image to another color?

29 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2010 年 7 月 9 日
回答済み: DGM 2023 年 1 月 7 日
I have an image that I want to modify. I want to find all the pixels of one particular color value and set them to another color.

採用された回答

MathWorks Support Team
MathWorks Support Team 2010 年 7 月 9 日
There is a MATLAB file that allows you to change certain colors within images to other specified colors.
The file, find_color.m, (located at the bottom of the page) converts the original image into RGB format and then finds a specified color in that image. It then returns a second image with the pixels of the specified color set to zero as well as an index to these pixels. You can specify the color to find in the function call or by clicking on the color in the original image.
NOTE: This MATLAB file is meant as an example. You will need to modify it in order to suit your specific needs.

その他の回答 (1 件)

DGM
DGM 2023 年 1 月 7 日
The attached function is simple, but it works. It works by considering a specified RGB tuple and doing a range (box) selection in RGB.
The following wouldn't have worked in 2010, but the expansion could have been done with bsxfun(). This demonstrates both box and simple distance selection in RGB (broadly selecting the cyan near the image center). The same concept can be applied to other color models.
% inputs
inpict = imread('rainbow.png'); % uint8
pickedcolor = [0.5 1 1]; % unit-scale double
tol = [0.2 0.3 0.3]; % pick some tolerance per axis
% show the image
imshow(inpict)
% prepare inputs
pickedcolor = permute(pickedcolor,[1 3 2]); % reorient tuple along page axis
tol = permute(tol,[1 3 2]); % reorient tuple along page axis
inpict = im2double(inpict); % unit-scale double
% perform box (range) selection in RGB
boxmask = all(abs(inpict-pickedcolor) <= tol,3);
imshow(boxmask)
% perform ellipsoid (euclidean distance) selection in RGB
distmask = sum(((inpict-pickedcolor)./tol).^2,3) <= 1;
imshow(distmask)
Alternatively, max/min limits can be specified per channel and a mask formed that way. See also the Color Thresholder app.
To change the color in the selected region, see the following:
My typical interpretation of "change the color" is not "replace the color", though that could be simple enough if using MIMT.
A = replacepixels([0.8 0.3 1],inpict,distmask); % apply new color to the selected region
Though normally, I'd say that "change the color" means exactly that
aa = imtweak(inpict,'hsl',[-1/3 1 -1]); % rotate hue, invert lightness
B = replacepixels(aa,inpict,distmask); % apply to the selected region
The links above include some examples of doing color adjustments without MIMT, but they're generally cumbersome and limited to the available color models. Feel free to use whatever tools you prefer.

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by