MATLAB: How to add a boundary into a image for saving

image processing

As the title, I have an image A and boundary B = bwboundaries(C) if I wanna show it, I can use imshow (A); hold on; visboundaries(B,'Color',''); if I use save button. the image I save don't have the same resolution as my source image so what should I do now?

Best Answer

The problem may just be with either the way you save or with way you used bwboundaries. I wrote some sample code using bwperim to get boundary image of a region then save it in png format to save lossless
img = imread('peppers.png');
figure, imshow(img);
% getting a segment
h=imfreehand;
position = wait(h);
region=uint8(roipoly(img,position(:,1),position(:,2)));
region=region.*img;
figure,imshow(region); title('Segmented Region');
% use bwperim rather than bwboundaries to get an Image of boundary.
boundImg = uint8(bwperim(rgb2gray(region),4));
boundImg(boundImg ~=0) = max(img(:));
figure, imshow(img + boundImg)
% save image without loosing information..
imwrite(img + boundImg, 'writeImg.png');
% check
read = imread('writeImg.png');
figure, imshow(read)
isequal(read, img + boundImg)