从图像中提取已知形状

Extract already know shape from image

我正在尝试提取这篇文章

由此

我试过检测形状,没办法,训练一个 haarscade...(我没有底片)没办法,....位置可以变化(不是所有的都被插入)并且角度不是一样..我不能一张一张裁剪:-(

有什么建议吗???提前致谢

PS原图在这里https://pasteboard.co/JaTSoJF.png(对不起> 2Mb)

在处理@ganeshtata 后我们得到了

import cv2
import numpy as np
img = cv2.imread('cropsmall.png')
height, width = img.shape[:2]
green_channel = img[:,0:] # Blue channel extraction
res = cv2.fastNlMeansDenoising(green_channel, None, 3, 7, 21) # Non-local means denoising
cv2.imshow('denoised',res)
edges = cv2.Canny(res, 11, 11, 3) # Edge detection
kernel = np.ones((30, 30),np.uint8)
closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel) # Morphological closing
im2, contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Find all contours in the image

for cnt in contours: # Iterate through all contours
    x, y, w, h = cv2.boundingRect(cnt) # Reject contours whose height is less than half the image height
    if h < height / 2:
        continue
    y = 0 # Assuming that all shapes start from the top of the image
    cv2.rectangle(img, (x, y), \
          (x + w, y + h), (0, 255, 0), 2)
    cv2.imshow('IMG',img)
    cv2.imwrite("test.jpg",img)
    cv2.waitKey(0)

这给了我们

不错...

我使用以下方法提取问题中指定的模式。

  1. 读取图像并从图像中提取蓝色通道。

    import cv2
    import numpy as np
    img = cv2.imread('image.png') 
    height, width = img.shape[:2]
    blue_channel = img[:,:,0]
    

    蓝色通道 -

  2. 在蓝色通道图像上应用 OpenCV 的 Non-local Means Denoising algorithm。这确保图像中的大部分随机噪声被平滑。

    res = cv2.fastNlMeansDenoising(blue_channel, None, 3, 7, 21)
    

    去噪图像 -

  3. 应用 Canny 边缘检测。

    edges = cv2.Canny(res, 1, 10, 3)
    

    边缘输出-

  4. 应用Morpological Closing尝试关闭图像中的小gaps/holes。

    kernel = np.ones((30, 30),np.uint8)
    closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
    

    应用形态学闭合后的图像 -

  5. 使用cv2.findContours. After finding all contours, we can determine the bounding box of each contour using cv2.boundingRect查找图像中的所有轮廓。

    im2, contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Find all contours
    
    for cnt in contours: # Iterate through all contours
        x, y, w, h = cv2.boundingRect(cnt) $ Get contour bounding box
        if h < height / 2: # Reject contours whose height is less than half the image height
            continue
        y = 0  # Assuming that all shapes start from the top of the image
        cv2.rectangle(img, (x, y), \
              (x + w, y + h), (0, 255, 0), 2)
    
  6. 最终结果-

完整代码 -

import cv2
import numpy as np
img = cv2.imread('image.png')
height, width = img.shape[:2]
blue_channel = img[:,:,0] # Blue channel extraction
res = cv2.fastNlMeansDenoising(blue_channel, None, 3, 7, 21) # Non-local means denoising
edges = cv2.Canny(res, 1, 10, 3) # Edge detection
kernel = np.ones((30, 30),np.uint8)
closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel) # Morphological closing
im2, contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Find all contours in the image

for cnt in contours: # Iterate through all contours
    x, y, w, h = cv2.boundingRect(cnt) # Reject contours whose height is less than half the image height
    if h < height / 2:
        continue
    y = 0 # Assuming that all shapes start from the top of the image
    cv2.rectangle(img, (x, y), \
          (x + w, y + h), (0, 255, 0), 2)

注意 - 此方法适用于您发布的示例图像。它 might/might 不能概括所有图像。