OPENCV modules in "OPENCV" python python cv2 some function usage and introduction

Source: Internet
Author: User
Tags reflection
Preface

I recently made a digital identification on the card. Call the Caffe module, directly using the Mnist model, but this article does not speak Caffe.

Need to first a series of pretreatment of the picture, the number of cards separated out, a bit of OCR feeling.

I'll write down all the OPENCV functions I use this time. 1. Read the video cv2. Videocapture ()

Parameter 1: Can be a number, corresponding to the camera head number. Can be a video name.

If you use a camera, follow the loop to read the frame continuously.

c = Cv2. Videocapture (0)
while 1:
    ret, image = C.read ()
    cv2.imshow ("Origin", image) # display screen
    Cv2.waitkey (1) # You have to go with that.

2. Waiting for Cv2.waitkey ()

Parameter 1: Wait time, in milliseconds.

Commonly used with cv2.imshow ()

Another practical function is to enter the IF condition statement by pressing the key

For example, press ESC to close the window, exit the loop, and end the program.

c = Cv2. Videocapture (0) while
1:
    ret, image = C.read ()
    cv2.imshow ("Origin", image)
    key = Cv2.waitkey (1)
    If key =:
        cv2.destroyallwindows () Break
        

3. Image plus Text cv2.puttext ()

Parameter 1: Image

Parameter 2: text content

Parameter 3: Coordinate position

Parameter 4: Font

Parameter 5: Font size

Parameter 6: Color

Parameter 7: Font weight

    c = Cv2. Videocapture (0) while
    1:
        ret, image = C.read ()
        cv2.puttext (image, ' Handsomehans ', (220,130), Cv2. font_hershey_simplex,4, (127,127,255), 2
        cv2.imshow ("Origin", image)
        key = Cv2.waitkey (1)
        if key = = 27 :
            cv2.destroyallwindows ()
            break

4. Image frame Cv2.rectangle ()

Parameter 1: Image

Parameter 2: upper left corner coordinate

Parameter 3: lower right corner coordinates

Parameter 4: The Color of the box

Parameter 5: The thickness of the box


5. Extract Image Contour cv2.findcontours ()

Parameter 1: Image

Parameter 2: Extraction rule. Cv2. Retr_external: Just look for the outer contour, Cv2. Retr_tree: Look inside and outside the outline.

Parameter 3: Output outline content format. Cv2. Chain_approx_simple: Output A small number of contour points. Cv2. Chain_approx_none: Output A large number of contour points.

Output parameter 1: image

Output parameter 2: Outline list

Output Parameter 3: Hierarchy

Contours_map, contours, hierarchy = Cv2.findcontours (image,cv2. Retr_external,cv2. Chain_approx_simple)

6. Draw the outline cv2.drawcontours ()

Parameter 1: Image

Parameter 2: Outline list

Parameter 3: Outline index, if negative, all outlines are drawn.

Parameter 4: Outline color

Parameter 5: Outline weight

Cv2.drawcontours (Image,contours,-1, (0,0,255), 2)

7. Determine whether the pixel points in a certain contour cv2.pointpolygontest ()

Parameter 1: A list of outlines

Parameter 2: Pixel point coordinates

Parameter 3: If true, outputs the pixel point to the contour nearest distance. If False, the output is a positive representation in the contour, 0 is the outline, and the negative is the outline.

result = Cv2.pointpolygontest (biggest, (w,h), False)

8. Contour Area Cv2.contourarea ()

Parameter 1: an outline

Area = Cv2.contourarea (Contours[i])

9. Find the Minimum box Cv2.minarearect () that contains the outline .

Parameter 1: an outline

Output parameter 1: Four corner point coordinates and offset angle

Ask for the smallest box and draw it:

Rect = Cv2.minarearect (contours[i])
box = np.int0 (cv2.boxpoints (rect)) # boxpoints () is a function
of opencv3 Cv2.drawcontours (image,[box],0, (0,255,255), 2)

10. Find the square box with the outline Cv2.boundingrect ()

Parameter 1: an outline

X, Y, W, h = cv2.boundingrect (Contours[i])

11. Image Color Conversion cv2.cvtcolor ()

Parameter 1: Image

Parameter 2: Conversion mode. Cv2. Color_bgr2gray: Convert to Grayscale. Cv2. COLOR_BGR2HSV: Converts to the HSV color space.

Gray = Cv2.cvtcolor (img,cv2. Color_bgr2gray)

12. Gaussian smoothing filter Cv2. Gaussianblur ()

Parameter 1: Image

Parameter 2: Filter size

Parameter 3: Standard deviation

Gray = Cv2. Gaussianblur (Gray, (3,3), 0) #模糊图像

13. Median filter Cv2.medianblur ()

Parameter 1: Image

Parameter 2: Filter size

Gray = Cv2.medianblur (gray,5) # fills White noise

14. Image Binary Cv2.threshold ()

Parameter 1: Grayscale images

Parameter 2: Threshold value

Parameter 3: Maximum value

Parameter 4: Conversion Mode CV2. Thresh_binary, Cv2. THRESH_BINARY_INV, Cv2. Thresh_trunc, Cv2. Thresh_tozero, Cv2. Thresh_tozero_inv

RET, thres = Cv2.threshold (gray,127,255,cv2. Thresh_binary)

15. Extended Image Cv2.copymakeborder ()

Parameter 1: Image

Parameter 2:top extension length

Parameter 3:down extension length

Parameter 4:left

Parameter 5:right

Parameter 6: Boundary type:

Border_constant: Constants, the added variables are all value colors [Value][value] | abcdef | [Value] [Value] [Value]

Border_reflicate: Fill directly with the color of the border, AAAAAA | ABCDEFG | Gggg
Border_reflect: Reflection, ABCDEFG | Gfedcbamn | Nmabcd
BORDER_REFLECT_101: Reflection, similar to the above, but in the reflection, will leave the boundary open, ABCDEFG | Egfedcbamne | Nmabcd
Border_wrap: Similar to this way ABCDF | MMABCDF | Mmabcd


Parameter 7: Constant value

IIMGG = Cv2.copymakeborder (num_thres,top,down,left,right,cv2. border_constant,value=0)

16. Rotating Image cv2.getrotationmatrix2d ()

Parameter 1: Rotation center point

Parameter 2: Rotation angle

Parameter 3: Scaling size

Output parameter 1: rotation matrix

Rotatematrix = cv2.getrotationmatrix2d (center= (THRES.SHAPE[1]/2, THRES.SHAPE[0]/2), angle = rect[2], scale = 1)
rotimg = Cv2.warpaffine (Thres, Rotatematrix, (thres.shape[1), thres.shape[0])




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.