Frequently used functions in the Python OpenCV

OpenCV (Open Source Computer Vision Library) is a powerful library used for a wide range of image and video processing tasks. The following are some of the most frequently used functions in the Python OpenCV library:

Basic Operations

  1. cv2.imread()

    • Loads an image from a file.
    • cv2.imread(path, flag)
    • Example: img = cv2.imread('image.jpg', cv2.IMREAD_COLOR)
  2. cv2.imshow()

    • Displays an image in a window.
    • cv2.imshow(window_name, image)
    • Example: cv2.imshow('image', img)
  3. cv2.imwrite()

    • Saves an image to a specified file.
    • cv2.imwrite(filename, img)
    • Example: cv2.imwrite('output.jpg', img)
  4. cv2.cvtColor()

    • Converts an image from one color space to another.
    • cv2.cvtColor(src, code)
    • Example: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Image Processing

  1. cv2.resize()

    • Resizes an image.
    • cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
    • Example: resized = cv2.resize(img, (100, 100))
  2. cv2.blur()

    • Blurs an image using a specific window.
    • cv2.blur(src, ksize)
    • Example: blurred = cv2.blur(img, (5, 5))
  3. cv2.Canny()

    • Edge detection.
    • cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]])
    • Example: edges = cv2.Canny(gray, 50, 150)
  4. cv2.threshold()

    • Applies a fixed-level threshold to each array element.
    • cv2.threshold(src, thresh, maxval, type)
    • Example: ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

Geometric Transformations

  1. cv2.warpAffine()

    • Applies an affine transformation to an image.
    • cv2.warpAffine(src, M, dsize)
    • Example: rotated = cv2.warpAffine(img, M, (cols, rows))
  2. cv2.getRotationMatrix2D()

    • Creates a matrix for rotation.
    • cv2.getRotationMatrix2D(center, angle, scale)
    • Example: M = cv2.getRotationMatrix2D((cols/2,rows/2), 90, 1)

Feature Detection

  1. cv2.findContours()

    • Finds contours in a binary image.
    • cv2.findContours(image, mode, method)
    • Example: contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  2. cv2.drawContours()

    • Draws contours on an image.
    • cv2.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]])
    • Example: cv2.drawContours(img, contours, -1, (0,255,0), 3)

These functions represent just a snapshot of what you can do with OpenCV in Python. Depending on the specific requirements of a project, you might use many other functions provided by this comprehensive library.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值