Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Displaying the image with the original colors

In the answer to this question, I wanted to display the image in its original colors, and so removed the gray parameter from this line of code:

plt.imshow(im_out, 'gray')

When doing this however I get the image displayed with yellow and purple colors as opposed to the image's original colors.

What should I do to display the image with its original colors?

Thanks.

EDIT 1 I came across this tutorial, and seems that I should use:

plt.imshow(cv2.cvtColor(im_out, cv2.COLOR_BGR2RGB))

However, when I did this, I got the following:

Calculated scale difference: 0.99
Calculated rotation difference: 44.51
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/user/opencv/modules/imgproc/src/color.cpp, line 10606
Traceback (most recent call last):
  File "align_surf.py", line 47, in <module>
    deskew()
  File "align_surf.py", line 9, in deskew
    plt.imshow(cv2.cvtColor(im_out, cv2.COLOR_BGR2RGB))
cv2.error: /home/user/opencv/modules/imgproc/src/color.cpp:10606: error: (-215) scn == 3 || scn == 4 in function cvtColor

How can I fix this issue?

EDIT 2 The reason of the above was that the image was read as follows in the original code:

orig_image = cv2.imread('1.jpg', 0)

So, I simply removed 0.

like image 881
Simplicity Avatar asked Sep 06 '17 23:09

Simplicity


People also ask

How do I save a color image in Python?

If you want to save a color image (3D ndarray ) as a grayscale image file, convert it to grayscale with cv2. cvtColor() and cv2. COLOR_BGR2GRAY . If you save 2D ndarray to a file and read it again with cv2.


1 Answers

Based on this tutorial, in order to fix the issue, I had to convert BGR to RGB, as follows:

plt.imshow(cv2.cvtColor(im_out, cv2.COLOR_BGR2RGB))
like image 131
Simplicity Avatar answered Oct 04 '22 19:10

Simplicity