I'm the only room owner for now, but I'd like to make those that are currently in the top of the MATLAB tags owners too... so you, and pretty much everyone here.
So, if this is to talk Matlab related topics I will trow the first question. Im working now with a code that takes long time and the longest thing is squeeze(stuff). Stuff is always (1x1xN). A better way of squeezing those dimensions?
I need to compute in one single Mat the color histogram for an image. I know there could be 3 different Mat instances for each channel's histogram but I wonder how correct this approach would be:
Mat histogram = new Mat();
MatOfInt histSize = new MatOfInt(256,256,256);
MatOfFloat ranges=new MatO...
The code that you have written is comparing the histogram between images, provided that they're grayscale. If you want to do this for RGB images, you need to determine how many bins you want per plane. Once you do this, for each RGB colour triplet that you have, you would determine a linear 1D...
It's written in MATLAB, but the output will give you a single 1D histogram
each unique colour tuple maps to a single index to be incremented into the histogram
however, you definitely need to quantize each colour channel.
doing the full 256 colours per channel would result in a 256^3 colour histogram
and that is too much memory to occupy for a single histogram
it's also too fine for any discriminatory analysis
usually when you have objects of a certain colour, they have a certain variance associated to them.
like some parts of an object look lighter green and darker green in other areas.
quantizing each colour channel will account for this variability.
I actually made this room because I wanted to explain to somebody how permute works.... then I thought to myself that we actually don't have a MATLAB room.
If you want to do it from first principles, you can normalize the image to [0,1], apply a gamma / power operation on that image, then rescale the image back to [0,255], but imadjust is suitable.
When I was trying to display an indexed image array of pixel values earlier I was wondering what the difference between using a display range with imshow() and actually changing the contrast was. Would this have to do with the gamma function?
Yes, imshow does that under the hood, but doesn't mutate the image.
it only shows you that, but doesn't modify the image at all. It's simply for visual inspection.
So technically it does imadjust when you specify a range as the second parameter to imshow, but it doesn't mutate the image. It only shows you the modified contrast range.
@rayryeng Yeah, lol. I've been working a lot. Actually I'm at work now, and I have to go. Just entered here out of curiosity. See you guys later, bye. :)
In terms of calculating the histogram, the computation of the frequency per intensity is correct though there is a slight error... more on that later. Also, I would personally avoid using loops here. See my small note at the end of this post.
Nevertheless, there are three problems with your co...
for the image, because the third dimension is expanded, to fill up the matrix, we copy the image for as many times as we have slices... so in this particular case, we have 2.
For the vector, the first and second dimensions need to be expanded to 500 x 500, and so for each element, we create a 500 x 500 matrix of a single number per slice.
and that explains the A and B matrices above.
the last thing that we do now... is use the eq function.
doing this element-wise means that for each slice, we check to see what elements are equal to the number that is dictated for that slice.
this will give us a 500 x 500 x 256 matrix where each slice is logical and tells you which locations are equal to the number dictated by that slice.
so the first slice of 500 x 500 gives you all the locations of the 0s.
the next slice of 500 x 500 gives you all of the locations of the 1s... all the way to the end of 255.
if you want to compute the histogram, you simply need to add up all of the values in each slice individually.
which is why I do a horizontal and vertical sum per slice.
The result is now still a 1 x 1 x 256 vector because we compressed each slice down to a single number.
which is why I reshaped it back to a single 1D vector.
It's pretty funny... those two lines of code required 15 minutes of explanation lol.
that's why I couldn't do it in a comments block. I had to bring you to a room for that.
hehe, That is true. I probably need to play a bit with it but I get the gist though... Creating a 1x1xn matrix, is that one of the common uses of permute?
Loops? Yikes. Anyway I think your explanation covered it. I am going to play with permute to understand the details. I think you have covered it all. Thank you for the explanation,,
Remember that for those dimensions that are singleton (1), the temporary matrix that is created is expanded to match the other matrix's dimensions of that particular one.
whenever an expansion happens, the values are copied over for as many elements as there are in that dimension.
so for example, if we did bsxfun(@eq 0, rand(256,256));
the first input is 1 x 1, the second input is 256 x 256... so the first input needs to be expanded to 256 x 256
Brilliant. I think the transcript worked because we were only two. If there are other irrelevant conversations, then they will be captured to. I had a look at todays' chat previously and I got all conversations.
It might be worth adding that link to the answer itself?