How to get the boundary points?

I have a mask(mask is for an image) which is saved in .mat format. Now, i want to take the boundary of this mask. Is it possible to convert from mask to boundary? Can anyone kindly guide me?

 Accepted Answer

Image Analyst
Image Analyst on 2 May 2016
Yes. Use bwboundaries() to get a list of (x,y) coorindates. Use bwperim() if you want an image of the outer boundary.

13 Comments

Image Analyst, thank you so much for your rapid response. Really appreciate it. The issue is that these masks have already been saved as .mat files (about 50 mat files per set). So should i have to convert each mat file to jpeg in order to use bwboundaries()? Or is there any other alternative? (kindly correct me if i am wrong)
Well, read it back in from the mat file then use bwboundaries
storedStructure = load(filename);
mark = storedStructure.binaryImage; % or whatever.
boundaries = bwboundaries(mask);
Thanks for your response Image Analyst. But i apologize that i forgot to tell you that my original image is a RGB. So i did following:
storedStructure = load('P3290190.mat');
I=imread('P3290190.jpg');
BW = im2bw(I, 0.5);
imwrite(BW,'BWfinal.jpg');
mask = storedStructure.BWfinal;
But i am getting an error as follows, Reference to non-existent field 'BWfinal'.
Because of the error i am unable to get the boundaries.What i am doing wrong here?
Also, suppose there are some segmented images which have been straight away saved to .mat(meaning, i dont have segmented jpeg images for that). Does that mean, i cannot get the boundary for those?
Also, the mat file which is in the storedStructure variable (eg:P3290190.mat) is in RGB format.
You don't have storedStructure.BWfinal because you don't have storedStructure. And that is because you are not loading in a mat file. You originally said that the mask was stored in a .mat file. But just now, you're calculating the mask in the same code. So don't use load() or a .mat file if it's not necessary and just do this:
I=imread('P3290190.jpg'); % Read rgb image.
mask = im2bw(I, 0.5); % Make sure it works on an RGB image first.
imwrite(mask,'BWfinal.jpg'); % If you need to.
boundaries = bwboundaries(mask);
Image Analyst, thank you sooo much for your response. Really admire your kindest support.
First, i apologize you if i confuse you. i would have been descriptive before.
Second,let me brief what i have so far. I have 3 masks.
1st mask (saved as .mat and .jpeg),
2nd mask (saved as .mat only),
3d mask (saved as .mat only).
From your 3rd response, i know that if i have RGB image how to find the boundaries.
But now when you look at the way 3 masks have been saved, the most generalize way would be to read back the .mat files, i guess
Kindly, note that when i type
storedStructure = load('P3290190.mat');
and type whos storedStructure following is what i obtained.
Name Size Bytes Class Attributes
storedStructure 1x1 4407854 struct
Actually, i got stuck with the command 'binaryImage'.
mask = storedStructure.binaryImage;
What exactly should i say for binaryImage (binaryImage of the mask? or original image?)
Third, thank you so much for your effort. You have been a great help so far even.
Thank you so much for your responses,Image Analyst. Really appreciate it. If possible kindly tell me why i am getting an error of:
'Reference to non-existent field 'BWfinal'
for the following line.
mask=storedStructure.binaryImage;
Thank you.
What does
fieldnames(storedStructure)
report?
Hi Walter, fieldnames(storedStructure) reports, following: ans =
'maskedRgbImage'
%maskedRgbImage = output Mat file
The solution is to not save anything as .mat files. I don't see why you need to or want to. I mean, WHY?
Image Analyst, i know. But the issue is all the images(about 700 images) are already segmented in mat format. But thank you so much for your support this far, Image Analyst. And Walter, thanks to you too.
Well you just have to figure out what the variable names were when they were saved, and then get back those same names, and make sure you're using the right one in the right place. It's just bookkeeping really. I mean, if the name is maskedRgbImage, it's probably an RGB image and don't use it where a gray scale image or binary (logical) image is expected.
Thank you so much Image Analyst. Really appreciate your response.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 2 May 2016

Commented:

on 4 May 2016

Community Treasure Hunt

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

Start Hunting!