Python Image Processing (2): Image Display, python Image Processing

Source: Internet
Author: User

Python Image Processing (2): Image Display, python Image Processing

Happy shrimp

Http://blog.csdn.net/lights_joy/ (QQ group: Visual EmbedLinux Tools 375515651)

Reprinted, but keep the author information



1. OpenCV Image Display


Previously, cv2.imshow was used to display images. However, windows that cannot be displayed in this way cannot be resized. When the displayed images are large, they cannot see the complete image, therefore, we first create a 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 uses window names to access windows, rather than window handles.


If the flags value is 0, the window size can be changed with the mouse. The displayed image also changes with the window size. Note that it may cause deformation of the image:



Cv2.namedWindow finally uses the following c function to complete specific functions:


/* create window */CVAPI(int) cvNamedWindow( const char* name, int flags CV_DEFAULT(CV_WINDOW_AUTOSIZE) );

The acceptable value of flags here is:

    //These 2 flags are used by cvNamedWindow and cvSet/GetWindowProperty    CV_WINDOW_NORMAL       = 0x00000000, //the user can resize the window (no constraint)  / also use to 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. matplotlib Image Display


Next, try to use matplotlib to display the image:

import cv2import matplotlib.pyplot as pltimg = cv2.imread('f:\\tmp\\cotton.jpg')plt.imshow(img)plt.show()

Incorrect image color:



After switching the 1st and 3rd channels, the following information is displayed:

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 is normal:



We can see the difference between cv2.imshow and plt. imshow. The image window displayed by cv2.imshow is difficult to scale proportionally, but if you use plt. imshow, You need to swap the first and third color channels.


3. Read images with plt


Compare the differences between plt. imread and cv2.imread:


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. The difference is still in the color channel:




4. matplotlib displays the grayscale image


For images with only one color channel, matplotlib can specify a map to convert an image with a single color channel to a color image.


Matplotlib supports the following map.


In [8]: import matplotlib.cm as cmIn [9]: cm.cmap_dOut[9]: {u'Accent': <matplotlib.colors.LinearSegmentedColormap at 0x22cbf50>, 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 result is as follows:



















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.