Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Image Segmentation into levels for depth map creation

Subject: Image Segmentation into levels for depth map creation

From: Lefteris Roupes

Date: 13 Oct, 2010 10:42:04

Message: 1 of 14

Hallo,
I am trying to make a code to segment an image into e.g. 5 levels.
I want to use the segmented image for the depth map of the image.
(In the depth map, white species will be closer in the 3d image that i will produce.So if I have more than 2 levels the result of the final 3d image will be better)
I have this code but it segments just in 2 levels, black and white..

I = imread('peppers.png');
subplot(1,2,1), imshow(I)
title('the image')
level = graythresh(I) % aplies Otsu's algorithm and finds the level for the segment
BW = im2bw(I,level); % makes the image black and white
subplot(1,2,2), imshow(BW)
title('segmented image')

Does everybody knows how can I segment a picture in more than two levels e.g. 5 ?
With "5 levels" I mean that the segmented image will have colours : black, white and three levels of gray.
If it's possible write your code in your replies.
Thank all of you in advance!

Subject: Image Segmentation into levels for depth map creation

From: Sean

Date: 13 Oct, 2010 14:21:03

Message: 2 of 14

"Lefteris Roupes" <lefterisant@hotmail.com> wrote in message <i942ds$1jg$1@fred.mathworks.com>...
> Hallo,
> I am trying to make a code to segment an image into e.g. 5 levels.
> I want to use the segmented image for the depth map of the image.
> (In the depth map, white species will be closer in the 3d image that i will produce.So if I have more than 2 levels the result of the final 3d image will be better)
> I have this code but it segments just in 2 levels, black and white..
>
> I = imread('peppers.png');
> subplot(1,2,1), imshow(I)
> title('the image')
> level = graythresh(I) % aplies Otsu's algorithm and finds the level for the segment
> BW = im2bw(I,level); % makes the image black and white
> subplot(1,2,2), imshow(BW)
> title('segmented image')
>
> Does everybody knows how can I segment a picture in more than two levels e.g. 5 ?
> With "5 levels" I mean that the segmented image will have colours : black, white and three levels of gray.
> If it's possible write your code in your replies.
> Thank all of you in advance!

If your image is uint8:

I = imread('cameraman.tif');
edges = linspace(0,255,5); %5 is n levels
[~,val] = histc(I,edges);
imtool(val*edges(2))

Subject: Image Segmentation into levels for depth map creation

From: Sean

Date: 13 Oct, 2010 14:31:05

Message: 3 of 14


> edges = linspace(0,255,5); %5 is n levels

Typo, that should read:

edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins

Subject: Image Segmentation into levels for depth map creation

From: Matteo Niccoli

Date: 13 Oct, 2010 14:34:04

Message: 4 of 14

You could try with this submission
http://www.mathworks.com/matlabcentral/fileexchange/26532-image-segmentation-using-otsu-thresholding
Good luck


"Lefteris Roupes" <lefterisant@hotmail.com> wrote in message <i942ds$1jg$1@fred.mathworks.com>...
> Hallo,
> I am trying to make a code to segment an image into e.g. 5 levels.
> I want to use the segmented image for the depth map of the image.
> (In the depth map, white species will be closer in the 3d image that i will produce.So if I have more than 2 levels the result of the final 3d image will be better)
> I have this code but it segments just in 2 levels, black and white..
>
> I = imread('peppers.png');
> subplot(1,2,1), imshow(I)
> title('the image')
> level = graythresh(I) % aplies Otsu's algorithm and finds the level for the segment
> BW = im2bw(I,level); % makes the image black and white
> subplot(1,2,2), imshow(BW)
> title('segmented image')
>
> Does everybody knows how can I segment a picture in more than two levels e.g. 5 ?
> With "5 levels" I mean that the segmented image will have colours : black, white and three levels of gray.
> If it's possible write your code in your replies.
> Thank all of you in advance!

Subject: Image Segmentation into levels for depth map creation

From: Lefteris Roupes

Date: 13 Oct, 2010 18:30:09

