Borrar filtros
Borrar filtros

how to write a shapefile from the array resulted from "bwboundaries"

23 visualizaciones (últimos 30 días)
Shaohui
Shaohui el 13 de En. de 2023
Respondida: Suman Sahu el 10 de Mzo. de 2023
Hi! I am a new user of Matlab and not very skilled with the syntax yet, so sorry if it sounds silly...
I would like to extract the boundaries of an image and export it as a shapefile for example.
I successfully plotted the boundaries following the tutorial (https://se.mathworks.com/help/images/boundary-tracing-in-images.html) but don't know how to write this cell object (boundaries) as a shapefile. Can I please use some help here? Thanks!
im = imread('E:\Hyytiala\ses\Canon 1730\s10\painted_IMG_3538_binarized.bmp.bmp');
imshow(im);
BW = im2bw(im);
imshow(BW);
dim = size(BW);
col = round(dim(2)/2)-90;
row = min(find(BW(:,col)));
% To trace the boundaries of all the coins in the image, use the bwboundaries function.
BW_filled = imfill(BW,'holes');
boundaries = bwboundaries(BW_filled);
% Display the original grayscale image and use the coordinates returned by bwtraceboundary to plot the border on the image.
imshow(im)
hold on;
for k=1:length(boundaries)
b = boundaries{k};
plot(b(:,2),b(:,1),'g','LineWidth',3);
end

Respuestas (1)

Suman Sahu
Suman Sahu el 10 de Mzo. de 2023
Hi Shaohui,
You have extracted the boundaries of the image and stored them in the “boundaries” cell array. In order to write the cell array into a Shapefile, the cell array first needs to be converted into a geospatial structure (geo struct). A geo struct is essentially a MATLAB struct that contains specific fields related to geographic information, such as Geometry, X, Y, and so on. To learn more about geographic data structures, refer to this documentation: Geographic Data Structures - MATLAB & Simulink - MathWorks
To write the Geo struct into a Shapefile, you can use a function shapewrite, below is the syntax for that:
shapewrite(geo_struct_obj, '<shapefilename.shp>');
The following code should help you in writing the boundaries cell array into a shapefile.
im = imread('E:\Hyytiala\ses\Canon 1730\s10\painted_IMG_3538_binarized.bmp.bmp');
imshow(im);
BW = im2bw(im);
imshow(BW);
dim = size(BW);
col = round(dim(2)/2)-90;
row = min(find(BW(:,col)));
% To trace the boundaries of all the coins in the image, use the bwboundaries function.
BW_filled = imfill(BW,'holes');
boundaries = bwboundaries(BW_filled);
% Display the original grayscale image and use the coordinates returned by bwtraceboundary to plot the border on the image.
imshow(im)
hold on;
for k=1:length(boundaries)
b = boundaries{k};
plot(b(:,2),b(:,1),'g','LineWidth',3);
end
%Extract the x and y coordinates of the boundary points into vectors
column1=[]
column2=[]
l=length(boundaries)
for i=1:l
column1=[column1; boundaries{i}(:,1)];
column2=[column2; boundaries{i}(:,2)];
end
column1=column1';
column2=column2';
%Create a struct
Boundaries_struct = struct();
Boundaries_struct.Geometry = 'MultiPoint';
Boundaries_struct.X = column1;
Boundaries_struct.Y = column2;
%Geometry, X and Y are required fields, you can add more fields to store any necessary data.
%write the struct into the shapefile
shapewrite(Boundaries_struct, 'boundaries_shapefile.shp');
Refer to following documentations to learn more:
Hope it was helpful.

Community Treasure Hunt

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

Start Hunting!

Translated by