The Python interface uses the OpenCV method

Source: Internet
Author: User
This time for you to bring the Python interface using the OpenCV method, Python interface using the OpenCV of attention to what, the following is the actual case, together to see.

First, in the ANACONDA2 configuration OpenCV

Unzip OPENCV, add system environment variables, computer--right-click Properties--Advanced system settings--environment variables--System variables--Edit path--> Add F:\Program Files (x86) \ Opencv-3.2.0-vc14\build\x64\vc14\bin

Copy Opencv/build/python/2.7/x64/cv2.pyd to anaconda2/lib/site-packages/

Note: From the above python/2.7 can be seen, OPENCV official Python interface only support Anaconda2 version, if you have Anaconda3, you can open cmd, and then execute Conda install-c https:// Conda.anaconda.org/menpo Opencv3;

You can also refer to this article for ANACONDA3 configuration

Open the Ipython test.

Import Cv2print (cv2.version)

Ii. Basic knowledge of OpenCV

1. Reading, displaying, and writing images

Import Cv2import Matplotlib.pyplot as plt# read the image, the second parameter can be 1 (default read color graph, can be omitted), 0 (read in grayscale) im = Cv2.imread (' empire.jpg ', 1) # function Imre AD () returns the image as a standard NumPy array h,w = Im.shape[:2]print h,w# The image is displayed, the first parameter is the window's name, followed by our image, and the window is automatically resized to the image size. Cv2.imshow (' Image ', img) cv2.waitkey (0) # To prevent the image from flashing through, wait indefinitely for keyboard input cv2.destroyallwindows () # Close all images # Save image (you must set the path and extension to save the image) Cv2.imwrite (' result.png ', IM) # Display images using PLT (can display pixel coordinates and pixel values), save image Plt.imshow (IM, cmap= ' gray ', interpolation= ' bicubic ') Plt.show () plt.savefig (' Figpath.png ', bbox_inches= ' tight ')

2. Color space Conversion

In OpenCV, images are stored in BGR order (that is, reverse RGB) instead of the traditional RGB color channel. The default is BGR when reading an image, but there are some conversion functions available. The conversion of the color space can be achieved using the function Cvtcolor ().

# 1. Using OPENCV to read and create grayscale images, press BGR order IM = Cv2.imread (' empire.jpg ') Gray = Cv2.cvtcolor (IM, cv2. Color_bgr2gray) # 2. Use Matplotlib.image to read in and create grayscale images, in RGB order import matplotlib.image as Mpl_imgim = Mpl_img.imread (' Empire.jpg ') Gray = Cv2.cvtcolor (IM, cv2. Color_rgb2gray) # Note: Notice the difference between 1 and 2 in the Color Conversion code # commonly used: Cv2. Color_bgr2rgb, Cv2. COLOR_GRAY2BGR, Cv2. Color_bgr2hsv

3. Draw lines, rectangles, circles, and polygons (curves) on the image

Draw Straight Line: Cv2.line ()

Import cv2# read images, in BGR order img = cv2.imread (' empire.jpg ') # incoming images, start coordinates, end coordinates, line color (color), line thickness (thickness) # Color:color of the Shape. For BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.# thickness:if-1 are passed for closed figures like circles, it'll fill the Shape, default thickness = 1.IMG = Cv2.line (IMG, (0, 0), (511, 511), (255, 0, 0), 5)

Draw rectangle: Cv2.rectangle ()

# Required incoming image, top left corner vertex coordinates, lower right vertex coordinates, color, lineweight img = Cv2.rectangle (IMG, (384, 0), (510, 128), (0, 255, 0), 3)

Draw a circle: cv2.circle ()

# need to pass in image, center point coordinates of Circle, radius, color, lineweight img = Cv2.circle (IMG, (447, X), (0, 0, 255), 1) # If-1 is passed for closed figures like CI Rcles, it'll fill the shape. Default thickness = 1

Drawing polygons (including curves): Cv2.polylines ()

# The data type of the array must be int32, if you know the curve equation, you can generate a bunch of points, you can draw a curve to the PTS = Np.array ([[[10,5],[20,30],[70,20],[50,10]], Np.int32) # The first parameter is-1, Indicates that the length of this dimension (number of points) is calculated from the subsequent dimension of pts = Pts.reshape (( -1,1,2)) # If the third argument is false, the polygon we get is not closed (end-to-end) img = Cv2.polylines (IMG, [pts], True, (0, 255, 255))

Add text to a Picture: Cv2.puttext ()

Font = Cv2. font_hershey_simplex# the 3rd to 6th parameter is: Bottom-left Corner where data starts, font size, color, Thicknesscv2.puttext (IMG, ' OpenCV ', (10,500), Font, 4, (255, 255, 255), 2, Cv2. LINE_AA)

4. Basic operation of the image

Get and modify pixel values

Import Cv2import numpy as Npimg = Cv2.imread (' messi5.jpg ') px = img[100, 100]print px[57, 68]# accessing only blue Pixelb Lue = img[100, +, 0]print blue57 # Modify the pixelimg[100, +] = [255, 255, 255]print img[100, 100][255 255 255]# Chan Nel 2 All values are set to 0 img[:,:, 2] = 0

Get Image Properties

img = cv2.imread (' messi5.jpg ') print Img.shape (960L, 1280L, 3L) print Img.size3686400print img.dtypeuint8

Select an image block

img = cv2.imread (' messi5.jpg ') # Select the ball and copy it to another regionball = img[280:340, 330:390] # Note: 340 and 390 do not take I mg[273:333, 100:160] = Ball

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Python OPENCV detects and extracts target colors

How Python writes data in-frame data to the database

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.