imshow error but imwrite ok in python

asked 2014-05-29 05:23:56 -0600

ChrisDing gravatar image

updated 2014-05-29 07:09:18 -0600

Heading ## The function cv2.imshow() display nothing! But cv2.imwrite() write image right way.


  • under ubuntu 12.04 LTS
  • Edit by sublime text 2
  • coding as follows:

import cv2
import numpy as np

if __name__ == "__main__":
     img = np.zeros((512,512,3), dtype=np.int)
     for j in range(512):
         for i in range(512):
              img.itemset((i,j,0), 255)
              img.itemset((i,j,1), 255)
              img.itemset((i,j,1), 255)

     #print img
     cv2.imshow("test",img)
     cv2.imwrite("test.jpg",img)
     cv2.waitKey(0)
     cv2.destroyAllWindows()

result:

  1. cv2.imshow() display an image with all pixels is [0,0,0]
  2. cv2.imwrite() write an image with all pixels is [255,255,255]
  3. I just test print the data of img, find that all pixels is [255,255,255]
edit retag flag offensive close merge delete

Comments

1

img = np.zeros((512,512,3), dtype=np.int) <- np.uint8 will behave as expected.

range for an int is [0 .. 2^31], so a value of 255 will look pretty black in imshow().

berak gravatar imageberak ( 2014-05-29 07:13:43 -0600 )edit