4
$\begingroup$

I have a rasterized image img that is created by this code:

img =  
   Image @ 
      Graphics[
        {Disk[], Disk[{4, 4}, 1.5], Disk[{-4, 5}, 2], Disk[{0, -6}, 4]},
        PlotRange -> 11]

enter image description here

I want to color the circles depending on their size (near white for the biggest, going through darker grays until the smallest gets colored black, or something similar).

I know it has something to do with MorphologicalComponents, but I don't know how to colorize the different components.

EDIT:

Actually the input image was not the circle one, it was this:

enter image description here

And when I run your code, even though the points image gets good results, this one gives some error. The output is this:

enter image description here

Which doesn't recognize the areas of any squares.

$\endgroup$
  • $\begingroup$ Colorize[m=MorphologicalComponents[ColorNegate@Binarize@img], ColorRules -> Join[{0 -> White}, Thread[SortBy[ComponentMeasurements[m, "FilledCount"], Last][[All, 1]] -> GrayLevel /@ {.1, .6, .8, .9}]]] - fiddle with values to taste... $\endgroup$ – ciao Aug 22 '15 at 22:45
  • $\begingroup$ @ciao Any way this can be extended for n objects instead of given 4? $\endgroup$ – Garmekain Aug 22 '15 at 23:58
3
$\begingroup$

You can use ArrayPlot

ArrayPlot[MorphologicalComponents[ColorNegate[i]]]

enter image description here

or you can use a ColorFunction. Here is a version for the second input image with the smaller regions using darker colors.

mimg = MorphologicalComponents[Import["http://i.stack.imgur.com/w4FkO.png"]];
map = Thread[
   Ordering@ComponentMeasurements[mimg, "Area"][[All, 2]] -> 
    Range[Max[mimg]]];
areas = mimg /. map;
Colorize[areas, ColorFunction -> "GrayTones"]

enter image description here

$\endgroup$
  • $\begingroup$ I actually do care about the colors. I want the smallest circle to be black, and the biggest circle white, and the middle ones with a level of gray, depending on its area. $\endgroup$ – Garmekain Aug 22 '15 at 22:42
  • $\begingroup$ Even as revised doesn't give the correct color dependency on area. $\endgroup$ – m_goldberg Aug 22 '15 at 22:54
  • $\begingroup$ With that last method, I noticed the colors goes from white to black starting from top to bottom. I actually need them to be in order from the smallest to the biggest. $\endgroup$ – Garmekain Aug 22 '15 at 22:54
  • $\begingroup$ I believe I've fixed the "direction" of the colors. $\endgroup$ – bill s Aug 24 '15 at 14:08
4
$\begingroup$

I started with ciao's code, but I didn't like the way the gray level values were obtained, so I re-worked his code to this:

img = 
  Image @ 
    Graphics[
      {Disk[], Disk[{4, 4}, 1.5], Disk[{-4, 5}, 2], Disk[{0, -6}, 4]},
      PlotRange -> 11]

With[{m = MorphologicalComponents[ColorNegate @ Binarize @ img]},
   Module[{items, n, areas, lvls},
     items = ComponentMeasurements[m, "FilledCount"];
     n = Length @ items;
     areas = items[[All, 2]];
     lvls = 
       Thread[
         Rule[
           Range @ n, 
           GrayLevel @ Sqrt @ Rescale[#, MinMax[areas], {.05, .85}] & /@ areas]];
     Colorize[m, ColorRules -> Join[{0 -> White}, lvls]]]]

colorized

$\endgroup$
  • $\begingroup$ I changed "FilledCount" with "Elongation" and it worked perfectly. $\endgroup$ – Garmekain Aug 23 '15 at 1:22
3
$\begingroup$

Sorry for my poor English to may be failure understand demand of your question.And as I understand the curently accepted answer have not at par slightly?


  • The first question

color the circles depending on their size (near white for the biggest, going through darker grays until the smallest gets colored black, or something similar)

This is my solution

img = Import["http://i.stack.imgur.com/CqRZd.png"] // ColorNegate;
MorphologicalComponents[img] // 
 Colorize[#, 
   ColorRules -> 
    Prepend[Normal[
      Blend[{Black, 
          White}, #] & /@ (ComponentMeasurements[img, "Area"] // 
          Association // (# - Min[#])/(Max[#] - Min[#]) &)], 
     0 -> Red]] &

enter image description here

(I set the background red is for conveniently looking the circle of White to Black,otherwise your white circle will be invisible.)


  • The second question

Which doesn't recognize the areas of any squares.

So you just colorize the squares or I take a wrong interpretation?But those place isn't a squres?

enter image description here

So I post a solution like follow

array = MorphologicalComponents[
   pic = Import["http://i.stack.imgur.com/w4FkO.png"]];
effect = SelectComponents[
   array, {"PerimeterLength", 
    "EquivalentDiskRadius"}, #/#2 - (4*Sqrt[Pi] // N) <= 2.8 &] // 
  Colorize[#, 
    ColorRules -> 
     Prepend[Normal[
       Blend[{Black, 
           White}, #] & /@ (ComponentMeasurements[#, "Area"] // 
           Association // (# - Min[#])/(Max[#] - Min[#]) &)], 
      0 -> Red]] &

enter image description here

ImageCompose[pic, {effect, 0.8}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.