Change color from a circle form in an image

2 次查看(过去 30 天)
Hello. I have to find circles in an image and to change their colors. I found the circles, but I don't know how to detect the color and change.

回答(3 个)

Luca Ferro
Luca Ferro 2023-1-6
  1 个评论
DGM
DGM 2023-1-6
Regarding the latter topic, this answer also applies and includes links to several other demos on various means to change colors.

请先登录,再进行评论。


Image Analyst
Image Analyst 2023-1-6
编辑:Image Analyst 2023-1-6
The color is the RGB values of the image array. Not sure what you mean by detect, but if you want a mask of certain colors, then try using the Color Thresholder on the Apps tab of the tool ribbon. Try HSV color space and click the button to export code.

DGM
DGM 2023-1-6
编辑:DGM 2023-1-6
I'm just going to throw this out there as one idea. You could also pick colors in a region near the known center (if you can rely on the center being clear).
This uses MIMT tools, but the links include examples that don't use MIMT. The core idea here is just a means to find the colored regions by identifying the most common color within each circle. This isn't really a good approach generally, but for this case with broad regions of uniform color.
% so you have an RGB image
inpict = imread('circlecircle.png');
inpict = im2double(inpict); % this is needed later
% say you already know where the circles are regardless of their color
C = [68 74; 134 115];
R = [51 58];
% create an indexed copy to simplify color picking
[indpict map] = rgb2ind(inpict,64);
% preallocate mask
ncircles = numel(R);
mask = false([size(indpict) ncircles]);
% sample those locations based on the circle geometry
imshow(inpict); % set up axes to use ROI tools
for b = 1:ncircles
% create mask
ROI = images.roi.Circle(gca);
ROI.Center = C(b,:);
ROI.Radius = R(b);
circmask = createMask(ROI);
% sample region in indexed copy
samplepx = indpict(circmask);
% find color of most common index
% integer-class indexed images index from zero
modecolor = map(mode(samplepx)+1,:)
modecolor = permute(modecolor,[1 3 2]);
% find all pixels in the ROI close to the most common color in the ROI
tol = 0.1; % pick some tolerance
thismask = all(abs(inpict-modecolor) <= tol,3) & circmask;
mask(:,:,b) = imclose(thismask,ones(3)); % fill in thin lines (optional)
end
% now you have masks for the colored region within each circle or partial circle
% you can use those in whatever way you choose
% alter the hue in each region
hueshift = rand(2,1); % amount to shift hue for each blob (random example)
outpict = inpict;
for b = 1:ncircles
% these are both from MIMT, but there are non-MIMT examples
modpict = imtweak(outpict,'hsl',[hueshift(b) 1 1]);
outpict = replacepixels(modpict,outpict,mask(:,:,b));
end
imshow(outpict)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by