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

24 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

DGM
DGM 2022-11-4
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 个评论
DGM
DGM 2022-11-6
Yeah, I basically looked at the file and assumed that other files generated by the same software would have the same formatting. Unless slic3r happens to add something other than polygon objects to some files, I tentatively assume that it should work. That's an approximation based on a single observation.
Swapping images.roi.Polygon() with images.roi.Freehand() doesn't really change anything, since their position properties are compatibly-oriented. The only real difference between the two is in how they're created when doing so interactively (with the mouse). Since they're being used programmatically, they behave the same.
Yes, I suppose you could rewrite it that way as well. I just saw potential value in knowing the factors which map between part geometry and image geometry. That, and it's a bit of a holdover from the prior code. You're free to adapt it however you feel suits your needs.
Simone Cotta Ramusino
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by