我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.arcLength()。
def __bound_contours(roi): """ returns modified roi(non-destructive) and rectangles that founded by the algorithm. @roi region of interest to find contours @return (roi, rects) """ roi_copy = roi.copy() roi_hsv = cv2.cvtColor(roi, cv2.COLOR_RGB2HSV) # filter black color mask1 = cv2.inRange(roi_hsv, np.array([0, 0, 0]), np.array([180, 255, 125])) mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))) mask1 = cv2.Canny(mask1, 100, 300) mask1 = cv2.GaussianBlur(mask1, (1, 1), 0) mask1 = cv2.Canny(mask1, 100, 300) # mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))) # Find contours for detected portion of the image im2, cnts, hierarchy = cv2.findContours(mask1.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5] # get largest five contour area rects = [] for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) x, y, w, h = cv2.boundingRect(approx) if h >= 15: # if height is enough # create rectangle for bounding rect = (x, y, w, h) rects.append(rect) cv2.rectangle(roi_copy, (x, y), (x+w, y+h), (0, 255, 0), 1); return (roi_copy, rects)
def find_squares(img): img = cv2.GaussianBlur(img, (5, 5), 0) squares = [] for gray in cv2.split(img): for thrs in xrange(0, 255, 26): if thrs == 0: bin = cv2.Canny(gray, 0, 50, apertureSize=5) bin = cv2.dilate(bin, None) else: retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY) bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: cnt_len = cv2.arcLength(cnt, True) cnt = cv2.approxPolyDP(cnt, 0.02 * cnt_len, True) if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt): cnt = cnt.reshape(-1, 2) max_cos = np.max([angle_cos(cnt[i], cnt[(i + 1) % 4], cnt[(i + 2) % 4]) for i in xrange(4)]) if max_cos < 0.1: squares.append(cnt) return squares
def find_triangles(filename): FIRST = 0 RED = (0, 0, 255) THICKNESS = 3 copy = img = cv2.imread(filename) grey_img = cv2.imread(file_name, cv2.IMREAD_GRAYSCALE) ret, thresh = cv2.threshold(grey_img, 127, 255, 1) contours, h = cv2.findContours(thresh, 1, 2) largest = None for contour in countours: approx = cv2.approxPolyDP(contour,0.01*cv2.arcLength(contour,True),True) if len(approx) == 3: #triangle found if largest is None or cv2.contourArea(contour) > cv2.contourArea(largest): largest = contour #write file cv2.drawContours(copy, [largest], FIRST, RED, THICKNESS) cv2.imwrite(filename +"_result", copy)
def remove_borders(image): ratio = image.shape[0] / 500.0 orig = image.copy() image = resize(image, height=500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(gray, 75, 200) _, cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cv2.imshow('edged', edged) cnts = sorted(cnts, key=cv2.contourArea, reverse=True) screenCnt = None for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) print(len(approx) == 4) if len(approx) == 4: screenCnt = approx break cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) if screenCnt is not None and len(screenCnt) > 0: return four_point_transform(orig, screenCnt.reshape(4, 2) * ratio) return orig
def img_contour_select(ctrs, im): # ???????????? cand_rect = [] for item in ctrs: epsilon = 0.02*cv2.arcLength(item, True) approx = cv2.approxPolyDP(item, epsilon, True) if len(approx) <= 8: rect = cv2.minAreaRect(item) if rect[1][0] < 20 or rect[1][1] < 20: continue if rect[1][0] > 150 or rect[1][1] > 150: continue #ratio = (rect[1][1]+0.00001) / rect[1][0] #if ratio > 1 or ratio < 0.9: # continue box = cv2.boxPoints(rect) box_d = np.int0(box) cv2.drawContours(im, [box_d], 0, (0,255,0), 3) cand_rect.append(box) img_show_hook("????", im) return cand_rect
def find_squares(img): img = cv2.GaussianBlur(img, (5, 5), 0) squares = [] for gray in cv2.split(img): for thrs in xrange(0, 255, 26): if thrs == 0: bin = cv2.Canny(gray, 0, 50, apertureSize=5) bin = cv2.dilate(bin, None) else: _retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY) contours, _hierarchy = find_contours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) cnt_len = cv2.arcLength(cnt, True) cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True) area = cv2.contourArea(cnt) if len(cnt) == 4 and 20 < area < 1000 and cv2.isContourConvex(cnt): cnt = cnt.reshape(-1, 2) max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)]) if max_cos < 0.1: if (1 - (float(w) / float(h)) <= 0.07 and 1 - (float(h) / float(w)) <= 0.07): squares.append(cnt) return squares
def homography(self, img, outdir_name=''): orig = img # 2?????? gray = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY) gauss = cv2.GaussianBlur(gray, (5, 5), 0) canny = cv2.Canny(gauss, 50, 150) # 2?????????? contours = cv2.findContours(canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)[1] # ??????????? contours.sort(key=cv2.contourArea, reverse=True) if len(contours) > 0: arclen = cv2.arcLength(contours[0], True) # ??????????? approx = cv2.approxPolyDP(contours[0], 0.01 * arclen, True) # warp = approx.copy() if len(approx) >= 4: self.last_approx = approx.copy() elif self.last_approx is not None: approx = self.last_approx else: approx = self.last_approx rect = self.get_rect_by_points(approx) # warped = self.transform_by4(orig, warp[:, 0, :]) return orig[rect[0]:rect[1], rect[2]:rect[3]]
def find_squares(img, cos_limit = 0.1): print('search for squares with threshold %f' % cos_limit) img = cv2.GaussianBlur(img, (5, 5), 0) squares = [] for gray in cv2.split(img): for thrs in xrange(0, 255, 26): if thrs == 0: bin = cv2.Canny(gray, 0, 50, apertureSize=5) bin = cv2.dilate(bin, None) else: retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY) bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: cnt_len = cv2.arcLength(cnt, True) cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True) if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt): cnt = cnt.reshape(-1, 2) max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)]) if max_cos < cos_limit : squares.append(cnt) else: #print('dropped a square with max_cos %f' % max_cos) pass return squares ### ### Version V2. Collect meta-data along the way, with commentary added. ###
def get_rectangles(contours): rectangles = [] for contour in contours: epsilon = 0.04*cv2.arcLength(contour,True) hull = cv2.convexHull(contour) approx = cv2.approxPolyDP(hull,epsilon,True) if (len(approx) == 4 and cv2.isContourConvex(approx)): rectangles.append(approx) return rectangles
def find_contours(self, img): thresh_img = self.threshold(img) _, contours, _ = cv2.findContours(thresh_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) result = [] for cnt in contours: approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True) if self.draw_approx: cv2.drawContours(self.out, [approx], -1, self.BLUE, 2, lineType=8) if len(approx) > 3 and len(approx) < 15: _, _, w, h = cv2.boundingRect(approx) if h > self.min_height and w > self.min_width: hull = cv2.convexHull(cnt) approx2 = cv2.approxPolyDP(hull,0.01*cv2.arcLength(hull,True),True) if self.draw_approx2: cv2.drawContours(self.out, [approx2], -1, self.GREEN, 2, lineType=8) result.append(approx2) return result
def __init__(self, rubiks_parent, index, contour, heirarchy): self.rubiks_parent = rubiks_parent self.index = index self.contour = contour self.heirarchy = heirarchy peri = cv2.arcLength(contour, True) self.approx = cv2.approxPolyDP(contour, 0.1 * peri, True) self.area = cv2.contourArea(contour) self.corners = len(self.approx) self.width = None # compute the center of the contour M = cv2.moments(contour) if M["m00"]: self.cX = int(M["m10"] / M["m00"]) self.cY = int(M["m01"] / M["m00"]) else: self.cX = None self.cY = None
def _get_tip_position(array, contour, verbose = False): approx_contour = cv2.approxPolyDP(contour, 0.08 * cv2.arcLength(contour, True), True) convex_points = cv2.convexHull(approx_contour, returnPoints = True) cx, cy = 999, 999 for point in convex_points: cur_cx, cur_cy = point[0][0], point[0][1] if verbose: cv2.circle(array, (cur_cx, cur_cy), 4, _COLOR_GREEN,4) if (cur_cy < cy): cx, cy = cur_cx, cur_cy (screen_x, screen_y) = pyautogui.size() height, width, _ = array.shape x = _round_int((float(cx))/(width-0)*(screen_x+1)) y = _round_int((float(cy))/(height-0)*(screen_y+1)) return (array, (x, y))
def img_contour_select(ctrs, im): # ???????????? cand_rect = [] for item in ctrs: epsilon = 0.02*cv2.arcLength(item, True) approx = cv2.approxPolyDP(item, epsilon, True) if len(approx) <= 8: rect = cv2.minAreaRect(item) #???????? if rect[2] < -10 and rect[2] > -80: continue if rect[1][0] < 10 or rect[1][1] < 10: continue #ratio = (rect[1][1]+0.00001) / rect[1][0] #if ratio > 1 or ratio < 0.9: # continue box = cv2.boxPoints(rect) box_d = np.int0(box) cv2.drawContours(im, [box_d], 0, (0,255,0), 3) cand_rect.append(box) img_show_hook("????", im) return cand_rect
def findEllipses(edges): contours, _ = cv2.findContours(edges.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) ellipseMask = np.zeros(edges.shape, dtype=np.uint8) contourMask = np.zeros(edges.shape, dtype=np.uint8) pi_4 = np.pi * 4 for i, contour in enumerate(contours): if len(contour) < 5: continue area = cv2.contourArea(contour) if area <= 100: # skip ellipses smaller then 10x10 continue arclen = cv2.arcLength(contour, True) circularity = (pi_4 * area) / (arclen * arclen) ellipse = cv2.fitEllipse(contour) poly = cv2.ellipse2Poly((int(ellipse[0][0]), int(ellipse[0][1])), (int(ellipse[1][0] / 2), int(ellipse[1][1] / 2)), int(ellipse[2]), 0, 360, 5) # if contour is circular enough if circularity > 0.6: cv2.fillPoly(ellipseMask, [poly], 255) continue # if contour has enough similarity to an ellipse similarity = cv2.matchShapes(poly.reshape((poly.shape[0], 1, poly.shape[1])), contour, cv2.cv.CV_CONTOURS_MATCH_I2, 0) if similarity <= 0.2: cv2.fillPoly(contourMask, [poly], 255) return ellipseMask, contourMask
def get_corners_from_contours(contours, corner_amount=4): """ Finds four corners from a list of points on the goal epsilon - the minimum side length of the polygon generated by the corners Parameters: :param: `contours` - a numpy array of points (opencv contour) of the points to get corners from :param: `corner_amount` - the number of corners to find """ coefficient = .05 while True: # print(contours) epsilon = coefficient * cv2.arcLength(contours, True) # epsilon = # print("epsilon:", epsilon) poly_approx = cv2.approxPolyDP(contours, epsilon, True) hull = cv2.convexHull(poly_approx) if len(hull) == corner_amount: return hull else: if len(hull) > corner_amount: coefficient += .01 else: coefficient -= .01
def get_contours(image, polydb=0.1, contour_range=7): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour contours = _findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = numpy.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) return screenCnt
def findSquare( self,frame ): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(blurred, 60, 60) # find contours in the edge map (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # loop over our contours to find hexagon cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:50] screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.004 * peri, True) # if our approximated contour has four points, then # we can assume that we have found our squeare if len(approx) >= 4: screenCnt = approx x,y,w,h = cv2.boundingRect(c) cv2.drawContours(image, [approx], -1, (0, 0, 255), 1) #cv2.imshow("Screen", image) #create the mask and remove rest of the background mask = np.zeros(image.shape[:2], dtype = "uint8") cv2.drawContours(mask, [screenCnt], -1, 255, -1) masked = cv2.bitwise_and(image, image, mask = mask) #cv2.imshow("Masked",masked ) #crop the masked image to to be compared to referance image cropped = masked[y:y+h,x:x+w] #scale the image so it is fixed size as referance image cropped = cv2.resize(cropped, (200,200), interpolation =cv2.INTER_AREA) return cropped
def imgSeg_contour(img, b,g,r, per): lower = np.array([0, 0, 0]) upper = np.array([b,g,r]) shapeMask = cv2.inRange(img, lower, upper) #http://stackoverflow.com/questions/27746089/python-computer-vision-contours-too-many-values-to-unpack _, cnts, hierarchy = cv2.findContours(shapeMask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:4] for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, per * peri, True) ### 0.04 ### if (len(approx) >= 4) and (len(approx) < 6): break return approx
def get_corners(contour): """ Given a contour that should have a rectangular convex hull, produce a sorted list of corners for the bounding rectangle :param contour: :return: """ hull = cv2.convexHull(contour) hull_poly = cv2.approxPolyDP(hull, 0.05 * cv2.arcLength(hull, True), True) return sort_corners(hull_poly)
def cannyThresholding(self, contour_retrieval_mode = cv2.RETR_LIST): ''' contour_retrieval_mode is passed through as second argument to cv2.findContours ''' # Attempt to match edges found in blue, green or red channels : collect all channel = 0 for gray in cv2.split(self.img): channel += 1 print('channel %d ' % channel) title = self.tgen.next('channel-%d' % channel) if self.show: ImageViewer(gray).show(window = title, destroy = self.destroy, info = self.info, thumbnailfn = title) found = {} for thrs in xrange(0, 255, 26): print('Using threshold %d' % thrs) if thrs == 0: print('First step') bin = cv2.Canny(gray, 0, 50, apertureSize=5) title = self.tgen.next('canny-%d' % channel) if self.show: ImageViewer(bin).show(window = title, destroy = self.destroy, info = self.info, thumbnailfn = title) bin = cv2.dilate(bin, None) title = self.tgen.next('canny-dilate-%d' % channel) if self.show: ImageViewer(bin).show(window = title, destroy = self.destroy, info = self.info, thumbnailfn = title) else: retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY) title = self.tgen.next('channel-%d-threshold-%d' % (channel, thrs)) if self.show: ImageViewer(bin).show(window='Next threshold (n to continue)', destroy = self.destroy, info = self.info, thumbnailfn = title) bin, contours, hierarchy = cv2.findContours(bin, contour_retrieval_mode, cv2.CHAIN_APPROX_SIMPLE) title = self.tgen.next('channel-%d-threshold-%d-contours' % (channel, thrs)) if self.show: ImageViewer(bin).show(window = title, destroy = self.destroy, info = self.info, thumbnailfn = title) if contour_retrieval_mode == cv2.RETR_LIST or contour_retrieval_mode == cv2.RETR_EXTERNAL: filteredContours = contours else: filteredContours = [] h = hierarchy[0] for component in zip(contours, h): currentContour = component[0] currentHierarchy = component[1] if currentHierarchy[3] < 0: # Found the outermost parent component filteredContours.append(currentContour) print('Contours filtered. Input %d Output %d' % (len(contours), len(filteredContours))) time.sleep(5) for cnt in filteredContours: cnt_len = cv2.arcLength(cnt, True) cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True) cnt_len = len(cnt) cnt_area = cv2.contourArea(cnt) cnt_isConvex = cv2.isContourConvex(cnt) if cnt_len == 4 and (cnt_area > self.area_min and cnt_area < self.area_max) and cnt_isConvex: cnt = cnt.reshape(-1, 2) max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)]) if max_cos < self.cos_limit : sq = Square(cnt, cnt_area, cnt_isConvex, max_cos) self.squares.append(sq) else: #print('dropped a square with max_cos %f' % max_cos) pass found[thrs] = len(self.squares) print('Found %d quadrilaterals with threshold %d' % (len(self.squares), thrs))
def findSignificantContours(img, sobel_8u, sobel): image, contours, heirarchy = cv2.findContours(sobel_8u, \ cv2.RETR_EXTERNAL, \ cv2.CHAIN_APPROX_SIMPLE) mask = np.ones(image.shape[:2], dtype="uint8") * 255 level1 = [] for i, tupl in enumerate(heirarchy[0]): if tupl[3] == -1: tupl = np.insert(tupl, 0, [i]) level1.append(tupl) significant = [] tooSmall = sobel_8u.size * 10 / 100 for tupl in level1: contour = contours[tupl[0]]; area = cv2.contourArea(contour) if area > tooSmall: cv2.drawContours(mask, \ [contour], 0, (0, 255, 0), \ 2, cv2.LINE_AA, maxLevel=1) significant.append([contour, area]) significant.sort(key=lambda x: x[1]) significant = [x[0] for x in significant]; peri = cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, 0.02 * peri, True) mask = sobel.copy() mask[mask > 0] = 0 cv2.fillPoly(mask, significant, 255, 0) mask = np.logical_not(mask) img[mask] = 0; return img
def categorize_contour( contour, perimeter_heuristic=PERIMETER_HEURISTIC, linear_heuristic=LINEAR_HEURISTIC): """ Categorize a contour as a box, a line, or speck """ area = cv2.contourArea(contour) perimeter = cv2.arcLength(contour, True) if perimeter <= perimeter_heuristic: return CONTOUR_SPECK ratio = area / perimeter if ratio < linear_heuristic: return CONTOUR_LINE else: return CONTOUR_BOX
def find_contours(mask, smooth_factor=0.005): """ Find the contours in a given mask """ border = 5 # Canny detection breaks down with the edge of the image my_mask = cv2.copyMakeBorder(mask, border, border, border, border, cv2.BORDER_CONSTANT, value=(0, 0, 0)) my_mask = cv2.cvtColor(my_mask, cv2.COLOR_BGR2GRAY) if is_cv2(): contours, _ = cv2.findContours(my_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) else: _, contours, _ = cv2.findContours(my_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # shift the contours back down for contour in contours: for pnt in contour: if pnt[0][1] > border: pnt[0][1] = pnt[0][1] - border else: pnt[0][1] = 0 if pnt[0][0] > border: pnt[0][0] = pnt[0][0] - border else: pnt[0][0] = 0 closed_contours = [] for contour in contours: epsilon = smooth_factor*cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, epsilon, True) area = cv2.contourArea(approx) # if they are too small they are not edges if area < 200: continue closed_contours.append(approx) return closed_contours
def find_likely_rectangles(contours, sigma): contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10] possible = [] for c in contours: # approximate the contour peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, sigma * peri, True) box = make_box(approx) possible.append(box) return possible
def extract_rect(im): imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray, 127, 255, 0) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # finding contour with max area largest = None for cnt in contours: if largest == None or cv2.contourArea(cnt) > cv2.contourArea(largest): largest = cnt peri = cv2.arcLength(largest, True) appr = cv2.approxPolyDP(largest, 0.02 * peri, True) #cv2.drawContours(im, appr, -1, (0,255,0), 3) points_list = [[i[0][0], i[0][1]] for i in appr] left = sorted(points_list, key = lambda p: p[0])[0:2] right = sorted(points_list, key = lambda p: p[0])[2:4] print("l " + str(left)) print("r " + str(right)) lu = sorted(left, key = lambda p: p[1])[0] ld = sorted(left, key = lambda p: p[1])[1] ru = sorted(right, key = lambda p: p[1])[0] rd = sorted(right, key = lambda p: p[1])[1] print("lu " + str(lu)) print("ld " + str(ld)) print("ru " + str(ru)) print("rd " + str(rd)) lu_ = [ (lu[0] + ld[0])/2, (lu[1] + ru[1])/2 ] ld_ = [ (lu[0] + ld[0])/2, (ld[1] + rd[1])/2 ] ru_ = [ (ru[0] + rd[0])/2, (lu[1] + ru[1])/2 ] rd_ = [ (ru[0] + rd[0])/2, (ld[1] + rd[1])/2 ] print("lu_ " + str(lu_)) print("ld_ " + str(ld_)) print("ru_ " + str(ru_)) print("rd_ " + str(rd_)) src_pts = np.float32(np.array([lu, ru, rd, ld])) dst_pts = np.float32(np.array([lu_, ru_, rd_, ld_])) h,w,b = im.shape H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) print("H" + str(H)) imw = cv2.warpPerspective(im, H, (w, h)) return imw[lu_[1]:rd_[1], lu_[0]:rd_[0]] # cropping image
def camera_gesture_trigger(): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray,(5,5),0) ret,thresh1 = cv2.threshold(blur,70,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) max_area=0 for i in range(len(contours)): cnt=contours[i] area = cv2.contourArea(cnt) if(area>max_area): max_area=area ci=i cnt=contours[ci] hull = cv2.convexHull(cnt) moments = cv2.moments(cnt) cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True) hull = cv2.convexHull(cnt,returnPoints = False) defects = cv2.convexityDefects(cnt,hull) if defects is not None: if defects.shape[0] >= 5: return 1 return 0
def get_approx_contour(contour, tol=.01): """Get rid of 'useless' points in the contour""" epsilon = tol * cv2.arcLength(contour, True) return cv2.approxPolyDP(contour, epsilon, True)
def get_image_xy_corner(self): """get ?artesian coordinates from raster""" import cv2 if not self.image_path: return False image_xy_corners = [] img = cv2.imread(self.image_path, cv2.IMREAD_GRAYSCALE) imagem = (255 - img) try: ret, thresh = cv2.threshold(imagem, 10, 128, cv2.THRESH_BINARY) try: contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) except Exception: im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) hierarchy = hierarchy[0] hierarhy_contours = [[] for _ in range(len(hierarchy))] for fry in range(len(contours)): currentContour = contours[fry] currentHierarchy = hierarchy[fry] cc = [] # epsilon = 0.0005 * cv2.arcLength(contours[len(contours) - 1], True) approx = cv2.approxPolyDP(currentContour, self.epsilon, True) if len(approx) > 2: for c in approx: cc.append([c[0][0], c[0][1]]) parent_index = currentHierarchy[3] index = fry if parent_index < 0 else parent_index hierarhy_contours[index].append(cc) image_xy_corners = [c for c in hierarhy_contours if len(c) > 0] return image_xy_corners except Exception as ex: self.error(ex) return image_xy_corners
def boundingRects(scale, contours): for contour in contours: epsilon = 0.1 * cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, epsilon, True) x, y, w, h = cv2.boundingRect(approx) yield [x * scale, y * scale, w * scale, h * scale]
def get_contours(image, polydb=0.03, contour_range=5, show=False): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour # if cv2version == 3: im2, contours, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours = _findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = np.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) if show: #this is for debugging puposes new_image = image.copy() cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2) cv2.imshow("Contour1 image", new_image) cv2.waitKey(0) cv2.destroyAllWindows() return screenCnt
def get_contours(image, polydb=0.03, contour_range=5, show=False): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour if cv2version == 3: im2, contours, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = np.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) if show: #this is for debugging puposes new_image = image.copy() cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2) cv2.imshow("Contour1 image", new_image) cv2.waitKey(0) cv2.destroyAllWindows() return screenCnt
def get_contours(image, polydb=0.1, contour_range=7, show=False): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour contours = _findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = numpy.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) if show: #this is for debugging puposes new_image = image.copy() cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2) cv2.imshow("Contour1 image", new_image) cv2.waitKey(0) cv2.destroyAllWindows() return screenCnt
def get_contours(image, polydb=0.03, contour_range=7, show=False): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour # if cv2version == 3: im2, contours, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours = _findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = numpy.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) if show: #this is for debugging puposes new_image = image.copy() cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2) cv2.imshow("Contour1 image", new_image) cv2.waitKey(0) cv2.destroyAllWindows() return screenCnt
def countVertices(contour): peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) return len(approx) #Classifies a contour as a generic polygon shape
def find_display_contour(edge_img_arr): display_contour = None edge_copy = edge_img_arr.copy() contours,hierarchy = cv2.findContours(edge_copy, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) top_cntrs = sorted(contours, key = cv2.contourArea, reverse = True)[:10] for cntr in top_cntrs: peri = cv2.arcLength(cntr,True) approx = cv2.approxPolyDP(cntr, 0.02 * peri, True) if len(approx) == 4: display_contour = approx break return display_contour
def FindField(self): #Feld: Hue zwischen 60 und 100 LowerGreen = np.array([40,0,0]) UpperGreen = np.array([90,255,150]) mask = cv2.inRange(self.ImgHSV,LowerGreen,UpperGreen) # plt.figure() # plt.imshow(mask,cmap='gray') mask = self.SmoothFieldMask(mask) # plt.figure() # plt.imshow(mask.copy(),cmap='gray') im2, contours, hierarchy = cv2.findContours(mask.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) if(len(contours) <= 0): return contours_sorted = sorted(contours, key = cv2.contourArea, reverse=True)[:10] peri = cv2.arcLength(contours_sorted[0],True) approx = cv2.approxPolyDP(contours_sorted[0], 0.02*peri, True) if(len(approx) >-1):#== 4): self.FieldContours = approx cv2.rectangle(mask,(((self.FieldContours[0])[0])[0],((self.FieldContours[0])[0])[1]),(((self.FieldContours[2])[0])[0],((self.FieldContours[2])[0])[1]),(128,128,128),3) # plt.imshow(mask, cmap="gray") # plt.show()
def detect_cnt_again(poly, base_img): """ ??????????????????? :param poly: ndarray :param base_img: ndarray :return: ndarray """ # ?????????????????flag flag = False # ????????????????????????? top_left, bottom_left, top_right, bottom_right = get_corner_node_list(poly) roi_img = get_roi_img(base_img, bottom_left, bottom_right, top_left, top_right) img = get_init_process_img(roi_img) # ????????? cnt = get_max_area_cnt(img) # ????????????????????? if cv2.contourArea(cnt) > roi_img.shape[0] * roi_img.shape[1] * SHEET_AREA_MIN_RATIO: flag = True poly = cv2.approxPolyDP(cnt, cv2.arcLength((cnt,), True) * 0.1, True) top_left, bottom_left, top_right, bottom_right = get_corner_node_list(poly) if not poly.shape[0] == 4: raise PolyNodeCountError # ????????????????? base_poly_nodes = np.float32([top_left[0], bottom_left[0], top_right[0], bottom_right[0]]) base_nodes = np.float32([[0, 0], [base_img.shape[1], 0], [0, base_img.shape[0]], [base_img.shape[1], base_img.shape[0]]]) transmtx = cv2.getPerspectiveTransform(base_poly_nodes, base_nodes) if flag: img_warp = cv2.warpPerspective(roi_img, transmtx, (base_img.shape[1], base_img.shape[0])) else: img_warp = cv2.warpPerspective(base_img, transmtx, (base_img.shape[1], base_img.shape[0])) return img_warp
def info(self): s = [] s.append('Located %d squares within image') s.append('Each square is a contour from cv2.findContours') i = 0 for sq in self.squares: i += 1 s.append('Square %d : length = %d : area = %d' % (sq.ID, cv2.arcLength(sq.contour, True), sq.area)) return '\n'.join(s)
def process_captcha(self, image): """ TODO: DOC """ cv2_img = cv2.cvtColor(numpy.array(image), cv2.COLOR_BGR2GRAY) # Find the threshold of the image so that we can identify contours. ret, thresh = cv2.threshold( cv2_img, 127, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C ) # Find the contours of the image _, contours, hierarchy = cv2.findContours( thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE ) # Find the largest contour in the image with 4 points. This is the # rectangle that is required to crop to for the captcha. largest_contour = None for contour in contours: if (len(cv2.approxPolyDP(contour, 0.1*cv2.arcLength(contour, True), True)) == 4) and (2500 < cv2.contourArea(contour) < 4000): if isinstance(largest_contour, type(None)): largest_contour = contour continue if cv2.contourArea(contour) > cv2.contourArea(largest_contour): largest_contour = contour # If we don't have a matching contour, don't try to crop and such if isinstance(largest_contour, type(None)): return None # If we do have a matching contour, build the rectangle crop_x, crop_y, crop_width, crop_height = cv2.boundingRect( largest_contour ) # Crop down to the contour rectangle image = image.crop( ( crop_x, crop_y, crop_x + crop_width, crop_y + crop_height ) ) return image
def extract_bv(image): clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) contrast_enhanced_green_fundus = clahe.apply(image) # applying alternate sequential filtering (3 times closing opening) r1 = cv2.morphologyEx(contrast_enhanced_green_fundus, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1) R1 = cv2.morphologyEx(r1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1) r2 = cv2.morphologyEx(R1, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1) R2 = cv2.morphologyEx(r2, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1) r3 = cv2.morphologyEx(R2, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1) R3 = cv2.morphologyEx(r3, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1) f4 = cv2.subtract(R3,contrast_enhanced_green_fundus) f5 = clahe.apply(f4) # removing very small contours through area parameter noise removal ret,f6 = cv2.threshold(f5,15,255,cv2.THRESH_BINARY) mask = np.ones(f5.shape[:2], dtype="uint8") * 255 im2, contours, hierarchy = cv2.findContours(f6.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: if cv2.contourArea(cnt) <= 200: cv2.drawContours(mask, [cnt], -1, 0, -1) im = cv2.bitwise_and(f5, f5, mask=mask) ret,fin = cv2.threshold(im,15,255,cv2.THRESH_BINARY_INV) newfin = cv2.erode(fin, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1) # removing blobs of microaneurysm & unwanted bigger chunks taking in consideration they are not straight lines like blood # vessels and also in an interval of area fundus_eroded = cv2.bitwise_not(newfin) xmask = np.ones(image.shape[:2], dtype="uint8") * 255 x1, xcontours, xhierarchy = cv2.findContours(fundus_eroded.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in xcontours: shape = "unidentified" peri = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, 0.04 * peri, False) if len(approx) > 4 and cv2.contourArea(cnt) <= 3000 and cv2.contourArea(cnt) >= 100: shape = "circle" else: shape = "veins" if(shape=="circle"): cv2.drawContours(xmask, [cnt], -1, 0, -1) finimage = cv2.bitwise_and(fundus_eroded,fundus_eroded,mask=xmask) blood_vessels = cv2.bitwise_not(finimage) dilated = cv2.erode(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7)), iterations=1) #dilated1 = cv2.dilate(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1) blood_vessels_1 = cv2.bitwise_not(dilated) return blood_vessels_1
def get_contour_perim_by_hull_perim(contour): return (cv2.arcLength(contour, True) / cv2.arcLength(get_convex_hull(contour), True))
def get_contour_perim_by_bounding_box_perim(contour): return (cv2.arcLength(contour, True) / cv2.arcLength(get_bounding_rect(contour), True))
def _find_hull_defects(self, segment): # Use cv2 findContours function to find all the contours in segmented img contours, hierarchy = cv2.findContours(segment, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # assume largest contour is the one of interest max_contour = max(contours, key=cv2.contourArea) epsilon = 0.01*cv2.arcLength(max_contour, True) max_contour = cv2.approxPolyDP(max_contour, epsilon, True) # determine convex hull & convexity defects of the hull hull = cv2.convexHull(max_contour, returnPoints=False) defects = cv2.convexityDefects(max_contour, hull) return (max_contour, defects)
def __filter_contours(input_contours, min_area, min_perimeter, min_width, max_width, min_height, max_height, solidity, max_vertex_count, min_vertex_count, min_ratio, max_ratio): """Filters out contours that do not meet certain criteria. Args: input_contours: Contours as a list of numpy.ndarray. min_area: The minimum area of a contour that will be kept. min_perimeter: The minimum perimeter of a contour that will be kept. min_width: Minimum width of a contour. max_width: MaxWidth maximum width. min_height: Minimum height. max_height: Maximimum height. solidity: The minimum and maximum solidity of a contour. min_vertex_count: Minimum vertex Count of the contours. max_vertex_count: Maximum vertex Count. min_ratio: Minimum ratio of width to height. max_ratio: Maximum ratio of width to height. Returns: Contours as a list of numpy.ndarray. """ output = [] for contour in input_contours: x,y,w,h = cv2.boundingRect(contour) if (w < min_width or w > max_width): continue if (h < min_height or h > max_height): continue area = cv2.contourArea(contour) if (area < min_area): continue if (cv2.arcLength(contour, True) < min_perimeter): continue hull = cv2.convexHull(contour) solid = 100 * area / cv2.contourArea(hull) if (solid < solidity[0] or solid > solidity[1]): continue if (len(contour) < min_vertex_count or len(contour) > max_vertex_count): continue ratio = (float)(w) / h if (ratio < min_ratio or ratio > max_ratio): continue output.append(contour) return output
def main(): # gets screen size w, h = pyautogui.size() # takes screen screenshot. Returns hsv format image scrn_scrnshot = Screenshot.this(0, 0, w, h, 'hsv') #cv2.imshow('img', scrn_scrnshot) # cv2.waitKey(0) # find Grand exchange window lower_hsv = np.array([12, 0, 7]) upper_hsv = np.array([40, 62, 64]) # mask of applied values mask = cv2.inRange(scrn_scrnshot, lower_hsv, upper_hsv) cv2.imshow('img', mask) cv2.waitKey(0) return # find contours to get sides of rectangle _, contours, h = cv2.findContours(mask, 1, 2) for cnt in contours: # looks for biggest square # if cv2.contourArea(cnt) <= 1695.0: # continue # checks contour sides approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True) # Square found here vvvv if len(approx) == 4: #print("square of {}".format(cv2.contourArea(cnt))) # cv2.drawContours(rs_window,[cnt],0,(255,255,255),-1) # get geometry of approx # add rs coords x, y, w, h = cv2.boundingRect(cnt) print(cv2.contourArea(cnt))