Clear Filters
Clear Filters

Saving image from figure with freehand draw

5 views (last 30 days)
I read an image, open a figure, and imshow that image on the figure. Then I imfreehand on that figure. How do I automatically save that resultant image in the figure that has the freehand drawing on it??? Do I need to "burn" the freehand drawing into the image or can is there a function that saves everything displaying on a figure. I want to save it to a folder in my computer.

Accepted Answer

Image Analyst
Image Analyst on 2 Aug 2017

More Answers (1)

Walter Roberson
Walter Roberson on 26 Jul 2017
You could use something like the shape inserter from Vision Toolbox. Or you could grab the vertices and poly2mask and find the boundary and use the boundary indices to scribble in the image.
It is possible to do a screen capture, but that might well be at a different resolution than your original image; is that acceptable, or do you need it the original resolution?
  3 Comments
Walter Roberson
Walter Roberson on 27 Jul 2017
h = imfreehand(....);
Then when ready
lineRGB = [192 16 45]; %adjust the line color as appropriate
%freehand into mask enclosing entire area
freehand_pos = getPosition(h);
freehand_mask = poly2mask(freehand_pos(:,1), freehand_pos(:,2), size(YourImage,1), size(YourImage,2) );
%get boundary of mask
B = bwboundaries(freehand_mask);
P = B{1};
%convert boundary to linear indices
Pind = sub2ind(size(YourImage), P);
%burn into a copy, not original
newImage = YourImage;
%start burning
slicesize = size(YourImage,1) * size(YourImage,2);
for K = 1 : size(newImage, 3)
newImage( Pind + (K-1) * slicesize ) = lineRGB(K);
end
The Pind + (K-1) * slicesize part is computing linear indices for each color plane.
It would not entirely surprise me if the boundaries generated this way tend to be inside the line you drew. If so then I might try something like
SE = strel('square',2);
Pind = find(imtophat(freehand_mask, SE));
replacing the block at "get boundary of mask"
awezmm
awezmm on 1 Aug 2017
It doesn't work. Seems like nothing was burned

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!