How to retrieve multiband data using shape file?

조회 수: 8 (최근 30일)
gauri
gauri 2024년 5월 14일
답변: Samay Sagar 2024년 7월 29일
I have kept the data and shape file on following link
I am using the code for extracting seven band data as follows;
% Get image info:
DataFile='C:\working_bpt\bands\Bagpat_7Bands.dat'
[Z,R] = readgeoraster(DataFile);
disp(size(Z));
p = R.ProjectedCRS;
[x,y] = worldGrid(R);
[lon,lat] = projinv(p,x,y);
ShapeFile='C:\working_bpt\bands\BAGHPAT.shp'
S = shaperead(ShapeFile);
info = shapeinfo(ShapeFile);
crs = info.CoordinateReferenceSystem;
[S(1).lon,S(1).lat] = projinv(crs,S(1).X,S(1).Y);
% Remove trailing nan from shapefile
rx = S(1).lon(1:end-1);
ry = S(1).lat(1:end-1);
logical_mask = inpolygon(S(1).lon,S(1).lat,rx,ry);
% Use the logical mask to extract data
extracted_data = Z(logical_mask);
disp(extracted_data);
it is giving following informations
DataFile =
'C:\working_bpt\bands\Bagpat_7Bands.dat'
5490 5490 7
ShapeFile =
'C:\working_bpt\bands\BAGHPAT.shp'
disp(size(extracted_data));
1 9
OR
ShapeFile =
'C:\working_bpt\bands\BAGHPAT.shp'
disp(extracted_data);
1794 1842 1829 1858 1934 1968 2315 2635 2578
My target is to retrieve the data from seven band data of dimensions
number of lines = 5490
number of samples = 5490
number of bands =7
and dimensions of output file are supposed to be
number of lines =1153
number of samples = 1315
number of bands =7
I request you to kindly look on it and suggest me how to get seven band data using shape file.

답변 (1개)

Samay Sagar
Samay Sagar 2024년 7월 29일
To extract data from the seven-band image using the shapefile, you can follow these steps. This will involve reading the image data, using the shapefile to create a mask, and applying this mask to extract the relevant data from the image. Here is how you can do it:
% Load the synthetic image data
load('sample_image.mat', 'Z', 'R');
% Read the shapefile
ShapeFile = 'sample_shapefile.shp';
S = shaperead(ShapeFile);
% Use shapefile coordinates directly
x = [S.X];
y = [S.Y];
% Remove trailing NaN from shapefile
x = x(~isnan(x));
y = y(~isnan(y));
% Convert shapefile coordinates to pixel indices
row = floor((y - R.YWorldLimits(1)) / R.CellExtentInWorldY) + 1;
col = floor((x - R.XWorldLimits(1)) / R.CellExtentInWorldX) + 1;
% Create a mask from the polygon
logical_mask = poly2mask(col, row, size(Z, 1), size(Z, 2));
% Initialize extracted data array
extracted_data = zeros([sum(logical_mask(:)), size(Z, 3)]);
% Extract data from each band
for band = 1:size(Z, 3)
band_data = Z(:,:,band);
extracted_data(:, band) = band_data(logical_mask);
end
disp(size(extracted_data));
disp(extracted_data);
For more information about “poly2mask” , you can refer the following documentation:

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by