7
$\begingroup$

I have a image of 1000 x 1000 pixels of which I have made 5 x 5 partitions. After that I am looking for a way to count number of color pixels in each partition. Thanks in advance for help.

ImagePartition[I,200]//Grid

I got the following partitions:

partition image

$\endgroup$
3
  • $\begingroup$ Don't use I as a variable name. $\endgroup$
    – flinty
    Sep 13, 2020 at 13:41
  • $\begingroup$ Ok thanks for reply, I actually pasted the image in the place of I but I found pasting it here difficult so wrote I instead of the image. $\endgroup$ Sep 13, 2020 at 13:49
  • $\begingroup$ No problem. You can paste the link to this image i.sstatic.net/nmMA6.png from your previous question. I've used that one in my answer. $\endgroup$
    – flinty
    Sep 13, 2020 at 14:05

5 Answers 5

4
$\begingroup$
img = RemoveAlphaChannel@Import["https://i.sstatic.net/nmMA6.png"];
parts = ImagePartition[img, 200];

(* see the documentation here: https://wolfram.com/xid/0enzd2s6c-6u9yga *)
countColors[img_] := Module[{count = 0},
  ImageScan[If[Mean[#] != 1, count++] &, img];
  Return[count]
]

Map[countColors, parts, {2}] //Grid
16306   7574    24259   7669    16459
18185   25874   19051   26063   18207
22254   20128   17908   20212   22234
18035   25878   19046   26033   18066
16216   7063    24078   7175    16327

For colour frequencies per part, this will get you a matrix of associations that count the commonest colours appearing in each. An association contains colour name $\rightarrow$ frequency pairs. I've not used DominantColors because it does clustering and may return inexact colours not present in the image. Instead I've used the 32 most common colours including white as the basis for counting:

cols = TakeLargestBy[Tally[Flatten[ImageData[img], 1]], Last, 32];
NearestColorName = ResourceFunction["NearestColorName"];

tallycols[img_] := Association[
  (RGBColor[#] -> Count[ImageData[img], #, 2]) & /@ cols[[All, 1]]
]

partcols = Map[tallycols, parts, {2}];

You can then have a look at partcols[[2, 3]] for example: colours association

If you need to name them you should use NearestColorName, use ColorData, or use a custom mapping as in an answer to a previous question of yours.

$\endgroup$
3
  • $\begingroup$ This finds the number of pixels in each 200x200 chunk that are not white. $\endgroup$
    – flinty
    Sep 13, 2020 at 13:58
  • $\begingroup$ Thanks for the great help and interest. This is nice to know that in first partition there are 16306 color pixels but can I find that out of these how many are for example pink, indigo, etc. and also want to include white ones also in the counts. $\endgroup$ Sep 14, 2020 at 20:33
  • $\begingroup$ @AtiqueKhan I've updated the answer. You could also just change the function countColors here from Mean[#] != 1 to something else like for example to search for red pixels: # == {1.,0.,0.} $\endgroup$
    – flinty
    Sep 14, 2020 at 22:09
3
$\begingroup$
HowManyNonWhite[i_Image] := (Times @@ ImageDimensions[i]) - 
  Count[ImageData[RemoveAlphaChannel[i]], {1., 1., 1.}, {2}]
Map[HowManyNonWhite,ImagePartition[image,200],{2}]

Where image is the original image.

$\endgroup$
3
$\begingroup$

I would suggest this:

img = Import["https://i.sstatic.net/nmMA6.png"];
parts = ImagePartition[img, 200];

countWhitePixels[im_] := Length@PixelValuePositions[im, White, 0]
200^2 - Map[countWhitePixels, parts, {2}] // MatrixForm

Mathematica graphics

$\endgroup$
2
$\begingroup$

A threshold for "white" sometimes is useful. Here I've set it rather high, and the counts do not change with a threshold higher than 0.9999. Compare with @flinty and @SHuisman, which produce slightly higher counts, to determine which method might be most appropriate.

img = Import["https://i.sstatic.net/nmMA6.png"];
parts = ImagePartition[RemoveAlphaChannel[img], 200]

Map[Total[ImageData@Binarize[#, {0., 0.9999}], 2] &, parts, {2}]
(*
{{16297,  7561, 24258,  7639, 16448},
 {18182, 25858, 19048, 26049, 18197},
 {22249, 20105, 17885, 20182, 22225},
 {18029, 25870, 19037, 26014, 18055},
 {16208,  7050, 24071,  7146, 16317}}
*)
$\endgroup$
0
$\begingroup$

The DominantColors function can be used to count the number of pixels of each color. However, this also includes the count for white pixels

imgParts = ImagePartition[i, 200];
Grid[
    DominantColors[#, All, {"Color", "Count"}] & /@ imgParts, 
    Frame -> All
]
$\endgroup$
6
  • 1
    $\begingroup$ One has to be careful using DominantColors because it does clustering. I've found that if you look at the white colour for instance and do FullForm on it, you'll see it is a very close white but RGBColor[0.9999603021318101`, 0.9999708034308904`, 0.9999640700153131`] is not a value that appears in the ImageData. $\endgroup$
    – flinty
    Sep 15, 2020 at 17:15
  • $\begingroup$ Sâu ... Thanks for the help I tried to use your method and got the following result of number of colors against each color which solved my problem much , kindly guide that how automatically mathematica can detect those number and I can further do some coputations using those number. $\endgroup$ Sep 16, 2020 at 7:55
  • $\begingroup$ As @flinty has mentioned, DominantColors uses a clustering algorithm. From the documentation, you can see that the various methods that you can tell Mathematica to use are KMeans, KMedoids, MedianCut, MinVariance, and Octree. By default, Mathematica picks the most suitable method to generate color clusters. For the last part of your question, can you explain what sort of computations are you trying to do? $\endgroup$
    – Sâu
    Sep 16, 2020 at 11:48
  • $\begingroup$ Thanks. I am trying to sum up all the color counts that appear in all boxes one by one which manually will be difficult if the number of boxes are large such as 40000. $\endgroup$ Sep 16, 2020 at 12:20
  • $\begingroup$ and yes what @flinty has mentioned about DominantColors is right but the way this is counting the colors it seems right so far. $\endgroup$ Sep 16, 2020 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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