How can I change an array from grayscale to binary?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
light_dark
el 10 de Jul. de 2015
Comentada: Image Analyst
el 10 de Jul. de 2015
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
1 comentario
Respuesta aceptada
Image Analyst
el 10 de Jul. de 2015
Try
array8 = uint8(255 * mat2gray(array));
if you want a uint8 array in the range 0-255.
2 comentarios
Image Analyst
el 10 de Jul. de 2015
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.
Más respuestas (2)
Image Analyst
el 10 de Jul. de 2015
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 comentarios
Ver también
Categorías
Más información sobre Image Processing Toolbox en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!