How To Delete The Other Object From Figure By Using Opencv?
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
Solution 1:
You can use multi-scale template matching approach.
You want to remove the following image:
- 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 = Nonefor 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 isNoneor 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)
- Fill the rectangle
cv2.rectangle(image, (startX, startY), (endX, endY), (255, 255, 255), -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)
Post a Comment for "How To Delete The Other Object From Figure By Using Opencv?"