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