MATLAB Answers

Can we count the color pixels in each box like it did in mathematica?

1 view (last 30 days)
Atique Khan
Atique Khan on 16 Sep 2020
Commented: Adam Danz on 16 Sep 2020
I am working in a project to split an image into small boxes of equal size which I have made. The problem is that when OI concatinate the boxes again to count the number of color pixels and use it for further computation then there are many new colors of which I dont know the name and it coounted to be about more than 17000 in an 1000 x 1000 image. I splitted an imae in mathematica and ploted the number of color pixels with their shade that made my work quite easy but the porblem is that when I work for larger box number it keep on running and very slow computation as compared to matlab. I want to do that in matlab to reduce the computaional cost. Kindly help.
This is what mathematica gives me for a 25 x 25 image when it is splitted into 5 x 5 boxes and each box contained 25 pixels.

  3 Comments

Adam Danz
Adam Danz on 16 Sep 2020
Here's some more background on this
"The problem is ... there are many new colors of which I dont know the name and it counted to be about more than 17000 in an 1000 x 1000 image."
If two different algorithms gave two different numbers of colors, assuming there are no random processes, it must be the case that
  1. the input image differed between the two analyses, or
  2. the algorithm differs between the two analyses
You can easily rule out the first possibiliity (make sure you're analyzing the same exact image file).
The algorithm that listed 17000+ colors (in the link at the top of this comment) counts every single color even if the RGB value differs by 1/1000th. The other algorithm may be more robust and may be eliminating near-duplicates which would also explain why it's taking so much time.
We can't suggest what to do to get the same result as the other algorithm without knowing that algorithm (I'm not suggesting that we figure that out and take hours or days to solve that on our ends). But it seems reasonable that your first step would be to figure out what algorithm is used and then try to find the same or something similar in Matlab.
The second step might be simply to define exactly what you want the output to be and work on improving the Matlab code you already have. For example, it would be relatively easy to define a tolerance level such that all very-similar colors are grouped as one.
Mario Malic
Mario Malic on 16 Sep 2020
"This is what mathematica gives me for a 25 x 25 image when it is splitted into 5 x 5 boxes and each box contained 25 pixels."
From the image and this information from the post, it's possible to deduce that all pixels are in the each box. Adam, I am impressed by the amount of work you invested in that just-for-fun post (+1 on that).
Regarding the computational cost, I doubt it's that big, even though it's 3 milion elements (1000x1000x3) that should be analysed. The thing is that, if image you're analysing is a colorful image, then think about how can you display all the colors in the box, you'll have to set some threshold on count of colors.
About the process, load the image and split the image array according to the sizes, and set up the process for 1 box and just loop it for remaining boxes.
Adam Danz
Adam Danz on 16 Sep 2020
Thanks, Mario.
@Atique Khan , on second glance, it looks like the Mathematica algorithm is also not differentiating between very similar but numerically different colors so I can't imagine what it's doing.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!