How can I change an array from grayscale to binary?
1 次查看(过去 30 天)
显示 更早的评论
I have a 2D array representing a grayscale image. This array is stored as doubles. When I try to use the function im2bw on the array stored as a double, I get a completely white image regardless of the threshold value. The documentation says the function accepts doubles as input. When I typecast the array to uint8 by calling converted_array = uint8(array), the im2bw function works properly.
Problem is that I'm using LabVIEW to create the 2D array and even if I typecast it in LabVIEW, MATLAB still sees the array as doubles.
Am I missing something when I'm using the im2bw function? How can I get it to work with doubles?
Edit: Some sample data from the doubles array
4294967295.00000 4278124286.00000 4210752250.00000 4278124286.00000 4210752250.00000 4294967295.00000 4244438268.00000 4193909241.00000 4261281277.00000 4294967295.00000 4278124286.00000 4294967295.00000 4193909241.00000 4294967295.00000 4294967295.00000 4278124286.00000
What the data looks like after LabVIEW converts it to U8:
255 254 250 254 250 255 251 249 253 255 255 255 249 255 255 255
采纳的回答
Image Analyst
2015-7-10
Try
array8 = uint8(255 * mat2gray(array));
if you want a uint8 array in the range 0-255.
2 个评论
Image Analyst
2015-7-10
im2bw() works with 8 or 16 bit integers or with floating point numbers. The numbers you have are too large for 16 bits so that's why they need to be doubles. If they're doubles, then MATLAB's convention is that all floating point numbers (i.e. single or double) must be in the range 0 to 1. If you just take your raw numbers, unscaled, then they aren't in that 0-1 range that MATLAB requires, hence the unexpected results. You can either divide by the max value you have, which will work unless you have negative values in your data. Or you can call mat2gray() which scales your min-to-max range into 0-to-1. This will work even if the double data has negative values. If you want, you could leave it as floating point after just calling mat2gray() and it should work and be in the correct 0-1 range, but you might want to convert to uint8 with the uint8() function because most people are used to dealing with gray scale images as unsigned integers.
更多回答(2 个)
Image Analyst
2015-7-10
I never use it. I simply threshold using some value that I have determined.
binaryImage = grayImage > someThreshold;
You might like to use my visual/interactive app for thresholding an image. See my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 for that plus several other nice apps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!