how to make kmeans be consistent with assignment values?
3 次查看(过去 30 天)
显示 更早的评论
I'm following along with this example here: https://www.mathworks.com/help/images/land-classification-with-color-features-and-superpixels.html and tweaking it to fit my needs.
The kmeans function seems to 'randomly' select a value for each segmentation. For example, a certain segment might be assigned the value "1" on the initial run of the code. When I run the code again it might perhaps be assigned a "4" or any value 1,2,3, or 4. I need this to be consistent between running the code. Basically I want green land to always be assigned a value of 1 and desert land to always be assigned a value of 2...and so on.
How do I do this? How do I make kmeans to produce values on a consistent basis?
Thanks,
Nicholas
-----------------------------------------
Here's what I'm using the code for in case it helps, reading this isn't exactly necessary to my question.
I'm reading in a picture of a Rubik's cube face. I want to use matlab to segment the photo and automatically decide what colors are what. And it's working kinda. Using the code from that example I have matlab assigning values between 1 and 6 to each color square of the rubik's cube face. I then take the image Lout and reduce it down to a 3x3 matrix consisting of values 1:6. Each element represents a square of the rubik's face....I want to use the same piece of code to do this 5 more times for the other 5 faces of the rubik's cube. From that I want to produce a 3D plot of the rubik's cube utilizing these 6 3x3 matrices to accurately color the cube (haven't worked all the details out yet). But, basically I need the kmeans function to be consistent so that when I run the code for the second photo I dont get the color red to correspond to the value 2 for once face and red to correspond to the value 6 on another face. I need it to be consistent.
0 个评论
回答(2 个)
Walter Roberson
2017-4-22
"How do I make kmeans to produce values on a consistent basis?"
One way is to set the random number seed to a consistent value before each run.
The other way is to use the kmeans Start option to provide initial cluster centroids; the default is random initial centroids.
Note: in either approach, what would be consistent would be the output assignment given the exact same input. If, for example, you had two images with the exact same view and focus and shutter time, but there was even a trivial change in lighting, or if the CCD quantization noise was not exactly the same between the two pictures, then the output assignments could be completely different.
There is no way, with kmeans(), to say anything like "always make cluster 4 correspond to Green" -- or any other objective meaning. You can just make it consistent with itself for identical input.
If you need to assign objective meaning to the cluster labeling, then you need to post-process the output of kmeans() to figure out which cluster number is associated with which meaning, and then renumber.
Image Analyst
2017-4-23
Your approach is totally misguided. You should not be using kmeans at all. I guess I see why you might think you should, if you didn't really think about the problem too much. You see 6 possible colors so you think you should use kmeans with a k of 6. Seems reasonable at first, right? OK, now start thinking - what if there are not 6 colors in the image and I'm forcing it to find 6 colors? What if one side is solid green? So you have only 2 colors (black and green), NOT 6. So it might give you 4 shades of green and 2 shades of black. This is not what you want. I know how to answer the general question about ordering kmeans classes but I'm not going to answer that because I don't want to send you on a wild goose chase.
What you need to do is to take a picture with all 6 colors and then measure the RGB values of each of the 6 colors. Then use a template to measure the colors of all 9 squares with regionprops to get the 9 colors. Now simply use Pythagorean theorem and sqrt() and min() to find out which of the 6 colors is in each of the 9 locations. kmeans() if not even used at all. It's just simple basic color segmentation in RGB color space, like in my tutorial in my File Exchange. After that you'll have a 9 element array (one for each square location) that can have one of 6 values (1-6 for the 6 possible colors).
5 个评论
Image Analyst
2017-12-9
I just ran it and it ran fine. I'm attaching it here. Use the peppers demo image and tell me if it runs. Did you get an error on your own image or a demo image?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
