2

I am loading 16 bit image using openCV in Python. Then I do some processing on it and save it back on the disc using imwrite() function of openCV. Surprisingly, the image is rescaled between 0-255

On the other hand, if I save image without any processing on the disc using imwrite() then image is saved as a 16-bit. I suspect that performing operations on numpy array somehow changes the type of the array?

My code is:

img16Bit = cv2.imread( 'map.pgm', -1 )
imwrite( "actualImage.jpg", img16Bit )         # works fine
# does some processing on img16Bit
imwrite( 'processedImage.jpg', img16Bit )      # image is scaled bw 0-255
#                                              #          colour-transformed
#                                              #          into grayscale-16bit
#                                              #          colordepth
user3666197
  • 1
  • 6
  • 50
  • 92
user1388142
  • 581
  • 2
  • 11
  • 26
  • 1
    What kind of processing are you performing on the image? Check the documentation, probably one or more of the ops are converting the image to grayscale. – MattDMo May 17 '15 at 04:09
  • Hope this helps: "Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function. If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving." (from http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imwrite) – heltonbiker May 17 '15 at 04:14
  • First, try to inspect the real state of the `img16Bit` with **`img16Bit.shape`** and **`img16Bit.dtype`**. Next you may want to revise your original post, so as to meet the StackOverflow Netiquette altogether with a MCVE ( a **Minimum-Complete-Verifiable-Example of code ) showing what-you-have-tried so far**. Community members are typically professionals who love to answer good questions on MCVE-related issues. **Enjoy being StackOverflow Contributing Member & do support this Community Netiquette** – user3666197 May 17 '15 at 06:17

0 Answers0