How do I change an image into 2 main colors?

11 vues (au cours des 30 derniers jours)
Seth Yoder
Seth Yoder le 7 Mai 2021
Commenté : DGM le 9 Mai 2021
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)

Réponses (2)

Image Analyst
Image Analyst le 8 Mai 2021
Use rgb2ind(), I believe it's something like
[indexedImage, clrmap] = rgb2ind(rgbImage, 2); % Get 2 main colors.
  3 commentaires
Image Analyst
Image Analyst le 9 Mai 2021
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 le 9 Mai 2021
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.

Connectez-vous pour commenter.


DGM
DGM le 9 Mai 2021
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.

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