我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.threshold()。
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 getmarkerboundingrect(img, mkpos, mksize): buffer = int(mksize * 0.15) x = mkpos[0] - buffer y = mkpos[1] - buffer w = mksize + buffer*2 h = mksize + buffer*2 roi = img[y:y+h, x:x+w] grayroi = getgrayimage(roi) ret, binimage = cv2.threshold(grayroi,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(binimage) # stats[0], centroids[0] are for the background label. ignore # cv2.CC_STAT_LEFT, cv2.CC_STAT_TOP, cv2.CC_STAT_WIDTH, cv2.CC_STAT_HEIGHT lblareas = stats[1:,cv2.CC_STAT_AREA] imax = max(enumerate(lblareas), key=(lambda x: x[1]))[0] + 1 boundingrect = Rect(stats[imax, cv2.CC_STAT_LEFT], stats[imax, cv2.CC_STAT_TOP], stats[imax, cv2.CC_STAT_WIDTH], stats[imax, cv2.CC_STAT_HEIGHT]) return boundingrect.addoffset((x,y))
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 find_lines(img): edges = cv2.Canny(img,100,200) threshold = 60 minLineLength = 10 lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold, 0, minLineLength, 20); if (lines is None or len(lines) == 0): return #print lines for line in lines[0]: #print line cv2.line(img, (line[0],line[1]), (line[2],line[3]), (0,255,0), 2) cv2.imwrite("line_edges.jpg", edges) cv2.imwrite("lines.jpg", img)
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 execute_Threshold(proxy,obj): try: img=obj.sourceObject.Proxy.img.copy() except: img=cv2.imread(__dir__+'/icons/freek.png') # img = cv2.imread('dave.jpg',0) ?? img = cv2.medianBlur(img,5) img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) if obj.globalThresholding: ret,th1 = cv2.threshold(img,obj.param1,obj.param2,cv2.THRESH_BINARY) obj.Proxy.img = cv2.cvtColor(th1, cv2.COLOR_GRAY2RGB) if obj.adaptiveMeanTresholding: th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ cv2.THRESH_BINARY,11,2) obj.Proxy.img = cv2.cvtColor(th2, cv2.COLOR_GRAY2RGB) if obj.adaptiveGaussianThresholding: th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,17,2) obj.Proxy.img = cv2.cvtColor(th3, cv2.COLOR_GRAY2RGB)
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 getmarkercenter(image, pos): mkradius = getapproxmarkerradius(image) buffer = int(mkradius * 0.15) roisize = mkradius + buffer # half of the height or width x = pos[0] - roisize y = pos[1] - roisize w = 2 * roisize h = 2 * roisize roi = image[y:y+h, x:x+w] grayroi = getgrayimage(roi) ret, binimage = cv2.threshold(grayroi,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(binimage) # stats[0], centroids[0] are for the background label. ignore lblareas = stats[1:,cv2.CC_STAT_AREA] ave = np.average(centroids[1:], axis=0, weights=lblareas) return tuple(np.array([x, y]) + ave) # weighted average pos of centroids
def find_bibs(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY); binary = cv2.GaussianBlur(gray,(5,5),0) ret,binary = cv2.threshold(binary, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU); #binary = cv2.adaptiveThreshold(binary, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) #ret,binary = cv2.threshold(binary, 190, 255, cv2.THRESH_BINARY); #lapl = cv2.Laplacian(image,cv2.CV_64F) #gray = cv2.cvtColor(lapl, cv2.COLOR_BGR2GRAY); #blurred = cv2.GaussianBlur(lapl,(5,5),0) #ret,binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU); #cv2.imwrite("lapl.jpg", lapl) edges = cv2.Canny(image,175,200) cv2.imwrite("edges.jpg", edges) binary = edges cv2.imwrite("binary.jpg", binary) contours,hierarchy = find_contours(binary) return get_rectangles(contours)
def diff_rect(img1, img2, pos=None): """find counters include pos in differences between img1 & img2 (cv2 images)""" diff = cv2.absdiff(img1, img2) diff = cv2.GaussianBlur(diff, (3, 3), 0) edges = cv2.Canny(diff, 100, 200) _, thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) if not contours: return None contours.sort(key=lambda c: len(c)) # no pos provide, just return the largest different area rect if pos is None: cnt = contours[-1] x0, y0, w, h = cv2.boundingRect(cnt) x1, y1 = x0+w, y0+h return (x0, y0, x1, y1) # else the rect should contain the pos x, y = pos for i in range(len(contours)): cnt = contours[-1-i] x0, y0, w, h = cv2.boundingRect(cnt) x1, y1 = x0+w, y0+h if x0 <= x <= x1 and y0 <= y <= y1: return (x0, y0, x1, y1)
def calculate_entropy(image): entropy = image.copy() sum = 0 i = 0 j = 0 while i < entropy.shape[0]: j = 0 while j < entropy.shape[1]: sub_image = entropy[i:i+10,j:j+10] histogram = cv2.calcHist([sub_image],[0],None,[256],[0,256]) sum = 0 for k in range(256): if histogram[k] != 0: sum = sum + (histogram[k] * math.log(histogram[k])) k = k + 1 entropy[i:i+10,j:j+10] = sum j = j+10 i = i+10 ret2,th2 = cv2.threshold(entropy,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) newfin = cv2.erode(th2, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1) return newfin
def threshold(im_gray, method): ''' ??????????thresholding??????????? ??????thresholding????????OpenCV?? ''' if method == 'fixed': threshed_im = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY) elif method == 'mean': threshed_im = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -22) elif method == 'gaussian': threshed_im = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 7) else: return None return threshed_im
def classify(img): cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img2 = cv2.medianBlur(cimg, 13) ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY) t2 = copy.copy(thresh1) x, y = thresh1.shape arr = np.zeros((x, y, 3), np.uint8) final_contours = [] image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #cv2.imshow('image', image) #k = cv2.waitKey(0) for i in range(len(contours)): cnt = contours[i] if cv2.contourArea(cnt) > 3000 and cv2.contourArea(cnt) < 25000: cv2.drawContours(img, [cnt], -1, [0, 255, 255]) cv2.fillConvexPoly(arr, cnt, [255, 255, 255]) final_contours.append(cnt) #cv2.imshow('arr', arr) #k = cv2.waitKey(0) return arr
def adaptive_threshold(image, above_thresh_assigned=255, kind='mean', cell_size=35, c_param=17, thresh_style=cv.THRESH_BINARY_INV): ''' :param kind: specify adaptive method, whether 'mean' or 'gaussian'. :param cell_size: n for the region size (n x n). :param c_param: subtraction constant. :return: a binary version of the input image. ''' if kind == 'mean': method = cv.ADAPTIVE_THRESH_MEAN_C elif kind == 'gaussian': method = cv.ADAPTIVE_THRESH_GAUSSIAN_C else: raise ValueError('Unknown adaptive threshold method.') return cv.adaptiveThreshold(image, above_thresh_assigned, method, thresh_style, cell_size, c_param)
def CropLowerBoundary(img): # img_gray = ToGrayImage(path) _,img_bi = cv2.threshold(img,60,255,cv2.THRESH_BINARY) threshold_rate = 0.95 threshold_row = -1 row,col = img_bi.shape for tmp_r in range(row-1,-1,-1): tmp_sum = sum(img_bi[tmp_r]) rate = float(tmp_sum)/255/col # print(rate) if rate>threshold_rate: threshold_row = tmp_r break img = img[0:threshold_row,:] # plt.imshow(img,"gray") # plt.show() return img
def EdgeDetection(img): img = cv2.fastNlMeansDenoising(img,None,3,7,21) _,img = cv2.threshold(img,30,255,cv2.THRESH_TOZERO) denoise_img = img laplacian = cv2.Laplacian(img,cv2.CV_64F) sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # x sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3) # y canny = cv2.Canny(img,100,200) contour_image, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) return {"denoise":denoise_img,"laplacian":laplacian,"canny":canny,"sobely":sobely,"sobelx":sobelx,"contour":contour_image} # GrayScale Image Convertor # https://extr3metech.wordpress.com
def FindLowerBoundary(path,mode): img_gray = ToGrayImage(path) _,img_bi = cv2.threshold(img_gray,10,255,cv2.THRESH_BINARY) threshold_rate = 0.8 threshold_row = -1 row,col = img_bi.shape for tmp_r in range(row-1,-1,-1): tmp_sum = sum(img_bi[tmp_r]) rate = float(tmp_sum)/255/col if rate>threshold_rate: threshold_row = tmp_r break return threshold_row # GrayScale Image Convertor # https://extr3metech.wordpress.com
def MyDenoiseSobely(path): img_gray = ToGrayImage(path) img_mydenoise = MyDenoise(img_gray,5) img_denoise = cv2.fastNlMeansDenoising(img_mydenoise,None,3,7,21) _,img_thre = cv2.threshold(img_denoise,100,255,cv2.THRESH_TOZERO) sobely = cv2.Sobel(img_thre,cv2.CV_64F,0,1,ksize=3) return sobely
def find_contour(self, img_src, Rxmin, Rymin, Rxmax, Rymax): cv2.rectangle(img_src, (Rxmax, Rymax), (Rxmin, Rymin), (0, 255, 0), 0) crop_res = img_src[Rymin: Rymax, Rxmin:Rxmax] grey = cv2.cvtColor(crop_res, cv2.COLOR_BGR2GRAY) _, thresh1 = cv2.threshold(grey, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) cv2.imshow('Thresh', thresh1) contours, hierchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) # draw contour on threshold image if len(contours) > 0: cv2.drawContours(thresh1, contours, -1, (0, 255, 0), 3) return contours, crop_res # Check ConvexHull and Convexity Defects
def detect_edges(images): def blur(image): return cv2.GaussianBlur(image, (5, 5), 0) def canny_otsu(image): scale_factor = 255 scaled_image = np.uint8(image * scale_factor) otsu_threshold = cv2.threshold( cv2.cvtColor(scaled_image, cv2.COLOR_RGB2GRAY), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[0] lower_threshold = max(0, int(otsu_threshold * 0.5)) upper_threshold = min(255, int(otsu_threshold)) edges = cv2.Canny(scaled_image, lower_threshold, upper_threshold) edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) return np.float32(edges) * (1 / scale_factor) blurred = [blur(image) for image in images] canny_applied = [canny_otsu(image) for image in blurred] return canny_applied
def process_img(img): original_image=img processed_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) processed_img = cv2.Canny(processed_img, threshold1=200, threshold2=300) processed_img = cv2.GaussianBlur(processed_img, (3,3), 0 ) copy=processed_img vertices = np.array([[30, 240], [30, 100], [195, 100], [195, 240]]) processed_img = roi(processed_img, np.int32([vertices])) verticesP = np.array([[30, 270], [30, 230], [197, 230], [197, 270]]) platform = roi(copy, np.int32([verticesP])) # edges #lines = cv2.HoughLinesP(platform, 1, np.pi/180, 180,np.array([]), 3, 2) #draw_lines(processed_img,lines) #draw_lines(original_image,lines) #Platform lines #imgray = cv2.cvtColor(platform,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(platform,127,255,0) im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(original_image, contours, -1, (0,255,0), 3) try: platformpos=contours[0][0][0] except: platformpos=[[0]] circles = cv2.HoughCircles(processed_img, cv2.HOUGH_GRADIENT, 1, 20, param1=90, param2=5, minRadius=1, maxRadius=3) ballpos=draw_circles(original_image,circles=circles) return processed_img,original_image,platform,platformpos,ballpos
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 find_bib(image): width, height, depth = image.shape gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY); #gray = cv2.equalizeHist(gray) blurred = cv2.GaussianBlur(gray,(5,5),0) debug_output("find_bib_blurred", blurred) #binary = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blockSize=25, C=0); ret,binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU); #ret,binary = cv2.threshold(blurred, 170, 255, cv2.THRESH_BINARY); debug_output("find_bib_binary", binary) threshold_contours,hierarchy = find_contours(binary) debug_output("find_bib_threshold", binary) edges = cv2.Canny(gray,175,200, 3) edge_contours,hierarchy = find_contours(edges) debug_output("find_bib_edges", edges) contours = threshold_contours + edge_contours debug_output_contours("find_bib_threshold_contours", image, contours) rectangles = get_rectangles(contours) debug_output_contours("find_bib_rectangles", image, rectangles) potential_bibs = [rect for rect in rectangles if is_potential_bib(rect, width*height)] debug_output_contours("find_bib_potential_bibs", image, potential_bibs) ideal_aspect_ratio = 1.0 potential_bibs = sorted(potential_bibs, key = lambda bib: abs(aspect_ratio(bib) - ideal_aspect_ratio)) return potential_bibs[0] if len(potential_bibs) > 0 else np.array([[(0,0)],[(0,0)],[(0,0)],[(0,0)]]) # # Checks that the size and aspect ratio of the contour is appropriate for a bib. #
def predict(url): global model # Read image image = io.imread(url) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) image = cv2.resize(image, (500, 500), interpolation=cv2.INTER_CUBIC) # Use otsu to mask gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) mask = cv2.medianBlur(mask, 5) features = describe(image, mask) state = le.inverse_transform(model.predict([features]))[0] return {'type': state}
def find_chars(img): gray = np.array(img.convert("L")) ret, mask = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY) image_final = cv2.bitwise_and(gray, gray, mask=mask) ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY_INV) kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) dilated = cv2.dilate(new_img, kernel, iterations=1) # Image.fromarray(dilated).save('out.png') # for debugging _, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) coords = [] for contour in contours: # get rectangle bounding contour [x, y, w, h] = cv2.boundingRect(contour) # ignore large chars (probably not chars) if w > 70 and h > 70: continue coords.append((x, y, w, h)) return coords # find list of eye coordinates in image
def cropCircle(img, resize=None): if resize: if (img.shape[0] > img.shape[1]): tile_size = (int(img.shape[1] * resize / img.shape[0]), resize) else: tile_size = (resize, int(img.shape[0] * resize / img.shape[1])) img = cv2.resize(img, dsize=tile_size, interpolation=cv2.INTER_CUBIC) else: tile_size = img.shape gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY); _, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY) _, contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) main_contour = sorted(contours, key=cv2.contourArea, reverse=True)[0] ff = np.zeros((gray.shape[0], gray.shape[1]), 'uint8') cv2.drawContours(ff, main_contour, -1, 1, 15) ff_mask = np.zeros((gray.shape[0] + 2, gray.shape[1] + 2), 'uint8') cv2.floodFill(ff, ff_mask, (int(gray.shape[1] / 2), int(gray.shape[0] / 2)), 1) rect = maxRect(ff) rectangle = [min(rect[0], rect[2]), max(rect[0], rect[2]), min(rect[1], rect[3]), max(rect[1], rect[3])] img_crop = img[rectangle[0]:rectangle[1], rectangle[2]:rectangle[3]] cv2.rectangle(ff, (min(rect[1], rect[3]), min(rect[0], rect[2])), (max(rect[1], rect[3]), max(rect[0], rect[2])), 3, 2) return [img_crop, rectangle, tile_size]
def test_initial_pass_through_compare(self): original = cv2.imread(os.path.join(self.provider.assets, "start_screen.png")) against = self.provider.get_img_from_screen_shot() wrong = cv2.imread(os.path.join(self.provider.assets, "battle.png")) # convert the images to grayscale original = mask_image([127], [255], cv2.cvtColor(original, cv2.COLOR_BGR2GRAY), True) against = mask_image([127], [255], cv2.cvtColor(against, cv2.COLOR_BGR2GRAY), True) wrong = mask_image([127], [255], cv2.cvtColor(wrong, cv2.COLOR_BGR2GRAY), True) # initialize the figure (score, diff) = compare_ssim(original, against, full=True) diff = (diff * 255).astype("uint8") self.assertTrue(score > .90, 'If this is less then .90 the initial compare of the app will fail') (score, nothing) = compare_ssim(original, wrong, full=True) self.assertTrue(score < .90) if self.__debug_pictures__: # threshold the difference image, followed by finding contours to # obtain the regions of the two input images that differ thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] # loop over the contours for c in cnts: # compute the bounding box of the contour and then draw the # bounding box on both input images to represent where the two # images differ (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2) cv2.rectangle(against, (x, y), (x + w, y + h), (0, 0, 255), 2) # show the output images diffs = ("Original", original), ("Modified", against), ("Diff", diff), ("Thresh", thresh) images = ("Original", original), ("Against", against), ("Wrong", wrong) self.setup_compare_images(diffs) self.setup_compare_images(images)
def load(self, filename, analyze_only): # Load image, then do various conversions and thresholding. self.img_orig = cv2.imread(filename, cv2.IMREAD_COLOR) if self.img_orig is None: raise CompilerException("File '{}' not found".format(filename)) self.img_grey = cv2.cvtColor(self.img_orig, cv2.COLOR_BGR2GRAY) _, self.img_contour = cv2.threshold(self.img_grey, 250, 255, cv2.THRESH_BINARY_INV) _, self.img_text = cv2.threshold(self.img_grey, 150, 255, cv2.THRESH_BINARY) self.root_node = None self.contours = self.find_contours() self.contour_lines, self.contour_nodes = self.categorize_contours() self.build_graph() self.build_parse_tree() self.parse_nodes() if not analyze_only: self.python_ast = self.root_node.to_python_ast()
def foreground(self, image, smooth=False, grayscale=False): """ Extract foreground from background :param image: :param smooth: :param grayscale: :return: """ if smooth and grayscale: image = self.toGrayscale(image) image = self.smooth(image) elif smooth: image = self.smooth(image) elif grayscale: image = self.toGrayscale(image) fgmask = self.fgbg.apply(image) ret, mask = cv2.threshold(fgmask, 200, 255, cv2.THRESH_BINARY_INV) mask_inv = cv2.bitwise_not(mask) return mask_inv
def overlay_img(self): """Overlay the transparent, transformed image of the arc onto our CV image""" #overlay the arc on the image rows, cols, channels = self.transformed.shape roi = self.cv_image[0:rows, 0:cols] #change arc_image to grayscale arc2gray = cv2.cvtColor(self.transformed, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(arc2gray, 10, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) #black out area of arc in ROI img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv) img2_fg = cv2.bitwise_and(self.transformed, self.transformed, mask=mask) #put arc on ROI and modify the main image dst = cv2.add(img1_bg, img2_fg) self.cv_image[0:rows, 0:cols] = dst
def extract_color( src, h_th_low, h_th_up, s_th, v_th ): hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) if h_th_low > h_th_up: ret, h_dst_1 = cv2.threshold(h, h_th_low, 255, cv2.THRESH_BINARY) ret, h_dst_2 = cv2.threshold(h, h_th_up, 255, cv2.THRESH_BINARY_INV) dst = cv2.bitwise_or(h_dst_1, h_dst_2) else: ret, dst = cv2.threshold(h, h_th_low, 255, cv2.THRESH_TOZERO) ret, dst = cv2.threshold(dst, h_th_up, 255, cv2.THRESH_TOZERO_INV) ret, dst = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY) ret, s_dst = cv2.threshold(s, s_th, 255, cv2.THRESH_BINARY) ret, v_dst = cv2.threshold(v, v_th, 255, cv2.THRESH_BINARY) dst = cv2.bitwise_and(dst, s_dst) dst = cv2.bitwise_and(dst, v_dst) return dst
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 recognize_text(original): idcard = original gray = cv2.cvtColor(idcard, cv2.COLOR_BGR2GRAY) # Morphological gradient: kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) opening = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel) # Binarization ret, binarization = cv2.threshold(opening, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # Connected horizontally oriented regions kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1)) connected = cv2.morphologyEx(binarization, cv2.MORPH_CLOSE, kernel) # find countours _, contours, hierarchy = cv2.findContours( connected, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE ) return contours, hierarchy
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 _extract_arm(self, img): # find center region of image frame (assume center region is 21 x 21 px) center_half = 10 # (=(21-1)/2) center = img[self.height/2 - center_half : self.height/2 + center_half, self.width/2 - center_half : self.width/2 + center_half] # determine median depth value median_val = np.median(center) '''mask the image such that all pixels whose depth values lie within a particular range are gray and the rest are black ''' img = np.where(abs(img-median_val) <= self.abs_depth_dev, 128, 0).astype(np.uint8) # Apply morphology operation to fill small holes in the image kernel = np.ones((5,5), np.uint8) img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) # Find connected regions (to hand) to remove background objects # Use floodfill with a small image area (7 x 7 px) that is set gray color value kernel2 = 3 img[self.height/2-kernel2:self.height/2+kernel2, self.width/2-kernel2:self.width/2+kernel2] = 128 # a black mask to mask the 'non-connected' components black mask = np.zeros((self.height + 2, self.width + 2), np.uint8) floodImg = img.copy() # Use floodFill function to paint the connected regions white cv2.floodFill(floodImg, mask, (self.width/2, self.height/2), 255, flags=(4 | 255 << 8)) # apply a binary threshold to show only connected hand region ret, floodedImg = cv2.threshold(floodImg, 129, 255, cv2.THRESH_BINARY) return floodedImg
def thresholding(img_grey): """ This functions creates binary images using thresholding :param img_grey: greyscale image :return: binary image """ # # Adaptive Gaussian # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2) # Otsu's thresholding after Gaussian filtering blur = cv.GaussianBlur(img_grey, (5, 5), 0) ret3, img_binary = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) # invert black = 255 ret, thresh1 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV) return thresh1
def thresholding(img_grey): """ This functions creates binary images using thresholding :param img_grey: greyscale image :return: binary image """ # # Global # ret1, thresh1 = cv.threshold(img_grey, 127, 255, cv.THRESH_BINARY_INV) # show_img(thresh1) # # # Adaptive Mean # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2) # ret2, thresh2 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV) # show_img(thresh2) # # # Adaptive Gaussian # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2) # ret3, thresh3 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV) # show_img(thresh3) # Otsu's thresholding after Gaussian filtering blur = cv.GaussianBlur(img_grey, (5, 5), 0) ret4, img_otsu = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) ret4, thresh4 = cv.threshold(img_otsu, 127, 255, cv.THRESH_BINARY_INV) # show_img(thresh4) return thresh4
def goal_force(arr, sx, sy, dx, dy, d_star): # sx, sy :- source dx, dy:- destination d_star:- threshold distance from goal forcex = 0 forcey = 0 tau = 1 #constant printx('10') d = math.sqrt((dx-sx)*(dx-sx) + (dy-sy)*(dy-sy)) if d > d_star: forcex += ((d_star*tau*math.sin(math.atan2(dx-sx, dy-sy)))) forcey += ((d_star*tau*math.cos(math.atan2(dx-sx, dy-sy)))) else: forcex += ((dx-sx)*tau) forcey += ((dy-sy)*tau) printx('11') return (forcex, forcey)
def classify(img): cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img2 = cv2.medianBlur(cimg, 13) ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY) t2 = copy.copy(thresh1) x, y = thresh1.shape arr = np.zeros((x, y, 3), np.uint8) final_contours = [] image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #cv2.imshow('image', image) #k = cv2.waitKey(0) for i in range(len(contours)): cnt = contours[i] if cv2.contourArea(cnt) > 35000 and cv2.contourArea(cnt) < 15000: cv2.drawContours(img, [cnt], -1, [0, 255, 255]) cv2.fillConvexPoly(arr, cnt, [255, 255, 255]) final_contours.append(cnt) cv2.imshow('arr', arr) k = cv2.waitKey(0) return arr
def goal_force(arr, sx, sy, dx, dy, d_star): # sx, sy :- source dx, dy:- destination d_star:- threshold distance from goal forcex = 0 forcey = 0 tau = 1000000 #constant printx('10') d = math.sqrt((dx-sx)*(dx-sx) + (dy-sy)*(dy-sy)) if d > d_star: forcex += ((d_star*tau*math.sin(math.atan2(dx-sx, dy-sy)))) forcey += ((d_star*tau*math.cos(math.atan2(dx-sx, dy-sy)))) else: forcex += ((dx-sx)*tau) forcey += ((dy-sy)*tau) printx('11') return (forcex, forcey)
def classify(img): cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img2 = cv2.medianBlur(cimg, 13) ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY) t2 = copy.copy(thresh1) x, y = thresh1.shape arr = np.zeros((x, y, 3), np.uint8) final_contours = [] image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #cv2.imshow('image', image) #k = cv2.waitKey(0) for i in range(len(contours)): cnt = contours[i] if cv2.contourArea(cnt) > 3600 and cv2.contourArea(cnt) < 25000: cv2.drawContours(img, [cnt], -1, [0, 255, 255]) cv2.fillConvexPoly(arr, cnt, [255, 255, 255]) final_contours.append(cnt) cv2.imshow('arr', arr) k = cv2.waitKey(0) return arr
def goal_force(arr, sx, sy, dx, dy, d_star): # sx, sy :- source dx, dy:- destination d_star:- threshold distance from goal forcex = 0 forcey = 0 tau = 20 #constant printx('10') d = math.sqrt((dx-sx)*(dx-sx) + (dy-sy)*(dy-sy)) if d > d_star: forcex += ((d_star*tau*math.sin(math.atan2(dx-sx, dy-sy)))) forcey += ((d_star*tau*math.cos(math.atan2(dx-sx, dy-sy)))) else: forcex += ((dx-sx)*tau) forcey += ((dy-sy)*tau) printx('11') return (forcex, forcey)
def getNextWindow(temp_p_map, threshold): p = WinProp() loc = np.argmax(temp_p_map) p.y = loc / temp_p_map.shape[1] p.x = loc % temp_p_map.shape[1] p.val = temp_p_map[p.y,p.x] if p.val > threshold: p.have_max = True else: p.have_max = False return p # ================================================================================================
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 getEdges(gray,detector,min_thr=None,max_thr=None): """ Where detector in {1,2,3,4} 1: Laplacian 2: Sobelx 3: Sobely 4: Canny 5: Sobelx with possitive and negative slope (in 2 negative slopes are lost) """ if min_thr is None: min_thr = 100 max_thr = 200 if detector == 1: return cv2.Laplacian(gray,cv2.CV_64F) elif detector == 2: return cv2.Sobel(gray,cv2.CV_64F,1,0,ksize=-1) elif detector == 3: return cv2.Sobel(gray,cv2.CV_64F,0,1,ksize=-1) elif detector == 4: return cv2.Canny(gray,min_thr,max_thr) # Canny(min_thresh,max_thresh) (threshold not to the intensity but to the # intensity gradient -value that measures how different is a pixel to its neighbors-) elif detector == 5: sobelx64f = cv2.Sobel(gray,cv2.CV_64F,1,0,ksize=5) abs_sobel64f = np.absolute(sobelx64f) return np.uint8(abs_sobel64f)
def merge_recs(recs, threshold): filtered_recs = [] while len(recs) > 0: r = recs.pop(0) recs.sort(key=lambda rec: rec.distance(r)) merged = True while(merged): merged = False i = 0 for _ in range(len(recs)): if r.overlap(recs[i]) > threshold or recs[i].overlap(r) > threshold: r = r.merge(recs.pop(i)) merged = True elif recs[i].distance(r) > r.w/2 + recs[i].w/2: break else: i += 1 filtered_recs.append(r) return filtered_recs