Convert unsigned 24-bit RGB BMP to 16 gray
(Originally posted by jswang on 7/31/2006)
Convert unsigned pseudo 24-bit RGB image file to 16-bit grayscale file.
Our original data is 16-bit grayscale data acquired using Labview. As the Labview can not handle 16-bit grayscale image, the data was saved as unsigned 24-bit colour BMP image. In the saved BMP image format, it used the blue channel to store the lower 8-bit and green channel to store the higher 8-bit, i.e., the 16-bit data D is expressed as D = B + (2^8)*G, where B is 8-bit blue channel value and G is 8-bit green channel value. (The red channel was not used here and was 0 for all pixels).
For such pseudo 24-bit RGB image, is there a simple way that I can convert it to a 16-bit grayscale image in the ImagePro?
0
Comments
(Originally posted by KevinR on 7/31/2006)
That's an interesting way to get the data. I would have tried floating point TIFF, if Labview supports it, but if it works, it works. This does lead to some interesting issues in generating multiple outputs from a single command, though.
Extract the desired color channels, convert them to 16-bit, scale the green and add the blue.
The following code does this. Note that both the blue and green channels should be converted to the same type, 16-bit, before combining them in order to _not_ autoscale the data.
' Convert a mixed channel image into a 16-bit output Sub Color_extract() Dim chanGreen As Integer, chanBlue As Integer, blue16 As Integer Dim source As Integer, out As Integer ' Identify the source document ret = IpDocGet(GETACTDOC, 0, source) ' Extract green/blue color channels. ' Blue will be the last image created, green will be the ' image before that numerically. ret = IpCmChannelExtract(CM_RGB, CM_RGB, 14) ret = IpDocGet(GETACTDOC, 0, chanBlue) chanGreen = chanBlue - 1 ' Convert the green channel to a 16 bit image, scale it ret = IpAppSelectDoc(chanGreen) out = IpWsConvertImage(IMC_GRAY16, CONV_DIRECT , 0, 0, 0, 0) ret = IpOpNumberArithmetics(256.0, OPA_MULT, 0) ' Convert the blue channel to a 16 bit image ret = IpAppSelectDoc(chanBlue) blue16 = IpWsConvertImage(IMC_GRAY16, CONV_DIRECT , 0, 0, 0, 0) ' Add the blue ret = IpAppSelectDoc(out) ret = IpOpImageArithmetics(blue16, 0.0, OPA_ADD, 0) ' Clean up scratch images ret = IpAppSelectDoc(chanGreen) ret = IpDocClose() ret = IpAppSelectDoc(chanBlue) ret = IpDocClose() ret = IpAppSelectDoc(blue16) ret = IpDocClose() End Sub
Note that this simple bit of code doesn't do any error checking - I would suggest checking the image class with IpDocGet(GETDOCINFO...) before attempting the conversion.