gpt4 book ai didi

python - 如何清理这张图片(opencv-python)?

转载 作者:行者123 更新时间:2023-12-02 17:01:00 28 4
gpt4 key购买 nike

我对opencv真的很陌生。如何在不丢失信息的情况下消除背景噪音?
我是从这个开始的:Otsu 对它进行了阈值化。我试过腐 eclipse 、膨胀、双边过滤。我的目标是在边界上得到一个矩形,这样我就可以透视变换阈值图片,但它很难找到轮廓。或者也许有不同的更好的方法?

最佳答案

这是在 Python/OpenCV 中执行此操作的一种方法。

  • 阅读输入
  • 模糊它
  • 转换为HSV并提取饱和 channel
  • 阈值饱和图像
  • 用形态关闭和打开清理它并保存为掩码
  • 重新创建您的 OTSU 阈值图像
  • 将黑色写入掩码为黑色(零)的 OTSU 图像
  • 为了比较,将黑色写入掩码为黑色(零)的输入图像
  • 保存结果

  • 输入:

    enter image description here
    import cv2
    import numpy as np

    # read image
    img = cv2.imread('circuit_board.jpg')

    # blur
    blur = cv2.GaussianBlur(img, (3,3), 0)

    # convert to hsv and get saturation channel
    sat = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)[:,:,1]

    # threshold saturation channel
    thresh = cv2.threshold(sat, 50, 255, cv2.THRESH_BINARY)[1]

    # apply morphology close and open to make mask
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
    morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
    mask = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel, iterations=1)

    # do OTSU threshold to get circuit image
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

    # write black to otsu image where mask is black
    otsu_result = otsu.copy()
    otsu_result[mask==0] = 0

    # write black to input image where mask is black
    img_result = img.copy()
    img_result[mask==0] = 0

    # write result to disk
    cv2.imwrite("circuit_board_mask.png", mask)
    cv2.imwrite("circuit_board_otsu.png", otsu)
    cv2.imwrite("circuit_board_otsu_result.png", otsu_result)
    cv2.imwrite("circuit_board_img_result.png", img_result)


    # display it
    cv2.imshow("IMAGE", img)
    cv2.imshow("SAT", sat)
    cv2.imshow("MASK", mask)
    cv2.imshow("OTSU", otsu)
    cv2.imshow("OTSU_RESULT", otsu_result)
    cv2.imshow("IMAGE_RESULT", img_result)
    cv2.waitKey(0)

    蒙版图片:

    enter image description here

    OTSU 阈值图像:

    enter image description here

    大通结果:

    enter image description here

    图像结果:

    enter image description here

    关于python - 如何清理这张图片(opencv-python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61130257/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com