Python cv2 library and matplotlib Library color space arrangement inconsistent

Source: Internet
Author: User

The following issues were found in Python today:

1. After the from matplotlib import Pyplot as PLT, then import cv2

Cv2.imshow () is not working, and I don't know why.

My workaround now is to use CV2 to read the image and then call Plt.imshow () to display the image without using cv2.imshow () to display the image

The color space in the CV2 Library of 2.OpenCV is arranged differently from the color space in the Matplotlib library.

The color arrangement in the CV2 is (b,g,r), and the arrangement in the Matplotlib Library is (r,g,b)

As a result of the first problem, I can only write two programs to prove that two libraries are not the same color arrangement

Source image (image from the network):

Use OPENCV to read and use cv2.imshow display

Import NumPy as NP Import  = cv2.imread ('/home/zh/pic/1.jpg')while(True):    Cv2.imshow ('cv2.imshow', img)    = Cv2.waitkey (0) & 0xFF    if k = =:        cv2.destroyallwindows ()         break

The results are displayed correctly, as shown in:

But if you use Pyplot.imshow () in Matplotlib, the color is reversed, i.e. the color of R space and b space is swapped, as shown below:

ImportNumPy as NPImportCv2 fromMatplotlibImportPyplot as PLTIMG_BGR= Cv2.imread ('/home/zh/pic/1.jpg') Img_rgb=Np.zeros (Img_bgr.shape, Img_bgr.dtype) img_rgb[:,:,0]= img_bgr[:,:,2]img_rgb[:,:,1] = img_bgr[:,:,1]img_rgb[:,:,2] =Img_bgr[:,:,0]plt.subplot (121), Plt.imshow (IMG_BGR) plt.xticks ([]), Plt.yticks ([]) Plt.subplot (122), Plt.imshow (IMG_RGB) plt.xticks ([]), Plt.yticks ([]) plt.show ()

The image read directly into the Cv2.imread () in Plt.imshow (), the color is incorrect, but if the cv2.imread () read the image of the R channel and the value of the B channel to swap, it is correct, the lower left image is Plt.imshow () Directly displays the result of the image read by Cv2.imread (), and the right image below is the result of the swap of the color space

These two questions, hope netizens attention

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.