You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how can i control cluster
7 views (last 30 days)
Show older comments
when i segment a color image its divided into three clusters i need one cluster of then which is of my interest, but every time the cluster changes i.e for each iteration and i get different cluster, simply i want to get fix cluster number not variable.
thanks in advance

16 Comments
Walter Roberson
on 17 Feb 2017
Please delete your code out of the tags and add it to your question.
Image Analyst
on 18 Feb 2017
Attach your image. It's hard to discuss image processing without an image, don't you agree? In many cases kmeans is not appropriate for doing color classification - you can probably easily think up such cases. I have a kmeans color classification routine/demo if you want proof.
Niaz Ahmad
on 28 Feb 2017
this is among one of the image here we want only the diseased portion to be extracted and to calculate its feature and then classify the disease in it????
Walter Roberson
on 28 Feb 2017
You see that yellowing? You also get that kind of yellowing when there is iron deficiency, which is not a disease. You also get leaf yellowing when the plant is being over-watered. You also get leaf yellowing when the plant is being under-watered.
You also get leaf yellowing when there is fungal rot of the roots; some definitions of "disease" exclude that.
You see that brown spot? You also get that brown spots not so different from that from some kinds of caterpillar or wasp damage, which is not a disease.
Now, it might be the case that those kinds of plants, in that area, are all watered properly, and do not get attacked by caterpillars or wasps. But that would be something contextual rather than absolute: in other cases, the yellowing or brown spot would have to be discounted as irrelevant.
Therefore there are no absolute features applicable to all leaf disease situations that can be applied. You need to come up with something relevant to your situation.
Image Analyst
on 28 Feb 2017
And what would k be for that image? 2? 4? More? What if the background were all "green" instead of dirt? Do you expect to have to determine k for each individual image?
Niaz Ahmad
on 1 Mar 2017
After segmentation i got this image, now to extract the background and shows only the affected area which forms circular ring, its actually early blight disease and i want to detect this disease in crop using image processing???
Image Analyst
on 1 Mar 2017
Just invert the mask that you used to blacken the image.
background = ~mask;
Niaz Ahmad
on 1 Mar 2017
how can we perform k-means clustering, where k=3 and for every image we should get only this form of cluster, means that the cluster should remain constant and does not change?

