How do I write a code for given formula?

2 visualizzazioni (ultimi 30 giorni)
Devendra
Devendra il 14 Mar 2024
Modificato: Image Analyst il 14 Mar 2024
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.
I would be highly obliged for kind cooperation.
Dave

Risposte (1)

Image Analyst
Image Analyst il 14 Mar 2024
I feel that kmeans is not a good method in your situation. kmeans is good when you know that you have the required number of clusters. If you don't, then it will force you to because that's how it works. So if you have a parcel with the vast majority (or all) of it being either cropland or non-cropland, you are forcing it to have two clusters even when there is really only one cluster. So for this reason, kmeans is not robust or reliable across the entire range of data from 0% (only one cluster present) to half and half (both clusters present) to 100% (of one cluster only).
You are better off using a "fixed" model that you have trained it on what is cropland and what is noncropland. If it's a simple as cropland is green and non-cropland is any other color, then you can simply do color segmentation with the ColorThresholder tool on the Apps tab of the tool ribbon.
If it's more complicated than that (multiple classes of cropland colors and "dirt" colors) then you can use discriminant analysis. I'm attaching a demo for that. You train a model by tracing regions of known classes, then you can apply it to any new/arbitrary data to estimate what class each pixel might be.
For further references see VisionBibiography
  2 Commenti
Devendra
Devendra il 14 Mar 2024
Modificato: Walter Roberson il 14 Mar 2024
Thanks a lot for your suggestions. But I have about 250 parcels and doing classification in loop. Therefore it is difficult to train the model for all parcels. Suppose if I were to follow the procedure which I narrated in my question then how to write matlab code for that. Following is matlab code for extracting the data for each parcel
% 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);
---------------------------------
end
Dave
Image Analyst
Image Analyst il 14 Mar 2024
Modificato: Image Analyst il 14 Mar 2024
If you don't want to use supervised training, like discriminant analysis or deep learning, and insist on unsupervised training, like kmeans, then you will have to be willing to accept very wrong classification estimates for shapes/regions that are mostly cropland, or mostly non-cropland.
If you know every shape/region has a good amount of both cropland and non-cropland pixels in it, then you can use kmeans but I doubt this will be the case.
Assuming you want to use this method (that I'm recommending against), what do you want to do with the classification information? Get the area of each class?

Accedi per commentare.

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by