How can I convert a svg file to matrix or image in Matlab?

25 views (last 30 days)
I have a .svg file containing the slicing of a model. How can I import and read the slices in Matlab? They are binary slices and I need to loop over each of them to obtain the corresponding matrix. I'd rather not use Exchange functions, but that is not mandatory.
  3 Comments

Sign in to comment.

Accepted Answer

DGM
DGM on 4 Nov 2022
If you have raster images embedded in an SVG file, you may be able to extract them something like this:
% filename of svg file
fname = 'multiobject.svg.fakeextension.txt';
% extract image extensions and data from file
% ignore all other objects in the file
str = fileread(fname);
blockinfo = regexp(str,'data:image/(.*?);base64,([^"]*)','tokens');
for k = 1:numel(blockinfo)
% get the info for this image
thisext = blockinfo{k}{1};
thisdata = blockinfo{k}{2};
% decode the image file
thisdata = matlab.net.base64decode(thisdata);
% write the file using the original format extension
fname = sprintf('myextractedfile_%04d.%s',k,thisext);
fid = fopen(fname,'w');
fwrite(fid,thisdata,'uint8');
fclose(fid);
end
Now all the files can simply be read from disk.
A = imread('myextractedfile_0001.png');
imshow(A)
Without knowing what exactly is in the file, I can't know for sure if that's what you need.
  13 Comments
Simone Cotta Ramusino
Simone Cotta Ramusino on 6 Nov 2022
Ok, anyway I could adapt it if the svg file changed, fixing the regex syntax I think.
I got it, indeed nothing changes, the "aliasing" is still there, but I think it is inescapable.
Yes, in the end I think I will leave it untouched, so I can use the scaling factor, if necessary.
Thank you very much, you were crystal clear and helped me a lot. I am sorry I was not very precise at first.
Thanks again

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Identification in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!