Skip to content Skip to sidebar Skip to footer

Opencv Different Outputs With Imshow And Imwrite

I read two images into numpy arrays using open cv. I tried two different equations for adding these images Equation 1: img = (img_one/2) + (img_two/2) Equation 2: img = (0.

Solution 1:

The difference of output is not due to using operator * or /, but due to cv2.imshow(), In the first case when you use mul_img = 0.5*tiger + 0.5*nature The dtype of returned matrix implicitly converted to floar32, because you used floating number as one of the operands. But in the second case, The dtype of both the matrix and number is int only, So the dtype of the returned matrix from div_img = tiger/2 + nature/2 would be of type uint8.

Now cv2.imshow() has some serious issues while rendering 4-channel RGBA images, it ignores the alpha channel or rendering Mat with floating point numbers, etc. Now you are left with 2 solutions:

  • Use cv2.imwrite() to debug the images:

    cv.imwrite("path/to/img.jpg", mul_img)
    
  • Convert the image to uint8 before cv2.imshow()

    mul_img = mul_img.astype(np.uint8)
    

Post a Comment for "Opencv Different Outputs With Imshow And Imwrite"