def __test_safety_zone_pic(pic): hsv = cv2.cvtColor(pic.img, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, Tester.__LOWER, Tester.__UPPER) mask = cv2.erode(mask, None, iterations=2) mask = cv2.GaussianBlur(mask, (1, 1), 0) contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1:3] i = -1 for c in contours: i += 1 perimeter = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.03 * perimeter, True) if hierarchy[0][i][3] != -1 or cv2.contourArea( c) < 200 or not cv2.isContourConvex(approx): continue if len(approx) == 4: children = Tester.__get_children(hierarchy, i, contours) if len(children) > 0: bigchild = Tester.__biggest_child(children) cv2.drawContours(frame, [bigchild], -1, (0, 255, 0), 10) blurred = cv2.GaussianBlur(hsv, (5, 5), 0) centerpt = Tester.__center_of_contour(bigchild) if Tester.__is_point_red(centerpt, blurred): perimeterchild = cv2.arcLength(bigchild, True) approxchild = cv2.approxPolyDP(bigchild, 0.01 * perimeter, True) if len(approxchild) > 6: return Tester.SAFETY_ZONE_DETECTED, centerpt return Tester.NOTHING_DETECTED, (0, 0)
def trouverTresorParRobot(self, image, orientationRobot, orientationCamera, positionXRobot, positionYRobot, repereX, repereY, ratio, montrerImage = False, nomPhoto =""): tableY = 111 tableXMax = 56 tableXMin = 174 demiRobot = 10.5 cote = 0 TresorX = 0 TresorY = 0 distance = 5000 maximumPerimetreTresor = 300 minimumPerimetreTresor = 25 pourcentageDeContour = 0.075 demiTable = int((tableY/2) / ratio) XMaximumTable = int(repereX + (tableXMax / ratio)) XMinimumTable = int(repereX - (tableXMin / ratio)) tableContours = np.array([[XMaximumTable, repereY + demiTable], [XMaximumTable, repereY - demiTable], [XMinimumTable, repereY - demiTable], [XMaximumTable, repereY - demiTable], [XMinimumTable, repereY + demiTable], [XMinimumTable, repereY - demiTable], [XMinimumTable, repereY + demiTable], [XMaximumTable, repereY + demiTable]]) imageHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) imageCouleur = cv2.inRange(imageHSV, self.bas_jaune, self.haut_jaune) image = cv2.bitwise_and(image, image, mask=imageCouleur) imageGrise = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) photo, contours, hierarchie = cv2.findContours(imageGrise,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) for contour in contours: epsilon = pourcentageDeContour * cv2.arcLength(contour, True) perimetre = cv2.approxPolyDP(contour,epsilon, True) if minimumPerimetreTresor < cv2.arcLength(perimetre, True) < maximumPerimetreTresor and len(perimetre) < 7 and len(perimetre) >= 4: centreX, centreY = self.trouverCentre(contour) angleTresor = orientationRobot + 90 - orientationCamera + self.recupererAngle(centreX) x1 = positionXRobot + demiRobot / ratio * cos(radians(orientationRobot)) y1 = positionYRobot - demiRobot / ratio * sin(radians(orientationRobot)) x2 = self.recupererPositionXAvecAngle(angleTresor, x1) y2 = self.recupererPositionYAvecAngle(angleTresor, y1) coteTemp = 0 for tableCountour in range(1,4): seCroise, pointDeCroisement = self.trouverCroisement((x1,y1),(x2,y2), (tableContours[tableCountour * 2]), (tableContours[tableCountour * 2 + 1])) if seCroise == True: coteTemp = tableCountour break distanceRobotTresor = self.calculerDistanceDuRobot(pointDeCroisement, positionXRobot, positionYRobot) if distance > distanceRobotTresor: TresorX = pointDeCroisement[self.xIndex] TresorY = pointDeCroisement[self.yIndex] distance = distanceRobotTresor cote = coteTemp return (TresorX, TresorY), distance, cote
def trouverRectangle(self,image,montrerImage,basCouleur,hautCouleur): centreX = 0 centreY = 0 pourcentageDeContour = 0.03 imageHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) imageCouleur = cv2.inRange(imageHSV, basCouleur, hautCouleur) image = cv2.bitwise_and(image, image, mask=imageCouleur) imageGrise = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if (montrerImage): cv2.imshow("photo", image) cv2.waitKey(0) contours, hierarchie = cv2.findContours(imageGrise, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) for contour in contours: epsilon = pourcentageDeContour * cv2.arcLength(contour, True) perimetre = cv2.approxPolyDP(contour,epsilon, True) if self.minimumPerimetreRobot < cv2.arcLength(perimetre, True) < self.maximumPerimetreRobot: centreX, centreY = self.trouverCentre(contour) cv2.circle(image, (centreX, centreY), 5, (255, 255, 0), 5) if (montrerImage): cv2.imshow("photo", image) cv2.waitKey(0) return centreX,centreY
def drawContours_filterByPerimeter(img, contours, **params): """Summary Args: img (TYPE): Description contours (TYPE): Description **params (TYPE): Description Returns: TYPE: Description """ # minPerimeter = params.get("minPerimeter", 0) maxPerimeter = params.get("maxPerimeter", 10000) thickness = params.get("thickness", 1) useRandomColor = params.get("useRandomColor", True) # if useRandomColor: for i, contour in enumerate(contours): perimeter = cv2.arcLength(contour, True) if (perimeter >= minPerimeter) and (perimeter <= maxPerimeter): color_rand = np.random.randint(255, size=3) cv2.drawContours(img, contours, i, color_rand, thickness) else: color = params.get("color", (255, 255, 255)) for i, contour in enumerate(contours): perimeter = cv2.arcLength(contour, True) if (perimeter >= minPerimeter) and (perimeter <= maxPerimeter): cv2.drawContours(img, contours, i, color, thickness)
def aspect_circle(): im=cv2.imread('00011.bmp') im1= im.copy() im_gray= cv2.cvtColor(im1,cv2.COLOR_BGR2GRAY) im_gray2= im_gray.copy() gray_smooth = cv2.medianBlur(im_gray,5) thresh = np.zeros(im_gray.shape,np.uint8) gr_bool =(gray_smooth>100)&(gray_smooth<130) thresh[gr_bool]=255 contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) for h,cnt in enumerate(contours): area = cv2.contourArea(cnt) if area >32000: pass elif area >100: rect = cv2.minAreaRect(cnt) xy,hw,angle = enumerate(rect) box= cv2.cv.BoxPoints(rect) box= np.int0(box) cv2.drawContours(im1,[box],0,(0,255,0),1) x,y,w,h=cv2.boundingRect(cnt) cv2.rectangle(im1,(x,y),(x+w,y+h),(255,0,0),1) a= cv2.arcLength(cnt,True) print a approx = cv2.approxPolyDP(cnt,0.1*cv2.arcLength(cnt,True),True) print approx print xy plt.subplot(1,2,1),plt.imshow(im) plt.subplot(1,2,2),plt.imshow(im1) plt.show()
def encContour(i): ''' To returns the contour which encloses the whold body of the person in the image Inputs: i : name of the file Outputs: the contour which encloses the whole body ''' image=cv2.imread(i) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,21,8) x=np.zeros(image.shape,dtype=np.uint8) can=cv2.Canny(gray,120,200) closing=cv2.morphologyEx(can,cv2.MORPH_CLOSE,np.ones((8,8),np.uint8),iterations=2) contours,hei=cv2.findContours(closing.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) if len(contours) == 1 : return contours[0] else: k=0 t=contours[0] for i in contours : if cv2.arcLength(t,True) < cv2.arcLength(i,True): t=i return t
def __init__(self, array, epsilon=-1, resample=-1): try: self.points = np.copy(array) if epsilon > 0: self.points = cv2.approxPolyDP(self.points, epsilon, True) if resample > 0: pass self.moments = cv2.moments(self.points, False) zeroth = self.moments["m00"] self.x = self.moments["m10"] / zeroth self.y = self.moments["m01"] / zeroth self.area = cv2.contourArea(self.points) self.perim = cv2.arcLength(self.points,True) self.hull = cv2.convexHull(self.points) self.area_hull = cv2.contourArea(self.hull) self.perim_hull = cv2.arcLength(self.hull,True) rect = cv2.minAreaRect(self.points) box = cv.BoxPoints(rect) a = np.complex(box[0][0], box[0][1]) b = np.complex(box[1][0], box[1][1]) c = np.complex(box[2][0], box[2][1]) self.w, self.h = sorted([np.abs(a-b), np.abs(c-b)]) self.is_valid = True except ZeroDivisionError: self.area = 0 self.perim = 0 self.hull = 0 self.area_hull = 0 self.perim_hull = 0 self.w, self.h = 0,0 self.is_valid = False
def filter_cnts(cnt_list): global using_VGA useful_cnts = list() # uses area and length to filter for cnt in cnt_list: if not using_VGA: if MIN_AREA < cv2.contourArea(cnt) < MAX_AREA: if MIN_CNT_LENGTH < cv2.arcLength(cnt, 1) < MAX_CNT_LENGTH: useful_cnts.append(cnt) else: # print("Wrong Lenght " + str(MIN_CNT_LENGTH) + " < " + str(cv2.arcLength(cnt, 1)) + # " < " + str(MAX_CNT_LENGTH)) continue else: # print ("Wrong Area: " + str(MIN_AREA) + " < " + str(cv2.contourArea(cnt)) + " < " + str(MAX_AREA)) continue else: if MIN_AREA / 2 < cv2.contourArea(cnt) < MAX_AREA / 2: if MIN_CNT_LENGTH / 1.4 < cv2.arcLength(cnt, 1) < MAX_CNT_LENGTH / 1.4: useful_cnts.append(cnt) else: print("Wrong Lenght " + str(MIN_CNT_LENGTH / 1.4) + " < " + str(cv2.arcLength(cnt, 1)) + " < " + str(MAX_CNT_LENGTH / 1.4)) continue else: print ("Wrong Area: " + str(MIN_AREA / 2) + " < " + str(cv2.contourArea(cnt)) + " < " + str(MAX_AREA / 2)) continue return useful_cnts
def canny(): can = process(snap())[-2] contours, hierarchy = cv2.findContours(can,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) longest = None longest_len = 0 for cont in contours: l = cv2.arcLength(cont,True) if l > longest_len: longest = cont longest_len = l hull = cv2.convexHull(longest) approx = cv2.approxPolyDP(longest,0.01*cv2.arcLength(longest,True),True) rect = cv2.minAreaRect(longest) box = cv2.cv.BoxPoints(rect) box = np.int0(box) ellipse = cv2.fitEllipse(longest) print ellipse orig = snap() cv2.ellipse(orig,ellipse,(0,255,0),2) # cv2.drawContours(orig,[box],-1,(0,0,255),2) # cv2.drawContours(orig, [longest], -1, (0,0,255), 2) # cv2.drawContours(orig, [hull], -1, (0,255,0), 2) # cv2.drawContours(orig, [approx], -1, (255,0,0), 2) cv2.imshow("l", orig) raw_input("t") cv2.destroyAllWindows()
def is_square(contour): peri = cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, 0.02*peri, True) #if contour has 4 points, if len(approx) >= 4 and len(approx) <= 6: (x, y, w, h) = cv2.boundingRect(approx) aspectRatio = w/ float(h) # find areas area = cv2.contourArea(contour) hullArea = cv2.contourArea(cv2.convexHull(contour)) solidity = area / float(hullArea) keepSolidity = solidity > 0.9 keepAspectRatio = (aspectRatio >= 0.8 and aspectRatio <= 1.2 ) keepSize = cv2.arcLength(contour, True) > 60 # Check that it has children. (-1 if none) # form: [next, previous, child, parent] if keepSolidity and keepAspectRatio and keepSize: return True else: return False
def find_squares(imgOrig): img = cv2.GaussianBlur(imgOrig, (5, 5), 0) squares = [] for gray in cv2.split(img): for thrs in xrange(0, 255, 20): if thrs == 0: bin = cv2.Canny(gray, 25, 350, apertureSize=3) kernel = np.ones((3,3), np.uint8) bin = cv2.dilate(bin, kernel) #bin = cv2.erode(bin, kernel) #Imagen(bin).show() else: retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY) #Imagen(bin).show() contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: #contornos cerrados cnt_len = cv2.arcLength(cnt, True) epsilon = 0.025*cv2.arcLength(cnt,True) cnt = cv2.approxPolyDP(cnt, epsilon, True) if len(cnt) == 4 and cv2.contourArea(cnt) > 50 and cv2.contourArea(cnt) <1700 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.5 and colorCorrecto(imgOrig,cnt) and proporcionesCorrectas(cv2.minAreaRect(cnt))): squares.append(cnt) return squares
def encContour(i): ''' inputs: i: name of the image in jpg format output: enclosing contour ''' image=cv2.imread(i) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,8) can=cv2.Canny(gray,120,200) closing=cv2.morphologyEx(can,cv2.MORPH_CLOSE,np.ones((3,3),np.uint8),iterations=1) contours,hei=cv2.findContours(closing.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) #x=np.zeros(closing.shape,dtype=np.uint8) #cv2.drawContours(x,contours,0,(255),2) #cv2.imwrite('TEST.jpg',x) if len(contours) == 1 : return contours[0] else: k=0 t=contours[0] for i in contours : if cv2.arcLength(t,True) < cv2.arcLength(i,True): t=i return t
def trouverRectangle(self, image, montrerImage, basCouleur, hautCouleur, ratio): centreX = 0 centreY = 0 pourcentageDeContour = 0.03 imageHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) imageCouleur = cv2.inRange(imageHSV, basCouleur, hautCouleur) image = cv2.bitwise_and(image, image, mask=imageCouleur) imageGrise = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if (montrerImage): cv2.imshow("photo", image) cv2.waitKey(0) contours, hierarchie = cv2.findContours(imageGrise, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) for contour in contours: epsilon = pourcentageDeContour * cv2.arcLength(contour, True) perimetre = cv2.approxPolyDP(contour,epsilon, True) if self.minimumPerimetreRobot < cv2.arcLength(perimetre, True) < self.maximumPerimetreRobot: centreX, centreY = self.trouverCentre(contour) cv2.circle(image, (centreX, centreY), 5, (255, 255, 0), 5) if (montrerImage): cv2.imshow("photo", image) cv2.waitKey(0) angle = (centreX - self.centreCameraX) / self.centreCameraX * self.angleDemiCamera baseRobot = tan(radians(angle)) * self.hauteurRobot / ratio positionXReel = centreX - baseRobot angle = (centreY - self.centreCameraY) / self.centreCameraY * self.angleDemiCamera baseRobot = tan(radians(angle)) * self.hauteurRobot / ratio positionYReel = centreY - baseRobot return positionXReel,positionYReel
def longestContour(contours): cnt = contours[0] maxlength = 0 for contour in contours: if cv2.arcLength(contour, True) > maxlength: maxlength = cv2.arcLength(contour, True) cnt = contour return cnt
def compute_shape_metrics(labels): reader = csv.reader(open(labels)) results = None for row in reader: if row[0] == "IMG": continue name = row[0] if (row[1] == " metal_lines" or row[1]==" metal_tree"): continue buf = 10 x = int(row[2]) y = int(row[3]) x_size = int(row[4])/2 + buf y_size = int(row[5])/2 + buf # Load Images img = cv2.imread(name+".jpg") # Color Space Conversion lab = cv2.cvtColor(img,cv2.COLOR_BGR2LAB) win = img[y-y_size:y+y_size,x-x_size:x+x_size] lab_win = lab[y-y_size:y+y_size,x-x_size:x+x_size] mid = (float(np.max(lab_win[...,1]))+float(np.min(lab_win[...,1])))/2. thresh = cv2.threshold(lab_win[...,1],mid,255,cv2.THRESH_BINARY)[1] # Contours contours, hier = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) largest_area = 0 largest_contour = 0 for i in contours: area = cv2.contourArea(i) if area > largest_area: largest_area = area largest_contour = i # Hulls hull = cv2.convexHull(largest_contour,returnPoints=False) hull_pts = cv2.convexHull(largest_contour) # Perimeter Difference perimeter_ratio = cv2.arcLength(hull_pts,True)/cv2.arcLength(largest_contour,True) # Area Difference area_ratio = cv2.contourArea(largest_contour)/cv2.contourArea(hull_pts) # Max Convexity Defect defects = cv2.convexityDefects(largest_contour,hull) depth = np.max(defects[...,-1])/256.0 rect = cv2.boundingRect(hull_pts) defect_ratio = depth/np.max(rect[2:]) # Combined Score print perimeter_ratio,area_ratio,defect_ratio,np.sum(defects[...,-1]),row[-1],row[1] #print perimeter_ratio,area_ratio,np.max(defects[...,-1]),np.sum(defects[...,-1]),row[-1],row[1] if results == None: results = np.array([perimeter_ratio,area_ratio,defect_ratio,]) #results = np.array([perimeter_ratio,area_ratio,defect_ratio,np.sum(defects[...,-1])]) #results = np.array([perimeter_ratio,area_ratio,np.max(defects[...,-1]),np.sum(defects[...,-1])]) else: results = np.vstack((results,np.array([perimeter_ratio,area_ratio,defect_ratio]))) #results = np.vstack((results,np.array([perimeter_ratio,area_ratio,defect_ratio,np.sum(defects[...,-1])]))) #results = np.vstack((results,np.array([perimeter_ratio,area_ratio,np.max(defects[...,-1]),np.sum(defects[...,-1])]))) return results
def get_cube_upright(): # Uses the depth image to only take the part of the image corresponding to the closest point and a bit further global depth_img_avg global img_bgr8_clean closest_pnt = np.amin(depth_img_avg) # resize the depth image so it matches the color one depth_img_avg = cv2.resize(depth_img_avg, (1280, 960)) # generate a mask with the closest points img_detection = np.where(depth_img_avg < closest_pnt + val_depth_capture, depth_img_avg, 0) # put all the pixels greater than 0 to 255 ret, mask = cv2.threshold(img_detection, 0.0, 255, cv2.THRESH_BINARY) # convert to 8-bit mask = np.array(mask, dtype=np.uint8) im2, contours, hierarchy = cv2.findContours(mask, 1, 2, offset=(0, -6)) useful_cnts = list() uprightrects = list() img_bgr8_clean_copy = img_bgr8_clean.copy() for cnt in contours: if 9000 < cv2.contourArea(cnt) < 15000: if 420 < cv2.arcLength(cnt, 1) < 560: useful_cnts.append(cnt) else: print("Wrong Lenght 450 < " + str(cv2.arcLength(cnt, 1)) + str(" < 570")) else: print ("Wrong Area: 9000 < " + str(cv2.contourArea(cnt)) + " < 15000") for index, cnts in enumerate(useful_cnts): min_area_rect = cv2.minAreaRect(cnts) # minimum area rectangle that encloses the contour cnt (center, size, angle) = cv2.minAreaRect(cnts) width, height = size[0], size[1] if not (0.7*height < width < 1.3*height): print("Wrong Height/Width: " + str(0.7*height) + " < " + str(width) + " < " + str(1.3*height)) continue points = cv2.boxPoints(min_area_rect) # Find four vertices of rectangle from above rect points = np.int32(np.around(points)) # Round the values and make it integers cv2.drawContours(img_bgr8_clean_copy, [points], 0, (0, 0, 255), 2) cv2.drawContours(img_bgr8_clean_copy, cnts, -1, (255, 0, 255), 2) cv2.waitKey(1) # if we rotate more than 90 degrees, the width becomes height and vice-versa if angle < -45.0: angle += 90.0 width, height = size[0], size[1] size = (height, width) rot_matrix = cv2.getRotationMatrix2D(center, angle, 1.0) # rotate the entire image around the center of the parking cell by the # angle of the rotated rect imgwidth, imgheight = (img_bgr8_clean.shape[0], img_bgr8_clean.shape[1]) rotated = cv2.warpAffine(img_bgr8_clean, rot_matrix, (imgheight, imgwidth), flags=cv2.INTER_CUBIC) # extract the rect after rotation has been done sizeint = (np.int32(size[0]), np.int32(size[1])) uprightrect = cv2.getRectSubPix(rotated, sizeint, center) uprightrects.append(uprightrect) uprightrect_copy = uprightrect.copy() cv2.drawContours(uprightrect_copy, [points], 0, (0, 0, 255), 2) cv2.imshow('uprightRect ' + str(index), uprightrect_copy) cv2.imshow('RBG', img_bgr8_clean_copy) cv2.waitKey(1) objects_detector(uprightrects)
def _squares(rawimg): """Convert given image to gray and split with adaptove thresholding to detect edges. Filter out contours that are square like and return them along with their hierarchy. :rawimg: cv2 image :returns: list of edges, list of hierarchy """ #Downscale and rescale to remove noise frame=cv2.pyrDown(rawimg) #lose colors gray=cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY) frame=cv2.pyrUp(gray) #Use Otsu threshold to make binary #ret,otsu=cv2.threshold(frame,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) #Use adaptive threshold to make binary adaptive=cv2.adaptiveThreshold(frame,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,3) cv2.adaptiveThreshold #find the edges edges=cv2.Canny(adaptive,0,255) #cv2.imshow("adaptive",adaptive) #cv2.imshow("gray",gray) #cv2.imshow("edges",edges) #Approximate the contours into polygons contours,hierarchy=cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) squares=[] hiersq=[] for cnt,hier in zip(contours,hierarchy[0]): approx=cv2.approxPolyDP(cnt,0.05*cv2.arcLength(cnt,True),True) if len(approx)!=4: continue elif cv2.contourArea(approx)<200: continue elif not cv2.isContourConvex(approx): continue length=cv2.arcLength(approx,True) area=cv2.contourArea(approx) area2=(length/4)*(length/4) if area/area2<0.9: continue squares.append(approx) hiersq.append(hier) return squares,hiersq
def __get_grid_for_table__(self,directory,region): """ directory - contains a set of aligned images extract the grid for a given region/table the region/table is specified by min_x,max_x,min_y,max_y :return: """ assert region[0]<region[1] assert region[2]<region[3] # todo - refactor!! horizontal_lines = [] vertical_lines = [] # extract all horizontal lines horizontal_contours = self.__get_contour_lines_over_image__(directory,True) # useful for when you want to draw out the image - just for debugging mask = np.zeros((3744,5616),dtype=np.uint8) delta = 50 for cnt in horizontal_contours: shape = cnt.shape cnt = cnt.reshape((shape[0],shape[2])) max_x,max_y = np.max(cnt,axis=0) min_x,min_y = np.min(cnt,axis=0) if (min_y>=region[2]-delta) and (max_y<=region[3]+delta): # sanity check - if this an actual grid line - or just a blip? perimeter = cv2.arcLength(cnt,True) if perimeter > 100: horizontal_lines.append(cnt) # cv2.drawContours(mask,[cnt],0,255,1) horizontal_lines.sort(key = lambda l:l[0][1]) vertical_contours = self.__get_contour_lines_over_image__(directory,False) delta = 400 for cnt in vertical_contours: shape = cnt.shape cnt = cnt.reshape((shape[0],shape[2])) max_x,max_y = np.max(cnt,axis=0) min_x,min_y = np.min(cnt,axis=0) interior_line = (min_x >= region[0]-100) and (max_x <= region[1]+100)and(min_y>=region[2]-delta) and (max_y<=region[3]+delta) through_line = (min_x >= region[0]-100) and (max_x <= region[1]+100) and (min_y < region[2]) and(max_y > region[3]) if interior_line or through_line: perimeter = cv2.arcLength(cnt,True) if perimeter > 1000: vertical_lines.append(cnt) vertical_lines.sort(key = lambda l:l[0][0]) cv2.drawContours(mask,vertical_lines,0,255,-1) cv2.drawContours(mask,vertical_lines,1,255,-1) return horizontal_lines,vertical_lines
def isEqualEdges(self): arbitraryEdge = np.array([self.contour[0], self.contour[1]]) arbitraryNormalLength = cv2.arcLength(arbitraryEdge, False) for corner in range(1 , len(self.contour) - 1): length = cv2.arcLength(np.array([self.contour[corner], self.contour[corner + 1]]), False) if abs(length - arbitraryNormalLength) > 10: return False return True
def get_filtered_contours(self,img, contour_type): hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) if contour_type == "CONE": frame_threshed = cv2.inRange(hsv_img, self.CONE[0], self.CONE[1]) ret,thresh = cv2.threshold(frame_threshed,22,255,0) elif contour_type == "DUCK_COLOR": frame_threshed = cv2.inRange(hsv_img, self.DUCK[0], self.DUCK[1]) ret,thresh = cv2.threshold(frame_threshed,30,255,0) elif contour_type == "DUCK_CANNY": frame_threshed = cv2.inRange(hsv_img, self.DUCK[0], self.DUCK[1]) frame_threshed = cv2.adaptiveThreshold(frame_threshed,255,\ cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,5,2) thresh = cv2.Canny(frame_threshed, 100,200) else: return filtered_contours = [] contours, hierarchy = cv2.findContours(\ thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) contour_area = [ (cv2.contourArea(c), (c) ) for c in contours] contour_area = sorted(contour_area,reverse=True, key=lambda x: x[0]) height,width = img.shape[:2] for (area,(cnt)) in contour_area: # plot box around contour x,y,w,h = cv2.boundingRect(cnt) box = (x,y,w,h) d = 0.5*(x-width/2)**2 + (y-height)**2 if not(h>15 and w >10 and h<200 and w<200 and d < 120000): continue if contour_type == "DUCK_CANNY": continue if contour_type =="DUCK_COLOR": # extra filtering to remove lines if not(h>25 and w>25): continue if d>90000: if not(h>35 and w>35): continue if cv2.contourArea(cnt)==0: continue val = cv2.arcLength(cnt,True)**2/ cv2.contourArea(cnt) if val > 35: continue rect = cv2.minAreaRect(cnt) ctr, sides, deg = rect val = 0.5*cv2.arcLength(cnt,True) / (w**2+h**2)**0.5 if val < 1.12: continue #if area > 1000: continue mask = np.zeros(thresh.shape,np.uint8) cv2.drawContours(mask,[cnt],0,255,-1) mean_val = cv2.mean(img,mask = mask) aspect_ratio = float(w)/h filtered_contours.append( (cnt, box, d, aspect_ratio, mean_val) ) return filtered_contours
def get_mask(character, border_limit): mask = np.zeros(character.shape, np.uint8) upper = character[0:22, 0:128] # upper_rgb = character_rgb[0:22, 0:128] lower = character[42:64, 0:128] # lower_rgb = character_rgb[42:64, 0:128] ret, thresh_upper = cv2.threshold(upper, 127, 255, 0) contours_upper, hierarchy = cv2.findContours(thresh_upper,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) ret, thresh_lower = cv2.threshold(lower, 127, 255, 0) contours_lower, hierarchy = cv2.findContours(thresh_lower,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours_upper: # Height coordinates of the contour val = 128 for cn in cnt: if cn[:, 1][0] < val: val = cn[:, 1][0] if val> (22-border_limit): if cv2.contourArea(cnt) < 20 or 0 < cv2.arcLength(cnt, False) < 15: # cv2.drawContours(upper_rgb, [cnt], 0, (0, 255, 0), -1) cv2.drawContours(mask, [cnt], 0, 255, -1) for cnt in contours_lower: # Height coordinates of the contour val1 = 0 for cn in cnt: if cn[:, 1][0] > val1: val1 = cn[:, 1][0] if val1 < border_limit: if cv2.contourArea(cnt)<20 or 0<cv2.arcLength(cnt, False)<15: # cv2.drawContours(lower_rgb,[cnt],0,(0,255,0),-1) cv2.drawContours(mask[42:64, 0:128],[cnt],0,255,-1) character = cv2.bitwise_xor(character, mask) # cv2.imshow("char", character) # # cv2.imshow("upper", lower) # # # cv2.imshow("mask", mask1) # cv2.imwrite("C:/Users/Naleen/Desktop/New folder (2)/character.jpg", character) # # cv2.imwrite("C:/Users/Naleen/Desktop/New folder (2)/lower.jpg", lower) # # cv2.imwrite("C:/Users/Naleen/Desktop/New folder (2)/mask.jpg", mask) # # # cv2.waitKey(0) return character
def findContours(request): im = cv2.imread('flower2.jpg') initial_mean_color = cv2.mean(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) #cv2.drawContours(im, contours, -1, (123,255,31), 3) # img = cv2.imread('octa.png') # ret,thresh = cv2.threshold(img,127,255,0) # contours,hierarchy = cv2.findContours(thresh, 1, 2) cnt = contours[0] M = cv2.moments(cnt) #centroids cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) #contour area area = cv2.contourArea(cnt) #contour perimeter perimeter = cv2.arcLength(cnt,True) #contour approximation epsilon = .01*cv2.arcLength(cnt,True) approx = cv2.approxPolyDP(cnt,epsilon,True).tolist() #convex hull hull = cv2.convexHull(cnt).tolist() #convexity isContourConvex = cv2.isContourConvex(cnt) jsonData = { "centroid":{ "cx":cx, "cy":cy }, "area":area, "perimeter":perimeter, "approx":approx, "approx-length":len(approx), "hull":hull, "hull-length":len(hull), "is-convour-convx":isContourConvex, "initial_mean_color":initial_mean_color } return JsonResponse(jsonData)
def findLongest(contours): maxsize = 0 count = 0 best = 0 for cnt in contours: if cv2.arcLength(cnt, False) > maxsize: maxsize = cv2.arcLength(cnt,False) best = count print(best) count = count + 1 return best
def find_centroids(image): h, w = image.shape[:2] morse, cnts, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) morse_cnts = [] morse_cent = [] for c in cnts: if (cv2.arcLength(c, 1) > 30) and (cv2.arcLength(c, 1) < ((2 * h + 2 * w) * 0.5)): morse_cnts.append(c) M = cv2.moments(c) morse_cent.append([int(M['m10'] / M['m00']), int(M['m01'] / M['m00'])]) # create list of centroids return morse_cent, morse_cnts
def play(img): gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,127,255,0) contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) grid1_number=[] grid1_number_position=[] grid2_number=[] grid2_number_position=[] for counter in range (len(contours)): if cv2.contourArea(contours[counter])>800 and cv2.contourArea(contours[counter])<2000: cv2.drawContours(img,contours,counter,(0,255,0),2) cv2.arcLength(contours[counter],True) M = cv2.moments(contours[counter]) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) area=cv2.contourArea(contours[counter]) perimeter=cv2.arcLength(contours[counter],True) # print "Area = ", area, "Centroid = ", cx, ", ", cy, "Perimeter = ", perimeter ,"Number", number(area,perimeter) # cv2.circle(img,(cx,cy), 5, (0,0,255), -1) found_number=number(area,perimeter) if cx < 300: grid1_number.append(found_number) grid1_number_position.append(cy*10+cx) else: grid2_number.append(found_number) grid2_number_position.append(cy*10+cx) for i in range( len( grid1_number_position ) ): least = i for k in range( i + 1 , len( grid1_number_position ) ): if grid1_number_position[k] < grid1_number_position[least]: least = k tmp=grid1_number_position[least] grid1_number_position[least]=grid1_number_position[i] grid1_number_position[i]=tmp tmp1=grid1_number[least] grid1_number[least]=grid1_number[i] grid1_number[i]=tmp1 for i in range( len( grid2_number_position ) ): least = i for k in range( i + 1 , len( grid2_number_position ) ): if grid2_number_position[k] < grid2_number_position[least]: least = k tmp=grid2_number_position[least] grid2_number_position[least]=grid2_number_position[i] grid2_number_position[i]=tmp tmp1=grid2_number[least] grid2_number[least]=grid2_number[i] grid2_number[i]=tmp1 matrix=[] for i in range( 1,len( grid2_number ),2 ): two_digit_number=(grid2_number[i-1]*10)+grid2_number[i] matrix.append([two_digit_number,cell_number((grid2_number_position[i-1]+grid2_number_position[i])/2)]) return grid1_number,matrix
def detect_breaks(self): _,thr = cv2.threshold(self.gray,214,255,cv2.THRESH_BINARY) _,con,_ = cv2.findContours(thr,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) squares=[c for c in con if self._contour_is_square(c) and cv2.contourArea(c)>=16] if not squares: return ([],[]) a=statistics.median(cv2.contourArea(c) for c in squares) p=statistics.median(cv2.arcLength(c,True) for c in squares) blanks=[c for c in con if abs(cv2.contourArea(c)-a)<.1*a and abs(cv2.arcLength(c,True)-p)<.05*p] dist=int(p/8) yc = sorted({c[0,1] for s in blanks for c in s}) xc = sorted({c[0,0] for s in blanks for c in s}) return (self._squares_to_breaks(yc,dist),self._squares_to_breaks(xc,dist))
def blood_vessels(image): retina = copy.copy(image); retina_blue , retina_green , retina_red = cv2.split(retina); clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) retina_green = clahe.apply(retina_green) kernel = np.ones((10,10),np.uint8) blackhat = cv2.morphologyEx(retina_green, cv2.MORPH_BLACKHAT, kernel) #blackhat = cv2.GaussianBlur(blackhat,(15,15),0) blackhat = cv2.medianBlur(blackhat,5) blackhat = cv2.equalizeHist(blackhat) ret, output_green = cv2.threshold(blackhat,blackhat.max()-10,255,cv2.THRESH_BINARY); blood_vessel = cv2.medianBlur(output_green,5) vessels = copy.copy(blood_vessel) mask1 = np.ones(retina_green.shape, dtype="uint8") * 255 img,contours,hierarchy = cv2.findContours(blood_vessel, 1, 2) for items in contours: area = cv2.contourArea(items) if (area < 10): cv2.drawContours(mask1, [items], -1, 0, -1) vessel = cv2.bitwise_and(vessels,vessels, mask=mask1); blood_clot = copy.copy(vessel) mask2 = np.ones(retina_green.shape, dtype="uint8") * 255 img,contours,hierarchy = cv2.findContours(vessel, 1, 2) for items in contours: area = cv2.contourArea(items) perimeter = cv2.arcLength(items,True) if perimeter !=0: R= 4*np.pi*area/math.pow(perimeter,2); if R>0.5: cv2.drawContours(mask2, [items], -1, 0, -1) blood_vessel = cv2.bitwise_and(blood_clot,blood_clot, mask=mask2); kernel = np.ones((5,5),np.uint8) blood_vessel = cv2.morphologyEx(blood_vessel, cv2.MORPH_CLOSE, kernel) hemo = cv2.bitwise_not(mask2); img,contours,hierarchy = cv2.findContours(blood_vessel, 1, 2) thickness = []; number_vessel = len(contours); total_area = 0; for items in contours: area = cv2.contourArea(items) total_area = total_area + area; perimeter = cv2.arcLength(items,True) if(perimeter !=0) : thickness.append(area/perimeter); if(len(thickness)!=0): max_vessel = np.asarray(thickness).max(); else: max_vessel = 0; mean = np.average(np.asarray(thickness)); return (mean,max_vessel,number_vessel,total_area);
def analyseParticles(connectedParticles, binary, newlabels, numberOfParticle): """ Calculates the solid fraction and the specific surface. """ # count pixel per particle sizespx0 = ndimage.sum(binary, newlabels, range(numberOfParticle)) sizespx = sizespx0[sizespx0 != 0] # get shape factor of particles fcirc = np.zeros(numberOfParticle) for i in range(1,numberOfParticle): actParticle = (newlabels == i).astype(np.uint8) actParticle *= 255 new = np.zeros((actParticle.shape[0],actParticle.shape[1],3), dtype=np.uint8) new[:,:,1] = actParticle helper = cv2.cvtColor(new, cv2.COLOR_RGB2GRAY) helper[helper > 0] = 255 helper = cv2.GaussianBlur(helper,(5,5),0) helper = cv2.Canny(helper, 10, 200) contours, hierarchy = cv2.findContours(helper, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) arclength = cv2.arcLength(contours[0],True) # contours[0] because there is only 1 contour area = sizespx0[i] fcirc[i] = (4. * np.pi * area) / arclength**2 # conversion factor between pixel and µm² pxArea = (_CONVERSIONFACTOR_FOR_PIXEL) ** 2 realSize = np.sum(sizespx) fs = realSize * 100. / (binary.shape[0] * binary.shape[1]) # determine perimeter perimeter = 0. for i in range(connectedParticles.max()+1): actParticle = (connectedParticles == i).astype(np.uint8) actParticle *= 255 new = np.zeros((actParticle.shape[0],actParticle.shape[1],3), dtype=np.uint8) new[:,:,1] = actParticle helper = cv2.cvtColor(new, cv2.COLOR_RGB2GRAY) helper[helper > 0] = 255 helper = cv2.GaussianBlur(helper,(5,5),0) helper = cv2.Canny(helper, 10, 200) contours, hierarchy = cv2.findContours(helper, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) perimeter += cv2.arcLength(contours[0],True) # contours[0] because there is only 1 contour so = (perimeter * _CONVERSIONFACTOR_FOR_PIXEL)/(realSize * pxArea) return fs, so, sizespx * pxArea, fcirc
def findRectangles(cnts): new_cnts = [] i = 0 for cnt in cnts: eps = 0.12*cv2.arcLength(cnt,True) cnt = cv2.approxPolyDP(cnt, eps, True) if cv2.isContourConvex(cnt) and cv2.arcLength(cnt,True) > 25 and cv2.contourArea(cnt) > 50 and cv2.contourArea(cnt) < 300000 and len(cnt) == 4: new_cnts.append(cnt) #print(i) i = i+1 #print(cv2.arcLength(cnt,True)) #print(cv2.contourArea(cnt)) return new_cnts
def evaluate(image): #copy the image as to not destroy it #image = image.copy() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (11, 11), 0) #cv2.imshow("Image", image) # The first thing we are going to do is apply edge detection to # the image to reveal the outlines of the coins edged = cv2.Canny(blurred, 30, 150) #cv2.imshow("Edges", edged) # Find contours in the edged image. (_,cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #sort them largest to smallest cnts = sorted(cnts, key = cv2.contourArea, reverse = True) #calculate the peremiter #if there are no visible contours in the image, return a fitness of 0 if len(cnts) == 0: return 0 perimeter = cv2.arcLength(cnts[0], True) #find the convexHull cnt = cnts[0] hull = cv2.convexHull(cnt,returnPoints = False) hull_for_len = cv2.convexHull(cnt,returnPoints = True) defects = cv2.convexityDefects(cnt,hull) #calculate the length of the convexHull convexHull = cv2.arcLength(hull_for_len, True) #Draw Convex Hull on image for i in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) cv2.line(image,start,end,[255,0,0],2) #Draw Perimeter on image cv2.drawContours(image, cnts, 0, (0, 255, 0), 2) #calulate fitness fitness = perimeter / convexHull #Return (fitness,drawnImage) return (fitness,image)
def create_coco_dataset(set: str, min_area_percentage: float = 0.05): """Returns a tf dataset with (image_file_name, corners) Args: set (str): 'train' or 'val """ if set != 'train' and set != 'val': print("Error: Invalid dataset requested") exit dataDir = 'd:/datasets/coco' dataType = '{}2017'.format(set) annDir = '{}/annotations'.format(dataDir) annFile = '{}/instances_{}.json'.format(annDir, dataType) print("Will use annotations in " + annFile) coco = COCO(annFile) category_ids = coco.getCatIds(catNms=['tv']) img_ids = coco.getImgIds(catIds=category_ids) # Only use those annotations with only four keypoints ann_ids = coco.getAnnIds(imgIds=img_ids, catIds=category_ids, iscrowd=None) annotations = coco.loadAnns(ann_ids) image_files = [] labels = [] for ann in annotations: # Filter out images containing multiple TVs annsForThisImage = coco.loadAnns( coco.getAnnIds(imgIds=[ann['image_id']], catIds=category_ids)) if len(annsForThisImage) > 1: continue mask = coco.annToMask(ann) # Create a polygon from this binary mask contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contour = contours[0] # Estimate a polygon for the contour epsilon = 0.05 * cv2.arcLength(contour, True) estimated_polygon = cv2.approxPolyDP(contour, epsilon, True) num_points = estimated_polygon.shape[0] if num_points == 4: coco_img = coco.loadImgs(ann['image_id'])[0] if cv2.contourArea( estimated_polygon ) >= min_area_percentage * coco_img['width'] * coco_img['height']: image_file = '{}/images/{}/{}'.format(dataDir, dataType, coco_img['file_name']) image_files.append(image_file) labels.append(get_corner_points(estimated_polygon)) # color = (0,255,0) # # For debugging: load image, draw contours and show it # img = coco.loadImgs(ann['image_id'])[0] # image = cv2.imread('{}/images/{}/{}'.format(dataDir, dataType, img['file_name'])) # cv2.drawContours(image, [estimated_polygon], 0, color, 3) # cv2.imshow('Contours overlay', image) # cv2.waitKey(1) files_tensor = tf.constant(image_files) labels_tensor = tf.constant(labels) dataset = tf.data.Dataset.from_tensor_slices((files_tensor, labels_tensor)) print("Size of Dataset: {} images".format(len(dataset))) for element in dataset.as_numpy_iterator(): print(element) break return dataset
# 3.图像的矩 M = cv2.moments(cnt) # print(M) # 重心/质心/形心: cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) print(cx, cy) # 4.面积 area = cv2.contourArea(cnt) # 或者 # area = M['m00'] print(area) # 5.周长 perimeter = cv2.arcLength(cnt, True) print(perimeter) # 6.(最小)外接矩 # 外接矩:不旋转,返回左上角点、宽高度 x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(img_origin, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imshow('rectangle', img) # 最小外接矩:有旋转,返回box对象:box与rectangle的区别就在于box有旋转角 rect = cv2.minAreaRect(cnt) box = cv2.boxPoints(rect) # np.int0:矩阵全部转换为int类型,np.int会出错 box = np.int0(box) # 或者用下面的语句转换 # box = box.astype(np.int32) img = cv2.drawContours(img_origin, [box], 0, (0, 0, 255), 2)
# find contours in the mask and initialize the current (x, y) center of the ball cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) center = None cntr = ([[-1], [-1]]) # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use it to compute the minimum enclosing circle and centroid # detect near-circles in the hsv filtered image contour_list = [] circularity_list = [] for contour in cnts: epsilon = 0.2 * cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, epsilon, True) area = cv2.contourArea(contour) # Filter based on length and area if (1 < len(approx) < 1000) & (upper_area > area > lower_area): contour_list.append(contour) area = cv2.contourArea(contour) perimeter = cv2.arcLength(contour, True) circularity = (4 * np.pi * area) / (perimeter**2) circularity_list.append(circularity) cv2.drawContours(frame, contour_list, -1, (255, 0, 0), 2) # if picking based on circularity if contour_list != []:
def linetrace(data): global angular, stage, delay, T_finish, timer, jucha_start, count, obstacle, tunnel_count, hope i = 0 R_deg = [] L_deg = [] np_arr = np.fromstring(data.data, np.uint8) frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) left_eye = frame[0:360, 0:320] right_eye = frame[0:360, 320:640] upper_right_eye = frame[0:90, 480:640] left_hsv = hsv[0:360, 0:320] right_hsv = hsv[0:360, 320:640] upper_right_hsv = hsv[0:90, 480:640] ROI = frame[270:360, 0:640] left_ROI = ROI[:, :ROI.shape[1] / 2] right_ROI = ROI[:, ROI.shape[1] / 2:] gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) median = cv2.medianBlur(gray, 3) gray_blurred = cv2.GaussianBlur(median, (3, 3), 0) ret, thresh = cv2.threshold(gray_blurred, 210, 255, cv2.THRESH_BINARY) #ret,thresh = cv2.threshold(gray_blurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) #thresh = cv2.adaptiveThreshold(gray_blurred,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) roi = thresh[270:360, 0:640] edge = cv2.Canny(roi, 180, 360) left_edge = edge[:, :edge.shape[1] / 2] right_edge = edge[:, edge.shape[1] / 2:] if stage != 3: L_lines = cv2.HoughLines(left_edge, 1, np.pi / 180, 60) R_lines = cv2.HoughLines(right_edge, 1, np.pi / 180, 60) if stage == 3: L_lines = cv2.HoughLines(left_edge, 1, np.pi / 180, 70) R_lines = cv2.HoughLines(right_edge, 1, np.pi / 180, 70) if L_lines is not None: L_lines = [l[0] for l in L_lines] for rho, theta in L_lines: a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho l_x1 = int(x0 + 1000 * (-b)) l_y1 = int(y0 + 1000 * (a)) l_x2 = int(x0 - 1000 * (-b)) l_y2 = int(y0 - 1000 * (a)) L_degree = np.arctan2(l_y2 - l_y1, l_x2 - l_x1) * 180 / np.pi if L_degree < -10: L_deg.append(L_degree) if R_lines is not None: R_lines = [l[0] for l in R_lines] for rho, theta in R_lines: a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho r_x1 = int(x0 + 1000 * (-b)) r_y1 = int(y0 + 1000 * (a)) r_x2 = int(x0 - 1000 * (-b)) r_y2 = int(y0 - 1000 * (a)) R_degree = np.arctan2(r_y2 - r_y1, r_x2 - r_x1) * 180 / np.pi if R_degree > 10: R_deg.append(R_degree) if len(L_deg) != 0: L_degree = max(L_deg) rho, theta = L_lines[L_deg.index(L_degree)] a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho l_x1 = int(x0 + 1000 * (-b)) l_y1 = int(y0 + 1000 * (a)) l_x2 = int(x0 - 1000 * (-b)) l_y2 = int(y0 - 1000 * (a)) cv2.line(left_ROI, (l_x1, l_y1), (l_x2, l_y2), (0, 0, 255), 2) i += 1 if len(R_deg) != 0: R_degree = min(R_deg) rho, theta = R_lines[R_deg.index(R_degree)] a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho r_x1 = int(x0 + 1000 * (-b)) r_y1 = int(y0 + 1000 * (a)) r_x2 = int(x0 - 1000 * (-b)) r_y2 = int(y0 - 1000 * (a)) cv2.line(right_ROI, (r_x1, r_y1), (r_x2, r_y2), (0, 255, 0), 2) i += 1 if i == 2: angular = (L_degree + R_degree) * 0.002 cv2.putText(frame, '2', (120, 140), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 255, 255), 2) cv2.putText(frame, str(round(L_degree + R_degree)), (200, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 200, 200), 2) elif i == 1: if len(L_deg) != 0: if L_degree >= -49: angular = -(L_degree + 49) * 0.033 else: angular = (L_degree + 49) * 0.033 cv2.putText(frame, '1', (120, 140), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 0, 255), 2) cv2.putText(frame, str(round(L_degree)), (200, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 200), 2) else: if R_degree <= 48: angular = -(R_degree - 48) * 0.033 else: angular = (R_degree - 48) * 0.033 cv2.putText(frame, '1', (120, 140), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 255, 0), 2) cv2.putText(frame, str(round(R_degree)), (200, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 200, 0), 2) else: angular = 0 if stage == 100: if i == 2: turtlemove(0.16, angular) elif i == 1: turtlemove(0.13, angular) else: turtlemove(0.16, 0) if stage == 10 and i >= 1 and count > 20: if T_finish == 0: stage = 10010 if T_finish == 1 and count > 30: stage = 100 if jucha_start == 1 and count > 30: stage = 1111 if jucha_exit == 1: stage = 100 count = 0 if stage == 11 and i >= 1 and count > 20: if T_finish == 0: stage = 10011 if T_finish == 1 and count > 30: stage = 100 count = 0 if stage == 10010: turtlemove(0.13, angular) delay += 1 exit_line = [] _, contours, hierarchy = cv2.findContours(thresh[0:360, 320:640], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for c in contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) (x, y, w, h) = cv2.boundingRect(approx) x_end = x + w y_end = y + h if x_end == 320 and y_end >= 359 and y > 50 and h > 60: cv2.drawContours(right_eye, [c], -1, (0, 255, 0), 3) exit_line.append(x) if len(exit_line) == 1 and delay > 100: print("Exit!!!") turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) turtlemove(0.1, 0) rospy.sleep(rospy.Duration(0.9)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) stage = 10 T_finish = 1 if stage == 10011: turtlemove(0.13, angular) delay += 1 exit_line = [] _, contours, hierarchy = cv2.findContours(thresh[0:360, 0:320], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for c in contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) (x, y, w, h) = cv2.boundingRect(approx) x_end = x + w y_end = y + h if x == 0 and h > 90 and w > 60 and w < 80 and y > 60: cv2.drawContours(left_eye, [c], -1, (0, 255, 0), 3) exit_line.append(x) if len(exit_line) >= 1 and delay > 100: print("Exit!!!") turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) turtlemove(0.1, 0) rospy.sleep(rospy.Duration(0.8)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) stage = 11 T_finish = 1 if stage == 44 and i == 1 and len(L_deg) != 0: stage = 444 if stage == 111: turtlemove(0.09, angular) timer += 1 print('timer', timer) mask_blue = cv2.inRange(upper_right_hsv, lower_blue, upper_blue) blue = cv2.bitwise_and(upper_right_eye, upper_right_eye, mask=mask_blue) blue_gray = cv2.cvtColor(blue, cv2.COLOR_BGR2GRAY) blue_gray_blurred = cv2.GaussianBlur(blue_gray, (5, 5), 0) ret_b, thresh_b = cv2.threshold(blue_gray_blurred, 0, 255, cv2.THRESH_BINARY) _, blue_contours, hierarchy = cv2.findContours(thresh_b, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) blue_x = [] blue_y = [] for c in blue_contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) (x, y, w, h) = cv2.boundingRect(approx) end = x + w if w > 30 and w < 100 and h > 30 and h < 90 and end == 160: blue_x.append(x) blue_y.append(y) cv2.drawContours(upper_right_eye, [c], -1, (255, 0, 0), 3) if len(blue_x) > 0 and timer > 150: turtlemove(0, 0) rospy.sleep(rospy.Duration(0.2)) turtlemove(0.08, 0) rospy.sleep(rospy.Duration(0.3)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.2)) stage = 10 jucha_start = 1 #cv2.imshow('upper_right_eye',upper_right_eye) if stage == 1111: if i == 2: turtlemove(0.15, angular) elif i == 1: turtlemove(0.13, angular) else: turtlemove(0.15, 0) if stage == 103: if i == 2: turtlemove(0.16, angular) elif i == 1: if len(L_deg) != 0: turtlemove(0.13, angular) else: turtlemove(0.12, angular) else: turtlemove(0.16, 0) if stage == 300: hope += 1 if i == 2: turtlemove(0.13, angular) elif i == 1: if len(L_deg) != 0: turtlemove(0.13, angular) else: turtlemove(0.12, angular) #elif i==0 and nnn>60 and hope>100: elif i == 0 and nnn > 60: stage = 3 else: turtlemove(0.13, 0) if stage == 3: pub_stage.publish(stage) #tunnel_count+=1 #if i==2 and tunnel_count>300: if i >= 1 and len(R_deg) != 0: stage = 30 pub_stage.publish(stage) if stage == 30: if i == 2: turtlemove(0.2, angular) elif i == 1: turtlemove(0.05, angular) else: turtlemove(0.15, 0) #cv2.imshow('roi', roi) cv2.imshow('thresh', thresh) #cv2.imshow('edge', edge) cv2.imshow('frame', frame) cv2.waitKey(1)
kernel1=np.ones((8,8),np.uint8) kernel2=np.ones((10,10),np.uint8) name='18' img=cv2.imread('..\\images\\'+name+'.png',0) th = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 201, 35) dilation = cv2.dilate(th, kernel1, iterations=1) erosion = cv2.erode(dilation,kernel2,iterations = 1) laplacian = cv2.Laplacian(erosion, cv2.CV_64F) sobel_8u = np.uint8(laplacian) # cv2.imwrite('C:\\Users\\CXH\Desktop\\'+name+'b.png',sobel_8u) image, contours, hierarchy = cv2.findContours(sobel_8u,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) a=[] ccc=[] wocao=np.zeros(img.shape,np.uint8) for contour in contours : length=cv2.arcLength(contour,True) if length>4000: a.append(length) ccc.append(contour) wocao = cv2.drawContours(wocao, ccc, -1, (255,255,255), 1) # lines = cv2.HoughLines(wocao,1,np.pi/180,600) # print lines.__len__() # for line in lines: # for rho,theta in line: # aa = np.cos(theta) # bb = np.sin(theta) # x0 = aa*rho # y0 = bb*rho # x1 = int(x0 + 1000*(-bb)) # y1 = int(y0 + 1000*(aa)) # x2 = int(x0 - 1000*(-bb))
#Capture frame-by-frame ret, frame = cap.read() if ret == True: #grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #Canny canny = cv2.Canny(frame, 80, 240, 3) #contours contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for i in range(0, len(contours)): #approximate the contour with accuracy proportional to #the contour perimeter approx = cv2.approxPolyDP(contours[i], cv2.arcLength(contours[i], True) * 0.02, True) #Skip small or non-convex objects if (abs(cv2.contourArea(contours[i])) < 100 or not (cv2.isContourConvex(approx))): continue #triangle if (len(approx) == 3): x, y, w, h = cv2.boundingRect(contours[i]) cv2.putText(frame, 'TRI', (x, y), cv2.FONT_HERSHEY_SIMPLEX, scale, (255, 255, 255), 2, cv2.LINE_AA) s = pygame.mixer.Sound("C1.wav") s.play() elif (len(approx) >= 4 and len(approx) <= 6):
k = cv2.waitKey(0) # kSize = 15 # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kSize, kSize)) #np.ones((5,5),np.uint8) # erosion = cv2.erode(result, kernel, iterations = 1) # result = cv2.dilate(erosion, kernel, iterations = 1) # # cv2.imshow('image',result) # k = cv2.waitKey(0) contours, h = cv2.findContours(result, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) print "found", len(contours), "contours" for i, cnt in enumerate(contours): print cnt area = cv2.contourArea(cnt) #if area > 100: perim = cv2.arcLength(cnt, True) M = cv2.moments(cnt) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) cv2.drawContours(result, [cnt], 0, (0, 255, 0), 1) cv2.circle(result, (cx, cy), 5, (0, 0, 255), -1) cv2.imshow('image', result) k = cv2.waitKey(0) else: cv2.imwrite(argv[3], result)
params.adaptiveThreshConstant) contours, hierarchy = cv.findContours(threshImage, cv.RETR_LIST, cv.CHAIN_APPROX_NONE) # Draw contours drawing = np.zeros((threshImage.shape[0], threshImage.shape[1], 3), dtype=np.uint8) for i in range(len(contours)): color = (0, 255, 0) cv.drawContours(drawing, contours, i, (0, 255, 0), 1, cv.LINE_8, hierarchy, 0) # Show in a window for cnt in contours: cnt_len = cv.arcLength(cnt, True) cnt_orig = cnt cnt = cv.approxPolyDP(cnt, params.polygonalApproxAccuracyRate * cnt_len, True) if len(cnt) == 4 and cv.contourArea( cnt) > 50 and cv.isContourConvex(cnt): cnt = cnt.reshape(-1, 2) candidatesAux = [] for i in range(4): candidatesAux.append([cnt[i][0], cnt[i][1]]) candidates.append(candidatesAux) candidates_contours.append(cnt_orig) candidates_len.append(cnt_len) #candidates = sorted(candidates, key=itemgetter(1))
def get_edges(image_list): ### construct the argument parser and parse the arguments for imag in image_list: #loading image image = cv2.imread(imag) # Compute the ratio of the old height to the new height, clone it, # and resize it easier for compute and viewing ratio = image.shape[0] / 500.0 orig = image.copy() #print(image.shape) image = imutils.resize(image, height=500) #print(image.shape) #cv2.imshow('My name is kartik',image) #image = cv2.resize(image, (image.shape[0]//10,image.shape[1]//10)) ### convert the image to grayscale, blur it, and find edges in the image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Gaussian Blurring to remove high frequency noise helping in # Contour Detection gray = cv2.GaussianBlur(gray, (5, 5), 0) # Canny Edge Detection edged = cv2.Canny(gray, 10, 300) print("STEP 1: Edge Detection") # cv2.imshow("Image", image) #cv2.imshow("Edged", edged) # finding the contours in the edged image, keeping only the # largest ones, and initialize the screen contour cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) ## What are Contours ? ## Contours can be explained simply as a curve joining all the continuous ## points (along the boundary), having same color or intensity. ## The contours are a useful tool for shape analysis and object detection ## and recognition. # Handling due to different version of OpenCV #cnts = cnts[0] if imutils.is_cv2() else cnts[1] cnts = cnts[0] # Taking only the top 5 contours by Area cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] ### Heuristic & Assumption # A document scanner simply scans in a piece of paper. # A piece of paper is assumed to be a rectangle. # And a rectangle has four edges. # Therefore use a heuristic like : we’ll assume that the largest # contour in the image with exactly four points is our piece of paper to # be scanned. #screenCnt=[] # looping over the contours for c in cnts: ### Approximating the contour #Calculates a contour perimeter or a curve length peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.01 * peri, True) #0.02 # if our approximated contour has four points, then we # can assume that we have found our screen screenCnt = approx if len(approx) == 4: screenCnt = approx break # show the contour (outline) print("STEP 2: Finding Boundary") #cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) orig1 = orig.copy() cv2.drawContours(orig1, [scale_contour(screenCnt, ratio)], -1, (0, 0, 255), 10) #cv2.imshow('mum',orig1) #cv2.imshow() #orig=cv2.resize(orig,(image.shape[1],image.shape[0])) #print(orig.shape,image.shape) #cv2.imshow("Boundary", cv2.resize(np.hstack((orig1,orig)),(orig1.shape[1]//3,orig1.shape[0]//6))) # to help in visualization #print(imag) #image=np.hstack((image,orig)) cv2.imwrite('./output_images/{}_output.jpg'.format(imag[-7:-4]), orig1) cv2.waitKey(0) cv2.destroyAllWindows()
img_color = cv.imread('../sample/some_figure.png', cv.IMREAD_COLOR) img_gray = cv.cvtColor(img_color, cv.COLOR_RGB2GRAY) ret, img_binary = cv.threshold(img_gray, 50, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) cv.imshow('binary', img_binary) contours, hierarchy = cv.findContours(img_binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) i = 0 for cnt in contours: # contour를 근사화 함 epsilon = 0.005 * cv.arcLength(cnt, True) approx = cv.approxPolyDP(cnt, epsilon, True) size = len(approx) # 꼭지점개수 cv.line(img_color, tuple(approx[0][0]), tuple(approx[size - 1][0]), (0, 255, 0), 3) for k in range(size - 1): cv.line(img_color, tuple(approx[k][0]), tuple(approx[k + 1][0]), (0, 255, 0), 3) # 꼭지점의 갯수에 따라 도형을 판정함. if cv.isContourConvex(approx): if size == 3: setLabel(img_color, "triangle", cnt) elif size == 4: setLabel(img_color, "rectangle", cnt)
def imageCb(self, msg): try: cv_image_raw = self.bridge.imgmsg_to_cv2( msg, "bgr8") #convert image to OpenCV format self.imagerate.sleep() cv_image = cv2.resize(cv_image_raw, (640, 480)) #resize image gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY) #Convert to greyscale _, threshold = cv2.threshold( gray, 100, 255, cv2.THRESH_BINARY) #threshold image to remove black and white _, contours, _ = cv2.findContours( threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #contour the image height = self.local_pos.z #height = self.marker_sp.position.z area_min = -self.a_min_480 * np.exp( -self.b_min_480 * abs(height) ) + self.c_min_480 + 300 #-400 #calculate the minimum area +300 was used for simulated test area_max = -self.a_max_480 * np.exp(-self.b_max_480 * abs( height)) + self.c_max_480 + 1000 #calculate the maximum area for cnt in contours: approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True) #create points using approx area = cv2.contourArea(approx) #calculate area of points if len(approx) <= 5 and len( approx) > 3 and area >= area_min and area <= area_max: (x_square, y_square, w, h) = cv2.boundingRect( approx) #Get imformation about square rectangle pxl_width = area**(1 / 2.0 ) #Get the width of the box in pixels pxl_m_ratio = pxl_width / self.marker_width_max #Get ratio of pixels to meters if pxl_m_ratio == 0: pass else: self.circle_marker_sp.position.x = -( (cv_image.shape[1] / 2 - (x_square + pxl_width / 2)) / pxl_m_ratio ) #only calculate the distance if pxl_m_ratio is not 0 self.circle_marker_sp.position.y = ( (cv_image.shape[0] / 2 - (y_square + pxl_width / 2)) / pxl_m_ratio) self.circle_marker_sp.position.z = self.local_pos.z #Z_position of marker is set to the current height of the uav using estimated values current_time = time.time( ) - self.start_time #This should make no difference as it was disabled in the PX4 software #f = open("/home/honeybee/autolanding_1.txt","a+") #f.write("%f,%f,%f,%f,%f,%f,%f,%f,%f,\n" % (self.circle_marker_sp.position.x,self.circle_marker_sp.position.y,self.marker_sp.position.z,self.local_pos.z,current_time,self.local_pos.x,self.local_pos.y,self.marker_sp.position.x,self.marker_sp.position.y)) #f.close() self.circle_marker_spotted = 1 #Marker has been spotted self.last_time = time.time( ) #last time marker was spotted it updated cv2.drawContours(cv_image, [approx], 0, (50, 255, 0), 2) #Draws the marker //Can be removed if time.time( ) - self.last_time >= 5: #Has the marker stopped being identified for more than 5 seconds self.circle_marker_spotted = 0 if abs( self.local_pos.z ) >= 10: #If the UAV flies too high for some reason then enter bailout state self.circle_marker_spotted = -1 self.image_pub.publish(self.bridge.cv2_to_imgmsg( cv_image, "bgr8")) #publish drawn square except CvBridgeError as e: print(e)
edge = cv2.Canny(gray, 75, 200) #fil the space #edge1 = cv2.morphologyEx(edge, cv2.MORPH_CLOSE, (25,25), iterations = 1) edge = cv2.dilate(edge, (101,101)) #show image cv2.imshow('edge image', edge) cv2.waitKey() cv2.destroyAllWindows() #draw contours (_, cnts,_) = cv2.findContours(edge.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10] for c in cnts: peri = 0.02*cv2.arcLength(c, True) approx = cv2.approxPolyDP(c,peri, True) #the book has approx 4 sides if(len(approx) == 4): screenCnt = approx break; #draw Contour cnt = img.copy() cv2.drawContours(cnt, [screenCnt], -1, (0,225,0) , 2) cv2.imshow('Book',cnt ) cv2.waitKey() cv2.destroyAllWindows() #perspective trasform
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) smooth = cv2.GaussianBlur(imgray, (11, 11), 0) ret, thresh = cv2.threshold(smooth, 155, 255, 0) emage, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) img_contours = np.copy(img) min_area = 10000 max_area = 100000 #object_contours= [cnt for cnt in contours if max_area>cv2.contourArea(cnt) > min_area] areas = [] #object_contours= [cnt for cnt in contours if cv2.contourArea(cnt) > 1000] #perimeter = cv2.arcLength(object_contours[0],True) epsilon = 0.1 * cv2.arcLength(object_contours[0], True) approx = cv2.approxPolyDP(object_contours[0], epsilon, True) areas.append(approx) cv2.drawContours(img_contours, areas, -1, (0, 255, 0), 3) print(approx) pts1 = np.float32(areas[0]) pts2 = np.float32([[0, 0], [0, 442], [442, 442], [442, 0]]) M = cv2.getPerspectiveTransform(pts1, pts2) inv_M = np.linalg.inv(M) dst = cv2.warpPerspective(img, M, (1000, 600)) cv2.imshow("img", img_contours) cv2.imshow("img2", dst) cv2.waitKey(-1)
cv2.imshow('canny edge detection', canny) canny = cv2.cvtColor(canny, cv2.COLOR_GRAY2BGR) #contour to find leafs bordered = cv2.cvtColor(canny, cv2.COLOR_BGR2GRAY) contours, hierarchy = cv2.findContours(bordered, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) maxC = 0 for x in range(len(contours)): if len(contours[x]) > maxC: maxC = len(contours[x]) maxid = x perimeter = cv2.arcLength(contours[maxid], True) #print perimeter Tarea = cv2.contourArea(contours[maxid]) cv2.drawContours(neworiginal, contours[maxid], -1, (0, 0, 255)) cv2.imshow('Contour', neworiginal) #cv2.imwrite('Contour complete leaf.jpg',neworiginal) #Creating rectangular roi around contour height, width, _ = canny.shape min_x, min_y = width, height max_x = max_y = 0 #frame = canny.copy() # computes the bounding box for the contour, and draws it on the frame, for contour, hier in zip(contours, hierarchy): (x, y, w, h) = cv2.boundingRect(contours[maxid])
def GetFiveFeatures(frame): # 判空 if frame == None: return [False, 0.0, 0.0, 0.0, 0.0, 0.0, frame] origin = frame grayimage = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 高斯滤波 blur = cv2.GaussianBlur(grayimage, (5, 5), 0) # 二值化:用大津法,此处选项若是THRESH_BINARY_INV,则同意选用白色背景的图片样本 ret, otsu = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # 找轮廓 contours = cv2.findContours(otsu, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 轮廓集数目 largest_area = 0 largest_contour_index = 0 num = len(contours[1]) for i in range(num): area = cv2.contourArea(contours[1][i], False) if area > largest_area: largest_area = area largest_contour_index = i print "最大面积" + str(largest_area) maxContour = contours[1][largest_contour_index] # 画轮廓 cv2.drawContours(origin, maxContour, -1, (0, 0, 255), 2) # 1. 矩形度 # 查找最小外接矩形 minAreaRect = cv2.minAreaRect(maxContour) box = cv2.boxPoints(minAreaRect) box = np.int0(box) # 画轮廓 cv2.drawContours(origin, [box], 0, (0, 255, 0), 2) # 计算最小外接矩形面积 minAreaRect_Area = int(cv2.contourArea(box, False)) print "最小外接矩形面积" + str(minAreaRect_Area) # 错误判断,如果分母为零,直接报错 if minAreaRect_Area == 0.0: return [False, 0.0, 0.0, 0.0, 0.0, 0.0, origin] # 特征一:矩形度的计算 P_rect = largest_area * 1.0 / minAreaRect_Area # 统一结果为3位有理数 P_rect = round(P_rect, 3) print "矩形度" + str(P_rect) # 2. 延长度 # 质心计算 M = cv2.moments(maxContour) Centroid_x = int(M['m10'] / M['m00']) Centroid_y = int(M['m01'] / M['m00']) print "质心" + str(Centroid_x) + " " + str(Centroid_y) cv2.circle(origin, (Centroid_x, Centroid_y), 3, (255, 255, 255), -1) # 获取长轴 Major_Axis_Length = 0 Major_Axis_Angle = 0 Major_Axis_End_x = 0 Major_Axis_End_y = 0 Major_Axis_Begin_x = 0 Major_Axis_Begin_y = 0 # 此处需要注意质心是否在轮廓内 # 找长轴 for angle in range(180): theta = angle * 3.14 / 180.0 lengthBackward = 0 lengthForward = 0 point_End_x = Centroid_x point_End_y = Centroid_y point_Begin_x = Centroid_x point_Begin_y = Centroid_y # 步进越小准确率越高,设置成1可以先找到准确的直线角度,再根据角度和质心得到的直线计算出正确的长轴和轮廓交点 while cv2.pointPolygonTest(maxContour, (point_End_x, point_End_y), False) > 0: lengthForward = lengthForward + 0.1 point_End_x = int( round(point_End_x + lengthForward * math.cos(theta))) point_End_y = int( round(point_End_y + lengthForward * math.sin(theta))) while cv2.pointPolygonTest(maxContour, (point_Begin_x, point_Begin_y), False) > 0: lengthBackward = lengthBackward + 0.1 point_Begin_x = int( round(point_Begin_x - lengthBackward * math.cos(theta))) point_Begin_y = int( round(point_Begin_y - lengthBackward * math.sin(theta))) if lengthForward + lengthBackward >= Major_Axis_Length: Major_Axis_Length = lengthForward + lengthBackward Major_Axis_Angle = angle Major_Axis_End_x = point_End_x Major_Axis_End_y = point_End_y Major_Axis_Begin_x = point_Begin_x Major_Axis_Begin_y = point_Begin_y # 计算实际长轴长度 Real_Major_Axis_Length = math.sqrt( math.pow((Major_Axis_End_x - Major_Axis_Begin_x), 2) + math.pow((Major_Axis_End_y - Major_Axis_Begin_y), 2)) Real_Major_Axis_Length = round(Real_Major_Axis_Length, 1) print "长轴长度 = " + str(Real_Major_Axis_Length) print "长轴角度 = " + str(Major_Axis_Angle) # print "起点 = " + "x: " + str(Major_Axis_Begin_x) + " y: " + str(Major_Axis_Begin_y) # print "起点 = " + "x: " + str(Major_Axis_End_x) + " y: " + str(Major_Axis_End_y) # 画长轴 cv2.line(origin, (Major_Axis_Begin_x, Major_Axis_Begin_y), (Major_Axis_End_x, Major_Axis_End_y), (255, 0, 0), 2) # 找短轴 # 1. 先得到长轴直线的表达式y=k*x+b,用来计算点到直线距离和判断点在直线上方还是下方 Major_Axis_k = math.tan(Major_Axis_Angle * 3.14 / 180.0) Major_Axis_b = Centroid_y - Major_Axis_k * Centroid_x # 2. 点(x0,y0)到直线(Ax+By+C=0)的距离为d =abs (A*x0+B*y0+C)/sqrt(A^2+B^2) Minor_Axis_A = Major_Axis_k Minor_Axis_B = -1 Minor_Axis_C = Major_Axis_b # 3. 遍历轮廓上的点 Minor_Axis_Under_length = 0 Minor_Axis_Above_length = 0 Minor_Axis_Under_End_x = 0 Minor_Axis_Under_End_y = 0 Minor_Axis_Above_End_x = 0 Minor_Axis_Above_End_y = 0 # 轮廓点集 ContourItems = maxContour.shape[0] for item in range(ContourItems): point_x = maxContour[item][0][0] point_y = maxContour[item][0][1] # 判断点在直线哪一侧 # 上侧 if point_y > int(Major_Axis_k * point_x + Major_Axis_b): # 点到直线距离 dis = abs((Minor_Axis_A * point_x + Minor_Axis_B * point_y + Minor_Axis_C) / math.sqrt(Minor_Axis_A * Minor_Axis_A + Minor_Axis_B * Minor_Axis_B)) if dis >= Minor_Axis_Above_length: Minor_Axis_Above_length = dis Minor_Axis_Above_End_x = point_x Minor_Axis_Above_End_y = point_y # 下侧 elif point_y < int(Major_Axis_k * point_x + Major_Axis_b): # 点到直线距离 dis = abs((Minor_Axis_A * point_x + Minor_Axis_B * point_y + Minor_Axis_C) / math.sqrt(Minor_Axis_A * Minor_Axis_A + Minor_Axis_B * Minor_Axis_B)) if dis >= Minor_Axis_Under_length: Minor_Axis_Under_length = dis Minor_Axis_Under_End_x = point_x Minor_Axis_Under_End_y = point_y # 第三种可能就是轮廓与直线的交点 # # 标记两个点,可以忽略 cv2.circle(origin, (Minor_Axis_Above_End_x, Minor_Axis_Above_End_y), 4, (255, 255, 255), -1) cv2.circle(origin, (Minor_Axis_Under_End_x, Minor_Axis_Under_End_y), 4, (255, 255, 255), -1) # 画出两点直线 cv2.line(origin, (Minor_Axis_Under_End_x, Minor_Axis_Under_End_y), (Minor_Axis_Above_End_x, Minor_Axis_Above_End_y), (0, 255, 255), 3) # 计算实际短轴长度 Real_Minor_Axis_Length = math.sqrt( math.pow((Minor_Axis_Above_End_x - Minor_Axis_Under_End_x), 2) + math.pow((Minor_Axis_Above_End_y - Minor_Axis_Under_End_y), 2)) Real_Minor_Axis_Length = round(Real_Minor_Axis_Length, 1) print "短轴长度 = " + str(Real_Minor_Axis_Length) # 错误判断,如果分母为零,直接报错 if Real_Minor_Axis_Length == 0.0: return [False, P_rect, 0.0, 0.0, 0.0, 0.0, origin] # 计算延长度 P_extend = Real_Major_Axis_Length * 1.0 / Real_Minor_Axis_Length P_extend = round(P_extend, 3) print "延长度 = " + str(P_extend) # 画出与长轴距离最远的两点的辅助线,使用时可以不用,画图用作论文使用 # 画出长轴右方 line_above_k = math.tan((Major_Axis_Angle - 90) * 3.14 / 180.0) line_above_b = Minor_Axis_Above_End_y - line_above_k * Minor_Axis_Above_End_x Minor_Axis_Above_Begin_x = int( (line_above_b - Major_Axis_b) / (Major_Axis_k - line_above_k)) Minor_Axis_Above_Begin_y = int(line_above_k * Minor_Axis_Above_Begin_x + line_above_b) cv2.line(origin, (Minor_Axis_Above_Begin_x, Minor_Axis_Above_Begin_y), (Minor_Axis_Above_End_x, Minor_Axis_Above_End_y), (255, 0, 255), 3) line_under_k = math.tan((Major_Axis_Angle - 90) * 3.14 / 180.0) line_under_b = Minor_Axis_Under_End_y - line_under_k * Minor_Axis_Under_End_x Minor_Axis_Under_Begin_x = int( (line_under_b - Major_Axis_b) / (Major_Axis_k - line_under_k)) Minor_Axis_Under_Begin_y = int(line_under_k * Minor_Axis_Under_Begin_x + line_under_b) cv2.line(origin, (Minor_Axis_Under_Begin_x, Minor_Axis_Under_Begin_y), (Minor_Axis_Under_End_x, Minor_Axis_Under_End_y), (255, 255, 0), 3) # 3. 球状型 # 遍历轮廓每个点,求与质心最近的距离,作为最大内接圆半径, # 初始化一个距离 min_radius = math.pow((maxContour[0][0][0] - Centroid_x), 2) + math.pow( (maxContour[0][0][1] - Centroid_y), 2) for item in range(ContourItems): point_x = maxContour[item][0][0] point_y = maxContour[item][0][1] local_radius = math.pow((point_x - Centroid_x), 2) + math.pow( (point_y - Centroid_y), 2) if local_radius <= min_radius: min_radius = local_radius min_radius = int(math.sqrt(min_radius)) cv2.circle(origin, (Centroid_x, Centroid_y), min_radius, (0, 255, 255), 2) # 遍历轮廓每个点,求与质心最远的距离,作为最小外接圆半径, # 初始化一个距离 max_radius = math.pow((maxContour[0][0][0] - Centroid_x), 2) + math.pow( (maxContour[0][0][1] - Centroid_y), 2) for item in range(ContourItems): point_x = maxContour[item][0][0] point_y = maxContour[item][0][1] local_radius = math.pow((point_x - Centroid_x), 2) + math.pow( (point_y - Centroid_y), 2) if local_radius >= max_radius: max_radius = local_radius max_radius = int(math.sqrt(max_radius)) cv2.circle(origin, (Centroid_x, Centroid_y), max_radius, (255, 0, 255), 2) # 错误判断,如果分母为零,直接报错 if max_radius == 0.0: return [False, P_rect, P_extend, 0.0, 0.0, 0.0, origin] P_spherical = min_radius * 1.0 / max_radius P_spherical = round(P_spherical, 3) print "球状型: " + str(P_spherical) # 错误判断,如果分母为零,直接报错 if Real_Major_Axis_Length == 0.0: return [False, P_rect, P_extend, P_spherical, 0.0, 0.0, origin] # 4. 叶状型 P_leaf = min_radius * 1.0 / Real_Major_Axis_Length P_leaf = round(P_leaf, 3) print "叶状型 = " + str(P_leaf) # 错误判断,如果分母为零,直接报错 if Real_Major_Axis_Length == 0.0: return [False, P_rect, P_extend, P_spherical, P_leaf, 0.0, origin] # 5. 似圆度 P_circle = largest_area * 4.0 / (3.14 * Real_Major_Axis_Length * Real_Major_Axis_Length) P_circle = round(P_circle, 2) print "似圆度 = " + str(P_circle) # 错误判断,如果分母为零,直接报错 if Real_Major_Axis_Length == 0.0: return [False, P_rect, P_extend, P_spherical, P_leaf, P_circle, origin] # 6. 复杂度 ArcLength = cv2.arcLength(maxContour, True) P_complecate = ArcLength * ArcLength * 1.0 / (4 * 3.14 * largest_area) P_complecate = round(P_complecate, 2) print "复杂度 = " + str(P_complecate) # 可以捎带返回处理完之后的画完辅助线的图 return [True, P_rect, P_extend, P_spherical, P_leaf, P_circle, origin]
def approx_polygon(self, contour): epsilon = 0.04 * cv2.arcLength(contour, True) return cv2.approxPolyDP(contour, epsilon, True)
def segment(self, frame): blur = cv2.medianBlur(frame, 51) # Get red channel r = blur.copy() r[:, :, 0] = 0 r[:, :, 1] = 0 # Convert to greyscale grey = self.bgr_2_grey(r) # Down sample 1/2 x 1/2 scale = 2 w = int(grey.shape[1] / scale) h = int(grey.shape[0] / scale) half = cv2.pyrDown(grey, dstsize=(w, h)) img = half.copy() # Largest grey value L = img.max() # mean grey value muT = np.mean(img) # Number of pixels N = img.size Gv = cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=5) Gh = cv2.Sobel(img, cv2.CV_32F, 0, 1, ksize=5) g = np.sqrt((Gh**2) + (Gv**2)) gmean = np.mean(g) if gmean == 0: return frame h = np.zeros(L) h_prime = np.zeros(L) w = np.zeros(L) sigma_T = 0 maxKt = 0 max_eta = 0 optimal_thresh = 0 final = img.copy() for i in xrange(0, L): # Number of pixels at threshold i h[i] = len(np.extract(img == i, img)) if h[i] == 0: h[i] == 1 w[i] = (L / 1) * (1 / gmean) h_prime[i] = w[i] * h[i] sigma_T = sigma_T + ((i - muT)**2) * (h_prime[i] / N) for t in xrange(0, L): lambda_t = (-t + L) * (1 / gmean) # Classes C0 = np.extract(img <= t, img) C1 = np.extract(img > t, img) if C0.any() and C1.any(): # mean of classes mu_0 = np.mean(C0) mu_1 = np.mean(C1) omega_0 = float(len(C0)) / float(N) omega_1 = float(len(C1)) / float(N) sigma_B = (omega_0 * omega_1) * ((mu_1 - mu_0)**2) eta_t = sigma_B / sigma_T kt = lambda_t * eta_t if kt > maxKt: maxKt = kt optimal_thresh = t # Threshold using calculated threshold value (otimal threshold) temp_img = img.copy() w = int(temp_img.shape[1] * scale) h = int(temp_img.shape[0] * scale) # Scale up to original size temp_img = cv2.pyrUp(temp_img, dstsize=(w, h)) ret, mask = cv2.threshold(temp_img, optimal_thresh, 255, cv2.THRESH_BINARY) # Invert mask to the region we want (the lumen) mask_inv = cv2.bitwise_not(mask) # Get masked images # colour_masked = cv2.bitwise_and(crop,crop,mask = mask_inv) grey_masked = cv2.bitwise_and(temp_img, temp_img, mask=mask_inv) # Find contours in masked image and get largest region contours = cv2.findContours(grey_masked.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] mask = np.ones(frame.shape[:2], dtype="uint8") * 255 c = max(contours, key=cv2.contourArea) # Mask to leave only the largest thresholded region mask_cnt = cv2.drawContours(mask, [c], -1, 0, -1) mask_cnt_inv = cv2.bitwise_not(mask_cnt) LRC = cv2.bitwise_and(frame, frame, mask=mask_cnt_inv) ## Zabulis et al. smax = 0 for i in contours: A = cv2.contourArea(i) if A != 0 and cv2.arcLength(i, True) != 0: C = A / cv2.arcLength(i, True) mask = np.ones(grey.shape[:2], dtype="uint8") * 255 mask_cnt = cv2.drawContours(mask, [i], -1, 0, -1) mask_inv = cv2.bitwise_not(mask_cnt) colour_masked = cv2.bitwise_and(frame, frame, mask=mask_inv) mI = cv2.mean(grey, mask=mask_inv) I = 1 + (1 - mI[0]) S = (I**2) * C * A if S > smax: smax = S LRC = colour_masked.copy() LRC[mask == 255] = (255, 255, 255) return LRC
def calc_parameters(img_org, show_params=False, show_hist=False, show_img=False): img_dst = np.copy(img_org) img_gray = cv2.cvtColor(img_org, cv2.COLOR_BGR2GRAY) img_sat = cv2.cvtColor(img_org, cv2.COLOR_BGR2HSV)[:, :, 1] # Processing img_blur = cv2.medianBlur(img_gray, 7) ret_binar, img_binar = cv2.threshold( img_blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) kernel_er = np.ones((3, 3), np.uint8) img_eroded = cv2.erode(img_binar, kernel_er, iterations=4) kernel_cl = np.ones((11, 11), np.uint8) img_closed = cv2.morphologyEx(img_eroded, cv2.MORPH_CLOSE, kernel_cl) kernel_grad = np.ones((3, 3), np.uint8) img_grad = cv2.morphologyEx(img_closed, cv2.MORPH_GRADIENT, kernel_grad) #opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) mask = cv2.bitwise_not(img_grad) img_masked = cv2.bitwise_and(img_dst, img_dst, mask=mask) # Find contours image, contours, hierarchy = cv2.findContours(img_grad, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Calculate area and perimeter max_area = 0 max_contour = None for c in contours: cont_area = cv2.contourArea(c) if cont_area > max_area: max_contour = c max_area = cont_area perimeter = cv2.arcLength(max_contour, True) (x, y), radius = cv2.minEnclosingCircle(max_contour) ellipse = cv2.fitEllipse(max_contour) axes = ellipse[1] minor_ell, major_ell = axes min_maj_ell_ratio = minor_ell / major_ell perimeter_ell = np.pi * (3 / 2 * (minor_ell + major_ell) - np.sqrt(minor_ell * major_ell)) perimeter_ratio = perimeter_ell / perimeter center = (int(x), int(y)) radius = int(radius) circle_area = np.pi * radius**2 circ_area_ratio = max_area / circle_area # Histogram hist_mask = np.zeros(img_org.shape) hist_mask = cv2.fillPoly(hist_mask, pts=[max_contour], color=(255, 255, 255)).astype(np.uint8)[:, :, 0] img_hist = cv2.bitwise_and(img_org, img_org, mask=hist_mask) color = ('b', 'g', 'r') histograms = [] for i, col in enumerate(color): hist = cv2.calcHist([img_org], [i], hist_mask, [256], [0, 256]) histograms.append(hist) # print(np.argmax(hist)) if show_hist: plt.plot(hist, color=col) plt.xlim([0, 256]) if show_hist: plt.show() img_hist_gray = cv2.cvtColor(img_org, cv2.COLOR_BGR2GRAY) img_hist_gray = cv2.bitwise_and(img_hist_gray, img_hist_gray, mask=hist_mask) hist = cv2.calcHist([img_hist_gray], [0], hist_mask, [256], [0, 256]) histograms.append(hist) if show_hist: plt.plot(hist, color='m') plt.xlim([0, 256]) plt.show() #data = histograms[1] #for d in data: #plt.hist(data, bins=60) hist_params = ['mean', 'var', 'skew', 'kurt'] hist_params_dict = {param: [] for param in hist_params} hue = cv2.cvtColor(img_hist, cv2.COLOR_BGR2HSV)[:, :, 0] hist_types = ['blue', 'green', 'red', 'gray', 'hue'] dataset = [ img_hist[:, :, 0], img_hist[:, :, 1], img_hist[:, :, 2], img_hist_gray, hue ] for hist_type, data in zip(hist_types, dataset): data = data[data > 0].ravel() hist_params_dict[f'{hist_params[0]}'].append(np.mean(data)) hist_params_dict[f'{hist_params[1]}'].append(np.var(data)) hist_params_dict[f'{hist_params[2]}'].append(skew(data)) hist_params_dict[f'{hist_params[3]}'].append(kurtosis(data)) params_dict = {} # Calculate hu moments retval = cv2.moments(img_closed) hu = cv2.HuMoments(retval, 7) for i in range(0, 7): hu[i] = -1 * math.copysign(1.0, hu[i]) * math.log10(abs(hu[i])) params_dict[f'hu{i}'] = float(hu[i]) params_dict['max_area'] = int(max_area) params_dict['circ_area_ratio'] = circ_area_ratio params_dict['perimeter'] = int(perimeter) params_dict['min_maj_ell_ratio'] = min_maj_ell_ratio params_dict['perimeter_ratio'] = perimeter_ratio for p in hist_params_dict: for i, colorscale in enumerate(hist_types): params_dict[p + '_' + colorscale] = hist_params_dict[p][i] if show_params: print('\t', ' '.join(f'{ht:6}' for ht in hist_types)) print( 'mean:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[0]}'])) print( 'var:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[1]}'])) print( 'skew:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[2]}'])) print( 'kurt:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[3]}'])) # print('kurt:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[3]}'])) print('max contour area:', int(max_area)) print('max contour length:', int(perimeter)) print('circle area ratio:', int(circ_area_ratio * 100)) print('hu moments:') for i, h in enumerate(hu): print(f'\thu{i}: {h[0]}') if show_img: img_dst = cv2.ellipse(img_dst, ellipse, (0, 0, 255), 2) img_dst = cv2.circle(img_dst, center, radius, (255, 0, 0), 2) img_dst = cv2.drawContours(img_dst, contours, -1, (255, 255, 255), 2) # cv2.imshow('img_org',img_org) # cv2.imshow('binar', img_binar) # cv2.imshow('gradient', img_grad) # cv2.imshow('img_masked', img_masked) # cv2.imshow('closing', img_closed) # cv2.imshow('img_hist',img_hist) # cv2.imshow('img_hist_gray',img_hist_gray) # cv2.imshow('hist_mask',hist_mask) # cv2.imshow('img_dst',img_dst) dst = concatenate_images(img_blur, img_masked, hist_mask, img_binar, img_closed, img_dst) cv2.imshow(' ', dst) cv2.waitKey(0) # cv2.destroyAllWindows() return histograms, params_dict
# # for i,are in a2: # if are <100: # continue # cv2.drawContours(img22, contours, i, (0, 0, 255), 3) # print(i,are) # cv2.imshow('drawContours',img22) # cv2.waitKey(0) # cv2.destroyAllWindows() # TODO 截取原图,把长方形纠正 cnt = contours[0] print(cnt) hull = cv2.convexHull(cnt) epsilon = 0.001 * cv2.arcLength(hull, True) simplified_cnt = cv2.approxPolyDP(hull, epsilon, True) epsilon = 0.1 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) print(approx) cv2.drawContours(img22, [approx], 0, (255, 0, 0), 3) cv2.imshow('approxPolyDP', img22) cv2.waitKey(0) exit(3) # findHomography(srcPoints, dstPoints, method=None, ransacReprojThreshold=None, mask=None, maxIters=None, confidence=None) # H = cv2.findHomography(srcPoints=cnt.astype('single'), dstPoints=np.array([[[0., 0.]], [[2150., 0.]], [[2150., 2800.]], [[0., 2800.]]])) # M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # now that we have our screen contour, we need to determine
# ぼかして均一化 blurred = cv2.GaussianBlur(gray, (5, 5), 0) # 輪郭だけにする edged = cv2.Canny(blurred, 50, 200, 255) # 輪郭検出 contours, hierarchy = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) min_w = 500 # 検出された輪郭の数繰り返してLCD部分検出 for cnt in contours: #輪郭の長さを取得 arc_length = cv2.arcLength(cnt, True) # レシオ0.02で近似図形を取得 approx = cv2.approxPolyDP(cnt, 0.02 * arc_length, True) # 近似図形の頂点が4つ(四角形)なら if len(approx) == 4: # 輪郭の周りを四角い枠で囲った枠の位置(XY座標)と幅と高さを得る #[lcd_x, lcd_y, lcd_w, lcd_h] = cv2.boundingRect(cnt) box = cv2.boundingRect(cnt) [lcd_x, lcd_y, lcd_w, lcd_h] = box # 縦横比がLCDのサイズのようであれば ToDo かつ既定エリアサイズ以上なら if 2 <= lcd_w / lcd_h <= 3: # resized_imageに赤色の四角(LCDの候補)を描画する
def object_detection(frame, lower_hsv, upper_hsv, erosion, dilation): """ Track the color in the frame """ # If mode debug is active, make lower and upper parameters # be the ones from the trackbars if args.debug: color_lower = lower_hsv color_upper = upper_hsv erosion_iter = erosion dilation_iter = dilation # Else, use the lower and upper hardcoded parameters else: # Color range of wanted object color_lower = (90, 80, 55) color_upper = (107, 251, 255) erosion_iter = 1 dilation_iter = 5 # color_lower = (76, 96, 0) # color_upper = (129, 255, 255) # Blur frame, and convert it to the HSV color space blurred = cv2.GaussianBlur(frame, (11, 11), 0) frameHSV = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) # Construct a mask for the color, then perform # a series of erodes and dilates to remove any small # blobs left in the mask mask = cv2.inRange(frameHSV, color_lower, color_upper) mask = cv2.erode(mask, None, iterations=erosion_iter) mask = cv2.dilate(mask, None, iterations=dilation_iter) # Find contours in the mask contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(contours) # Initialize variables contours_circles = [] # Will contain contours with circularity center = None # Will contain x and y coordinates of objects detection = False x = None y = None radius = None # Only proceed if at least one contour was found if len(contours) > 0: # Go through every contour and check circularity, # if it falls between the range, append it for contour in contours: perimeter = cv2.arcLength(contour, True) if perimeter > 0: area = cv2.contourArea(contour) circularity = 4 * math.pi * (area / (perimeter * perimeter) ) # Formula for circularity if 0.85 < circularity < 1.05: contours_circles.append(contour) # Only proceed if at least one contour with circularity was found if len(contours_circles) > 0: # find the largest contour in the mask, then use # it to compute the radius and centroid max_contour = max(contours_circles, key=cv2.contourArea) radius = cv2.minEnclosingCircle(max_contour)[1] M = cv2.moments(max_contour) # Check if the computed radius meets a minimum size if radius > 5: # Object has been detected!, get center coordinates, # and draw a circle in the frame to visualize detection detection = True center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) x, y = center # draw the circle and centroid on the frame cv2.circle(frame, center, int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) return x, y, radius, detection, frame # Return the position, radius of the object, and the frame
def process_image(image_path): """ Retrieves contours of circumferences and other (reference) objects. :param image_path: path to source image in filesystem. :return: number of circumferences found, or None if error reading image """ global __image_path, __original_image, __config_image, __contour_boxes, __original_circumferences, __circumferences # load the image __original_image = cv2.imread(image_path) if __original_image is None: return None # save image path __image_path = image_path # the image we'll display in the configuration screen, with all the detected circumferences __config_image = __original_image.copy() # reset boxes and circumferences __contour_boxes.clear() __original_circumferences.clear() __circumferences.clear() # Reduce background noise and apply canny edge detection temp_image = do_pre_processing(__original_image) # find contours _, cnts, _ = cv2.findContours(image=temp_image, mode=cv2.RETR_CCOMP, method=cv2.CHAIN_APPROX_NONE) for c in cnts: area = cv2.contourArea(c) # ignore small contours if area < 10000: continue # rotated bounding box of the contour box = cv2.minAreaRect(c) box = cv2.boxPoints(box) box = np.array(box, dtype="int") # order the box points in top-left, top-right, bottom-right, and bottom-left box = perspective.order_points(box) # save these boxes so we can browse them in the GUI __contour_boxes.append(box) perimeter = cv2.arcLength(c, closed=True) approx = cv2.approxPolyDP(c, epsilon=0.01 * perimeter, closed=True) # Look for circular objects if len(approx) > 10 and len(approx) < 20: # filter with contour properties # Bounding rectangle x, y, w, h = cv2.boundingRect(c) # aspect ratio aspect_ratio = float(w) / h # solidity hull = cv2.convexHull(c) hull_area = cv2.contourArea(hull) solidity = float(area) / hull_area # valid properties size_ok = w > 25 and h > 25 solidity_ok = solidity > 0.9 aspect_ratio_ok = aspect_ratio >= 0.8 and aspect_ratio <= 1.2 if size_ok and solidity_ok and aspect_ratio_ok: # get centroid M = cv2.moments(c) (cx, cy) = int(M['m10'] / M['m00']), int(M['m01'] / M['m00']) # Save circumference and centroid circumference = (c, (cx, cy)) __circumferences.append(circumference) # Draw circumferences to display all of them in the configuration screen cv2.drawContours(__config_image, [c], 0, color=(0, 255, 0), thickness=5) # convert config image to pil __config_image = convert_cv_to_pil(__config_image) # keep a copy of the originals before selecting finals if len(__circumferences) > 2: __original_circumferences = copy.copy(__circumferences) return len(__circumferences)
def traffic_sign(data): global stage, jucha_finish, jucha_exit, count if stage != 3: np_arr = np.fromstring(data.data, np.uint8) frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) center_frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) right_eye = frame[0:360, 320:640] left_eye = frame[0:360, 0:320] center_eye = center_frame[0:360, 150:440] gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) median = cv2.medianBlur(gray, 3) gray_blurred = cv2.GaussianBlur(median, (3, 3), 0) ret, thresh = cv2.threshold(gray_blurred, 210, 255, cv2.THRESH_BINARY) mask_blue = cv2.inRange(hsv, lower_blue, upper_blue) blue = cv2.bitwise_and(frame, frame, mask=mask_blue) blue_gray = cv2.cvtColor(blue, cv2.COLOR_BGR2GRAY) blue_gray_blurred = cv2.GaussianBlur(blue_gray, (5, 5), 0) ret_b, thresh_b = cv2.threshold(blue_gray_blurred, 0, 255, cv2.THRESH_BINARY) _, blue_contours, hierarchy = cv2.findContours(thresh_b, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) _, center_blue_contours, hierarchy = cv2.findContours( thresh_b[0:360, 150:440], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) _, right_blue_contours, hierarchy = cv2.findContours( thresh_b[0:180, 320:640], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) blue_x = [] blue_y = [] center_blue_x = [] center_blue_y = [] exit_jucha = [] jucha_sign = 0 if stage == 100: #stage select if T_finish == 0: for c in center_blue_contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) (x, y, w, h) = cv2.boundingRect(approx) end = x + w #print('x',x,'y',y,'w',w,'h',h,'len(approx)',len(approx)) #cv2.drawContours(center_eye, [c], -1, (255, 0, 0), 3) if w > 20 and w < 100 and h < 60 and x < 250 and end < 280 and len( approx) != 3 and len(approx) != 6: #print('x',x,'y',y,'w',w,'h',h,'len(approx',len(approx)) center_blue_x.append(x) center_blue_y.append(y) cv2.drawContours(center_eye, [c], -1, (255, 0, 0), 3) #cv2.imshow('center_eye',center_eye) if obstacle_finish == 1: for c in right_blue_contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) (x, y, w, h) = cv2.boundingRect(approx) #print('x',x,'y',y,'w',w,'h',h) #cv2.drawContours(frame, [c], -1, (255, 0, 0), 3) if w > 40 and w < 100 and h > 40 and h < 100 and y < 100: jucha_sign = 1 #cv2.drawContours(frame, [c], -1, (255, 0, 0), 3) if jucha_sign == 1 and T_finish == 1 and nnnn == 0 and nn == 0 and dist_chadan > 50 and jucha_finish == 0 and obstacle_finish == 1: stage = 111 if len(center_blue_x) == 2 and T_finish == 0 and nnnn >= 5 and abs( center_blue_y[0] - center_blue_y[1] ) > 70 and center_blue_y[0] * center_blue_y[1] == 0 and abs( center_blue_x[0] - center_blue_x[1]) < 150: if center_blue_x.index( max(center_blue_x)) == center_blue_y.index( max(center_blue_y)): turtlemove(0, 0) rospy.sleep(rospy.Duration(1)) #turtlemove(0.14,0) #rospy.sleep(rospy.Duration(0.4)) #turtlemove(0,0) #rospy.sleep(rospy.Duration(0.2)) stage = 10 #left in T else: turtlemove(0, 0) rospy.sleep(rospy.Duration(1)) #turtlemove(0.14,0) #rospy.sleep(rospy.Duration(0.4)) #turtlemove(0,0) #rospy.sleep(rospy.Duration(0.2)) stage = 11 #right in T if stage == 103: mask_red = cv2.inRange(hsv, lower_red, upper_red) red = cv2.bitwise_and(frame, frame, mask=mask_red) red_gray = cv2.cvtColor(red, cv2.COLOR_BGR2GRAY) red_gray_blurred = cv2.GaussianBlur(red_gray, (5, 5), 0) ret_b, thresh_r = cv2.threshold(red_gray_blurred, 0, 255, cv2.THRESH_BINARY_INV) _, red_contours, hierarchy = cv2.findContours( thresh_r[0:360, 0:320], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for c in red_contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) (x, y, w, h) = cv2.boundingRect(approx) end = x + w #print('x',x,'y',y,'w',w,'h',h,'len(approx',len(approx)) if x == 0 and y == 0 and w < 60 and w > 20 and h < 60 and h > 20 and len( approx) == 4 and chadan_finish == 1 and hope > 100: cv2.drawContours(left_eye, [c], -1, (0, 255, 0), 3) stage = 300 #cv2.imshow('thresh_r',thresh_r) #cv2.waitKey(1) if stage == 10: #turn left count += 1 if T_finish == 0: turtlemove(0.13, 0.6) elif obstacle_finish == 1: turtlemove(0.14, 0.6) else: turtlemove(0.17, 0.6) if stage == 11: # turn right count += 1 if T_finish == 0: turtlemove(0.13, -0.3) else: turtlemove(0.15, -0.6) if stage == 0: #sinho turtlemove(0, 0) print('sinho!') keypoints_green = turtle_video_siljun.find_color( right_eye, lower_green, upper_green, stage) if keypoints_green: print('green signal detected.') print("LET'S GO !!!") stage = 100 if stage == 1111: #jucha if jucha_finish == 0: dot_line = 0 bottom = frame[180:360, 320:640] gray = cv2.cvtColor(bottom, cv2.COLOR_BGR2GRAY) median = cv2.medianBlur(gray, 3) gray_blurred = cv2.GaussianBlur(median, (3, 3), 0) ret, thresh = cv2.threshold(gray_blurred, 200, 255, cv2.THRESH_BINARY) _, contours, hierarchy = cv2.findContours( thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(bottom, contours, -1, (0, 255, 0), 3) #print('len(contours)',len(contours)) if len(contours) == 2: for c in contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) #print('approx',approx) if len(approx) == 4: (x, y, w, h) = cv2.boundingRect(approx) ar = w / float(h) #print('x',x,'y',y,'w',w,'h',h,'ar',ar) end = x + w if ar > 1 and end < 320: dot_line += 1 cv2.drawContours(bottom, [c], -1, (255, 0, 0), 2) if dot_line >= 2 and jucha_finish == 0: cv2.putText(bottom, '2', (120, 140), cv2.FONT_HERSHEY_SIMPLEX, 5, (255, 0, 0), 2) #print('parking check') turtlemove(0, 0) rospy.sleep(rospy.Duration(1)) turtlemove(1, 0) rospy.sleep(rospy.Duration(1.3)) turtlemove(0, 0) rospy.sleep(rospy.Duration(1)) #parking_right=0 if parking > 0: ('parking_right!') turtlemove(0, -1.3) rospy.sleep(rospy.Duration(1.2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) if dist_chadan > 40: turtlemove(0.15, 0) rospy.sleep(rospy.Duration(2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(1)) turtlemove(-0.15, 0) rospy.sleep(rospy.Duration(2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) turtlemove(0, -1.3) rospy.sleep(rospy.Duration(1.2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) else: turtlemove(-0.15, 0) rospy.sleep(rospy.Duration(2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(1)) turtlemove(0.15, 0) rospy.sleep(rospy.Duration(2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) turtlemove(0, -1.3) rospy.sleep(rospy.Duration(1.2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) jucha_finish = 1 stage = 1111 else: print('parking_left') turtlemove(0, 1.3) rospy.sleep(rospy.Duration(1.2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) if dist_chadan > 40: turtlemove(0.15, 0) rospy.sleep(rospy.Duration(2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(1)) turtlemove(-0.15, 0) rospy.sleep(rospy.Duration(2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) turtlemove(0, 1.3) rospy.sleep(rospy.Duration(1.2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) else: turtlemove(-0.15, 0) rospy.sleep(rospy.Duration(2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(1)) turtlemove(0.15, 0) rospy.sleep(rospy.Duration(2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) turtlemove(0, 1.3) rospy.sleep(rospy.Duration(1.2)) turtlemove(0, 0) rospy.sleep(rospy.Duration(0.3)) jucha_finish = 1 stage = 1111 #cv2.imshow('bottom', bottom) #cv2.waitKey(1) if jucha_finish == 1: for c in blue_contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) (x, y, w, h) = cv2.boundingRect(approx) end = x + w #print('x',x,'y',y,'w',w,'h',h,'len(approx',len(approx)) cv2.drawContours(frame, [c], -1, (255, 0, 0), 3) if y == 0 and x < 500 and h < 50 and w < 80 and h > 30 and w > 65: #cv2.drawContours(frame, [c], -1, (255, 0, 0), 3) #print('x',x,'y',y,'w',w,'h',h) exit_jucha.append(x) if jucha_finish == 1 and len(exit_jucha) >= 1: stage = 10 jucha_exit = 1
ret,thresh = cv2.threshold(imgray,60,255,0) cv2.imshow("Thresh", thresh) kernel = np.ones((3,3),np.uint8) thresh = cv2.dilate(thresh,kernel,iterations = 1) thresh = cv2.erode(thresh,kernel,iterations = 1) cv2.imshow("Eroded", thresh) thresh = cv2.bitwise_not(thresh) cv2.imshow("Thresh2", thresh) im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #print(contours) contour_list = [] rejected_contours = [] circleCenters = [] for contour in contours: approx = cv2.approxPolyDP(contour,0.01*cv2.arcLength(contour,True),True) area = cv2.contourArea(contour) if isCircle(area): contour_list.append(contour) center = np.array(contour).sum(axis = 0) / len(contour) circleCenters.append(center[0]) else: rejected_contours.append(contour) for circle in circleCenters: cv2.circle(img, (circle[0], circle[1]), 4, (255,0,0), -1) # Calculate Distance between circles print(circleCenters) circleCenters.sort(key=lambda x: x[1])
def feature_extract(img): names = [ 'area', 'perimeter', 'pysiological_length', 'pysiological_width', 'aspect_ratio', 'rectangularity', 'circularity', 'mean_r', 'mean_g', 'mean_b', 'stddev_r', 'stddev_g', 'stddev_b', 'contrast', 'correlation', 'inverse_difference_moments', 'entropy' ] df = pd.DataFrame([], columns=names) #Preprocessing gs = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) blur = cv2.GaussianBlur(gs, (25, 25), 0) ret_otsu, im_bw_otsu = cv2.threshold( blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) kernel = np.ones((50, 50), np.uint8) closing = cv2.morphologyEx(im_bw_otsu, cv2.MORPH_CLOSE, kernel) #Shape features contours, _ = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnt = contours[0] M = cv2.moments(cnt) area = cv2.contourArea(cnt) perimeter = cv2.arcLength(cnt, True) x, y, w, h = cv2.boundingRect(cnt) aspect_ratio = float(w) / h rectangularity = w * h / area circularity = ((perimeter)**2) / area #Color features red_channel = img[:, :, 0] green_channel = img[:, :, 1] blue_channel = img[:, :, 2] blue_channel[blue_channel == 255] = 0 green_channel[green_channel == 255] = 0 red_channel[red_channel == 255] = 0 red_mean = np.mean(red_channel) green_mean = np.mean(green_channel) blue_mean = np.mean(blue_channel) red_std = np.std(red_channel) green_std = np.std(green_channel) blue_std = np.std(blue_channel) #Texture features textures = mt.features.haralick(gs) ht_mean = textures.mean(axis=0) contrast = ht_mean[1] correlation = ht_mean[2] inverse_diff_moments = ht_mean[4] entropy = ht_mean[8] vector = [ area, perimeter, w, h, aspect_ratio, rectangularity, circularity, red_mean, green_mean, blue_mean, red_std, green_std, blue_std, contrast, correlation, inverse_diff_moments, entropy ] df_temp = pd.DataFrame([vector], columns=names) df = df.append(df_temp) return df
gray = cv2.bilateralFilter(gray, 11, 17, 17) edged = cv2.Canny(gray, 30, 200) #finding the contours in the edged image, keeping only the largest one which happend to be the screen contour _, cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #sorting the contour areas that were found cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:10] scrnCnt = None #looping over the contours for c in cnts: #approximate the contour peri = cv2.arcLength(c, True) #if the approx poly has 4 points,we can conclude the contour to be the screen, since this also happens to be the largest contour approx = cv2.approxPolyDP(c, 0.02 * peri, True) if len(approx) == 4: scrnCnt = approx break #OpenCV comes with a draw contour functionality to mark these contours #we use that the library function to mark the contours print scrnCnt cv2.drawContours(copy, [scrnCnt], -1, (0, 255, 0), 3) plt.figure("Recognizing rectangle")
def processing(): # Do some preprocessing # filepath = r'dataset\sample\SS 316 P&C_#1_002.tif' filepath = r'dataset\sample\800_020.tif' img = cv2.imread(filepath) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #blur = cv2.GaussianBlur(gray, (5, 5), 0) blur = cv2.medianBlur(gray, 10) input = blur canny = contourDetection.generateCanny(input, alg="MED", debug=False) # dilate then erode the canny to close edges kernel = np.ones((3, 3), np.uint8) # MODIFY THIS AS NECESSARY closing = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel) #dilation = cv2.dilate(canny, kernel, iterations=1) contours, hierarchy = contourDetection.detectContours(closing, debug=False) # Create the mask based on the detected contours image = None mask = np.ones(img.shape[:2], dtype="uint8") * 255 validContours = [] # List of valid contours print("Looping through contours") for contour in contours: approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True) area = cv2.contourArea(contour) if area > 25: validContours.append(contour) cv2.drawContours(mask, [contour], -1, (0, 255, 0), -1) # Uncomment below lines to show mask and image as its being drawn (unstable) # cv2.imshow('mask', mask) # cv2.imshow('image', image) # cv2.waitKey(1) print("Contour analysis complete.") """ maskBGR = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) _, alpha = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY) b, g, r = cv2.split(maskBGR) rgba = [b, g, r, 0.5] maskBGR = cv2.merge(rgba, 4) image = cv2.addWeighted(img, 0.5, maskBGR, 0.5, 0.0) """ image = cv2.bitwise_and(img, img, mask=mask) # cv2.drawContours(image, validContours, -1, (0, 255, 0), 1) # Convert numpy array mask into an openCV image # mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) # Take the mask, blur it, and apply corner detection maskBlur = cv2.GaussianBlur(mask, (9, 9), 0) # corners = cornerDetection.detectHarrisCornerGeneric(mask, debug=False) corners = cornerDetection.goodFeatureShiTomasi(mask) print(type(corners)) # Also get new contours while you're at it #cv2.imshow("MASK", mask) #cv2.waitKey(0) maskContours, maskHierarchy = contourDetection.detectContours(mask, debug=False) #cv2.drawContours(image, maskContours, -1, (255, 0, 0), 1) print(type(mask)) print(type(image)) # image[corners > 0.01 * corners.max()] = [0, 0, 255] # threshold = 0.1 * corners.max() # image[corners > threshold] = [0, 0, 255] print("Corners Detected: {}".format(corners.size)) # Convert mask to color mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) for i in corners: x, y = i.ravel() cv2.circle(img, (x, y), 3, 255, -1) #cv2.drawContours(mask, maskContours, -1, (0, 255, 0), -1) #cv2.drawContours(image, validContours, -1, (0, 255, 0), -1) # NUMBER OF CONNECTED COMPONEENTS: cv2.imshow("ORIGINAL", img) cv2.imshow("MASK", mask) cv2.imshow("IMAGE", image) if cv2.waitKey(0) and 0xff == 27: cv2.destroyAllWindows()
color = cv2.cvtColor(output1, cv2.COLOR_RGB2HSV) mask = cv2.inRange(color, np.array([96, 84, 170]), np.array([255, 255, 255])) mask2 = cv2.erode(mask, kernel=None, iterations=1) mask3 = cv2.dilate(mask2, kernel=None, dst=None, iterations=8) output = cv2.bitwise_and(frame,frame, mask=mask3) # maski na wykrycie kolor bialego razem z zakresem barw RGB mask11 = cv2.inRange(output1, np.array([195,195,200]), np.array([255, 255, 255])) mask21 = cv2.erode(mask11, kernel=None, iterations=1) mask31 = cv2.dilate(mask21, kernel=None, dst=None, iterations=8) output2 = cv2.bitwise_and(frame,frame, mask=mask31) # proba wykrycia koordynatow koloru bialego i stworzenie zmiennych x1 i y1 # ktore pomoga w tworzeniu okna przesuwnego za dronem try: kupa1, kupa2 = cv2.findContours(mask31, mode=cv2.RETR_CCOMP, method=cv2.CHAIN_APPROX_SIMPLE) for i in kupa1: epsilon = 0.001 * cv2.arcLength(i, True) approx = cv2.approxPolyDP(i, epsilon, True) rect = cv2.minAreaRect(approx) box = cv2.boxPoints(rect) box = np.int0(box) if box[0][0] > 126 and box[0][1] > 126: x1 = box[0][0] - 125 y1 = box[0][1] - 125 cv2.drawContours(frame, [box], 0, (255, 255, 255), 2) except: print('Nie wykryto piłeczek1') # proba wykrycia koordynatow koloru pomaranczowego i stworzenie zmiennych x1 i y1 # ktore pomoga w tworzeniu okna przesuwnego za dronem try: kupa1, kupa2 = cv2.findContours(mask3, mode=cv2.RETR_CCOMP, method=cv2.CHAIN_APPROX_SIMPLE) for i in kupa1:
iterations=1) # closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) invert = 255 - opening # cv2.imshow('Bien So', invert) config = r'--oem 1 --psm 7 outputbase' data = pytesseract.image_to_string(invert, lang='eng', config=config) check(data, listOfResult[i], True) if len(numberPlate) == 0: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) thresh = cv2.Canny(gray, 20, 200) contours, h = cv2.findContours(thresh, 1, 2) largest_rectangle = [0, 0] for cnt in contours: approx = cv2.approxPolyDP(cnt, 0.05 * cv2.arcLength(cnt, True), True) if len(approx) == 4: area = cv2.contourArea(cnt) if area > largest_rectangle[0] and area >= img.shape[1] * img.shape[0] * 0.01 and area <= img.shape[ 1] * \ img.shape[0] * 0.9: largest_rectangle = [cv2.contourArea(cnt), cnt, approx] if largest_rectangle != [0, 0]: sumImgHaveROI += 1 # minumBox bounding rect = cv2.minAreaRect(largest_rectangle[1]) box = cv2.boxPoints(rect) box = np.int0(box) # sort by X reChange = reorder(box)
#print( M ) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) print('centroid = ', cx, cy) cv2.line(imgContours, (cx - 10, cy - 10), (cx + 10, cy + 10), red, 2) cv2.line(imgContours, (cx - 10, cy + 10), (cx + 10, cy - 10), red, 2) cv2.drawContours(imgContours, cnt, -1, purple, 7) # Area area = cv2.contourArea(cnt) print('area = ', area) # Perimeter perimeter = cv2.arcLength(cnt, True) print('perimeter = ', perimeter) # Contour Approximation epsilon = 0.005 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) #print('approx', approx) #cv2.drawContours(imgContours, approx, -1, red, 10) print('approx contour length = ', len(approx)) #cv2.imshow('approx over yellow mask', imgContours) # Hull hull = cv2.convexHull(cnt) #print('hull', hull) print('hull contour length = ', len(hull)) #cv2.drawContours(imgContours, hull, -1, red, 10)