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

조회 수: 36 (최근 30일)
Simone Cotta Ramusino
Simone Cotta Ramusino 2022년 11월 3일
댓글: Simone Cotta Ramusino 2022년 11월 6일
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
Simone Cotta Ramusino
Simone Cotta Ramusino 2022년 11월 3일
I want to extract the content (93 binary slices) from the SVG file.
DGM
DGM 2022년 11월 4일
Please provide an example of one such file.

댓글을 달려면 로그인하십시오.

채택된 답변

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
Simone Cotta Ramusino 2022년 11월 6일
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개)

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by