How do I change an image into 2 main colors?

11 次查看(过去 30 天)
I have an image that I am drawing using an arduino drawing robot, and I need to sort the image into 2 main colors (not including the background) so that I can use 2 markers to draw the image? How do I sort the photo into 2 colors (left marker and right marker)?
img2 = rgb2gray(img);
imshow(img2)
img3 = ~imbinarize(img2,'adaptive','ForegroundPolarity','dark',.5);
imshow(img3)
img4 = bwmorph(img3,'remove');
imshow(img4)
img5 = bwmorph(img4,'thin',inf);
imshow(img5)

回答(2 个)

Image Analyst
Image Analyst 2021-5-8
Use rgb2ind(), I believe it's something like
[indexedImage, clrmap] = rgb2ind(rgbImage, 2); % Get 2 main colors.
  3 个评论
Image Analyst
Image Analyst 2021-5-9
Yes, very unfortunate that he forgot to post the image. There are lots of algorithms to segment a continuous 3-D color gamut into two clusters. rgb2ind() is just one (using minimum variance quantization) - there are others if you don't think it does a good job.
In addition, after color segmentation you can clean up the image to reduce noise with morphological operations such as bwareaopen(), bwareafilt(), etc.
DGM
DGM 2021-5-9
The only reason I brought that up was to question whether it's best to quantize based on dominant colors or some specific predetermined colors. I guess you could use a prescribed colormap too, though it might be hard to deal with illumination nonuniformity. Maybe I'm entirely overthinking this for a simple robot demo.

请先登录,再进行评论。


DGM
DGM 2021-5-9
Not knowing how the FG is presented, it's anybody's guess how it should be processed. I would assume that the markers are colored already, so maybe I'd just try some color-based segmentation and get two masks. If it's preferable to have the masks combined into a color image either for human readability or for use as a label array, then that can be done:
maskL = % find this somehow
maskR = % find this somehow
mint = maskR & maskL;
maskL(mint) = 0; % remove intersection ambiguities
maskR(mint) = 0;
% generate 3-level indexed image/label array
indexedimg = uint8(maskL + maskR*2);
cmap = [0 0 0; 1 0 0; 0 0 1]
imshow(indexedimg,cmap)
I don't know what the canonical approach would be for these sorts of exercises.

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by