How to delete the other object from figure by using opencv?

旧城冷巷雨未停 提交于 2020-12-15 05:18:07

问题


I tried to detect the yellow lines in the following picture but the logo(yellow color) will be marked as well. My question is that how to mask the logo?

I use the standard code of the Hough Transform. My code is as follows:

import cv2
import numpy as np
img = cv2.imread('Road3.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


low_yellow=np.array([18, 94, 140])
up_yellow=np.array([48, 255, 255])
 mask=cv2.inRange(hsv, low_yellow, up_yellow)
 edges = cv2.Canny(mask,75,150)

 lines = cv2.HoughLinesP(edges,1,np.pi/180,50,maxLineGap=250)
  for line in lines:
 x1,y1,x2,y2 = line[0]
 cv2.line(img,(x1,y1),(x2,y2),(0,255,0),5)

 cv2.imshow('image', img)
 cv2.imshow("edges", edges)
 k = cv2.waitKey(0)
 cv2.destroyAllWindows()

回答1:


You can use multi-scale template matching approach.

You want to remove the following image:

    1. Detect the image
    import cv2
    import imutils
    import numpy as np
    
    template = cv2.imread("template/template.jpg")
    template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
    template = cv2.Canny(template, 50, 200)
    (h, w) = template.shape[:2]
    
    image = cv2.imread('nnEGw.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    found = None
    
    for scale in np.linspace(0.2, 1.0, 20)[::-1]:
         resized = imutils.resize(gray, width=int(gray.shape[1] * scale))
         r = gray.shape[1] / float(resized.shape[1])
    
         if resized.shape[0] < h or resized.shape[1] < w:
             break
    
         edged = cv2.Canny(resized, 50, 200)
         result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
         (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
    
         if found is None or maxVal > found[0]:
             found = (maxVal, maxLoc, r)
    
    (_, maxLoc, r) = found
    (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
    (endX, endY) = (int((maxLoc[0] + w) * r), int((maxLoc[1] + h) * r))
    
    cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
    cv2.imwrite("result/edges2.png", image)
    

      1. Fill the rectangle
    cv2.rectangle(image, (startX, startY), (endX, endY), (255, 255, 255), -1)
    

      1. Apply Hough Transform
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    
    low_yellow = np.array([18, 94, 140])
    up_yellow = np.array([48, 255, 255])
    mask = cv2.inRange(hsv, low_yellow, up_yellow)
    edges = cv2.Canny(mask, 75, 150)
    
    lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 50, maxLineGap=250)
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 5)
    
        # cv2.imshow('image', img)
        cv2.imwrite("result/edges3.png", edges)
    



来源:https://stackoverflow.com/questions/63509388/how-to-delete-the-other-object-from-figure-by-using-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!