【Opencv】【Python】Introduction and usage of some functions of opencv module cv2 in Python.

Title: '【Opencv】【Python】Introduction and Usage of Some Functions in the Opencv Module cv2 in Python'
Date: 2017-08-17
Permalink: /posts/2017/08/【Opencv】【Python】Introduction-and-Usage-of-Some-Functions-in-the-Opencv-Module-cv2-in-Python/

  • Opencv


Recently, I have been working on digit recognition on cards. I used the mnist model in the caffe module, but this article does not cover caffe.

First, we need to preprocess the images to separate the digits on the cards, giving it an OCR-like feel.

I will briefly explain all the opencv functions used in this process.

1. Read Video cv2.VideoCapture()#

Parameter 1: It can be a number corresponding to the camera ID or the name of a video file.

If using a camera, a loop is needed to continuously read frames.

c = cv2.VideoCapture(0)
while 1:
    ret, image = c.read()
    cv2.imshow("Origin", image) # Display the frame
    cv2.waitKey(1) # This line is necessary for the frame to be displayed

2. Wait cv2.waitKey()#

Parameter 1: Waiting time in milliseconds.

Usually used in conjunction with cv2.imshow()

Another useful feature is to enter an if statement based on key presses.

For example, the code below closes the window and exits the loop when the ESC key is pressed, ending the program.

c = cv2.VideoCapture(0)
while 1:
    ret, image = c.read()
    cv2.imshow("Origin", image)
    key = cv2.waitKey(1)
    if key == 27:

3. Add Text to Image 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 thickness

    c = cv2.VideoCapture(0)
    while 1:
        ret, image = c.read()
        cv2.imshow("Origin", image)
        key = cv2.waitKey(1)
        if key == 27:

4. Add Rectangle to Image cv2.rectangle()#

Parameter 1: Image

Parameter 2: Top-left corner coordinate

Parameter 3: Bottom-right corner coordinate

Parameter 4: Color of the rectangle

Parameter 5: Thickness of the rectangle

5. Extract Image Contours cv2.findContours()#

Parameter 1: Image

Parameter 2: Contour retrieval mode. cv2.RETR_EXTERNAL: retrieve only the external contours, cv2.RETR_TREE: retrieve all contours, including internal and external contours.

Parameter 3: Contour approximation method. cv2.CHAIN_APPROX_SIMPLE: compresses horizontal, vertical, and diagonal segments and leaves only their end points. cv2.CHAIN_APPROX_NONE: stores all the contour points.

Output parameter 1: Image

Output parameter 2: List of contours

Output parameter 3: Hierarchy of contours

contours_map, contours, hierarchy = cv2.findContours(image,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

6. Draw Contours on Image cv2.drawContours()#

Parameter 1: Image

Parameter 2: List of contours

Parameter 3: Index of the contour to draw. If negative, all contours are drawn.

Parameter 4: Contour color

Parameter 5: Contour thickness


7. Check if a Pixel is Inside a Contour cv2.pointPolygonTest()#

Parameter 1: List of a contour

Parameter 2: Pixel coordinates

Parameter 3: If True, outputs the shortest distance from the pixel to the contour. If False, outputs a positive value if the pixel is inside the contour, 0 if it is on the contour, and negative if it is outside the contour.

result = cv2.pointPolygonTest(biggest, (w,h), False)

8. Calculate Contour Area cv2.contourArea()#

Parameter 1: A contour

area = cv2.contourArea(contours[i])

9. Find Minimum Enclosing Rectangle for a Contour cv2.minAreaRect()#

Parameter 1: A contour

Output parameter 1: Four corner points and the rotation angle

To find the minimum enclosing rectangle and draw it:

rect = cv2.minAreaRect(contours[i])
box = np.int0(cv2.boxPoints(rect)) # boxPoints() is a function in opencv3

10. Find Bounding Rectangle for a Contour cv2.boundingRect()#

Parameter 1: A contour

x, y, w, h = cv2.boundingRect(contours[i])

11. Convert Image Color cv2.cvtColor()#

Parameter 1: Image

Parameter 2: Conversion method. cv2.COLOR_BGR2GRAY: convert to grayscale. cv2.COLOR_BGR2HSV: convert to HSV color space.

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

12. Apply Gaussian Smoothing Filter cv2.GaussianBlur()#

Parameter 1: Image

Parameter 2: Filter size

Parameter 3: Standard deviation

gray = cv2.GaussianBlur(gray,(3,3),0) # Blur the image

13. Apply Median Filtering cv2.medianBlur()#

Parameter 1: Image

Parameter 2: Filter size

gray = cv2.medianBlur(gray,5) # Remove white noise

14. Image Thresholding cv2.threshold()#

Parameter 1: Grayscale image

Parameter 2: Threshold value

Parameter 3: Maximum value


ret, thres = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)

15. Expand Image cv2.copyMakeBorder()#

Parameter 1: Image

Parameter 2: Length of top expansion

Parameter 3: Length of bottom expansion

Parameter 4: Length of left expansion

Parameter 5: Length of right expansion

Parameter 6: Border type:

BORDER_CONSTANT: Constant border value [value][value] | abcdef |

BORDER_REFLICATE: Replicate the border color, aaaaaa | abcdefg | gggg
BORDER_REFLECT: Reflect, abcdefg | gfedcbamn | nmabcd
BORDER_REFLECT_101: Reflect, similar to the above, but with an open boundary during reflection, abcdefg | egfedcbamne | nmabcd
BORDER_WRAP: Similar to this pattern, abcdf | mmabcdf | mmabcd

Parameter 7: Constant value

iimgg = cv2.copyMakeBorder(num_thres,top,down,left,right,cv2.BORDER_CONSTANT,value=0)

16. Rotate Image cv2.getRotationMatrix2D()#

Parameter 1: Rotation center

Parameter 2: Rotation angle

Parameter 3: Scale factor

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]))
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.