How to add a boundary into a image for saving

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?

 Respuesta aceptada

Gopichandh Danala
Gopichandh Danala el 28 de Jun. de 2017
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)

8 comentarios

R G
R G el 28 de Jun. de 2017
it have an error on line "region=region.*img;"
Gopichandh Danala
Gopichandh Danala el 28 de Jun. de 2017
Check the data type of both the variables region and img maybe they are different in your case and adjust
R G
R G el 28 de Jun. de 2017
I have got it, using:
region = cat(3,region,region,region); region=region.*img;
but the boundary line is too small, do you know how to make it bigger for making it clearer.
Just use imdilate on the boundary image.
% use bwperim rather than bwboundaries to get an Image of boundary.
boundImg = uint8(bwperim(rgb2gray(region),4));
boundImg = imdilate(boundImg, strel('disk',2)); % change strel size 2,3,.. as you need.. It will increase the boundary width..
boundImg(boundImg ~=0) = max(img(:));
figure, imshow(img + boundImg)
If you are satisfied with the answer please accept the answer
R G
R G el 28 de Jun. de 2017
perfect, thanks so much
Gopichandh Danala
Gopichandh Danala el 28 de Jun. de 2017
no problem
Warid Islam
Warid Islam el 19 de Jul. de 2019
Hi Gopichandh,
Is it possible to crop the image specified by the boundary above?
Of course. Use find() on region or position, then use max() and min() to find the max and min rows and columns, then use indexing to extract the cropped region.
[rows, columns] = find(region);
row1 = min(rows);
row2 = max(rows);
col1 = min(columns);
col2 = max(columns);
croppedImage = img(row1:row2, col1:col2, :);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

R G
el 27 de Jun. de 2017

Comentada:

el 19 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by