python - I'm trying to detect starbucks logo in a scene. How do I get rid of these artefacts in the bounding polylines? -
i built starbucks logo detector i'm getting these weird artefacts when draw polylines supposed surround logo.
here proper result looks like:
here examples of artefacts:
i'm using sift detect keypoints , draw rectangle taught in opencv tutorials shown here:
import numpy np import cv2 cap = cv2.videocapture(0) sift = cv2.xfeatures2d.sift_create() img1 = cv2.imread('logo.png', 0) img1.resize(512, 512) kp1, des1 = sift.detectandcompute(img1, none) while (true): ret, frame = cap.read() frame = findlogo(frame, kp1=kp1, des1=des1) cv2.imshow("frame",frame) if cv2.waitkey(1) & 0xff == ord(' '): break cap.release() out.release() cv2.destroyallwindows() def findlogo(frame, kp1, des1): min_match_count = 10 # initiate sift detector sift = cv2.xfeatures2d.sift_create() img2 = cv2.cvtcolor(frame, cv2.color_bgr2gray) # find keypoints , descriptors sift kp2, des2 = sift.detectandcompute(img2, none) if len(kp2) != 0 , des2 not none: flann_index_kdtree = 0 index_params = dict(algorithm=flann_index_kdtree, trees=5) search_params = dict(checks=50) flann = cv2.flannbasedmatcher(index_params, search_params) matches = flann.knnmatch(des1, des2, k=2) # store matches per lowe's ratio test. = [] m, n in matches: if m.distance < 0.7 * n.distance: good.append(m) if len(good) > min_match_count: src_pts = np.float32([kp1[m.queryidx].pt m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainidx].pt m in good]).reshape(-1, 1, 2) m, mask = cv2.findhomography(src_pts, dst_pts, cv2.ransac, 5.0) matchesmask = mask.ravel().tolist() # h, w = img1.shape h = 512 w = 512 pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2) if m not none: dst = cv2.perspectivetransform(pts, m) frame = cv2.polylines(frame, [np.int32(dst)], true, (0, 255, 255), 3, cv2.line_aa) else: print("not enough matches found - %d/%d" % (len(good), min_match_count)) matchesmask = none return frame i see occur when program cannot detect image. there lines in program prevent this, (and work of time when there nothing on screen), error still happening. changing min_match_count higher number didn't help. can see here:
the artefact appears when there 37 matches. setting high means program wont find logo unless phone still.
how can rid of artefacts? ideas?
the deformed contours due homography not being computed.
here way discard bad contours when object you're looking has rectangular shape:
- find shortest , longest sides of contour , calculate relative gap between both sides : | longest - shortest | / longest. if relative gap big means contour bad.
now, 4 methods can work any contours (not rectangular contours):
- set thresholds contour's area (that can calculated using
contourarea). contour's area should neither small nor large. - check number of inliers given
findhomographyfunction. if number bellow threshold (for instance, 10), discard contour. - check any edge of contour doesn't intersect other edge. if that's case, can sure contour bad.
- you can try function reads homography matrix values , returns true or false whether homography "nice" or not. (you might need tweak values inside function needs).
discarding bad contours not easy task, using several of methods explained above, should rid of majority of wrong contours.





Comments
Post a Comment