代码之家  ›  专栏  ›  技术社区  ›  Shahrukh Azhar

opencv中的图像处理

  •  -3
  • Shahrukh Azhar  · 技术社区  · 6 年前

    如何更改此图像:

    enter image description here

    进入此图像:

    enter image description here

    我在python中使用opencv,并尝试了侵蚀膨胀和平滑,但没有得到结果。

    4 回复  |  直到 6 年前
        1
  •  1
  •   Yves Daoust    6 年前

    为了保持边缘光滑,

    • 通过插值将图像放大一个因子(3或4);

    • 二进制化;

    • 平整的

    • 通过抽取减少。

    enter image description here

        2
  •  0
  •   sshashank124    6 年前

    直接使用以下示例 OpenCV's Image Thresholding Example

    import cv2 as cv
    
    img = cv.imread('roxy.png',0)
    # Otsu's thresholding after Gaussian filtering
    blur = cv.GaussianBlur(img,(5,5),0)
    _,res = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
    cv.imwrite('roxy_result.png', res)
    

    我得到了以下结果。多做一点实验就能得到你想要的

    enter image description here

        3
  •  0
  •   Ishara Madhawa    6 年前

    可以为图像设置阈值。请尝试以下代码:

    import cv2
    img = cv2.imread('input.png',0)
    threshold = 20
    img = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)[1]
    cv.imwrite('output.png', img)
    

    这里我手动设置了一个阈值(20),因为自动阈值计算算法会产生一些噪声。请记住,为阈值设置手动值不是一种好的做法。我认为这是一项具体的任务。

    结果如下:

        4
  •  0
  •   Jeru Luke    6 年前

    我想即兴创作。我选择的阈值是给定灰度图像中值的1/3,而不是通过反复试验手动应用阈值。

    import cv2
    
    img = cv2.imread(r'C:\Users\Desktop\roxy.png', 0)
    
    th = int(max(0, (1.0 - 0.66) * np.median(img)))
    ret, res = cv2.threshold(img, th, 255, cv2.THRESH_BINARY)
    cv2.imwrite(r'C:\Users\Desktop\roxy1res.png', res)
    

    enter image description here

    在此图像中,您可以看到许多点,这些点可以通过执行中值滤波来移除:

    median = cv2.medianBlur(res, 3)
    cv2.imwrite(r'C:\Users\Desktop\roxy2median.png', median)
    

    enter image description here

    但现在字母R有一些间隙,可以通过使用定制的核进行形态学侵蚀来重建。您可以使用内置内核,但生成的图像不会像您期望的那样好。

    kernel = np.array([1,1,1],  dtype=np.int8)
    kernel = kernel.reshape((1, 3))
    
    erosion = cv2.erode(median, kernel, iterations = 1)
    cv2.imwrite(r'C:\Users\Desktop\roxy3erosion.png', erosion)
    

    enter image description here

    字母R仍然不完美,但你可以对其进行微调。