trouble using im2bw for image thresholding

Hello, I need to threshold an image. This is what I've tried (see below) but I just get every pixel of the image with a value of 1. The original data is an ascii file. If it isn't possible to use im2bw in this situation then can anyone suggest an alternative way of thresholding?
Thanks
imagedata2 = dlmread('C:\data\3d_cells\17_8um_exp0_002_bf_run1.asc');
clims=[500 4000];
figure('Name','');
image2=imagesc(imagedata2,clims);colormap gray;
level = graythresh(image2);
BW2 = im2bw(image2,level);
clims = [0,1.5];
figure;
imagebw=imagesc(BW2,clims); colormap gray

 채택된 답변

Natasha
Natasha 2011년 5월 20일

0 개 추천

I did it this way;
imagedata2thresh=+(imagedata2>1420); %put threshold value
so I just put in the actual value that I want to use for the threshold instead of faffing around with levels.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2011년 5월 17일

1 개 추천

level = graythresh(imagedata2);
BWS = im2bw(imagedata2,level);
If this doesn't work what are class is imagedata2?
class(imagedata2)
and what is the Otsu effectiveness metric?
[level, em] = graythresh(imagedata2);

댓글 수: 4

Natasha
Natasha 2011년 5월 20일
I got the same thing happening (every pixel goes to 1). I don't need to use the graythresh part, I can just set the level myself, which is fine, I just put that in to be sure I wasn't choosing a stupid value, but actually I've tried setting a range of values for level (between 0 and 1) and I still always get the same thing. Imagedata2 is class double which should be ok according to the matlab help file;
"The input image can be of class uint8, uint16, single, int16, or double, and must be nonsparse"
I don't know what nonsparse means so maybe that's the problem...?
Natasha
Natasha 2011년 5월 20일
the Otsu metric comes out as level= 0 , em=0 but even if I manually set the level to something that isn't zero I still get the same thing happening.
Natasha
Natasha 2011년 5월 20일
Also it's not sparse so that's not the problem
Sean de Wolski
Sean de Wolski 2011년 5월 20일
Yes, the behavior your seeing is expected and I would say not properly documented. Graythresh requires that the data be uint8 so it silently converts your data to uint8. The conversion method bins all data close to each other together it doesn't pick a good range. That's why I asked what class your data is. If it's uint16, you should convert it to uint8 by selecting the range of values yourself and then thresholding. I think this behavior needs to have better documentation and I've considered writing an enhancement request.

댓글을 달려면 로그인하십시오.

질문:

2011년 5월 17일

댓글:

2019년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by