6

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.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • it's not clear from the documentation, but removing the 0 presumably defaults to '1' as per the c++ documentation: http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#cv2.imread – Tasos Papastylianou Sep 07 '17 at 00:09
  • 2
    also, `plt.imshow(img, 'gray')` instructs matplotlib to display the image `img` using the `gray` colormap; i.e. the implication is that `img` is a grayscale image. Otherwise dictating a colormap on a 'colour' image does not make sense. – Tasos Papastylianou Sep 07 '17 at 00:12

1 Answers1

8

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))
Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • how does this differ to the erroneous line in your question? (or was the last edit intended as an answer?) btw, note that opencv also has an imshow function of its own that you can use instead of matplotlib's, which would have bypassed this problem. (or equally, pyplot has an imread function you can use instead of opencv's) – Tasos Papastylianou Sep 07 '17 at 00:18