Walter Roberson
on 1 Mar 2017
We already talked about that below. You cannot control which cluster number will be assigned to which meaning. kmeans does not know anything about what the clusters mean.
Niaz Ahmad
on 1 Mar 2017
sir from the so discussion its clear that we cannot used k-mean clustering for the said work, so we have to use other methods for segmentation an order to obtain region of interest.
Walter Roberson
on 1 Mar 2017
No, the conclusion should be that you need to use techniques to match between cluster numbers and meaning. For example it might be the case that average rbg for the cluster allows you to determine which cluster you are dealing with.
Image Analyst
on 1 Mar 2017
Edited: Image Analyst
on 1 Mar 2017
I agree with Niaz. As I've said before, kmeans is a crummy method to do clustering and classification on plant images in general (because you'll never know how many clusters there might be), though it might work fine on one particular image if you know certain things in advance (like number of clusters).
You might look into Microsoft's research on flower-finder: https://www.microsoft.com/en-us/research/blog/researchers-team-up-with-chinese-botanists-on-machine-learning-flower-recognition-project/
You could do a similar thing but with different leaf diseases.
Niaz Ahmad
on 2 Mar 2017
[filename pathname]=uigetfile('*.jpg;*.png;*.jpeg;*.tif'); inputimage=imread([pathname filename]); outputimage=inputimage; [m,n,l]=size(outputimage); for i=1:m; for j=1:n; red = outputimage(i,j,1); green = outputimage(i,j,2); blue = outputimage(i,j,3); if (((red<green))||(red<blue)) outputimage(i,j,1)=0; outputimage(i,j,2)=0; outputimage(i,j,3)=0; end end end figure, subplot(2,1,1);imshow(inputimage);title ('Input Image'), subplot(2,1,2);imshow(outputimage);title ('Disease affected Area'), set(gcf, 'Position', get(0,'Screensize')); Please sir check this piece of code can we use it instead of clustering for achieving the above said result and if not so than any other alternate code?
Niaz Ahmad
on 3 Mar 2017
[filename pathname]=uigetfile('*.jpg;*.png;*.jpeg;*.tif'); inputimage=imread([pathname filename]); outputimage=inputimage; [m,n,l]=size(outputimage); for i=1:m; for j=1:n; red = outputimage(i,j,1); green = outputimage(i,j,2); blue = outputimage(i,j,3); if (((red<green))||(red<blue)) outputimage(i,j,1)=0; outputimage(i,j,2)=0; outputimage(i,j,3)=0; end end end figure, subplot(2,1,1);imshow(inputimage);title ('Input Image'), subplot(2,1,2);imshow(outputimage);title ('Disease affected Area'), set(gcf, 'Position', get(0,'Screensize')); Please sir check this piece of code can we use it instead of clustering for achieving the above said result and if not so than any other alternate code?
Answers (1)
Walter Roberson
on 17 Feb 2017
You cannot control which cluster number a particular point is assigned to if you are using the kmeans algorithm.
kmeans uses random initialization of centroids, so any point could end up randomly near any centroid.
The Mathwork's kmeans does have a parameter to allow you to pass in initial centroids instead of using random centroids. However, the kmeans algorithm actively moves points between centroids, so the centroids "move around" and can effectively swap identities.
If you have prior knowledge of the "right" cluster number for points in two different centroids, then you can relabel the index numbers.
Always remember, though: kmeans does not have any idea what the data means, so there is no way it can be told that "cancer state III must be assigned cluster #2" for example.
13 Comments
Niaz Ahmad
on 17 Feb 2017
sir how can i fix the cluster, please if some code is available for it??
Walter Roberson
on 17 Feb 2017
Suppose you cluster and you get back a variable named
clusteridx
Then start with
newidx = 0 * clusteridx;
Now if you have a point (r1, c1) that you know belongs to the cluster you want to be labeled as 1, and you have a point (r2, c2) that you know belongs to the cluster you want labeled as 2, then:
idx1 = clusteridx(r1, c1);
mask1 = clusteridx == idx1;
newidx( mask1 ) = 1;
idx2 = clusteridx(r2, c2);
mask2 = clusteridx == idx2;
newidx( mask2 ) = 2;
mask3 = clusteridx ~= 0 & ~mask1 & ~mask2;
newidx( mask3 ) = 3;
The problem is that in general you do not know ahead of time which point is going to belong to which cluster.
Do you know something ahead of time about the locations you want to label as 1? For example, do you know that it will be the brightest cluster, and that cluster 3 will be the darkest cluster? Or do you know that the cluster to be labeled as 1 is the red-est cluster?
With that kind of information known in advance, you could use regionprops() on the labeled image that is clusteridx and examine the properties to figure out which cluster index corresponds to which meaning, after which you can relabel.
Niaz Ahmad
on 17 Feb 2017
thank you sir for your valuable suggestions, sir can we do color segmentation for leaf related diseases without using clustering???
Walter Roberson
on 17 Feb 2017
"can we do color segmentation for leaf related diseases without using clustering???"
Yes. Use http://scholar.google.com to research color image segmentation methods; there are about 1 million articles to go through, so it is highly likely that somewhere in that haystack there is a method that does not use clustering.
Niaz Ahmad
on 18 Feb 2017
sir can we combine two cluster into one single cluster for example i have two cluster one is less affected with disease and the other one is more affected i want to combine them both and they should display to me as single disease cluster?
Walter Roberson
on 18 Feb 2017
newidx = idx;
newidx(newidx == 2) = 1; %merge class 2 with class 1
Niaz Ahmad
on 19 Feb 2017
Sir bundle of thanks for your kind guidance, sir which feature extraction and classification technique will be the best one for leaf related diseases?
Walter Roberson
on 19 Feb 2017
http://scholar.google.com shows at least 60 papers on that topic. I will leave it to you to read them and figure out which ones are best for you.
Niaz Ahmad
on 27 Feb 2017
sir can we used discrete wavelet transformation to extract features from RGB image, mean can we used for leaf diseases?
Walter Roberson
on 27 Feb 2017
I don't know. What were the conclusions in
and
Niaz Ahmad
on 28 Feb 2017
Sir i am still confuse after studying so many papers that how to extract features from this attached image, means which algorithm will be best for extracting more features, please sir guide in this respect. thanks
Image Analyst
on 28 Feb 2017
I guess you didn't see my comment. How can you expect to get good answers on image processing without even providing an image? I'm surprised Walter even went this far with you.
Walter Roberson
on 28 Feb 2017
"which algorithm will be best for extracting more features"
Which-ever one works best for your data.
No-one knows what the best set of features or best algorithm is for your situation. There is no known useful mathematical model of what diseased leaves look like compared to healthy leaves for the diseases of interest to you for the types of plants of interest to you for the growing conditions of interest to you and photographed with the technologies and lighting conditions of interest to you -- but we would need such a mathematical model in order to predict what the best features and algorithm was.
The literature is full of papers along the lines of "We had a variety of plants and growing conditions and diseases of interest to us, and we experimented with a few different algorithms, and this is what happened; maybe this would work okay for you too." But none of those are your situation -- not unless you are copying their data and seeing what you can come up with.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

