How do I change an image into 2 main colors?

11 visualizaciones (últimos 30 días)
Seth Yoder
Seth Yoder el 7 de Mayo de 2021
Comentada: DGM el 9 de Mayo de 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)

Respuestas (2)

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

Iniciar sesión para comentar.


DGM
DGM el 9 de Mayo de 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.

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by