Message: 5 of 14

Sorry, but this code that you gave me doesn't run..

I = imread('cameraman.tif');
edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
[~,val] = histc(I,edges);
imtool(val*edges(2))

Error: File: px.m Line: 3 Column: 3
Expression or statement is incorrect--possibly unbalanced (, {, or [.

If you find the error please tell me!
Thanks a lot!!

Subject: Image Segmentation into levels for depth map creation

From: Sean

Date: 13 Oct, 2010 18:46:04

Message: 6 of 14

"Lefteris Roupes" <lefterisant@hotmail.com> wrote in message <i94trh$o2b$1@fred.mathworks.com>...
> Sorry, but this code that you gave me doesn't run..
>
> I = imread('cameraman.tif');
> edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
> [~,val] = histc(I,edges);
> imtool(val*edges(2))
>
> Error: File: px.m Line: 3 Column: 3
> Expression or statement is incorrect--possibly unbalanced (, {, or [.

There's no error in the code you just posted. The error occurs in, I presume, your function px at line 3, just as the error states. The code above is in complete working condition and is just an example of the engine you could use.

I = imread('cameraman.tif');
edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
[~,val] = histc(I,edges);

%Instead of using imtool, here's a subplot so you can compare them.
subplot(1,2,1)
imshow(I)
subplot(1,2,2)
imshow(val*edges(2),[0 255])

Subject: Image Segmentation into levels for depth map creation

From: Sean

Date: 13 Oct, 2010 18:47:03

Message: 7 of 14

"Lefteris Roupes" <lefterisant@hotmail.com> wrote in message <i94trh$o2b$1@fred.mathworks.com>...
> Sorry, but this code that you gave me doesn't run..
>
> I = imread('cameraman.tif');
> edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
> [~,val] = histc(I,edges);
> imtool(val*edges(2))
>
> Error: File: px.m Line: 3 Column: 3
> Expression or statement is incorrect--possibly unbalanced (, {, or [.

There's no error in the code you just posted. The error occurs in, I presume, your function px at line 3, just as the error states. The code above is in complete working condition and is just an example of the engine you could use.

I = imread('cameraman.tif');
edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
[~,val] = histc(I,edges);

%Instead of using imtool, here's a subplot so you can compare them.
subplot(1,2,1)
imshow(I)
subplot(1,2,2)
imshow(val*edges(2),[0 255])

Subject: Image Segmentation into levels for depth map creation

From: Lefteris Roupes

Date: 14 Oct, 2010 08:44:06

Message: 8 of 14

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i94ur7$o9$1@fred.mathworks.com>...
> "Lefteris Roupes" <lefterisant@hotmail.com> wrote in message <i94trh$o2b$1@fred.mathworks.com>...
> > Sorry, but this code that you gave me doesn't run..
> >
> > I = imread('cameraman.tif');
> > edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
> > [~,val] = histc(I,edges);
> > imtool(val*edges(2))
> >
> > Error: File: px.m Line: 3 Column: 3
> > Expression or statement is incorrect--possibly unbalanced (, {, or [.
>
> There's no error in the code you just posted. The error occurs in, I presume, your function px at line 3, just as the error states. The code above is in complete working condition and is just an example of the engine you could use.
>
> I = imread('cameraman.tif');
> edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
> [~,val] = histc(I,edges);
>
> %Instead of using imtool, here's a subplot so you can compare them.
> subplot(1,2,1)
> imshow(I)
> subplot(1,2,2)
> imshow(val*edges(2),[0 255])


Sorry again, but this code still doesn't run,I don't know what it's wrong..

Subject: Image Segmentation into levels for depth map creation

From: Lefteris Roupes

Date: 14 Oct, 2010 08:51:05

Message: 9 of 14

"Matteo Niccoli" <matteo@matteoniccoli.com> wrote in message <i94g0s$qgl$1@fred.mathworks.com>...
> You could try with this submission
> http://www.mathworks.com/matlabcentral/fileexchange/26532-image-segmentation-using-otsu-thresholding
> Good luck

Hello and thank you for your reply.
I have found this file a few days ago,but I'm quite new user in Matlab and I can't find what I need..

Subject: Image Segmentation into levels for depth map creation

From: Cris Luengo

Date: 14 Oct, 2010 09:10:05

Message: 10 of 14

"Lefteris Roupes" <lefterisant@hotmail.com> wrote in message <i96fsm$eee$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i94ur7$o9$1@fred.mathworks.com>...
> > "Lefteris Roupes" <lefterisant@hotmail.com> wrote in message <i94trh$o2b$1@fred.mathworks.com>...
> > > Sorry, but this code that you gave me doesn't run..
> > >
> > > I = imread('cameraman.tif');
> > > edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
> > > [~,val] = histc(I,edges);
> > > imtool(val*edges(2))
> > >
> > > Error: File: px.m Line: 3 Column: 3
> > > Expression or statement is incorrect--possibly unbalanced (, {, or [.
> >
> > There's no error in the code you just posted. The error occurs in, I presume, your function px at line 3, just as the error states. The code above is in complete working condition and is just an example of the engine you could use.
> >
> > I = imread('cameraman.tif');
> > edges = linspace(0,255,6); %6 is n+1 edges to be 5 basins
> > [~,val] = histc(I,edges);
> >
> > %Instead of using imtool, here's a subplot so you can compare them.
> > subplot(1,2,1)
> > imshow(I)
> > subplot(1,2,2)
> > imshow(val*edges(2),[0 255])
>
>
> Sorry again, but this code still doesn't run,I don't know what it's wrong..

You probably have a function that shadows one of the original MATLAB functions. Run the commands one by one, to see which one causes the error. Then use "which" to see what function is being called. For example, say that the "histc" line is causing the error. Type
   which -all histc
you'll get a reply with a path to some function histc, as well as the path to ".../toolbox/matlab/datafun/histc.m", with the addition of "(shadowed)". You'd want to rename or delete the first function histc, so that the original MATLAB function can be called.

Cheers,
Cris.

Subject: Image Segmentation into levels for depth map creation

From: Lefteris Roupes

Date: 14 Oct, 2010 16:03:03

Message: 11 of 14

> You probably have a function that shadows one of the original MATLAB functions. Run the commands one by one, to see which one causes the error. Then use "which" to see what function is being called. For example, say that the "histc" line is causing the error. Type
> which -all histc
> you'll get a reply with a path to some function histc, as well as the path to ".../toolbox/matlab/datafun/histc.m", with the addition of "(shadowed)". You'd want to rename or delete the first function histc, so that the original MATLAB function can be called.
>
> Cheers,
> Cris.


Thank you Cris for your reply.You had right with the error,it gives me shadowed funkion.But I delete it and still doen't run..It says the same thing.."shadowed"..
What it's wrong?

Subject: Image Segmentation into levels for depth map creation

From: ImageAnalyst

Date: 15 Oct, 2010 02:57:41

Message: 12 of 14

On Oct 14, 12:03 pm, "Lefteris Roupes" <lefteris...@hotmail.com>
wrote:
> Thank you Cris for your reply.You had right with the error,it gives me shadowed funkion.But I delete it and still doen't run..It says the same thing.."shadowed"..
> What it's wrong?
----------------------------------------------------------
Then you have a second one with the same name as the built-in
function. type which -all histc again and report back here what it
says. Alternatively, you can use hist or imhist.

The solutions given so far split the image up into equally spaced
intensity intervals. You haven't said that that is what you want or
not. It is possible to split them up into non-uniformly spaced
intervals - it just depends on why you want to do this. So *why* do
you want to do this? Don't just say you want a depth map. OK, even
if intensity did relate to depth (distance from camera), which of
course it doesn't for the peppers image), there's no explanation as to
why you want to categorize it into 5 ranges instead of using the
original values.

Subject: Image Segmentation into levels for depth map creation

From: Lefteris Roupes

Date: 18 Oct, 2010 14:37:03

Message: 13 of 14

> Then you have a second one with the same name as the built-in
> function. type which -all histc again and report back here what it
> says. Alternatively, you can use hist or imhist.
>
> The solutions given so far split the image up into equally spaced
> intensity intervals. You haven't said that that is what you want or
> not. It is possible to split them up into non-uniformly spaced
> intervals - it just depends on why you want to do this. So *why* do
> you want to do this? Don't just say you want a depth map. OK, even
> if intensity did relate to depth (distance from camera), which of
> course it doesn't for the peppers image), there's no explanation as to
> why you want to categorize it into 5 ranges instead of using the
> original values.
----------------------------------------------------reply to ImageAnalyst--------------------
Hi ImageAnalyst,
sorry for the delay..
so it says:
>> which -all histc
C:\Program Files\MATLAB\R2008b\toolbox\matlab\datafun\histc.mexw32
C:\Program Files\MATLAB\R2008b\toolbox\matlab\datafun\histc.m % Shadowed
but the file histc.mexw32 I have deleted it.

As regards the thing that I want to do...:
I have a project to do.The name of the project is "depth image generation",and it will produce 3d anaglyph images.
In this project ,firstly,I have to do a program which will make the depth map of the image that I want to produce in 3d.
In the terminal stage of this project user will take the depth map of the image and with the help of the another software,the first image and the depth map together they will give us the 3d anaglyph image.
As we know, from the 3d theory,in the depth map of an image white things seems to be closer from the viewer (in the output 3d image), black things seems to further and gray things seems to be in the middle.
So I know that the depth map of the image it will be with the technique of segmentation.If the depth map has more than tho levels(black and white) the result will be better.So i want 5 levels ,to be much better..
The code i had posted gives an black and white segmented image but then the result of the 3d image will not be so good. I want the segmented image(depth map) to have 5 levels,white,black and three levels of gray.
I hope I helped you.Thank a lot for your interest!!

Subject: Image Segmentation into levels for depth map creation

From: ImageAnalyst

Date: 18 Oct, 2010 17:31:44

Message: 14 of 14

On Oct 18, 10:37 am, "Lefteris Roupes" <lefteris...@hotmail.com>
wrote:
> ----------------------------------------------------reply to ImageAnalyst--------------------
> Hi ImageAnalyst,
> sorry for the delay..
> so it says:>> which -all histc
>
> C:\Program Files\MATLAB\R2008b\toolbox\matlab\datafun\histc.mexw32
> C:\Program Files\MATLAB\R2008b\toolbox\matlab\datafun\histc.m       % Shadowed
> but the file histc.mexw32 I have deleted it.
>
> As regards the thing that I want to do...:
> I have a project to do.The name of the project is "depth image generation",and it will produce 3d anaglyph images.
> In this project ,firstly,I have to do a program which will make the depth map of the image that I want to produce in 3d.
> In the terminal stage of this project user will take the depth map of the image and with the help of the another software,the first image and the depth map together they will give us the 3d anaglyph image.
> As we know, from the 3d theory,in the depth map of an image white things seems to be closer from the viewer (in the output 3d image),  black things seems to further and gray things seems to be in the middle.
> So I know that the depth map of the image it will be with the technique of segmentation.If the depth map has more than tho levels(black and white) the result will be better.So i want 5 levels ,to be much better..
> The code i had posted gives an black and white segmented image but then the result of the 3d image will not be so good. I want the segmented image(depth map) to have 5 levels,white,black and three levels of gray.
> I hope I helped you.Thank a lot for your interest!!

-----------------------------------------------------------------------------------------------------------------------
You say "As we know, from the 3d theory,in the depth map of an image
white things seems to be closer from the viewer (in the output 3d
image), black things seems to further and gray things seems to be in
the middle. " This will be true only for uniformly colored objects
that are front illuminated - a very specific and controlled situation,
it will not be true in general obviously.

You say "If the depth map has more than tho levels(black and white)
the result will be better.So i want 5 levels ,to be much better.."
then won't 255 levels (i.e., no intensity quantization) be even
better?
Good luck with your project.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us