3 views (last 30 days)
Show older comments
Devendra on 14 Mar 2024
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula
Edited: Devendra on 14 Apr 2024
Accepted Answer: Walter Roberson
Open in MATLAB Online
% This is done through k-Means clustering and binarization to derive a
% binary classification of each parcel for cropland and non-cropland pixels.
% This process results in 2 clusters per parcel object for which the
% mean values are calculated. Herein, a threshold for pixels higher
% than 0.4 was defined for the derivation of CAF:
% 𝐶𝐴𝐹 = Σ𝑝 > 0 . 4 / Σ𝑝
% where Σp is the sum of all pixels and Σ𝑝 > 0 . 4 the sum of pixels
% which are located within the cluster with a mean NDVI higher than 0.4.
% open NDVI file
img = imread('ndvi.tif');
% pickup the shape files of parcels
d = uigetdir(pwd, 'Select a folder');
shapefiles = dir(fullfile(d, '*.shp'));
for m = 1:length(shapefiles)
shapefile = shapefiles(m);
disp(shapefile.name);
S = shaperead(shapefile.name);
polygon = polyshape([S.X], [S.Y]);
% Create a logical mask
logical_mask = inpolygon(lon, lat, polygon.Vertices(:, 1), polygon.Vertices(:, 2));
% Use the logical mask to extract data from ndvi image for parcel
parcel_ndvi = img(logical_mask);
% Apply k-means clustering with k=2
k = 2;
[idx, centroids] = kmeans(parcel_ndvi, k);
X = mean(idx(:,1));
Y = mean(idx(:,2));
end
I would appreciate any help to complete above code as per comments given at the top of this code.
Deve
5 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
Steven Lord on 14 Mar 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula#comment_3098661
It's not clear to me what help you're looking for. Please ask a specific question about where you're having difficulty and we may be able to provide some guidance about that specific area in your code.
Devendra on 14 Mar 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula#comment_3098931
Edited: Walter Roberson on 14 Mar 2024
Open in MATLAB Online
How to find cropland and non cropland cluster (which is which out of these two clusters) using the following matlab code line
% Apply k-means clustering with k=2
k = 2;
[idx, centroids] = kmeans(parcel_ndvi, k);
thanks.
Dave
Walter Roberson on 14 Mar 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula#comment_3098961
Open in MATLAB Online
You could take
mean(parcel_ndvi(idx == 1))
and compare it to
mean(parcel_ndvi(idx == 2))
to try to decide which cluster is which ?
Devendra on 14 Mar 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula#comment_3098976
Edited: Devendra on 15 Mar 2024
if mean(parcel_ndvi(idx == 1)) > mean(parcel_ndvi(idx == 2)) then
cropland = idx(1)
noncropland = idx(2)
otherwise
cropland = idx(2)
noncropland = idx(1)
endif
How do I write above lines in matlab code ?
Thanks a lot for your kind help.
Dave
Walter Roberson on 15 Mar 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula#comment_3099116
Open in MATLAB Online
if mean(parcel_ndvi(idx == 1)) > mean(parcel_ndvi(idx == 2))
cropland = 1;
noncropland = 2;
else
cropland = 2;
noncropland = 1;
end
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Walter Roberson on 14 Mar 2024
Open in MATLAB Online
The first thing you have to do is figure out which of the two clusters corresponds to which condition.
kmeans() clustering with two clusters can return either index being the one of interest -- even if you give it options for initial cluster centers, because of the way that cluster updates go, it is possible for the two clusters to effectively exchange identities.
Once you have figured out which is which, you would
mask = idx == CLUSTER_OF_INTEREST;
CAF = nnz(parcel_ndvi(mask) > 0.4) / (size(img,1)*size(img,2))
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Devendra on 14 Mar 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula#comment_3098981
Edited: Devendra on 15 Mar 2024
mask = idx == CLUSTER_OF_INTEREST;
CAF = nnz(parcel_ndvi(mask) > 0.4) / (size(img,1)*size(img,2))
it should be (size(idx,1)*size(idx,2)) or (size(img,1)*size(img,2)) ??
Thanks for your kind help.
Dave
Walter Roberson on 15 Mar 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula#comment_3099111
size(img,2)*size(img,2) is the number of pixels covered by img
Devendra on 14 Apr 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2094346-how-do-i-complete-my-matlab-code-for-a-given-formula#comment_3130986
Edited: Devendra on 14 Apr 2024
Thank you very much for your kind help. I am using following matlab lines to create mask over cropland.
if mean(parcel_ndvi(idx == 1)) > mean(parcel_ndvi(idx == 2))
cropland = 1;
noncropland = 2;
else
cropland = 2;
noncropland = 1;
end
mask = idx == cropland
I want to mask only those pixels under cropland whose ndvi values are greater than 0.4. I request you to please suggest me how to create the said mask using matlab code. I would appreciate your kind help.
Devendra
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
AI, Data Science, and StatisticsStatistics and Machine Learning Toolbox
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
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
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- 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
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office