opencv-常用代码

以下是一些OpenCV中常用的代码片段,涵盖了一些基本的图像处理和计算机视觉任务。

  1. 加载和显示图像:

    python 复制代码
    import cv2
    
    # 读取图像
    img = cv2.imread('image.jpg')
    
    # 显示图像
    cv2.imshow('Image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
  2. 调整图像大小:

    python 复制代码
    resized_img = cv2.resize(img, (width, height))
  3. 灰度转换:

    python 复制代码
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  4. 图像平滑:

    python 复制代码
    smoothed_img = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
  5. 边缘检测:

    python 复制代码
    edges = cv2.Canny(gray_img, threshold1, threshold2)
  6. 图像阈值处理:

    python 复制代码
    ret, binary_img = cv2.threshold(gray_img, threshold_value, max_value, cv2.THRESH_BINARY)
  7. 图像轮廓检测:

    python 复制代码
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  8. 绘制轮廓:

    python 复制代码
    cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
  9. 人脸检测:

    python 复制代码
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    faces = face_cascade.detectMultiScale(gray_img, scaleFactor=1.3, minNeighbors=5)
  10. 图像旋转:

    python 复制代码
    rows, cols = img.shape[:2]
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, scale)
    rotated_img = cv2.warpAffine(img, M, (cols, rows))
  11. 图像腐蚀与膨胀:

    python 复制代码
    kernel = np.ones((kernel_size, kernel_size), np.uint8)
    eroded_img = cv2.erode(img, kernel, iterations=1)
    dilated_img = cv2.dilate(img, kernel, iterations=1)
  12. 图像直方图均衡化:

    python 复制代码
    equ_img = cv2.equalizeHist(gray_img)
  13. 图像混合:

    python 复制代码
    blended_img = cv2.addWeighted(img1, alpha, img2, beta, gamma)
  14. 图像拼接:

    python 复制代码
    stitched_img = cv2.hconcat([img1, img2])
  15. 图像截取:

    python 复制代码
    roi = img[y:y+h, x:x+w]
  16. 图像相加:

    python 复制代码
    added_img = cv2.add(img1, img2)
  17. 图像减法:

    python 复制代码
    subtracted_img = cv2.subtract(img1, img2)
  18. 图像位运算:

    python 复制代码
    bitwise_and = cv2.bitwise_and(img1, img2)
    bitwise_or = cv2.bitwise_or(img1, img2)
    bitwise_xor = cv2.bitwise_xor(img1, img2)
    bitwise_not = cv2.bitwise_not(img)
  19. 图像平均模糊:

    python 复制代码
    averaged_img = cv2.blur(img, (kernel_size, kernel_size))
  20. 中值滤波:

    python 复制代码
    median_blurred_img = cv2.medianBlur(img, ksize)
  21. 自适应阈值:

    python 复制代码
    adaptive_thresh = cv2.adaptiveThreshold(
        gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
  22. 图像旋转缩放:

    python 复制代码
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, scale)
    rotated_resized_img = cv2.warpAffine(img, M, (new_cols, new_rows))
  23. SIFT 特征提取和匹配:

    python 复制代码
    sift = cv2.SIFT_create()
    keypoints1, descriptors1 = sift.detectAndCompute(img1, None)
    keypoints2, descriptors2 = sift.detectAndCompute(img2, None)
  24. SURF 特征提取和匹配:

    python 复制代码
    surf = cv2.SURF_create()
    keypoints1, descriptors1 = surf.detectAndCompute(img1, None)
    keypoints2, descriptors2 = surf.detectAndCompute(img2, None)
  25. ORB 特征提取和匹配:

    python 复制代码
    orb = cv2.ORB_create()
    keypoints1, descriptors1 = orb.detectAndCompute(img1, None)
    keypoints2, descriptors2 = orb.detectAndCompute(img2, None)
  26. 使用 FLANN 匹配器进行特征匹配:

    python 复制代码
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)
    flann = cv2.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(descriptors1, descriptors2, k=2)
  27. 简单图像深度学习任务:

    python 复制代码
    net = cv2.dnn.readNet('model.weights', 'model.cfg')
    blob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(300, 300), mean=(104.0, 177.0, 123.0))
    net.setInput(blob)
    detections = net.forward()
  28. HOG 特征提取与行人检测:

    python 复制代码
    hog = cv2.HOGDescriptor()
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
    boxes, weights = hog.detectMultiScale(img, winStride=(8, 8), padding=(0, 0), scale=1.05)

这些是一些基本的OpenCV代码示例,覆盖了图像处理和计算机视觉中的常见任务。在实际应用中,可能需要根据具体场景和需求进一步调整参数和算法。

相关推荐
John_ToDebug20 分钟前
大模型提示词(Prompt)终极指南:从原理到实战,让AI输出质量提升300%
人工智能·chatgpt·prompt
居然JuRan20 分钟前
LangGraph从0到1:开启大模型开发新征程
人工智能
双向3329 分钟前
实战测试:多模态AI在文档解析、图表分析中的准确率对比
人工智能
用户51914958484531 分钟前
1989年的模糊测试技术如何在2018年仍发现Linux漏洞
人工智能·aigc
人类发明了工具32 分钟前
【深度学习-基础知识】单机多卡和多机多卡训练
人工智能·深度学习
用户51914958484542 分钟前
检索增强生成(RAG)入门指南:构建知识库与LLM协同系统
人工智能·aigc
CoovallyAIHub43 分钟前
方案 | 动车底部零部件检测实时流水线检测算法改进
深度学习·算法·计算机视觉
CoovallyAIHub1 小时前
方案 | 光伏清洁机器人系统详细技术实施方案
深度学习·算法·计算机视觉
星期天要睡觉1 小时前
机器学习——CountVectorizer将文本集合转换为 基于词频的特征矩阵
人工智能·机器学习·矩阵
lxmyzzs1 小时前
【图像算法 - 14】精准识别路面墙体裂缝:基于YOLO12与OpenCV的实例分割智能检测实战(附完整代码)
人工智能·opencv·算法·计算机视觉·裂缝检测·yolo12