Python image Processing (2): Image display

Source: Internet
Author: User

Happy Shrimp

http://blog.csdn.net/lights_joy/(QQ group: Visual embedlinux Tools 375515651)

Welcome reprint, but please keep the author information



1. OpenCVImage Display


before using Cv2.imshow The image is displayed, but the window cannot be resized in such a way that it cannot be displayed when the displayed image is larger, so we first create the window and then display the image:


Import cv2img = Cv2.imread (' f:\\tmp\\cotton.jpg ') win = Cv2.namedwindow (' Test win ', flags=0) cv2.imshow (' Test win ', IMG) Cv2.waitkey (0)

OpenCV Use the window name to access the window instead of something like a window handle.


Flags to be 0 indicates that the window can be resized with the mouse, and the displayed image changes with the size of the window, and it is important to note that it may cause the image to deform:



Cv2.namedwindow end up using the following C function to accomplish specific functions:


/* Create window */CVAPI (int) Cvnamedwindow (const char* name, int flags Cv_default (cv_window_autosize));

Here's Flags the values that you can accept are:

    These 2 flags is used by Cvnamedwindow and cvset/getwindowproperty    cv_window_normal       = 0x00000000,//the user ca N Resize the window (no constraint)  /also use-Switch a fullscreen window to a normal size    cv_window_autosize
   = 0x00000001,//the user cannot resize the window, the size is constrainted by the image displayed    Cv_window_opengl       = 0x00001000,//window with OpenGL support

2.     matplotlibImage Display


Try it next matplotlib Display Image:

Import Cv2import matplotlib.pyplot as Pltimg = Cv2.imread (' f:\\tmp\\cotton.jpg ') plt.imshow (IMG) plt.show ()

The color of the image is incorrect:



the first 1 Channel and section 3 display after Channel swap:

Import NumPy as Npimport cv2import matplotlib.pyplot as Pltimg = Cv2.imread (' f:\\tmp\\cotton.jpg ') (R, G, b) =cv2.split (img ) Img=cv2.merge ([B,g,r]) Plt.imshow (IMG) plt.show ()

This time normal:



from the front you can see cv2.imshow with plt.imshow > the difference. cv2.imshow plt.imshow You need to swap the first color channel and the third color channel.  


3.      with plt reading Images


compare it again . Plt.imread and the Cv2.imread The difference:


Import NumPy as Npimport cv2import matplotlib.pyplot as Pltimg1 = Cv2.imread (' f:\\tmp\\cotton.jpg ') Img2 = Plt.imread (' f:\ \tmp\\cotton.jpg ') plt.subplot (121) plt.imshow (IMG1) plt.subplot (122) plt.imshow (IMG2) plt.show ()

The above code reads the same image and displays it in the same way, and the difference is on the color channel:




4.     matplotlibdisplaying grayscale graphs


for images with only one color channel, matplotlib You can specify a Map To convert an image of a single color channel to a color image.


matplotlib support the following Map .


In [8]: Import matplotlib.cm as CmIn [9]: cm.cmap_dout[9]: {u ' Accent ': <matplotlib.colors.linearsegmentedcolormap at 0 X22cbf50>, u ' accent_r ': <matplotlib.colors.linearsegmentedcolormap at 0x22d7150>,....... u ' Winter ': < Matplotlib.colors.LinearSegmentedColormap at 0x22d7290>, u ' winter_r ': < Matplotlib.colors.LinearSegmentedColormap at 0x22cb910>}

Select a Map to display:

Import NumPy as Npimport cv2import Matplotlib.pyplot as Pltimport matplotlib.cm as Cmimg = Plt.imread (' f:\\tmp\\cotton.jpg ') img = img[:,:,0]plt.subplot (121) Plt.imshow (IMG) plt.subplot (122) #plt. Colorbar () Plt.imshow (IMG, Cmap=cm.get_cmap ( ' Winter ')) Plt.show ()

The results are as follows:



















??

Python image Processing (2): Image display

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.