def checkOreantation(img): LMG = [] for name in ['5','twoTH','ThreeEN']: sample = cv2.imread(name+'.jpg') sample = cv2.cvtColor(sample, cv2.COLOR_BGR2GRAY) ret, sample = cv2.threshold(sample, 127, 255,0) sample, contours, hierarchy = cv2.findContours(sample, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE) sample = [] for cnt in contours: sample += cnt.tolist() sample = np.array(sample) LMG.append(sample) img, contours, hierarchy = cv2.findContours(img, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE) img = [] for cnt in contours: img += cnt.tolist() img = np.array(img) p_ret = cv2.matchShapes(img,LMG[0],1,0.0) for i in range(1,len(LMG)): ret = cv2.matchShapes(img,LMG[i],1,0.0) if ret < p_ret: p_ret = ret return ret
def shape_match(): img1 = cv2.imread('l1.png', 0) img2 = cv2.imread('l2.png', 0) ret, thresh1 = cv2.threshold(img1, 13, 255, 1) ret, thresh2 = cv2.threshold(img2, 13, 255, 1) im1,contours1,hierarchy = cv2.findContours(thresh1, 2, 1) im2,contours2,hierarchy = cv2.findContours(thresh2, 2, 1) print len(contours1), len(contours2) cv2.drawContours(img1,contours1, -1, (200,0,0)) cv2.drawContours(img2,contours2, -1, (200,0,0)) cv2.imshow("1",img1) cv2.imshow("2",img2) cv2.waitKey() #query_cnt = get_correct_cnt(contours2, img2) #if query_cnt is None: # print 'parse img failed' # return height, width = img1.shape area = height * width min_area = area / 25 max_area = area / 5 for cnt in contours1: print cv2.boundingRect(cnt) letter_area = get_cnt_area(cnt) if not (min_area < letter_area and letter_area < max_area): continue print cv2.matchShapes(cnt, query_cnt, 1, 0.0)
def trouverRobot(self, contoursRobot): precisionDroite = 1000 precisionGauche = 1000 contourDroit = None contourGauche = None for contour in contoursRobot: resultatsMatch = [] resultatsMatch.append((cv2.matchShapes(contour, self.cntRobotDroit, 1, 0.0), contour, 'Droite')) resultatsMatch.append((cv2.matchShapes(contour, self.cntRobotGauche, 1, 0.0), contour, 'Gauche')) meilleurMatch = min(resultatsMatch) precision, contour, position = meilleurMatch if precision < MAX_PRECISION: if (position == 'Droite') and (precision < precisionDroite): precisionDroite = precision contourDroit = copy.deepcopy(contour) elif (position == 'Gauche') and (precision < precisionGauche): precisionGauche = precision contourGauche = copy.deepcopy(contour) if (contourDroit is None) or (contourGauche is None): self.robotIdentifiee = None else: centre, orientation = self.trouverInfoRobot(contourGauche, contourDroit) self.robotIdentifiee = Robot(centre, orientation)
def match(cnt1, cnt2, mode=3): #threshold distance based matching via Humoments #cnt1 = the train countour set you want to match to #cnt2 = the test contour to match to cnt1 #mode = the various modes employ alternate algorithms if desired if mode==1: return cv2.matchShapes(cnt1,cnt2,cv.CV_CONTOURS_MATCH_I1,0) elif mode==2: return cv2.matchShapes(cnt1,cnt2,cv.CV_CONTOURS_MATCH_I2,0) else: return cv2.matchShapes(cnt1,cnt2,cv.CV_CONTOURS_MATCH_I3,0)
def trouverIles(self, contoursIles, couleur): for contour in contoursIles: resultatsMatch = [] resultatsMatch.append((cv2.matchShapes(contour, self.cntTriangle, 1, 0.0), contour, "Triangle")) resultatsMatch.append((cv2.matchShapes(contour, self.cntCercle, 1, 0.0), contour, "Cercle")) resultatsMatch.append((cv2.matchShapes(contour, self.cntCarre, 1, 0.0), contour, "Carre")) resultatsMatch.append((cv2.matchShapes(contour, self.cntPentagone, 1, 0.0), contour, "Pentagone")) meilleurMatch = min(resultatsMatch) precision, contour, nomForme = meilleurMatch if precision < MAX_PRECISION: centre = self.trouverCentre(contour) self.ilesIdentifiees.append(Ile(centre, couleur, nomForme)) print couleur print centre
def __calc_contours_similiar(checkedImage,dataImageList): checkedImage = chacracter.getContours(checkedImage) calc_contours_rel = [] if len(dataImageList) == 1: dataImage = chacracter.getContours(dataImageList[0]) tmp = cv2.matchShapes(checkedImage,dataImage,3,1.0) calc_contours_rel.append(tmp) return calc_contours_rel else: for checkingImage in dataImageList: checkingImage = chacracter.getContours(checkingImage) tmp = cv2.matchShapes(checkedImage,checkingImage,3,1.0) calc_contours_rel.append(tmp) tmp = None return calc_contours_rel
def __contour_compare(self,img1,img2): #edges1 = cv2.Canny(img1,10,200) #edges2 = cv2.Canny(img2,10,200) ret1, binary1 = cv2.threshold(img1,127,255,cv2.THRESH_BINARY) ret2, binary2 = cv2.threshold(img2,127,255,cv2.THRESH_BINARY) cturs1, hier1 = cv2.findContours(binary1,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cturs2, hier2 = cv2.findContours(binary2,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) if len(cturs1)==0 or len(cturs2)==0: return False """ ic1 = cv2.drawContours(img1,cturs1, 0, (255,255,255), 2) ic2 = cv2.drawContours(img2,cturs2, 0, (255,255,255), 2) cv2.imshow('img1',img1) cv2.imshow('img2',img2) cv2.waitKey(0) """ ct1, area1, arcl1 = self.__find_main_contour(cturs1) ct2, area2, arcl2 = self.__find_main_contour(cturs2) ret = cv2.matchShapes(ct1,ct2,1,0.0) #轮廓相似 if ret < 0.02: return True elif ret < 0.1: dea = abs(area1 - area2) min_area=0 if area1 > area2: min_area = int(area2/10) else: min_area = int(area1/10) if dea < min_area: return True return False
def shape_matches(templates, image, cap, smallest): #make a copy so that the contour get doesnt mess stuff up count_image = image.copy() #get the contours from the treshold image copy contours, hierarchy = cv2.findContours(count_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #foreach contour found draw a box around the area area = 0 rect = 0 match = 2 count = 0 for cnt in contours: #get rid of really small boxes narea = cv2.contourArea(cnt) x, y, w, h = cv2.boundingRect(cnt) if cap > narea > smallest: for template in templates: tcontour, th = cv2.findContours(template.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) mm = .2 temp = tcontour[0] for cnt2 in tcontour: tm = cv2.matchShapes(cnt2, cnt, 3, 0.0) if tm < mm: mm = tm temp = cnt2 if mm < match and mm != 0: match = mm rect = (x, y, w, h, mm) if match != 2: print match if match > .2: return 0 else: return rect
def load_shape_comparison(self): self.contour_list = [] for shape in self.shape_choices: dataset = os.path.abspath(utils.__file__)[:-12] + "/target_detection_masks/" + shape + "/" for filename in os.listdir(dataset): if filename.endswith(".png"): shape_mask_img = cv2.imread(os.path.join(dataset, filename),0) shape_mask_img = Image.open(os.path.join(dataset, filename)) shape_mask_img = utils.generate_gaussian_noise_by_level(shape_mask_img, 3, shape_mask_img.width) shape_mask_img = numpy.array(shape_mask_img) _,contours,_ = cv2.findContours(shape_mask_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) shape_contour = contours[0] comparison = cv2.matchShapes(shape_contour, self.target_contour, 1, 0.0) self.contour_list.append((shape, comparison)) smallest_difference_index = 0 for i in range(1, len(self.contour_list)): if(self.contour_list[i][1] < self.contour_list[smallest_difference_index][1]): smallest_difference_index = i self.shape_type = self.contour_list[smallest_difference_index][0]
def contour_match(template, contours, image): image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) bestCoef = None bestContour = None matched = [] for contour in contours: matchingCoef = cv2.matchShapes(template, contour, 1, 1.0) if matchingCoef > 0.0: matched.append((matchingCoef, contour)) if matchingCoef > 0.0 and (matchingCoef < bestCoef or bestCoef is None): bestCoef = matchingCoef bestContour = contour # print matchingCoef bestResults = sorted(matched, key=lambda x: x[0]) # draw_one_contour(image, bestContour) if bestResults > 2: draw_one_contour(image, bestResults[0][1], (255, 0, 0)) draw_one_contour(image, bestResults[1][1], (0, 255)) draw_one_contour(image, bestResults[2][1], (0, 0, 255)) print 'Best:', bestCoef print 'Best contour length: ', len(bestContour) for res in bestResults[:3]: contour = res[1] print '%0.3f %3d %0.2f %0.2f ' % (res[0], len(contour), contourSolidity(contour), contourExtent(contour)) # print 'Area: ', cv2.contourArea(contour) # print 'Perimeter: ', cv2.arcLength(contour, True) # print 'Solidity: ', contourSolidity(contour) return image
def cardRecognize(card): cardGray = cv2.cvtColor(card, cv2.COLOR_BGR2GRAY) _, cardThreshold = cv2.threshold(cardGray, 100, 255, cv2.THRESH_BINARY) showImg(cardThreshold) contours, _ = cv2.findContours(cardThreshold, 1, 2) possibleSuit = None minMatchValue = 0.05 for k, cardContour in enumerate(contours): k = k + 1 if len(cardContour) < 20 or k == len(contours): #print "Skip small points/last round" continue hasGetNew = 0 for suit,suitContour in templateObjs.items(): #Ref: http://docs.opencv.org/master/d5/d45/tutorial_py_contours_more_functions.html#gsc.tab=0 ret = cv2.matchShapes(suitContour, cardContour, 3, 0.0) if ret < minMatchValue: minMatchValue = ret possibleSuit = suit hasGetNew = 1 cv2.drawContours(card, [cardContour], 0, (0,255,0), -1) print "Round: " + str(k) + "/" + str(len(contours)) + ", count:" + str(len(cardContour)) + ", match: " + str(suit) + ", " + str(ret) showImg(card, str(possibleSuit)) print "Result: " + str(possibleSuit) + "\n"
def match_shapes(foldername, opname, method): foldername = _separator.split(foldername)[0] print "Starting [%s]" % opname # Loading images images = [] for filename in os.listdir(foldername): img = cv2.imread(os.path.join(foldername, filename), 0) images.append(Image(filename, img)) L = min(7, len(images)) # noqa: constant for img1 in images: similars = sorted([(cv2.matchShapes(img1.img, img2.img, method, 0), img2) for img2 in images]) plt.subplot(2, L, (L+1)/2), plt.imshow(img1.img, cmap="gray") plt.title("Target: " + img1.filename), plt.xticks([]), plt.yticks([]) for idx, elem in enumerate(similars[1:L+1]): plt.subplot(2, L, L + idx + 1), plt.imshow(elem[1].img, cmap="gray") plt.title("%s (%.2e)" % (elem[1].filename, elem[0])), plt.xticks([]), plt.yticks([]) mng = plt.get_current_fig_manager() mng.window.state('zoomed') plt.show() print "Done"
def compareContours(buttContours, compare_images, showAllImages=0, showSelectMask=0): imSelect = [] returnImg = [] principalContour = buttContours[0][0] for i in range(len(buttContours)): # if showAllImages!=0 then want show all compare_images if showAllImages==1:# shows real image cv2.imshow(str(compare_images[i][2]),compare_images[i][1]) # compare contours with matchShapes if buttContours[i][1]==None: print '¡¡¡¡¡¡¡¡¡¡ not found this contour ('+str(compare_images[i][2])+') !!!!!!!!!!!' else: comp1 = cv2.matchShapes(principalContour, buttContours[i][0], cv2.cv.CV_CONTOURS_MATCH_I1,0) comp2 = cv2.matchShapes(principalContour, buttContours[i][0], cv2.cv.CV_CONTOURS_MATCH_I2,0) comp3 = cv2.matchShapes(principalContour, buttContours[i][0], cv2.cv.CV_CONTOURS_MATCH_I3,0) # compare contours with alternative moments if buttContours[i][1] != None: contour = buttContours[i][0] area = buttContours[i][2] x,y,w,h = cv2.boundingRect(contour) aspect_ratio = float(w)/h equi_diameter = np.sqrt(4*area/np.pi) hull = cv2.convexHull(contour) hull_area = cv2.contourArea(hull) hull_defects = np.abs(hull_area-area) c,r = cv2.minEnclosingCircle(contour) circle_area = r*r*np.pi circle_defects = np.abs(circle_area-area) copy = compare_images[i][1].copy() cv2.putText(copy,'['+str(comp1)+', '+str(comp2)+', '+str(comp3)+']',(50,copy.shape[0]-25),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0)) cv2.putText(copy, '['+str(aspect_ratio)+', '+str(equi_diameter)+', '+str(hull_defects)+', '+str(circle_defects)+']',(50,copy.shape[0]-50),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0)) if comp1<cv2.getTrackbarPos('eps-I1','config2')/100.0 and comp2<cv2.getTrackbarPos('eps-I2','config2')/100.0 and comp3<cv2.getTrackbarPos('eps-I3','config2')/100.0: imSelect = imSelect + [[compare_images[i][0],copy,compare_images[i][2]]] returnImg = returnImg + [compare_images[i]] # shows selected images for img in imSelect: cv2.imshow('selected_'+str(img[2]),img[1]) if showSelectMask == 1:# show selected image mask cv2.imshow('im_mask_'+str(img[2]),img[0]) return returnImg
def verify_contour(self, contour): ''' Returns a numerical comparison between a contour and the perfect model of the rectangle. A perfectly matched contour returns 0.0. Useful for filtering contours. ''' return cv2.matchShapes(contour, self.model_2D, 3, 0.0)
def runOthers(imgList): #print (imgList) imgCircle = cv2.imread('imgs/circle.png',0) imgSquare = cv2.imread('imgs/square.png',0) imgTriangle = cv2.imread('imgs/triangle.png',0) imgStar = cv2.imread('imgs/star.png',0) boardXScale = 512 / 72 boardYScale = 512 / 46 scaledImgList = [(boardXScale * y, boardYScale * x) for x, y in imgList] # Create a black image pts = np.array(scaledImgList, np.int32) img = np.zeros((512,512,4), np.uint8) cv2.fillPoly(img, np.int32([pts]), (255,255,255)) #cv2.polylines(img,[pts],True,(255,255,255)) img2 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cmpScores = [] cmpScores.append(('Circle', cv2.matchShapes(img2, imgCircle, 1,0.0))) cmpScores.append(('Square', cv2.matchShapes(img2, imgSquare, 1,0.0))) cmpScores.append(('Triangle', cv2.matchShapes(img2, imgTriangle, 1,0.0))) cmpScores.append(('Star', cv2.matchShapes(img2, imgStar, 1,0.0))) cmpScores = sorted(cmpScores, key=lambda cmp: cmp[1]) if(cmpScores[0][0] == 'Triangle' and cmpScores[1][0] == 'Star'): dst = cv2.cornerHarris(img2,5,5,0.04) count = 0 for i in range(512): for j in range(512): if(dst[i,j] > 0.8): count = count + 1 if (count < 600): print("It's a " + cmpScores[0][0]) os.system("say 'Its a '" + cmpScores[0][0]) else: print("It's a " + cmpScores[1][0]) os.system("say 'Its a '" + cmpScores[1][0]) else: print("It's a " + cmpScores[0][0]) os.system("say 'Its a '" + cmpScores[0][0]) cv2.imshow('image',img2) cv2.waitKey(0) cv2.destroyAllWindows()
def plus_test(self, the_one): try: ret = cv2.matchShapes(the_one, self.plusCT, 1, 0.0) print(ret) if(ret < 0.1): return " plus" except: " noooo" return None
def search_object(files): standart = cv2.imread(files['key*']) ret, thresh = cv2.threshold(standart, 80, 255, cv2.THRESH_BINARY) contours,hierarchy = cv2.findContours(thresh, 2, 1) cnt1 = contours[0] contours,hierarchy = cv2.findContours(thresh2,2,1) cnt2 = contours[0] ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
def handle_frame(frame): update_move_queue() while frame.shape[0] * frame.shape[1] > max_frame_size: frame = cv2.pyrDown(frame) fg_mask = bg_sub.apply(frame) # cv2.imshow('mask_original', fg_mask) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) _, fg_mask = cv2.threshold(fg_mask, 128, 255, cv2.THRESH_BINARY) # cv2.imshow('mask_optimized', fg_mask) gray_m = cv2.bitwise_and(gray, gray, mask=fg_mask) # cv2.imshow('gray_m', gray_m) edges = cv2.Canny(gray_m, cv2.getTrackbarPos('canny min', 'edge'), cv2.getTrackbarPos('canny max', 'edge')) cv2.imshow('edge', edges) contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # filter based on shape and area diff_max = cv2.getTrackbarPos('diff% max', 'result') / 100.0 area_min = cv2.getTrackbarPos('area min', 'result') candidate_indexes = [] for ind, cnt in enumerate(contours): diff = cv2.matchShapes(cnt, arrow_cnt, 1, 0.0) if diff > diff_max: continue area = cv2.contourArea(cnt) if area < area_min: continue candidate_indexes.append(ind) # for ind in candidate_indexes: # print '[', ind, '] -->', hierarchy[0, ind, :] # remove redundant candidates # print 'Before cleaning duplicates:', len(candidate_indexes) dup_sum = 0 for ind in candidate_indexes: dup_sum += remove_duplicates(hierarchy, candidate_indexes, ind) # print 'Duplicates:', dup_sum # print 'After cleaning duplicates:', len(candidate_indexes) bounding_rectangles = {} for ind in candidate_indexes: bounding_rectangles[ind] = cv2.boundingRect(contours[ind]) arrows, slots = classify_contours(candidate_indexes, contours, frame) for ind in slots: x, y, w, h = bounding_rectangles[ind] cv2.putText(frame, str(ind), (x, y - 2), font, 0.5, (255, 255, 255), 1, cv2.CV_AA) cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) for ind in arrows: x, y, w, h = bounding_rectangles[ind] cv2.putText(frame, str(ind), (x, y - 2), font, 0.5, (255, 255, 255), 1, cv2.CV_AA) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # print 'slots:', len(slots), ', arrows:', len(arrows), '(diff<=', diff_max, ', area>=', area_min, ')' directions = direction_recognition(candidate_indexes, contours, frame) check_new_move(arrows, slots, directions, bounding_rectangles) cv2.imshow('result', frame)
def GetPupil(gray, thr, structuringElementSize): '''Given a gray level image, gray and threshold value return a list of pupil locations''' #this will create the temporal image in color. tempResultImg = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) cv2.imshow("TempResults", tempResultImg) props = RegionProps() GetPupilKMeans(gray) val, binI = cv2.threshold(gray, thr, 255, cv2.THRESH_BINARY_INV) # binI = cv2.adaptiveThreshold(gray, 255, cv2.cv.CV_ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 5, 10) cv2.imshow("Threshold", binI) # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (structuringElementSize, structuringElementSize)) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) binI = cv2.erode(binI, kernel, iterations=1) binI = cv2.dilate(binI, kernel, iterations=1) #Calculate blobs contours, hierarchy = cv2.findContours(binI, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) pupils = [] accepted = [] sliderVals = getSliderVals() for contour in contours: p = props.CalcContourProperties(contour, ['Area', 'Length', 'Centroid', 'Extend', 'ConvexHull', 'Boundingbox', 'EquivDiameter']) if p['Area'] < sliderVals['minGlint'] or p['Area'] > sliderVals['maxGlint']: continue # if p['Area'] < 700 or p['Area'] > 6000: # continue # print(p['Extend']) if p['Extend'] < 0.07: continue # Figure out if contour is a circle hull = p['ConvexHull'] # c = p['Centroid'] center, radius = cv2.minEnclosingCircle(hull) circle = np.array(getCircleSamples2(center, radius)).astype(int) retval = cv2.matchShapes(circle, hull, cv2.cv.CV_CONTOURS_MATCH_I1, 0) if retval > 0.05: continue # accepted.append(circle) accepted.append(contour) # c = p['Centroid'] pupil = (center, (p['EquivDiameter'], p['EquivDiameter']), 0.0) pupils.append(pupil) # cv2.drawContours(binI, accepted, -1, (255, 0, 0)) # cv2.imshow("Threshold",binI) return pupils
def find_closest_match(grp, cnt): minimum = 1000 for letter in grp: cnt1 = cPickle.loads(open("ITSP_database/" + letter+".txt").read()) temp = cv2.matchShapes(cnt, cnt1, 1, 0.0) if temp < minimum: minimum = temp l = letter #print l, return l
def find_best_5(grp, cnt): cmp_poly = {1000: '00', 999: '00', 1001: '00', 1002: '00', 1003: '00'} for letter in grp: cnt1 = cPickle.loads(open("ITSP_database_contour/" + letter+".txt").read()) temp1 = cv2.matchShapes(cnt, cnt1, 1, 0.0) max_index = max(cmp_poly) if temp1 < max_index: cmp_poly.pop(max_index) cmp_poly.update({temp1: letter}) return cmp_poly
def findMatchingContours(self): matchingContours = [] for i in range(len(self.imgContours)): result = cv2.matchShapes(self.letterContour, self.imgContours[i], 2, 0) if result <= self.correlationCutoff: if abs(self.letterPerimAreaRatio - calculatePerimAreaRatio(self.imgContours[i]) < self.perimAreaCutoff): if abs(self.letterWHRatio - calcBoundingRectRatio(self.imgContours[i]) < self.whRatioCutoff): matchingContours.append(self.imgContours[i]) print len(matchingContours) return tuple(matchingContours)
def extract_cards(img): gray = cv2.cvtColor(scene ,cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (1,1), 1000) flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY) im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) contours = sorted(contours, key=cv2.contourArea,reverse=True)[:20] CNT_THRESH = 0.05 print [cv2.matchShapes(CARD_CONTOUR, cnt, 1, 0.0) for cnt in contours]
def find_best_2(dict, cnt): keys = dict.keys() cmp_poly = {1000: '00', 999: '00'} for key in keys: cnt1 = cPickle.loads(open("ITSP_database_contour/" + dict[key] + ".txt").read()) temp1 = cv2.matchShapes(cnt, cnt1, 1, 0.0) max_index = max(cmp_poly) if temp1 < max_index: cmp_poly.pop(max_index) cmp_poly.update({temp1: dict[key]}) return cmp_poly
def templateMatchingForOrignalImage(img): buf = 1 mbuf = 0 for x in range(0,len(img_name)): num = cv2.matchShapes(makeContourImage(img), makeContourImage(standard_img[x]),1,0) if buf>num: buf = num mbuf = x cv2.imshow('Matching image',standard_img[mbuf]) # cv.imshow('Matching image', standard_img[mbuf]) return buf
def classify(self, match): """ Classify a match -> {alienContour, centroid, contour} into an alien category """ rval = None closestMatch = float("inf") for alien in self.aliens: humatch = cv2.matchShapes(self.aliens[alien], match['alien'], 1, 0) if humatch < closestMatch: closestMatch = humatch rval = alien return rval
def find_closest_match(grp, poly, cnt): minimum = 1000 l = '00' cmp_poly = find_best_5(grp, cnt) cmp_poly = find_best_3(cmp_poly, poly) cmp_poly = find_best_2(cmp_poly, cnt) for letter in cmp_poly.items(): poly1 = cPickle.loads(open("ITSP_database_poly/" + str(letter[1])+".txt").read()) temp = cv2.matchShapes(poly, poly1, 1, 0.0) if temp < minimum: l = letter[1] minimum = temp return l
def identify_shape(self, cnt): THRESH = 0.15 SHAPES = { "oval": self.load_contour("oval"), "diamond": self.load_contour("diamond"), "squiggle": self.load_contour("squiggle") } res = [(cv2.matchShapes(shape, cnt, 1, 0.0), name) for (name, shape) in SHAPES.iteritems()] score, shape = min(res) if score < THRESH: return (shape, score) else: return None
def contourRectangularity(contour): """ Uses shape matching to compute a scalar measure (Hu moment) of a contour. The smaller this number is, the more rectangular the contour is. The number is returned directly. """ # This is a sample rectangle. The cv2.matchShapes procedure is scale and # rotation invariant so all that matters is that this is a rectangle of # some sort. rect = np.array([[[0, 0]], [[150, 0]], [[150, 300]], [[0, 300]]]) return cv2.matchShapes(contour, rect, 2, 0.0)
def start(self): edged = self.filter(self.image) cnts = self.find_contours(edged.copy()) passed = [] conts = cv2.drawContours(self.image.copy(), cnts, -1, (255, 255, 0, 255), 2) for ind, cnt in enumerate(cnts): hull = cv2.convexHull(cnt, returnPoints=True) perim = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(hull, 21, True) # * perim, True) sides = len(approx) formats = [hull, perim, approx, sides] if self.sides_check(sides): print("%d passed with %d sides" % (ind, sides)) conts = cv2.drawContours(conts, [approx], -1, (0, 0, 255, 255), 4) passed.append([ind, formats]) u_passed_list = [] for con in passed: ind = con[0] warp = self.warp_rect(con[1][2]) blur = cv2.medianBlur(warp, 3) canny = cv2.Canny(blur, 300, 500) cv2.imshow("can " + str(ind), canny) contour = self.find_contours(canny, True)[0] # Get largest contour match = cv2.matchShapes(self.s_cnt[0], contour, 3, 1) u_passed_list.append([ind, match]) warp = cv2.drawContours(warp, [contour], -1, (0, 0, 255, 255), 4) warp = cv2.putText(warp, str(ind), (45, 45), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 1, (255, 255, 255, 255), 3, 8) cv2.imshow("warp " + str(ind), warp) low_high = sorted(u_passed_list, key=lambda x: x[1]) lowest = low_high[0][0] print("Selecting contour %d" % lowest) hull = cv2.convexHull(cnts[lowest], returnPoints=True) m = cv2.moments(hull) centered = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00'])) print("Selected object center %d : %d" % centered) conts = cv2.putText(conts, str("Target"), (centered[0] - 40, centered[1] - 30), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 1, (255, 255, 255, 255), 1, 8) conts = cv2.putText(conts, str("%d : %d" % centered), (centered[0] - 50, centered[1] + 60), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 1, (255, 255, 255, 255), 1, 8) cv2.imshow("source", self.image) cv2.imshow("edged", edged) cv2.imshow("contours", conts) self.wait_destroy()
plt.subplot(423),plt.imshow(img_binary,'gray'),plt.title('img_binary') zhifangtu(img_binary,424,str(maxMinAvarge(img_equ))) plt.subplot(425),plt.imshow(img,'gray'),plt.title('final') plt.figure(2) j=1 for i in number_image: plt.subplot(10,3,j),plt.imshow(i,'gray') j=j+1 plt.figure(3) j=1 tmp=getContoursRectImage(number_mod) plt.subplot(3,5,12),plt.imshow(number_image[3],'gray') m=cv2.moments(number_image[3]) hm=cv2.HuMoments(m) #image,contours,hierarchy = cv2.findContours(number_image[2].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) #hm=contours[0] epsilon = 0.1*cv2.arcLength(contours[0],True) hm = cv2.approxPolyDP(contours[0],epsilon,True) for i in tmp[0]: plt.subplot(3,5,j),plt.imshow(i,'gray'),plt.title(str(cv2.matchShapes(hm,tmp[1][j-1],2,0.0))) j=j+1 #print cv2.matchShapes(tmp[1][1],tmp[1][4],1,0.0) #print cv2.matchShapes(tmp[1][5],tmp[1][8],1,0.0) #plt.subplot(111),plt.imshow(number_mod,'gray') plt.show()
def match_dot(dot_cnt,src_cnt): out = cv2.matchShapes(dot_cnt, src_cnt, 1 ,0.0) return out
def fingerCursor(device): cap = cv2.VideoCapture(device) cap_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) cap_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) gesture2 = cv2.imread('gesture2.png') gesture2 = cv2.cvtColor(gesture2, cv2.COLOR_BGR2GRAY) _, gesture2, _ = cv2.findContours(gesture2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) skin_min = np.array([0, 0, 0], np.uint8) skin_max = np.array([0, 0, 255], np.uint8) topmost_last = (200, 100) traj = np.array([], np.uint16) traj = np.append(traj, topmost_last) dist_pts = 0 dist_records = [dist_pts] low_filter_size = 5 low_filter = deque( [topmost_last, topmost_last, topmost_last, topmost_last, topmost_last], low_filter_size) gesture_filter_size = 5 gesture_matching_filter = deque([0.0, 0.0, 0.0, 0.0, 0.0], gesture_filter_size) gesture_index_thres = 0.8 orange = (0, 97, 255) blue = (255, 0, 0) green = (0, 255, 0) kernel_size = 5 kernel1 = np.ones( (kernel_size, kernel_size), np.float32) / kernel_size / kernel_size kernel2 = np.ones((10, 10), np.uint8) / 100 while (cap.isOpened()): ret, frame_raw = cap.read() while not ret: ret, frame_raw = cap.read() frame_raw = cv2.flip(frame_raw, 1) frame = frame_raw[:round(cap_height), :round(cap_width)] cv2.imshow('raw_frame', frame) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, skin_min, skin_max) res = cv2.bitwise_and(hsv, hsv, mask=mask) res = cv2.erode(res, kernel1, iterations=1) res = cv2.dilate(res, kernel1, iterations=1) rgb = cv2.cvtColor(res, cv2.COLOR_HSV2BGR) cv2.imshow('rgb_2', rgb) gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY) # cv2.imshow('gray',gray) gray = cv2.GaussianBlur(gray, (11, 11), 0) cv2.imshow('gray', gray) im2, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) if len(contours) != 0: c = max(contours, key=cv2.contourArea) if cv2.contourArea(c) > 1000: topmost = tuple(c[c[:, :, 1].argmin()][0]) gesture_index = cv2.matchShapes(c, gesture2[0], 2, 0.0) gesture_matching_filter.append(gesture_index) sum_gesture = 0 for i in gesture_matching_filter: sum_gesture += i gesture_index = sum_gesture / gesture_filter_size print(gesture_index) dist_pts = dist(topmost, topmost_last) if dist_pts < 150: try: cv2.drawContours(rgb, [c], 0, (0, 255, 0), 5) low_filter.append(topmost) sum_x = 0 sum_y = 0 for i in low_filter: sum_x += i[0] sum_y += i[1] topmost = (sum_x // low_filter_size, sum_y // low_filter_size) if gesture_index > gesture_index_thres: traj = np.append(traj, topmost) dist_records.append(dist_pts) else: traj = np.array([], np.uint16) traj = np.append(traj, topmost_last) dist_pts = 0 dist_records = [dist_pts] pass topmost_last = topmost except: print('error') pass for i in range(1, len(dist_records)): thickness = int(-0.072 * dist_records[i] + 13) cv2.line(frame, (traj[i * 2 - 2], traj[i * 2 - 1]), (traj[i * 2], traj[i * 2 + 1]), orange, thickness) cv2.line(rgb, (traj[i * 2 - 2], traj[i * 2 - 1]), (traj[i * 2], traj[i * 2 + 1]), orange, thickness) j = cv2.waitKey(1) % 256 if j == ord('w'): orange = (255, 255, 255) if j == ord('b'): orange = (205, 0, 0) if j == ord('r'): orange = (0, 0, 255) cv2.circle(frame, topmost_last, 10, blue, 3) cv2.circle(rgb, topmost_last, 10, blue, 3) cv2.imshow('rgb', rgb) cv2.imshow('frame', frame_raw) if j == ord('q'): break cap.release() cv2.destroyAllWindows()
def generate_contours(gray, img2, sure_fg, colour_contours, cut): """ Function name: generate_contours Input: gray -> grayscale image img2 -> Image with the contours drawn for the colours sure_fg -> sure foreground generated in preprocess colour_contours -> number of contours for colours cut -> flag to save contours if -c flag is given Output: imshow -> output image with contours drawn contours, contours1 -> contours for colours and numbers respectively num_img -> image with colour contours numbered colour_dict, number_dict -> dictionary containing indices of colour/number contour respectively """ # Convert to grayscale, apply threshold to generate empty grid and add sure_fg #----------------------------------------------------------------------------- _, thresh = cv2.threshold(gray, 50, 255, 0) thresh = cv2.erode(thresh, kernel, iterations=1) thresh = cv2.bitwise_not(thresh) thresh = cv2.bitwise_or(thresh, sure_fg) thresh = cv2.bitwise_not(thresh) # finding contours for numbering them _, contours1, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # variables to store number of squares in the grid i.e. 25 (colour_grid_count), # row and column values and number of squares that have numbers colour_grid_count = row = column = number_grid_count = colour_position = 0 position = 50 # Iterate through the contours and number the squares containing the colours for i in range(len(contours1)): ctr = contours1[i - 1] perimeter = cv2.arcLength(ctr, True) if perimeter < 414 and perimeter > 399: arc_length = 0.1 * cv2.arcLength(ctr, True) approx_length = cv2.approxPolyDP(ctr, arc_length, True) colour_position = colour_grid_count + 1 colour_position = 56 - (colour_position + position - (10 * row)) column = column + 1 if (column == 5): row += 1 column = 0 if (len(approx_length) == 4): if (colour_grid_count < 25 and hierarchy[0][i - 1][2] != -1): # to indicate colour contour global number_contours number_contours += 1 ctr = contours1[i] x, y, w, h = cv2.boundingRect(ctr) x = int(x + w / 2) y = int(y + h / 2) grid_coordinates = (x - 10, y + 5) img2 = cv2.putText(img2, str(colour_position), grid_coordinates, font, 0.5, (0, 255, 0), 2, cv2.LINE_AA) global colour_dict colour_dict[str(i)] = str(colour_position) colour_grid_count += 1 num_img = copy.deepcopy(img2) num_img = cv2.resize(num_img, (800, 800)) # Finding contours for naming the squares with numbers _, thresh = cv2.threshold(gray, 127, 255, 0) _, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Iterate through the contours and label the squares containing numbers appropriately for i in range(len(contours)): ctr = contours[i - 1] perimeter = cv2.arcLength(ctr, True) if perimeter > 186 and perimeter < 196: number_grid_count += 1 if (number_grid_count < 21): ctr = contours[i] if ((i + 1) < len(contours)): ctr2 = contours[i + 1] correlation2 = cv2.matchShapes(ctr, ctr2, 1, 0.0) correlation1 = cv2.matchShapes(ctr, ctr, 1, 0.0) else: correlation2 = 0 correlation1 = 0 if (correlation2 > 0.02 or correlation1 > 0.02): cv2.drawContours(img2, contours, i - 1, (0, 0, 255), 3) ctr = contours[i - 1] l = list(ctr[ctr[:, :, 1].argmin()][0]) l[0] = l[0] + 15 l[1] = l[1] - 10 number_position = (l[0], l[1]) if number_grid_count < 7: # contours A1 to F1 symbol = ord('F') - number_grid_count + 1 name = chr(symbol) + '1' img2 = cv2.putText(img2, name, number_position, font, 0.6, (0, 255, 0), 2, cv2.LINE_AA) elif number_grid_count in [7, 9, 11, 13, 15]: # contours F2 to F6 symbol = ord('F') l = int(number_grid_count / 2) - 1 name = chr(symbol) + str(l) img2 = cv2.putText(img2, name, number_position, font, 0.6, (0, 255, 0), 2, cv2.LINE_AA) elif number_grid_count > 15 and number_grid_count < 21: # contours A6 to F6 symbol = ord('F') - number_grid_count + 15 name = chr(symbol) + '6' img2 = cv2.putText(img2, name, number_position, font, 0.6, (0, 255, 0), 2, cv2.LINE_AA) else: # contours A2 to A6 symbol = ord('A') l = int(number_grid_count / 2) - 2 name = chr(symbol) + str(l) img2 = cv2.putText(img2, name, number_position, font, 0.6, (0, 255, 0), 2, cv2.LINE_AA) # store the index of each contour of a square containing a number in a dictionary along with its label if cut == True: global number_dict number_dict[str(i - 1)] = name if (colour_contours == number_contours): imshow = img2 else: imshow = -1 return imshow, contours, contours1, num_img, colour_dict, number_dict
def imageShoot(): #img = cv2.imread('/Users/Nolan/Documents/theGreenGoal.png') #img2 = cv2.imread('/Users/Nolan/Documents/theGreenGoal.png') with picamera.PiCamera() as camera: time.sleep(2) with picamera.array.PiRGBArray(camera) as stream: camera.resolution = (768, 576) camera.capture(stream, format='bgr') # At this point the image is available as stream.array img = stream.array imgToMatch = cv2.imread('/Users/Nolan/Documents/frcGOAL.png') hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) lower_green = np.array([50, 50, 120]) upper_green = np.array([70, 255, 255]) mask = cv2.inRange(hsv, lower_green, upper_green) res = cv2.bitwise_and(img,img, mask= mask) edges = cv2.Canny(res,50,150,apertureSize = 3) edges1 = cv2.Canny(imgToMatch,50,150,apertureSize = 3) contours, hierarchy = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_KCOS) contours1, hierarchy1 = cv2.findContours(edges1, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_KCOS) goalNum = 0 contour1 = contours1[1] potGoalSpot = [] potGoalNum = [] for i in range(0, len(contours)): contour = contours[i] M = cv2.moments(contour) if M['m00'] <= 50: continue cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) area = str(M['m00']) b = random.randint(128, 255) g = random.randint(128, 255) r = random.randint(128, 255) cv2.drawContours(img, contours, i, [b, g, r], 5) cv2.putText(img, area,(cx, cy), cv2.FONT_HERSHEY_SIMPLEX, .5,(r, g, b),2) matching = cv2.matchShapes(contour1,contour,1,0.0) potGoalSpot.append(i) potGoalNum.append(matching) print(matching, i) whitePixels = 0 try: contour = contours[potGoalSpot[min(xrange(len(potGoalNum)),key=potGoalNum.__getitem__)]] except: print("Goal not found.") #cv2.drawContours(img2, contours, potGoalSpot[min(xrange(len(potGoalNum)),key=potGoalNum.__getitem__)], [0, 0, 255], 1) #img2[contour[0][0][1], contour[0][0][0]] = [255, 0, 0] # Finds the highest x value in the contour array newHigh = 0 newHighSpot = 0 isHigh = False for j in range(0, len(contour)): newHigh = contour[j][0][0] newHighSpot = j for k in range(0, len(contour)): if newHigh >= contour[k][0][0]: isHigh = True else: isHigh = False break if isHigh == True: break # Finds the lowest x value in the countour array newLow = 0 newLowSpot = 0 isLow = False for l in range(0, len(contour)): newLow = contour[l][0][0] newLowSpot = l for m in range(0, len(contour)): if newLow <= contour[m][0][0]: isLow = True else: isLow = False break if isLow == True: break pixelD = newHigh - newLow #cv2.imwrite("/Users/Nolan/Documents/theContors.png", img) #cv2.imwrite("/Users/Nolan/Documents/theContorsWithOne.png", img2) # Distance to an object, everythin in mm or pixels focalLength = 3.6 # Will change for every camera, in mm goalWidthReal = 508.1016 # real width of goal in mm. 1.667 ft imageWidth = 2592 # Will change for every camera, in pixels goalWidthPixels = pixelD # Determined above sensorWidth = 3.76 # Will change for every camera, in mm distanceToObject = (focalLength * goalWidthReal * imageWidth) / (goalWidthPixels * sensorWidth) height, width, channels = img.shape #This finds the lowest y value in the contour array newLowHeight = 0 newLowSpotHeight = 0 isLowHeight = False for l in range(0, len(contour)): newLowHeight = contour[l][0][1] newLowSpotHeight = l for m in range(0, len(contour)): if newLowHeight >= contour[m][0][1]: isLowHeight = True else: isLowHeight = False break if isLowHeight == True: break # Finds the distance in pixels between the center of the screen and newHighSpot... found in the first set of for loops toWidth = ((contour[newHighSpot][0][0] + contour[newLowSpot][0][0]) / 2) if toWidth < width / 2: xFromCenter = ((width / 2) - ((contour[newHighSpot][0][0] + contour[newLowSpot][0][0]) / 2)) * -1 elif toWidth > width / 2: xFromCenter = ((contour[newHighSpot][0][0] + contour[newLowSpot][0][0]) / 2) - (width / 2) else: xFromCenter = 0 # Finds the x angle the goal is at relative to where the robot is pointing xToCorrectReal = (goalWidthReal * xFromCenter) / goalWidthPixels xToCorrectAngle = np.arcsin(xToCorrectReal / distanceToObject) groundDistance = math.sqrt((distanceToObject * distanceToObject) - (1610.4 * 1610.4)) print("groundDistance " + groundDistance) print("xToCorrectAngle " + xToCorrectAngle) port.write((str(groundDistance) + " " + str(xToCorrectAngle)).encode('ascii'))
number_image = getContoursRectImage(img_binary) plt.subplot(421), plt.imshow(img_gray, 'gray'), plt.title('gray') zhifangtu(img_gray, 422, str(np.mean(img_gray))) plt.subplot(423), plt.imshow(img_binary, 'gray'), plt.title('img_binary') zhifangtu(img_binary, 424, str(maxMinAvarge(img_equ))) plt.subplot(425), plt.imshow(img, 'gray'), plt.title('final') plt.figure(2) j = 1 for i in number_image[0]: plt.subplot(10, 3, j), plt.imshow(i, 'gray') j = j + 1 plt.figure(3) j = 1 tmp = getContoursRectImage(number_mod) plt.subplot(3, 5, 12), plt.imshow(number_image[0][4], 'gray') i = 0 for i in range(len(tmp[0])): plt.subplot(3, 5, j), plt.imshow(tmp[0][i], 'gray'), plt.title( str(cv2.matchShapes(number_image[1][4], tmp[1][i], 2, 0.0))) j = j + 1 #print cv2.matchShapes(tmp[1][1],tmp[1][4],1,0.0) #print cv2.matchShapes(tmp[1][5],tmp[1][8],1,0.0) #plt.subplot(111),plt.imshow(number_mod,'gray') plt.show()
def process_contour(self, contour): """ Process the contour and detect any objects from it. :param contour: contour to be processed :return: 1 if detected, 0 otherwise """ try: detection = 0 area = contour.area # roughly cuts out the biggest and smallest areas if 10000 > area > 500: # Save matches with scores to dict so we can name them. match = {} cnt = contour.cnt x, y, w, h = contour.bounding_box # draws a new masked image based on contour. mask = np.zeros(self.__gray.shape, np.uint8) cv2.drawContours(mask, [cnt], 0, 255, -1) masked = cv2.bitwise_and(self.__gray, mask) # FAST points of interest analyze on the masked imge. kp = self.__fast.detect(masked[y:y + h, x:x + w], None) if len(kp) > 35: for template in self.__templates: aspect_ratio = contour.aspect_ratio angle = abs(contour.orientation) # compare the contours values against each templates values ret = cv2.matchShapes(template.cnt, cnt, 1, 0.0) angle_d = abs( (angle - template.angle) / template.angle) ar_d = abs((aspect_ratio - template.ar) / template.ar) # sums them up to roughly evaluate the feature matches match_value = ret + ar_d + angle_d name = template.name # rought estimate to cut out all over the top different objects if 0 < match_value < 2: # takes the average color value in the contours area mean = cv2.mean(self.__norm, mask=mask) # extract the features using brisk kp, des = self.__br.compute( masked[y:y + h, x:x + w], kp) # calculates the matches using the BF matcher. matches = self.__bf.match(template.des, des) # store all the good matches as per Lowe's ratio test. if len(matches) >= len(kp) * 0.65: # calculates the euclidean distance eucli = np.sqrt( (mean[0] - template.mean[0])**2 + (mean[1] - template.mean[1])**2 + (mean[2] - template.mean[2])**2) # compares the calculated value to maximum possible value eucli_d = eucli / self.__MAX_EUCLIDEAN match[name] = eucli_d else: match[name] = 0.6 else: match[name] = 1 # sorts the match dict sorted_matches = sorted(match, key=lambda x: match[x]) goods = [match[x] for x in sorted_matches if match[x] < 1] if len(goods) > 2: # Checks the best match percentage and choose the name accordingly if 0.1 > match[sorted_matches[0]] >= 0: contour.name = sorted_matches[0] detection += 1 elif 0.20 > match[sorted_matches[0]] >= 0.1: contour.name = 'm' detection += 1 elif 0.20 <= match[sorted_matches[0]] <= 0.8: detection += 1 # all the other contours will be ignored. return detection except cv2.error as e: print(e) return detection
for s in shapedict: curimage = cv2.imread(shapedict[s]) curimage2 = cv2.cvtColor(curimage, cv2.COLOR_BGR2GRAY) ret, curimage3 = cv2.threshold( curimage2, 127, 255, cv2.THRESH_BINARY_INV) #cv detects light contours or dark background _, curcnts, val = cv2.findContours(curimage3, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) (x, y, w, h) = cv2.boundingRect(curcnts[0]) cv2.rectangle(curimage, (x, y), (x + w, y + h), (0, 0, 255), 2) # cv2.imshow(s,curimage) shapedict[s] = curcnts[0] minvalue = 10 minshape = "No Shape" for s in shapedict: curms = cv2.matchShapes(shapedict[s], cnt, 1, 0.0) print(s, '\t', curms) if (curms < minvalue): minvalue = curms minshape = s #if(minshape!="Triangle"): time.sleep(5) print(minshape) cv2.waitKey(5000)
def shape_compare(contours, template_cnt): similarity_level = cv2.matchShapes(contours, template_cnt, 1, 0.0) return similarity_level
import cv2 im1 = cv2.imread("img1.jpeg", 0) im2 = cv2.imread("img2.jpeg", 0) d1 = cv2.matchShapes(im1, im2, cv2.CONTOURS_MATCH_I2, 0) print(d1)
traffic_contour, traffic_hierarchy = cv2.findContours(trafficMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) testimage = cv2.imread(ARROW, 1) tst_img = cv2.cvtColor(testimage, cv2.COLOR_BGR2HSV) testmask = cv2.inRange(tst_img, TRAFFIC_MIN1, TRAFFIC_MAX1) test_contour, test_heirarchy = cv2.findContours(testmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) test_area = cv2.contourArea(test_contour[0]) #cv2.drawContours(testimage, test_contour, -1, (0,255,0), 3) #plt.imshow(testimage, cmap='gray') #plt.show() for contour in traffic_contour: if cv2.matchShapes(contour,test_contour[0], 1,0.0) < 1000 and (cv2.contourArea(contour))/test_area < 1.5 and (cv2.contourArea(contour))/test_area > 0.1: score.append(cv2.matchShapes(contour,test_contour[0], 1,0.0)) new_contours.append(contour) print(len(traffic_contour)) print(len(score)) print(score) # score = # new_contours.append(score) #print(new_contours[2]) cv2.drawContours(image, new_contours, -1, (0,255,0), 3) plt.imshow(image,cmap='gray') plt.show()
ret, thresh1 = cv.threshold(template, 127, 255, 0) ret, thresh2 = cv.threshold(target_gray, 127, 255, 0) # find contours from template contours, hierarcy = cv.findContours(thresh1, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE) # we need to sort the contours by area so that we can remove the largest # contours which is the image outline sort_contours = sorted(contours, key=cv.contourArea, reverse=True) # We extract the secound largest contours which is will be our template contours template_contours = contours[1] # find contours from target image contours, hierarcy = cv.findContours(thresh2, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE) target_cpy = target.copy() for c in contours: match = cv.matchShapes(template_contours, c, 1, 0.0) print(match) if match < 0.15: closest_contours = c else: closest_contours = [] cv.drawContours(target, [closest_contours], -1, (0, 255, 0), 2) cv.imshow('contours', target) cv.waitKey(0) cv.destroyAllWindows()
# Extract all the contours from the image def get_all_contours(img): ref_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(ref_gray, 127, 255, 0) contours, hierarchy = cv2.findContours(thresh, 1, 2) return contours if __name__ == '__main__': # Boomerang reference image img1 = cv2.imread(sys.argv[1]) # Input image containing all the different shapes img2 = cv2.imread(sys.argv[2]) # Extract the reference contour ref_contour = get_ref_contour(img1) # Extract all the contours from the input image input_contours = get_all_contours(img2) closest_contour = input_contours[0] #min_dist = sys.maxint min_dist = 9223372036854775807 # Finding the closest contour for contour in input_contours: # Matching the shapes and taking the closest one ret = cv2.matchShapes(ref_contour, contour, 1, 0.0) if ret < min_dist: min_dist = ret closest_contour = contour cv2.drawContours(img2, [closest_contour], -1, (0, 0, 0), 3) cv2.imshow('Output', img2) cv2.waitKey()
def main(): init() while True: # Read frame _, image = cam.read() # Mirror if mirror: image = cv2.flip(image, flipCode=1) # Blur blur = 1 + 2 * cv2.getTrackbarPos('blur', 'Bars') blur_type = cv2.getTrackbarPos('blur_type', 'Bars') if blur_type == 0: blurred = cv2.GaussianBlur(image, (blur, blur), 0) elif blur_type == 1: blurred = cv2.medianBlur(image, blur) elif blur_type == 2: blurred = cv2.blur(image, (blur, blur)) elif blur_type == 3: blurred = cv2.bilateralFilter(image, 9, blur, blur) else: raise ValueError("Unsupported blur type") # Grayscale gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) # Invert if cv2.getTrackbarPos('invert', 'Bars'): gray = cv2.bitwise_not(gray) # Threshold threshold_min = cv2.getTrackbarPos('threshold_min', 'Bars') _, thresh = cv2.threshold(gray, threshold_min, 255, cv2.THRESH_BINARY) # Edges sigma = cv2.getTrackbarPos('sigma', 'Bars') / 100. v = np.median(image) canny_low = int(max(0, (1 - sigma) * v)) canny_high = int(min(255, (1 + sigma) * v)) edged = cv2.Canny(thresh, canny_low, canny_high) edged = cv2.dilate(edged, None, iterations=3) edged = cv2.erode(edged, None, iterations=2) # Find contours _, contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) best = None draw_contours = [] # (ret, contour) for contour in contours: if cv2.contourArea(contour) < 300: continue ret = cv2.matchShapes(contour, base_contour, 1, 0) if best is None or ret < best[0]: best = (ret, contour) draw_contours.append((ret, contour)) for ret, contour in draw_contours: M = cv2.moments(contour) text_pos = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00'])) if M['m00'] else (50, 50) mask = np.zeros(gray.shape, np.uint8) cv2.drawContours(mask, [contour], -1, 255, -1) color = cv2.mean(image, mask) if contour is best[1]: text = 'Screw' cv2.drawContours(image, [contour], -1, color, 2) cv2.putText(image, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=(0, 0, 0), thickness=4) cv2.putText(image, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=(224, 255, 224), thickness=2) else: text = '{:.3f}'.format(ret) cv2.drawContours(image, [contour], -1, color, 2) cv2.putText(image, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=(0, 0, 0), thickness=4) cv2.putText(image, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=(255, 255, 255), thickness=2) edged_bgr = cv2.cvtColor(edged, cv2.COLOR_GRAY2BGR) cv2.imshow("Main", np.hstack((image, edged_bgr))) if cv2.waitKey(1) == 27: break cv2.destroyAllWindows() cam.release()
import cv2 as cv2 import numpy as np img1 = cv2.imread('orange1.jpg',0) img2 = cv2.imread('orange2.jpg',0) ret, thresh = cv2.threshold(img1, 127, 255,0) ret, thresh2 = cv2.threshold(img2, 127, 200,0) contours,hierarchy = cv2.findContours(thresh,2,1) cnt1 = contours[0] contours,hierarchy = cv2.findContours(thresh2,2,1) cnt2 = contours[0] ret = cv2.matchShapes(cnt1,cnt2,3,0.0005) print ret
""" looping over the contours to compare all the contours present in the frame """ for cnt in contours: """ calculating the area approximating the points to identify the shapes """ area = cv2.contourArea(cnt) approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True) x = approx.ravel()[0] y = approx.ravel()[1] """ the shape detected is then compared with the base shape 'star' """ dist = cv2.matchShapes(cnt, cnt1, 1, 0.0) """ if the shape is matched, it will draw the borders around the shape using cv2 and a rectangle box keeping the shape in the center. Also shape tracking is done to check whether it moves beyond a specified limit or not """ if area > 400 and dist < 0.001: cv2.drawContours(frame, [approx], 0, (0, 255, 0), 2) (a, b, c, d) = cv2.boundingRect(cnt) cv2.rectangle(frame, (a, b), (a + c, b + d), (255, 0, 0), 1) cv2.putText(frame, "MATCHED", (x, y), font, 1, (255, 0, 0)) center = (b + b + d) / 2 if center > h // 2: cv2.putText(frame, 'SHAPE CROSSED', (500, h - 40), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2)
def findArrows(self): arrow1, arrow1_area = self.loadModelImage(ARROW1, TRAFFIC_MIN1, TRAFFIC_MAX1,0) arrow2, arrow2_area = self.loadModelImage(ARROW2, TRAFFIC_MIN1, TRAFFIC_MAX1,1) arrows = [] for contour in self.traffic_contour: if ((cv2.contourArea(contour) <= 1.5*arrow1_area and cv2.contourArea(contour) >= 0.8*arrow1_area and cv2.matchShapes(contour,arrow1[0], 3,0.0) < 1) or (cv2.contourArea(contour) <= 1.7*arrow2_area and cv2.contourArea(contour) >= 0.2*arrow2_area and cv2.matchShapes(contour, arrow2[0],1,0.0) < 5)): moment = cv2.moments(contour) cx = int(moment['m10']/moment['m00']) cy = int(moment['m01']/moment['m00']) (x,y),(MA,ma),angle = cv2.fitEllipse(contour) arrow = Arrow.Arrow(cx, cy, angle) #for test rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(self.image,[box],0,(0,0,255),2) #[X, Y, W, H] = cv2.boundingRect(contour) #cv2.rectangle(self.image, (X,Y), (X+W, Y+H), (255, 0, 0), 2) self.image[cy, cx] = [255, 0, 0] print(angle) arrows.append(arrow) plt.imshow(self.image,cmap='gray') plt.show() return arrows
cnt1 = contours[0] cap = cv2.VideoCapture(0) while (True): ret, frame = cap.read() im2 = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lb = np.array([30, 70, 70]) ub = np.array([90, 255, 255]) r = cv2.inRange(im2, lb, ub) im2 = cv2.bitwise_and(im2, im2, mask=r) im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY) thresh2 = cv2.threshold(im2, 127, 255, cv2.THRESH_BINARY_INV)[1] contours, hierarchy = cv2.findContours(thresh2, 2, 1) cnt2 = contours[0] ret = cv2.matchShapes(cnt1, cnt2, 1, 0.0) if (ret > 1): cv2.imshow('result2', thresh2) arduino = serial.Serial('/dev/ttyACM0', 9600) time.sleep(2) var = '1' arduino.write(var.encode()) cv2.imshow('result2', thresh2) cv2.imshow('result1', edges) if cv2.waitKey(1) & 0XFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def detect(self, image): pre_proc_frame = preprocess_frame(image) # Uncomment if debugging threshold value # cv.imshow('Frame Thresh', pre_proc_frame) _, contours, hierarchy = cv.findContours(pre_proc_frame, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours = sorted(contours, key=cv.contourArea, reverse=True) # cv.drawContours(image, contours, -1, (0, 0, 255), 3) filtered_contours = filter_contour_size(contours) cv.drawContours(image, filtered_contours, -1, (255, 0, 0), 3) if len(filtered_contours) > 0: symbol_contour = filtered_contours[0] x, y, w, h = cv.boundingRect(symbol_contour) cv.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) extLeft, extTop, extRight, extBottom = extract_extreme_points( symbol_contour) symbol_thresh = extract_detected_symbol_thresh( image, extLeft, extTop, extRight, extBottom) # cv.imshow("Detected Object", symbol_thresh) match_results = [] for train_symbol in self.train_symbols: match_score = cv.matchShapes(symbol_contour, train_symbol.contour, 1, 0.0) match_results.append({ 'score': match_score, 'symbol': train_symbol.name, 'id': train_symbol.id }) closest_match = min(match_results, key=lambda x: x['score']) if closest_match['score'] < MATCH_THRESHOLD: # If detected arrow, further derive arrow orientation if closest_match['id'] == 0: # Uncomment if debugging for arrow detection # cv.circle(image, (extLeft[0], extLeft[1]), 8, (0, 0, 255), 2) # cv.circle(image, (extTop[0], extTop[1]), 8, (0, 0, 255), 2) # cv.circle(image, (extRight[0], extRight[1]), 8, (0, 0, 255), 2) # cv.circle(image, (extBottom[0], extBottom[1]), 8, (0, 0, 255), 2) # cv.circle(image, (int(((extLeft[0] + extRight[0]) / 2)), int(((extTop[1] + extBottom[1]) / 2))), 8, (0, 0, 255), 2) arrow_name, arrow_id = derive_arrow_orientation( extLeft, extTop, extRight, extBottom) closest_match['symbol'] = arrow_name closest_match['id'] = arrow_id cv.putText( image, 'Symbol: ' + str(closest_match['symbol']) + '; ID: ' + str(closest_match['id']), (extLeft[0] - 50, extTop[1] - 20), font, 0.8, (0, 255, 0)) if self.match_symbol_id == closest_match['id']: self.match_count = self.match_count + 1 else: self.match_symbol_id = closest_match['id'] self.match_count = 1 if (self.match_count == MATCH_CONFIDENCE_COUNT): return (closest_match['id']) else: self.match_symbol_id = None self.match_count = 0 # Uncomment to visualize stream cv.imshow("Video Stream", image) key = cv.waitKey(1) & 0xFF
_, src_bin = cv2.threshold(src, 128, 255, cv2.THRESH_BINARY_INV) contours, _ = cv2.findContours(src_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 결과 영상 dst = cv2.cvtColor(src, cv2.COLOR_GRAY2BGR) # 입력 영상의 모든 객체 영역에 대해서 for pts in contours: if cv2.contourArea(pts) < 1000: continue rc = cv2.boundingRect(pts) cv2.rectangle(dst, rc, (255, 0, 0), 1) # 모양 비교 dist = cv2.matchShapes(obj_pts, pts, cv2.CONTOURS_MATCH_I3, 0) # moments = cv2.moments(src[50:100, 50:100]) # dist = cv2.HuMoments(moments) # str(round(dist, 4)) : dist 값을 소수점 4자리에서 반올림(round)해서 str으로 만들어준다. cv2.putText(dst, str(round(dist, 4)), (rc[0], rc[1] - 3), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 1, cv2.LINE_AA) if dist < 0.1: cv2.rectangle(dst, rc, (0, 0, 255), 2) cv2.imshow('obj', obj) cv2.imshow('dst', dst) cv2.waitKey(0)
ret, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) ### We need to sort the contours by area so that we can remove the largest contour which is the image outline sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True) ## We extract the second largest contour which will be our template contour template_contour = contours[1] ## Extract contours from second target image ret, contours, hierarchy = cv2.findContours(thresh2, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) for c in contours: ## Iterate through each contour in the target image and ### use cv2.matchShape to compare contour shapes match = cv2.matchShapes(template_contour, c, 1, 0.0) print(match) ## if the match value is less than 0.15 we if match < 0.15: closest_contour = c else: closest_contour = [] cv2.drawContours(target, [closest_contour], -1, (0, 255, 0), 3) cv2.imshow('Output', target) cv2.waitKey() cv2.destroyAllWindows() ###
def compare_cnts(self, ex_cnt): return cv2.matchShapes(self.cnt, ex_cnt, 1, 0.0) < .02
testImage = cv2.imread('1er_Reto_Vision_Imagenes/5.jpeg'); testImage = cv2.resize(testImage, None, fx=1, fy=.71, interpolation=cv2.INTER_AREA); hsvTestImage = cv2.cvtColor(testImage, cv2.COLOR_BGR2HSV) maskGreenTest = cv2.inRange(hsvTestImage, lower_Green, upper_Green) maskGreenTest = cv2.erode(maskGreenTest, kernel, iterations = 1) maskBlueTest = cv2.inRange(hsvTestImage, lower_Blue, upper_Blue) maskBlueTest = cv2.erode(maskBlueTest, kernel, iterations = 1) greenTestContours, hierarchy1 = cv2.findContours(maskGreenTest.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) blueTestContours, hierarchy1 = cv2.findContours(maskBlueTest.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) isGreenBoat = False isBlueBoat = False for test in greenTestContours: matchGreen = cv2.matchShapes(greenTarget, test, 3, 0.0) if (matchGreen < 2): greenBoat = test M = cv2.moments(test) cxGreen = int(M['m10'] / M['m00']) cyGreen = int(M['m01'] / M['m00']) isGreenBoat = True for test in blueTestContours: matchBlue = cv2.matchShapes(blueTarget, test, 3, 0.0) if (matchBlue < 2): blueBoat = test M = cv2.moments(test) cxBlue = int(M['m10'] / M['m00']) cyBlue = int(M['m01'] / M['m00']) isBlueBoat = True
def rectCost(contour): return cv2.matchShapes(contour, rect, 2, 0.0)
def calculate_distance(template_rag, template_attributes, compare_rag, compare_attributes): min_match = 10000000 stomach_index = 0 next_best_index = None template_indices = [] compare_indices = [] total_distance = 0 # Find top 2 regions! for iteration in range(2): min_match = 10000000 for idx in range(len(template_attributes)): if idx in template_indices: continue for j in range(len(compare_attributes)): if j in compare_indices: continue ret = cv2.matchShapes(template_attributes[idx][13], compare_attributes[j][13], 1, 0.0) if ret < min_match: min_match = ret template_indices.append(idx) compare_indices.append(j) total_distance += min_match #print (template_rag) # Calculate mean distance stomach_color_distance = math.sqrt( ((template_attributes[template_indices[iteration]][2][0]-compare_attributes[compare_indices[iteration]][2][0])**2) \ + ((template_attributes[template_indices[iteration]][2][1]-compare_attributes[compare_indices[iteration]][2][1])**2) \ + ((template_attributes[template_indices[iteration]][2][2]-compare_attributes[compare_indices[iteration]][2][2])**2)) total_distance += (stomach_color_distance / 100) b_d = cv2.compareHist( template_attributes[template_indices[iteration]][10], compare_attributes[compare_indices[iteration]][10], cv2.cv2.HISTCMP_CORREL) g_d = cv2.compareHist( template_attributes[template_indices[iteration]][11], compare_attributes[compare_indices[iteration]][11], cv2.cv2.HISTCMP_CORREL) r_d = cv2.compareHist( template_attributes[template_indices[iteration]][12], compare_attributes[compare_indices[iteration]][12], cv2.cv2.HISTCMP_CORREL) #hom_diff = abs(template_attributes[template_indices[iteration]][3] - compare_attributes[template_indices[iteration]][3]) #print(hom_diff) #total_distance += .1 * hom_diff # Calculate mean color distance of next best area # next_best_color_distance = math.sqrt( ((template_attributes[next_best_index[0]][1][0]-compare_attributes[next_best_index[1]][1][0])**2) \ # + ((template_attributes[next_best_index[0]][1][1]-compare_attributes[next_best_index[1]][1][1])**2) \ # + ((template_attributes[next_best_index[0]][1][2]-compare_attributes[next_best_index[1]][1][2])**2)) #print(next_best_color_distance) #total_distance += (.01 * next_best_color_distance) # # Calculate mean energy distance of next best area # total_distance += (2 * abs(template_attributes[next_best_index[0]][5] - compare_attributes[next_best_index[1]][5])) # print("energy") # print(abs(template_attributes[next_best_index[0]][6] - compare_attributes[next_best_index[1]][6])) # total_distance += abs(template_attributes[next_best_index[0]][6] - compare_attributes[next_best_index[1]][6]) print(total_distance) return total_distance
def main(path): original = cv2.imread(path) file_list = os.listdir(os.getcwd() + "\Sample Images") #put in the sample file location area_list = [] for i in file_list: image = cv2.imread("Sample Images\\" + i) edged_image = cv2.Canny(image.copy(), 150, 150) _, contours_image, heirarchy = cv2.findContours( edged_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) area_list.append(cv2.contourArea(contours_image[0])) if "test" in path: square = cv2.imread('square.jpeg') gray_square = cv2.cvtColor(square, cv2.COLOR_BGR2GRAY) ret, thresh_square = cv2.threshold(gray_square, 180, 255, 1) _, contours_sqr, heirarchy = cv2.findContours(thresh_square.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnt1 = contours_sqr[0] edged_image = cv2.Canny(original.copy(), 150, 150) _, contours, heirarchy = cv2.findContours(edged_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) required_matrix = np.zeros( (6, 4, 5)) #red-1,yellow-2,orange-3,green-4,blue-5 #small-1,large-2,medium-3 #triangle-1,rectangle-2,square-3,circle-4 font = cv2.FONT_HERSHEY_SIMPLEX countour_xy = [] for j in range(0, len(contours), 2): i = contours[j] approx = cv2.approxPolyDP(i, 0.01 * cv2.arcLength(i, True), True) x = len(approx) M = cv2.moments(i) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) size = "medium" x1 = i[0][0][0] + 1 y1 = i[0][0][1] + 1 color, d = colorOf( original[y1, x1]) #d is the color index of the required matrix area = cv2.contourArea(i) if (cv2.contourArea(i) < 3500): x = x - 1 if x == 3: if (area >= area_list[6]): size = "large" required_matrix[d, 3, 1] += 1 elif (area <= area_list[7]): size = "small" required_matrix[d, 1, 1] += 1 else: required_matrix[d, 2, 1] += 1 cv2.putText(original, size + " " + str(color) + " triangle", (cx, cy), font, 0.4, (0, 0, 0), 1, 0) continue elif x == 4: ret = cv2.matchShapes(i, cnt1, 1, 0.0) if ret < 0.05: if (area >= area_list[4]): size = "large" required_matrix[d, 3, 3] += 1 elif (area <= area_list[5]): size = "small" required_matrix[d, 1, 3] += 1 else: required_matrix[d, 2, 3] += 1 cv2.putText(original, size + " " + str(color) + " square", (cx, cy), font, 0.4, (0, 0, 0), 1, 0) else: if (area >= area_list[2]): size = "large" required_matrix[d, 3, 2] += 1 elif (area <= area_list[3]): size = "small" required_matrix[d, 1, 2] += 1 else: required_matrix[d, 2, 2] += 1 cv2.putText(original, size + " " + str(color) + " rectangle", (cx, cy), font, 0.4, (0, 0, 0), 1, 0) continue elif x == 5: cv2.putText(original, size + " " + str(color) + " pentagon", (cx, cy), font, 0.4, (0, 0, 0), 1, 0) continue elif x >= 6: if (area >= area_list[0]): size = "large" required_matrix[d, 3, 4] += 1 elif (area <= area_list[1]): size = "small" required_matrix[d, 1, 4] += 1 else: required_matrix[d, 2, 4] += 1 cv2.putText(original, size + " " + str(color) + " circle", (cx, cy), font, 0.4, (0, 0, 0), 1, 0) continue shape_List = ["", "Triangle", "Rectangle", "Square", "Circle"] size_List = ["", "Small", "Medium", "Large"] col_List = ["", "Red", "Yellow", "Orange", "Green", "Blue"] for colInd in xrange(1, 6): for sizeInd in xrange(1, 4): for shapeInd in xrange(1, 5): if (required_matrix[colInd][sizeInd][shapeInd] > 0): writecsv( col_List[colInd], shape_List[shapeInd], size_List[sizeInd], str(required_matrix[colInd][sizeInd] [shapeInd])[:-2]) cv2.imwrite(os.getcwd() + "\output" + path[len(path) - 5:], original)
# 显示图像函数 def ShowImage(name_of_image, image_, rate): img_min = cv2.resize(image_, None, fx=rate, fy=rate, interpolation=cv2.INTER_CUBIC) cv2.namedWindow(name_of_image, cv2.WINDOW_NORMAL) cv2.imshow(name_of_image, img_min) if cv2.waitKey(0) == 27: # wait for ESC to exit print('Not saved!') cv2.destroyAllWindows() elif cv2.waitKey(0) == ord('s'): # wait for 's' to save and exit cv2.imwrite(name_of_image + '.jpg', image_) # save print('Saved successfully!') cv2.destroyAllWindows() img = cv2.imread('shape.png', 0) ret, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) image, contours, hierarchy = cv2.findContours(thresh, 3, 2) img_bgr = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) cnt_a, cnt_b, cnt_c = contours[0], contours[1], contours[2] print(cv2.matchShapes(cnt_b, cnt_a, 1, 0.0)) print(cv2.matchShapes(cnt_b, cnt_b, 1, 0.0)) print(cv2.matchShapes(cnt_b, cnt_c, 1, 0.0)) cv2.drawContours(img_bgr, contours, -1, (255, 0, 0), 2) ShowImage('test', img_bgr, 1)
import numpy as np import matplotlib.pyplot as plt road1 = cv2.imread('road-1.jpg') road2 = cv2.imread('road-2.jpg') road1_gray = cv2.cvtColor(road1, cv2.COLOR_BGR2GRAY) road2_gray = cv2.cvtColor(road2, cv2.COLOR_BGR2GRAY) orb = cv2.ORB_create() bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # sobel边缘检测 sobel_1 = cv2.Sobel(road1_gray, cv2.CV_8U, 1, 1) sobel_2 = cv2.Sobel(road2_gray, cv2.CV_8U, 1, 1) sobel_matching = cv2.matchShapes(sobel_1, sobel_2, cv2.CHAIN_APPROX_SIMPLE, 0) kp1 = orb.detect(sobel_1, None) kp2 = orb.detect(sobel_2, None) kp1, des1 = orb.compute(sobel_1, kp1) kp2, des2 = orb.compute(sobel_2, kp2) # 特征点匹配 matches = bf.match(des1, des2) matches = sorted(matches, key=lambda x: x.distance) newimg = cv2.drawMatches(road1, kp1, road2, kp2, matches[:50], road2, flags=2) cv2.imwrite('sobel_matching_result.png', newimg) # Canny边缘检测 max1 = np.max(road1_gray) max2 = np.max(road2_gray) maximum = max1 if max1 >= max2 else max2 canny_1 = cv2.Canny(road1_gray, 0.5*max1, 0.9*max1)
#coding:utf-8 import cv2 import numpy as np imgToFind = cv2.imread('datas/toFind.jpg', 0) imgFromFind = cv2.imread('datas/fromFind.jpg', 0) ret, threshTo = cv2.threshold(imgToFind, 127, 255, cv2.THRESH_BINARY_INV) ret, threshFrom = cv2.threshold(imgFromFind, 127, 255, cv2.THRESH_BINARY_INV) cntTo = cv2.findContours(threshTo, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cntFrom = cv2.findContours(threshFrom, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cntTo = cntTo[0] cntFrom = cntFrom[0] diff = cv2.matchShapes(cntTo, cntFrom, 1, 0.0) print(diff)
def close_point(self, mat, split, colorspace): color = [self.options['color_%s' % s] for s in colorspace] distance = self.options['water_distance'] # threshed = [range_threshold(split[i], # color[i] - distance, color[i] + distance) # for i in range(1, len(color))] # mask = reduce(lambda x, y: cv2.bitwise_and(x, y), threshed) # mask, _ = thresh_color_distance(split, color, distance, weights=[0.5, 2, 2]) mask = np.full((mat.shape[0], mat.shape[1]), 0, dtype=np.int32) if self.dst is not None: print(len(self.dst)) cv2.fillPoly(mask, [np.int32(self.dst)], 1) if np.sum(mask) < mat.shape[0] * mat.shape[1] // 4 * 3: mask = None median_a = np.median(np.ma.array(split[1], mask=mask)) median_b = np.median(np.ma.array(split[2], mask=mask)) mask, _ = thresh_color_distance(split, [0, median_a, median_b], distance, weights=[0, 1, 1]) # thresh_a = cv2.bitwise_not(range_threshold(split[1], median_a-self.options['water_a'], median_a+self.options['water_a'])) # thresh_b = cv2.bitwise_not(range_threshold(split[2], median_b-self.options['water_a'], median_b+self.options['water_b'])) # self.post('a', thresh_a) # self.post('b', thresh_b) # mask = reduce(lambda x, y: cv2.bitwise_and(x, y), [mask, thresh_a, thresh_b]) # # contours = outer_contours(mask) _, contours, _ = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) self.post('something', mask) # # canny_post = np.zeros(mat.shape) # canny = simple_canny(mat, sigma=self.options['sigma_canny'], use_mean=True) # contours = outer_contours(canny) # for i in range(len((contours))): # cv2.drawContours(canny_post, contours, i, (i*20, i*20, i*20)) # self.post('canny', canny) if contours is not None and len(contours) > 0: # shm.torpedoes_stake.close_visible.set(True) def filter_ellipses(contour): if len(contour) < 5: return False if contour_area(contour) < 200: return False _, (MA, ma), _ = cv2.fitEllipse(contour) try: return ( self.options['max_eccentricity'] > 1 - (MA**2) / (ma**2))**(1 / 2) > self.options[ 'min_eccentricity'] and contour_area(contour) / ( MA * ma / 4 * pi) > self.options['min_elllipsish_thing'] except ZeroDivisionError: return False close = list(filter(filter_ellipses, contours)) close.sort(key=contour_area) close1 = None close2 = None try: close1 = { 'contour': close[0], 'centroid': contour_centroid(close[0]), 'area': contour_area(close[0]) } close2 = { 'contour': close[1], 'centroid': contour_centroid(close[1]), 'area': contour_area(close[1]) } if close1['centroid'][0] > close2['centroid'][0]: temp = close2 close2 = close1 close1 = temp except IndexError: pass if close1 is not None: shm.torpedoes_stake.close_left_x.set( close1['centroid'][0] + self.options['close_offset_x']) shm.torpedoes_stake.close_left_y.set( close1['centroid'][1] + self.options['close_offset_y']) shm.torpedoes_stake.close_left_size.set(close1['area']) shm.torpedoes_stake.close_left_visible.set(True) if close2 is not None: shm.torpedoes_stake.close_right_x.set( close2['centroid'][0] + self.options['close_offset_x']) shm.torpedoes_stake.close_right_y.set( close2['centroid'][1] + self.options['close_offset_y']) shm.torpedoes_stake.close_right_size.set(close2['area']) shm.torpedoes_stake.close_right_visible.set(True) else: shm.torpedoes_stake.close_right_visible.set(False) else: shm.torpedoes_stake.close_left_visible.set(False) shm.torpedoes_stake.close_right_visible.set(False) # _, contours, _ = cv2.findContours(canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) heart = [c for c in contours if cv2.contourArea(c) > 300] if heart: heart = min(heart, key=lambda c: cv2.matchShapes( heart_original, c, cv2.CONTOURS_MATCH_I1, 0)) print( cv2.matchShapes(heart_original, heart, cv2.CONTOURS_MATCH_I1, 0)) if not cv2.matchShapes(heart_original, heart, cv2.CONTOURS_MATCH_I1, 0) > 0.25: h_centroid = contour_centroid(heart) shm.torpedoes_stake.close_heart_x.set( h_centroid[0] + self.options['close_offset_x']) shm.torpedoes_stake.close_heart_y.set( h_centroid[1] + self.options['close_offset_y']) shm.torpedoes_stake.close_heart_size.set( contour_area(heart)) shm.torpedoes_stake.close_heart_visible.set(True) cv2.drawContours(mat, [heart], -1, (0, 0, 255), thickness=5) else: shm.torpedoes_stake.close_heart_visible.set(False) try: cv2.drawContours(mat, [close[0]], -1, (255, 0, 0), thickness=5) cv2.drawContours(mat, [close[1]], -1, (255, 0, 0), thickness=5) except IndexError: pass else: # shm.torpedoes_stake.close_visible.set(False) pass