def GetRadius(image): retina = copy.copy(image); retina_blue , retina_green , retina_red = cv2.split(retina) # scaling the histogram linearly to normalize all the images retina_green = cv2.bitwise_not(retina_green); ret, retina_green = cv2.threshold(retina_green,retina_green.max()-1,255,cv2.THRESH_BINARY); retina_green = cv2.bitwise_not(retina_green) retina_green = cv2.medianBlur(retina_green,55) img,contours,hierarchy = cv2.findContours(retina_green, 1, 2) area = 0; for item in contours: if cv2.contourArea(item) > area : area = cv2.contourArea(item) cnt = item leftmost = tuple(cnt[cnt[:,:,0].argmin()][0]) rightmost = tuple(cnt[cnt[:,:,0].argmax()][0]) topmost = tuple(cnt[cnt[:,:,1].argmin()][0]) bottommost = tuple(cnt[cnt[:,:,1].argmax()][0]) topmost = list(topmost); bottommost = list(bottommost); leftmost = list(leftmost); rightmost = list(rightmost); centre = [0,0]; centre[1] = (rightmost[0] + leftmost[0] + topmost[0] + bottommost[0])/4 ; centre[0] = (rightmost[1] + leftmost[1] + topmost[1] + bottommost[1])/4 ; radius_x = math.pow(rightmost[0]-leftmost[0],2) + math.pow(rightmost[1]-leftmost[1],2); radius_x = math.sqrt(radius_x)/2; radius_y = math.pow(topmost[0]-bottommost[0],2) + math.pow(topmost[1]-bottommost[1],2); radius_y = math.sqrt(radius_y)/2; radius =( radius_x + radius_y )/2; return(radius);
def track2(bs,img_copy,img, avg): x = -1 y = -1 img_copy = cv2.GaussianBlur(img_copy,(5,5),0) cv2.accumulateWeighted(img_copy,avg,0.4) res = cv2.convertScaleAbs(avg) res = cv2.absdiff(img, res) _,processed_img = cv2.threshold( res, 7, 255, cv2.THRESH_BINARY ) processed_img = cv2.GaussianBlur(processed_img,(5,5),0) _,processed_img = cv2.threshold( processed_img, 240, 255, cv2.THRESH_BINARY ) processed_img = bs.bg_subtractor.apply(processed_img, None, 0.05) # img_thresh = cv2.morphologyEx(img_thresh, cv2.MORPH_OPEN, kernel) if np.count_nonzero(processed_img) > 5: # Get the largest contour contours, hierarchy = cv2.findContours(processed_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) areas = [cv2.contourArea(c) for c in contours] max_index = np.argmax(areas) # Make sure it's big enough if cv2.contourArea(contours[max_index]) >= MIN_BLOB_SIZE: cv2.drawContours(img, contours, max_index, (255, 255, 255), -1) x, y = getCentroid(contours[max_index]) return x, y
def remove_blobs(image, max_area=1000, min_height=5): """ Remove blobs with thresholed size from the image. Parameters ------------ image : Numpy array max_area : float Area threshold min_height : float Height threshold Returns ------- newimage : Numpy array The image with blobs removed """ ret, img2 = cv2.threshold(image, 250, 255, 1) contours, hier = cv2.findContours(np.array(img2), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) mask = np.zeros(img2.shape, np.uint8) for cnt in contours: area = cv2.contourArea(cnt) x, y, width, height = cv2.boundingRect(cnt) if cv2.contourArea(cnt) > max_area or height < min_height: cv2.drawContours(mask,[cnt],0, 255, -1) return cv2.bitwise_and(img2, cv2.bitwise_not(mask))
def ImageSegmentation(self): kernel = np.array(self.coords_cell, np.int32) circle = np.zeros(self.image.shape[:2], np.uint8) # link with polylines the coordinates of "left click", thickness could be adjusted, # Could also fill inside the polyline cv2.polylines(circle,[kernel],False,(255,0,0), thickness=5) kernel2 = np.array(self.coords_cell, np.int32) circle2 = np.zeros(self.image.shape[:2], np.uint8) cv2.polylines(circle2,[kernel2],False,(255,0,0), thickness=4) # Segmentation of the protein accumulation using watershed self.segmentation = morphology.watershed(self.clean_image, self.markers, mask = circle) self.segmentation[self.segmentation < 1.5] = 0 self.segmentation = self.segmentation.astype('uint8') # Find contour of the segmented area contours,hierarchy = cv2.findContours(self.segmentation, 1, 2) # Find countour of the masked area contours_circle,hierarchy = cv2.findContours(circle2, 1, 2) self.area = [cv2.contourArea(cnt) for cnt in contours if (cv2.contourArea(cnt))!=0.0] self.area = sum(self.area) self.area_mask = [cv2.contourArea(cnt_cell) for cnt_cell in contours_circle] self.area_mask = sum(self.area_mask) if self.area > 0: self.surface_segmented.append(self.area) if self.area_mask > 0: self.surface_masked.append(self.area_mask)
def track(bs,img_copy,img, avg): x = -1 y = -1 img_copy = cv2.GaussianBlur(img_copy,(5,5),0) cv2.accumulateWeighted(img_copy,avg,0.4) res = cv2.convertScaleAbs(avg) res = bs.bg_subtractor.apply(res, None, 0.05) gradient = cv2.morphologyEx(res, cv2.MORPH_GRADIENT, kernel) processed_img = cv2.GaussianBlur(gradient,(5,5),0) _,threshold_img = cv2.threshold( processed_img, 20, 255, cv2.THRESH_BINARY ) if np.count_nonzero(threshold_img) > 5: contours, hierarchy = cv2.findContours(threshold_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # totally not from stack overflow areas = [cv2.contourArea(c) for c in contours] # max_index = np.argmax(areas) max_index = np.argmin(areas) # Make sure it's big enough if cv2.contourArea(contours[max_index]) >= MIN_BLOB_SIZE_ROBOT: # img_out = np.zeros(img_thresh.shape).astype(np.uint8) cv2.drawContours(img, contours, max_index, (255, 255, 255), -1) x, y = getCentroid(contours[max_index]) return x, y
def mergeSortContours(self, contoursList): contoursListCopy = contoursList[:] if len(contoursListCopy) > 0: mid = len(contoursListCopy)//2 leftHalf = contoursListCopy[:mid] rightHalf = contoursListCopy[mid:] leftHalf = mergeSort(leftHalf) rightHalf = mergeSort(rightHalf) i = j = k = 0 while i < len(leftHalf) and j < len(rightHalf): if cv2.contourArea(leftHalf[i])<cv2.contourArea(rightHalf[j]): contoursListCopy[k] = leftHalf[i] i = i + 1 else: contoursListCopy[k] = rightHalf[j] j = j + 1 k = k + 1 while i < len(leftHalf): contoursListCopy[k] = leftHalf[i] i = i + 1 k = k + 1 while j < len(rightHalf): contoursListCopy[k] = rightHalf[j] j = j + 1 k = k + 1 return contoursListCopy
def find_hottest_points(cv_image): clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(3,3)) #gray = clahe.apply(img) gray = clahe.apply(cv_image) gray = cv2.GaussianBlur (gray, (21,21), 0) min_thresh = cv2.threshold(gray, min_th, 255, cv2.THRESH_BINARY)[1] max_thresh = cv2.threshold(gray, max_th, 255, cv2.THRESH_BINARY_INV)[1] thresh = cv2.bitwise_and(min_thresh, max_thresh) thresh = cv2.dilate(thresh, None, iterations = 2) (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in cnts: if cv2.contourArea(c) > min_area and cv2.contourArea(c) < max_area: (x,y,w,h) = cv2.boundingRect(c) # cv2.rectangle(cv_image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.rectangle(cv_image, (x, y), (x+w, y+h), 0, 2) continue cv2.imshow("region_detector", cv_image) cv2.moveWindow("region_detector",900,0) cv2.imshow("band_threshold_image", thresh) cv2.moveWindow("band_threshold_image",900,400) cv2.waitKey(1)
def get_info(self, shred, contour, name): area = cv2.contourArea(contour) hull = cv2.convexHull(contour) hull_area = cv2.contourArea(hull) solidity = float(area) / hull_area # Also top and bottom points on the contour topmost = map(int, contour[contour[:, :, 1].argmin()][0]) bottommost = map(int, contour[contour[:, :, 1].argmax()][0]) tags = [] if solidity < 0.75: tags.append("Suspicious shape") width_mm = self.sheet.px_to_mm(shred.shape[0]) height_mm = self.sheet.px_to_mm(shred.shape[1]) ratio = shred.shape[0] / float(shred.shape[1]) return { "area": area, "ratio": ratio, "solidity": solidity, "topmost": topmost, "bottommost": bottommost, "width_mm": width_mm, "height_mm": height_mm }, tags
def get_centroids (contours, frame): centres = [] if contours: for i in range(len(contours)): moments = cv2.moments(contours[i]) centres.append((int(moments['m10']/moments['m00']), int(moments['m01']/moments['m00']))) if i>0: dist = calculateDistance(centres[i-1][0],centres[i-1][1],centres[i][0],centres[i][1]) area=cv2.contourArea(contours[i]) prevarea=cv2.contourArea(contours[i-1]) if dist < 120: if area > prevarea: rect = cv2.minAreaRect(contours[i]) box = cv2.boxPoints(rect) box = np.int0(box) print(box) frame = cv2.drawContours(frame,[box],0,(0,0,255),2) else : rect = cv2.minAreaRect(contours[i-1]) box = cv2.boxPoints(rect) box = np.int0(box) print(box) frame = cv2.drawContours(frame,[box],0,(0,0,255),2) else: rect = cv2.minAreaRect(contours[i]) box = cv2.boxPoints(rect) box = np.int0(box) frame = cv2.drawContours(frame,[box],0,(0,0,255),2) print(box) return centres, frame
def getShape(contour): p = cv2.arcLength(contour, True) aV = cv2.approxPolyDP(contour, 0.04 * p, True) vertices = len(aV) if vertices == 3: return 'Triangle' elif vertices == 4: rect = cv2.minAreaRect(contour) contourArea = cv2.contourArea(contour) fittedArea = rect[1][0] * rect[1][1] #print "Countor Area:", contourArea , " Fiited A:", fittedArea (x, y, w, h) = cv2.boundingRect(aV) ar = w / float(h) if .95 * fittedArea <= contourArea and ar >= 0.95 and ar <= 1.05: return 'Square' else: return 'Rectangle' elif vertices == 5: return 'Pentagon' elif vertices == 6: return 'Hexagon' elif vertices == 7: return 'Heptagon' else: (xC, yC), radius = cv2.minEnclosingCircle(contour) contourArea = cv2.contourArea(contour) fittedArea = radius*radius*3.14 # print "Countor Area:", contourArea , " Circle A:", fittedArea if abs(contourArea-fittedArea) / max(contourArea, fittedArea) < 0.10: return 'Circle' else: return str(str(len(aV))+'-Polygon') return 'Unknown'
def getLetter(self): if self.thresh is None: print "no thresh for getLetter" return 0 contours, hierarchy = cv2.findContours( self.thresh.copy() ,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # sort contours by cnt area / img area ratio from max to min contours = sorted(contours, key = lambda x: cv2.contourArea(x)/self.thresh.size, reverse = False) for cnt in contours: area = cv2.contourArea(cnt) # area calculation (x,y),radius = cv2.minEnclosingCircle(cnt) radius = int(radius) minArea = 3.14159*radius*radius*0.6 maxArea = 3.14159*radius*radius*1.4 # if actual area is close enough to 2*pi*r area && area > 10% of pic if area >= minArea and area <= maxArea and area > self.thresh.size*0.1: # copy thresh = cv2.bitwise_not(self.thresh) mask = np.zeros_like(thresh) # Create mask where white is what we want, black otherwise cv2.drawContours(mask, [cnt], 0, 255, -1) # Draw filled contour in mask self.letter = np.zeros_like(thresh) # Extract out the object and place into output image self.letter[mask == 255] = thresh[mask == 255]
def get_shape(contours, image_to_color, show_shape=False): min_shape_size = 200 shape = None for cnt in contours: approx = cv2.approxPolyDP(cnt,0.02*cv2.arcLength(cnt,True),True) # print len(approx) if len(approx) == 4 and cv2.contourArea(approx) > min_shape_size: shape = "square" cv2.drawContours(image_to_color, [cnt], 0, 255,-1) elif len(approx) > 7 and cv2.contourArea(approx) > min_shape_size: shape = "circle" cv2.drawContours(image_to_color, [cnt], 0, 255,-1) elif len(approx) == 3 and cv2.contourArea(approx) > min_shape_size: shape = "triangle" cv2.drawContours(image_to_color, [cnt], 0, 255,-1) elif len(approx) == 5 and cv2.contourArea(approx) > min_shape_size: shape = "pentagon" cv2.drawContours(image_to_color, [cnt], 0, 255,-1) elif len(approx) == 6 and cv2.contourArea(approx) > min_shape_size: shape = "hexagon" cv2.drawContours(image_to_color, [cnt], 0, 255,-1) if show_shape: cv2.imshow('image_to_color', image_to_color) # When you're done looking at the image, use this to close all windows: cv2.destroyAllWindows() return shape
def detect_color_blob(self, img, lower, upper, color): og_img = img hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, lower, upper) mask = cv2.erode(mask, (3,3), iterations=1) contours, h = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #if self.debugging: #cv2.drawContours(img, contours, -1, (0, 0, 255), 2) sorted_contours = sorted(contours, key = lambda c: cv2.contourArea(c), reverse=True) if len(sorted_contours) < 1: return None c = sorted_contours[0] area = cv2.contourArea(c) if area < 1000: # minimum area threshold return None perim = cv2.arcLength(c, True) # perimeter approx = cv2.approxPolyDP(c, 0.05 * perim, True) #detecting circles (x,y), radius = cv2.minEnclosingCircle(c) if len(approx) == 4: (x, y, w, h) = cv2.boundingRect(approx) ar = w / float(h) self.shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"
def find_blobs(img_data, verbose=False, min_area=None): """ Find second level contours in 16bit images Here is an example to illustrate the contours of the blob this function can find: .. image:: _static/blobs-contours.png""" blobs = [] copy_data = img_data.copy() if img_data.dtype == 'uint16': if verbose: print("16 bit image found. Scaling down to 8bit.") copy_data = (copy_data/2.**8).astype(np.uint8) retval, copy_data = cv2.threshold(copy_data, 140, 255, cv2.THRESH_BINARY) #cv2.imshow('threshold applied',copy_data) contours, hierarchy = cv2.findContours(copy_data, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE, ) ## select only second level contours: toplevel_indices, secondlevel_contours = [],[] if hierarchy == None: if verbose: print("Finished finding no second level contours.") return [] h = hierarchy[0] for i in range(len(h)): if h[i][3] == -1: toplevel_indices.append(i) for i in range(len(h)): if h[i][3] in toplevel_indices: if verbose: print("Found a second level contour. Starting at: %s." % contours[i][0]) if min_area != None: if cv2.contourArea(contours[i]) >= min_area: secondlevel_contours.append(contours[i]) else: secondlevel_contours.append(contours[i]) if verbose: print("Finished finding second level contours.") ## sort contours by largest first (if there are more than one) blobs = sorted(secondlevel_contours, key=lambda contour:cv2.contourArea(contour), reverse=True) return blobs
def extractblob(im): #print im.shape imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,0,255,0) contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) z=0; for cnt in contours: if cv2.contourArea(cnt)>100 and cv2.contourArea(cnt)<10000: #print cv2.contourArea(cnt) cv2.drawContours(im,[cnt],0,(0,255,0),3) x,y,w,h = cv2.boundingRect(cnt) rect=cv2.minAreaRect(cnt) #print rect box=cv2.cv.BoxPoints(rect) box=np.int0(box) #print box #cv2.drawContours(im,[box],0,(0,0,255),2) cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2) print "w=%d h=%d"%(w,h) c=Decimal(rect[1][0])/Decimal(rect[1][1]) d=Decimal(rect[1][1])/Decimal(rect[1][0]) #print c #print d if c>Decimal(4)/Decimal(2.5) or d>Decimal(4)/Decimal(2.5): continue crop=im[y:y+h,x:x+w] cv2.imwrite("crop%d.jpg"%(z),crop) cv2.imshow("crop%d"%(z),crop); z=z+1; cv2.imwrite("windowtitled.JPG", im) cv2.imshow("hello",im) cv2.waitKey(0)
def is_contour_bad(cnts, i, hierarchy): area = cv2.contourArea(cnts[i]) length = cv2.arcLength(cnts[i], True) extension = 0 shape = np.shape(cnts) cnts_number = shape[0] area = cv2.contourArea(cnts[i]) # if(area >= 10000): # print('inloop') # for i_h in xrange(0, cnts_number): # if((i == hierarchy[0][i_h][3]) & (i != i_h)): # area = area - cv2.contourArea(cnts[i_h]) if (area != 0): extension = ((length / 4) ** 2) / area if ((length >= 700) & (area >= 8000)): return 1 elif (extension >= 10): return 1 elif (area <= 50): return 1 else: return 0
def color_detect(self): while True: ret,frame = self.cap.read() imhsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) lower_range = np.array([125, 80, 80], dtype=np.uint8) upper_range = np.array([255, 255, 255], dtype=np.uint8) mask = cv2.inRange(imhsv,lower_range,upper_range) ret,thresh = cv2.threshold(mask,127,155,1) contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) max_area = 0 for cnt in contours: approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True) area = cv2.contourArea(approx) if area > max_area and area < 250000: max_area = area max_contour = cnt cv2.drawContours(frame,[max_contour],0,(0,255,0),2) if cv2.contourArea(max_contour)>15000: self.pub.publish('Find an Obstale') cv2.imshow('mask',mask) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0XFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def CropImage(img, features, silence=True): ret,thresh_flip = cv2.threshold(img,10,1,cv2.THRESH_BINARY) contours,hierarchy = cv2.findContours(thresh_flip, 1, 2) area = 0 for cnt in contours: if area < cv2.contourArea(cnt): area = cv2.contourArea(cnt) largest_contour = cnt cnt = largest_contour x,y,w,h = cv2.boundingRect(cnt) if w > h: max_dim = w else: max_dim = h cropped_img = np.zeros((max_dim, max_dim), dtype='uint8') cropped_img[0:h, 0:w] = features[y:y+h,x:x+w] if silence == False: plt.figure() plt.imshow(cropped_img ,cmap = 'gray') plt.show() return cropped_img
def cir(self,im): contours, hierachy = cv2.findContours(im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cv2.drawContours(im, contours,-1, (0,255,0), 3) contours = filter(lambda c: cv2.contourArea(c) >minContourArea, contours) contours = sorted(contours, key=cv2.contourArea, reverse=True) # Sort by largest contour if len(contours) > 0: #largestContour = contours[0] area = cv2.contourArea(contours[0]) print area if(area<50000 and area>30000): x=1 print bool(x)
def __init__(self, img): gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray_img = cv2.medianBlur(gray_img, 5) self.grey_image = gray_img contours, hierarchy = self.image_to_contours() card_index, card_contour = self.find_card_contour(contours) rect = cv2.minAreaRect(card_contour) box = cv2.cv.BoxPoints(rect) self.box = np.int0(box) # self.draw_contours(img, [box]) card_area = cv2.contourArea(card_contour) self.min_area = 0.07 * cv2.contourArea(card_contour) self.max_area = 0.6 * cv2.contourArea(card_contour) good_contours = self.remove_non_child([0], contours, hierarchy) # print "card area " + str(cv2.contourArea(card_contour)) # print "min " + str(self.min_area) # print "max " + str(self.max_area) good_contours = self.remove_bad_contours(good_contours) self.good_contours = sorted(good_contours, key=cv2.contourArea, reverse=True) self.symbol_contours = self.find_symbol_contours(self.good_contours)
def findContour(self): (cnts, _) = cv2.findContours(self.mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) dots = [] if len(cnts): cntsMax = cnts[0] for c in cnts: if cv2.contourArea(c) > cv2.contourArea(cntsMax): cntsMax = c if cv2.contourArea(cntsMax) > 100 and cv2.contourArea(cntsMax) < 5000: self.tresor = cntsMax x, y, w, h = cv2.boundingRect(self.tresor) dots.append((x, y, w, h)) # if max(w, h) > 100 and max(w, h) < 200: cv2.rectangle(self.image, (x, y), (x + w, y + h), (0, 255, 0), 2) self.addLabels(self.tresor) self.largeurTresorPixel = max(w, h) return self.largeurTresorPixel else: self.tresor = None return 0
def isolate_back(rear, feet, mask): ''' given a front, rear, feet position and a top mask (rat profile) returns the isolated points along the curve of the rats back returns null if there is no data ''' copy = mask.copy() if not rear: return try: # bound with rectangles cv2.rectangle(copy, feet, rear, 255, -1) # arch = edge cnts, h = cv2.findContours(copy,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:2] arch = cnts[0] for c in cnts: a = cv2.contourArea(c) a2 = cv2.contourArea(arch) if a < a2: arch = c return arch except: print 'Could not create contour' return np.array([np.nan])
def intersectionRate(s1, s2): x1, y1, x2, y2 = s1 s1 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]]) area, _intersection = cv2.intersectConvexConvex(s1, np.array(s2)) return 2 * area / (cv2.contourArea(s1) + cv2.contourArea(np.array(s2)))
def isTriangle(contour): x,y,w,h = cv2.boundingRect(contour) area=cv2.contourArea(contour) mathArea=(w*w*math.sqrt(3))/4 ratio=(area/mathArea)*100 print "triangle" h2= w*math.sqrt(3)/2 print h2 print h aSide=math.sqrt((w/2)*(w/2)+math.pow((w*math.sqrt(3)/2),2)) #print aSide #print w area=w*w * math.sqrt(3)/4 print area print cv2.contourArea(contour) areaRelative=area*0.05 print areaRelative relative=h2*0.05 print relative if h>=h2-relative and h<= h2+relative and cv2.contourArea(contour) >= area-areaRelative and cv2.contourArea(contour) <= area+areaRelative : return True else: return False
def calibrate_threshold(self, img): hsv1 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h1,s1,v1 = cv2.split(hsv1) dummy, v1 = cv2.threshold(v1, self.threshold, 255, cv2.THRESH_BINARY) v1 = self.smart_filter(h1, v1) contours, hier = cv2.findContours(v1, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = [cnt for cnt in contours if cv2.contourArea(cnt) > 3000] c1 = self.biggest_cnt(cnts) if c1 == None: self.feedback() return None c1 = cv2.approxPolyDP(c1, 5, True) self.rects = [cv2.boundingRect(c1)] v1 = np.zeros(v1.shape, np.uint8) cv2.drawContours(v1,[c1],-1,(255,0,0),-1) hull = cv2.convexHull(c1, returnPoints = False) defects = cv2.convexityDefects(c1, hull) sum_area = 0 for i in range(defects.shape[0]): s,e,f,d = defects[i][0] start = tuple(c1[s][0]) end = tuple(c1[e][0]) far = tuple(c1[f][0]) t = np.array([[start], [end], [far]]) sum_area += cv2.contourArea(t) self.defects_count = defects.shape[0] self.avg_area = cv2.contourArea(c1) self.prc_avg_area = self.avg_area/float(area(self.rects[0])) self.prc_defect_area = (sum_area/defects.shape[0])/self.avg_area self.feedback()
def getColorPoint(frame_HSV, min, max): frame_threshed = cv2.inRange(frame_HSV, min, max) # find contours in the threshold image contours,hierarchy = cv2.findContours(frame_threshed,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) # finding contour with maximum area and store it as best_cnt sorted_cnts = sorted(contours, lambda x, y: cv2.contourArea(x) > cv2.contourArea(y)) #pts = [] x, y = 0, 0 numarea = 0 for cnt in sorted_cnts: area = cv2.contourArea(cnt) if area < 5: continue M = cv2.moments(cnt) cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00']) x += cx * area y += cy * area numarea += area #cv2.circle(frame_threshed,(cx, cy),3,180,1) pts = [] if numarea: pts = [(int(x / numarea), int(y / numarea))] #cv2.circle(frame_threshed,pts[0],8,255,1) #cv2.imshow('threshed', frame_threshed) return pts
def select_roiShapes(image_orig, image_bin, poziva): img, contours, hierarchy = cv2.findContours(image_bin.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) regions_dict = {} regions_color = [] maxArea = 0 for contour in contours: area = cv2.contourArea(contour) if area > maxArea: maxArea = area for contour in contours: x, y, w, h = cv2.boundingRect(contour) area = cv2.contourArea(contour) if ( (area > 300) and (checkCircle(contour) or checkTriangle(contour) or checkSquare(contour) or checkOktagon(contour)) and (area * 3 > maxArea) ): region = image_bin[y : y + h + 1, x : x + w + 1] region_color = image_orig[y : y + h + 1, x : x + w + 1] regions_color.append(region_color) regions_dict[y] = [resize_region(region), (x, y, w, h)] cv2.rectangle(image_orig, (x, y), (x + w, y + h), (0, 255, 0), 3) sorted_regions_dict = collections.OrderedDict(sorted(regions_dict.items())) sorted_regions = np.array(sorted_regions_dict.values()) return image_orig, sorted_regions[:, 0], regions_color
def locate_color(self,thresh, cimg): centers = [] areas = [] ret,gray = cv2.threshold(thresh,127,255,0) gray2 = gray.copy() mask = np.zeros(gray.shape,np.uint8) contours, hier = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: #if 200<cv2.contourArea(cnt)<5000: M = cv2.moments(cnt) if M['m00'] != 0 and 1000<cv2.contourArea(cnt): cv2.drawContours(cimg,[cnt],0,(0,255,0),2) cv2.drawContours(mask,[cnt],0,255,-1) M = cv2.moments(cnt) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) area = cv2.contourArea(cnt) areas.append(area) tooClose = False for center in centers: if center[0] - cx < 5 and center[1] - cy < 5: tooClose = True if not tooClose: centers.append((cx,cy)) cv2.circle(cimg,(cx,cy),2,(0,0,255),3) cv2.imshow('contors',cimg) cv2.waitKey(3) return centers
def getVal(Self, frame, orgframe): if Self.width == -1: Self.width = frame.shape[0] Self.height = frame.shape[1] gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (9, 9), 0) _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) #thresh = cv2.bitwise_not(thresh) cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) maxArea = 0 maxIndex = -1 for i in xrange(len(cnts)): area = cv2.contourArea(cnts[i]) if area>maxArea: maxArea = area maxIndex = i hand = cnts[maxIndex] handLen = cv2.arcLength(hand, True) handCnt = cv2.approxPolyDP(hand, 0.0001*handLen, True) x, y, w, h = 0, 0, 0, 0 if (cv2.contourArea(hand)>Self.minDist): x, y, w, h = cv2.boundingRect(hand) if len(cnts)<Self.err: if Self.findy: cv2.line(orgframe, (0, y),(Self.width, y), (0, 255, 0), 2) if Self.findx: cv2.line(orgframe, (x, 0),(x, Self.height ), (0, 255, 0), 2) if Self.findy: cv2.line(orgframe, (0, Self.midY), (Self.width, Self.midY), (255, 0, 0), 2) if Self.findx: cv2.line(orgframe, (Self.midX, 0), (Self.midX, Self.height), (255, 0, 0), 2) cv2.imshow(Self.name, orgframe) return len(cnts), y, x
def sizeFiltering(contours): # this function filters out the smaller retroreflector (as well as any noise) by size # _, contours, _ = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) """blank_image = np.zeros((img.shape[0],img.shape[1],3), np.uint8) cv2.drawContours(blank_image, contours, -1, (255, 255, 255)) cv2.imshow("imagia", blank_image) cv2.waitKey()""" if len(contours) == 0: print "errorrrrr" return 0 big = contours[0] for c in contours: if type(c) and type(big) == np.ndarray: if cv2.contourArea(c) > cv2.contourArea(big): big = c else: print type(c) and type(big) return 0 """blank_image = np.zeros((img.shape[0],img.shape[1],3), np.uint8) cv2.drawContours(blank_image, big, -1, (255, 255, 255)) cv2.imshow("imagia", blank_image) cv2.waitKey()""" """blank_image = np.zeros((img.shape[0],img.shape[1],3), np.uint8) cv2.drawContours(blank_image, big, -1, (255, 255, 255))""" x, y, w, h = cv2.boundingRect(big) """cv2.rectangle(blank_image, (x,y), (x+w, y+h), (255,255,255)) cv2.imshow("rect", blank_image) cv2.waitKey()""" return big
bottom = median2 orientation = 1 elif(slope < 0 and dist > 0): #Orientation - South right = median1 bottom = median2 orientation = 2 elif(slope > 0 and dist > 0): #Orientation - West bottom = median1 right = median2 orientation = 3 #To ensure any unintended values do not sneak up when QR code is not present areatop = 0.0 arearight = 0.0 areabottom = 0.0 if(top < len(contours) and right < len(contours) and bottom < len(contours) and cv2.contourArea(contours[top]) > 10 and cv2.contourArea(contours[right]) > 10 and cv2.contourArea(contours[bottom]) > 10): tempL = [] tempM = [] tempO = [] # src - Source Points basically the 4 end co-ordinates of the overlay image # dst - Destination Points to transform overlay image src = [] N = (0,0) tempL = getVertices(contours,top,slope,tempL) tempM = getVertices(contours,right,slope,tempM) tempO = getVertices(contours,bottom,slope,tempO) L = updateCornerOr(orientation,tempL) M = updateCornerOr(orientation,tempM) O = updateCornerOr(orientation,tempO)
def tData(trainData): # imgTrainingNumbers = cv2.imread(trainData) imgTrainingNumbers = cv2.imread( trainData) # read in training numbers image if imgTrainingNumbers is None: # if image was not read successfully print("error: image not read from file \n\n" ) # print error message to std out os.system("pause") # pause so user can see error message return # and exit function (which exits program) # end if imgGray = cv2.cvtColor(imgTrainingNumbers, cv2.COLOR_BGR2GRAY) # get grayscale image imgBlurred = cv2.GaussianBlur(imgGray, (5, 5), 0) # blur # filter image from grayscale to black and white imgThresh = cv2.adaptiveThreshold( imgBlurred, # input image 255, # make pixels that pass the threshold full white cv2. ADAPTIVE_THRESH_GAUSSIAN_C, # use gaussian rather than mean, seems to give better results cv2. THRESH_BINARY_INV, # invert so foreground will be white, background will be black 11, # size of a pixel neighborhood used to calculate threshold value 2) # constant subtracted from the mean or weighted mean cv2.imshow("imgThresh", imgThresh) # show threshold image for reference imgThreshCopy = imgThresh.copy( ) # make a copy of the thresh image, this in necessary b/c findContours modifies the image npaContours, npaHierarchy = cv2.findContours( imgThreshCopy, # input image, make sure to use a copy since the function will modify this image in the course of finding contours cv2.RETR_EXTERNAL, # retrieve the outermost contours only cv2.CHAIN_APPROX_SIMPLE ) # compress horizontal, vertical, and diagonal segments and leave only their end points # declare empty numpy array, we will use this to write to file later # zero rows, enough cols to hold all image data npaFlattenedImages = np.empty( (0, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT)) intClassifications = [ ] # declare empty classifications list, this will be our list of how we are classifying our chars from user input, we will write to file at the end # possible chars we are interested in are digits 0 through 9, put these in list intValidChars intValidChars = [ ord('0'), ord('1'), ord('2'), ord('3'), ord('4'), ord('5'), ord('6'), ord('7'), ord('8'), ord('9'), ord('A'), ord('B'), ord('C'), ord('D'), ord('E'), ord('F'), ord('G'), ord('H'), ord('I'), ord('J'), ord('K'), ord('L'), ord('M'), ord('N'), ord('O'), ord('P'), ord('Q'), ord('R'), ord('S'), ord('T'), ord('U'), ord('V'), ord('W'), ord('X'), ord('Y'), ord('Z') ] for npaContour in npaContours: # for each contour if cv2.contourArea( npaContour ) > MIN_CONTOUR_AREA: # if contour is big enough to consider [intX, intY, intW, intH] = cv2.boundingRect( npaContour) # get and break out bounding rect # draw rectangle around each contour as we ask user for input cv2.rectangle( imgTrainingNumbers, # draw rectangle on original training image (intX, intY), # upper left corner (intX + intW, intY + intH), # lower right corner (0, 0, 255), # red 2) # thickness imgROI = imgThresh[intY:intY + intH, intX:intX + intW] # crop char out of threshold image imgROIResized = cv2.resize( imgROI, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT) ) # resize image, this will be more consistent for recognition and storage cv2.imshow("imgROI", imgROI) # show cropped out char for reference cv2.imshow("imgROIResized", imgROIResized) # show resized image for reference cv2.imshow( "training_numbers.png", imgTrainingNumbers ) # show training numbers image, this will now have red rectangles drawn on it intChar = cv2.waitKey(0) # get key press if intChar == 27: # if esc key was pressed sys.exit() # exit program elif intChar in intValidChars: # else if the char is in the list of chars we are looking for . . . print("hai") intClassifications.append( intChar ) # append classification char to integer list of chars (we will convert to float later before writing to file) npaFlattenedImage = imgROIResized.reshape( (1, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT) ) # flatten image to 1d numpy array so we can write to file later npaFlattenedImages = np.append( npaFlattenedImages, npaFlattenedImage, 0 ) # add current flattened impage numpy array to list of flattened image numpy arrays # end if # end if # end for print(npaFlattenedImages) fltClassifications = np.array( intClassifications, np.float32 ) # convert classifications list of ints to numpy array of floats npaClassifications = fltClassifications.reshape( (fltClassifications.size, 1) ) # flatten numpy array of floats to 1d so we can write to file later print("\n\ntraining complete !!\n") np.savetxt("classifications.txt", npaClassifications) # write flattened images to file np.savetxt("flattened_images.txt", npaFlattenedImages) # cv2.destroyAllWindows() # remove windows from memory return
erosion = cv2.erode(dilation, kernel, iterations=1) # Apply Gaussian Blur and Threshold filtered = cv2.GaussianBlur(erosion, (3, 3), 0) ret, thresh = cv2.threshold(filtered, 127, 255, 0) # Show threshold image cv2.imshow("Thresholded", thresh) # Find contours contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) try: # Find contour with maximum area contour = max(contours, key=lambda x: cv2.contourArea(x)) # Create bounding rectangle around the contour x, y, w, h = cv2.boundingRect(contour) # cv2.rectangle(crop_image, (x, y), (x + w, y + h), (0, 0, 255), 0) # Find convex hull hull = cv2.convexHull(contour) # Draw contour drawing = np.zeros(crop_image.shape, np.uint8) cv2.drawContours(drawing, [contour], -1, (0, 255, 0), 0) cv2.drawContours(drawing, [hull], -1, (0, 0, 255), 0) # Find convexity defects hull = cv2.convexHull(contour, returnPoints=False)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) lower = np.array([0, 48, 80], dtype="uint8") upper = np.array([20, 255, 255], dtype="uint8") skinMask = cv2.inRange(hsv, lower, upper) cv2.imshow('Threshold Hands', skinMask) # Getting the contours and convex hull skinMask1 = copy.deepcopy(skinMask) _, contours, hierarchy = cv2.findContours(skinMask1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) length = len(contours) maxArea = -1 if length > 0: for i in range(length): temp = contours[i] area = cv2.contourArea(temp) if area > maxArea: maxArea = area ci = i res = contours[ci] hull = cv2.convexHull(res) drawing = np.zeros(img.shape, np.uint8) cv2.drawContours(drawing, [res], 0, (0, 255, 0), 2) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3) isFinishCal, cnt = calculateFingers(res, drawing) print("Fingers", cnt) cv2.imshow('output', drawing) k = cv2.waitKey(10)
# print rect_co # if rect[2] > 100: # if rect[1]!=0: # rect_co.append(rect[1]) # if len(rect_co)>=2: # if (rect_co[-1]-rect_co[-2]) > 0: # count_down = rect[2]/60 # count_up = 0 # print 'down' ,count_down # elif (rect_co[-1]-rect_co[-2]) < 0: # count_up = rect[2]/60 # count_down = 0 # print 'up',count_up # continue area = cv2.contourArea(cnt) if area > areaTH: ################# # TRACKING # ################# #Missing conditions for multipersons, outputs and screen entries M = cv2.moments(cnt) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) x,y,w,h = cv2.boundingRect(cnt) # print 'working' # print w new = True if cy in range(up_limit,down_limit):
continue # So sánh sự khác nhau giữa khung hình hiện tại và khung hình ban đầu frameDelta = cv2.absdiff(firstFrame, gray) thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] # thresh = cv2.dilate(thresh, None, iterations=2) cnts = cv2.findContours( thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # Tạo vòng lặp for c in cnts: # Nếu đường viền quá nhỏ thì bỏ qua if cv2.contourArea(c) < conf["min_area"]: continue # Tạo một ô vuông quanh đường viền và vẽ nó lên khung hình # Cập nhật dòng chữ để cảnh báo người dùng (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) text = "Phat hien chuyen dong" if conf["use_telegram"]: GiamSat.ThongBao("Camera phát hiện chuyển động") # Ghi ảnh và thời gian hiện tai lên góc trái khung hình ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p") textColor = int if text == "Khong phat hien chuyen dong":
eroded = cv2.erode( dilation, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))) cv2.imshow("eroded", eroded) _, cnts, hier = cv2.findContours(dilation.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key=cv2.contourArea, reverse=True) #print(cnts) contour_list1 = cnts contour_list = [] if cnts is not None: for contour in cnts: approx = cv2.approxPolyDP( contour, 0.01 * cv2.arcLength(contour, True), True) area = cv2.contourArea(contour) if ((len(approx) > 10) and (area > 20)): contour_list.append(contour) if contour_list is not None: contour_list = np.array((contour_list)) cv2.drawContours(frame, contour_list, -1, (0, 255, 0), 2) contour_list = np.ndarray.flatten(contour_list[1]) acc = [] for i in contour_list: acc.append(i) if (len(acc) > 100): ex = [sum(acc) // len(acc)] * (100 - len(acc)) acc.extend(ex) acc = acc[:100] #print((acc))
def dir(): cap_region_x_begin=0.6 cap_region_y_end=0.6 threshold = 30 blurValue = 41 bgSubThreshold = 50 learningRate = 0 isBgCaptured = 0 triggerSwitch = False counter = 0 q = [] out = [] def printThreshold(thr): print("! Changed threshold to "+str(thr)) def removeBG(frame): fgmask = bgModel.apply(frame,learningRate=learningRate) kernel = np.ones((3, 3), np.uint8) fgmask = cv2.erode(fgmask, kernel, iterations=1) res = cv2.bitwise_and(frame, frame, mask=fgmask) return res def push(q, x): if len(q) < 5: q.append(x) else: for i in range(4): q[i] = q[i+1] q[4] = x camera = cv2.VideoCapture(0) camera.set(10,200) cv2.namedWindow('trackbar') cv2.createTrackbar('trh1', 'trackbar', threshold, 100, printThreshold) while camera.isOpened(): ret, frame = camera.read() threshold = cv2.getTrackbarPos('trh1', 'trackbar') frame = cv2.bilateralFilter(frame, 5, 50, 100) frame = cv2.flip(frame, 1) cv2.rectangle(frame, (int(cap_region_x_begin * frame.shape[1]), 0), (frame.shape[1], int(cap_region_y_end * frame.shape[0])), (255, 0, 0), 2) cv2.imshow('original', frame) if isBgCaptured == 1: img = removeBG(frame) img = img[0:int(cap_region_y_end * frame.shape[0]), int(cap_region_x_begin * frame.shape[1]):frame.shape[1]] gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (blurValue, blurValue), 0) ret, thresh = cv2.threshold(blur, threshold, 255, cv2.THRESH_BINARY) thresh1 = copy.deepcopy(thresh) _,contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) length = len(contours) maxArea = -1 if length > 0: c = max(contours, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) try: center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) except: pass push (q, center) if len(q)==5: if (q[0][0] - q[-1][0]) < -30: if len(out) < 5: out.append("Right") elif (q[0][0] - q[-1][0]) > 30: if len(out) < 5: out.append("Left") if abs(q[0][1] - q[-1][1]) > 60: if len(out) < 5: out.append("Up") print (out) if len(out) == 5: return(max(out, key = out.count)) for i in range(length): temp = contours[i] area = cv2.contourArea(temp) if area > maxArea: maxArea = area ci = i res = contours[ci] hull = cv2.convexHull(res) drawing = np.zeros(img.shape, np.uint8) cv2.drawContours(drawing, [res], 0, (0, 255, 0), 2) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3) cv2.imshow('output', drawing) k = cv2.waitKey(10) if k == 27: break elif k == ord('r'): bgModel = None triggerSwitch = False isBgCaptured = 0 print ('!!!Reset BackGround!!!') if isBgCaptured == 0: time.sleep(1) bgModel = cv2.createBackgroundSubtractorMOG2(0, bgSubThreshold) isBgCaptured = 1 print ('Background Captured')
#extract skin colur imagw mask = cv2.inRange(hsv, lower_skin, upper_skin) #extrapolate the hand to fill dark spots within mask = cv2.dilate(mask, kernel, iterations=4) #blur the image mask = cv2.GaussianBlur(mask, (5, 5), 100) #find contours _, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #find contour of max area(hand) cnt = max(contours, key=lambda x: cv2.contourArea(x)) #approx the contour a little epsilon = 0.0005 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) #make convex hull around hand hull = cv2.convexHull(cnt) #define area of hull and area of hand areahull = cv2.contourArea(hull) areacnt = cv2.contourArea(cnt) #find the percentage of area not covered by hand in convex hull arearatio = ((areahull - areacnt) / areacnt) * 100
dilation=cv2.dilate(img,kernal,iterations=10) kernal=np.ones((5,5),np.uint8) erosion=cv2.erode(img,kernal,iterations=100) #for y in range(0,5): kernal=np.ones((5,5),np.uint8) dilation=cv2.dilate(img,kernal,iterations=100) x=0 # for x in range(0,700): max =0 print "test" contours, hierarchy = cv2.findContours(img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: if x==0: max = cv2.contourArea(cnt) id=cnt x=x+1 elif cv2.contourArea(cnt) > max: max = cv2.contourArea(cnt) id=cnt # Bitwise-AND mask and original image res = cv2.bitwise_and(frame,frame, mask= img) x,y,w,h = cv2.boundingRect(id) frame2 = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2) #idx = 0# The index of the contour that surrounds your object #mask = np.zeros_like(img) # Create mask where white is what we want, black otherwise #cv2.drawContours(mask, contours, idx, 255, -1) # Draw filled contour in mask #out = np.zeros_like(img) # Extract out the object and place into output image
############################################################################### # Remove the arm by cropping the image and draw contours around the main object sign_image = bw_image[:h - 15, :] # Cropping 15 pixels from the bottom # Drawing a contour around white color. # 'contours' is a list of contours found. # 'hierarchy' is of no use as such. contours, hierarchy = cv2.findContours(sign_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Finding the contour with the greatest area. largestContourIndex = 0 if len(contours) <= 0: print "Skipping due to empty contour" continue largestContourArea = cv2.contourArea(contours[largestContourIndex]) i = 1 while i < len(contours): if cv2.contourArea(contours[i]) > cv2.contourArea( contours[largestContourIndex]): largestContourIndex = i i += 1 # Draw the largest contour in the image. cv2.drawContours(sign_image, contours, largestContourIndex, (255, 255, 255), thickness=-1) x, y, w, h = cv2.boundingRect( contours[largestContourIndex] ) # Draw a rectangle around the contour perimeter # cv2.rectangle(sign_image,(x,y),(x+w,y+h),(255,255,255),0,8)
rawCapture = PiRGBArray(camera, size=(640, 480)) time.sleep(1) for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): image = frame.array gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, thresh_img = cv2.threshold(gray, 100, 255, 0) contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for i in range(0, len(contours)): c = contours[i] approx = cv2.approxPolyDP(c, 0.1 * cv2.arcLength(c, True), True) val = False if hierarchy[0, i, 2] > -1 and cv2.contourArea( contours[i]) > 0 and cv2.contourArea(contours[i + 1]) > 0: ratio = cv2.contourArea(contours[i]) / cv2.contourArea( contours[i + 1]) if ratio < 1.5 and ratio > 1.25: val = True if len(approx) == 4 and val == True: M = cv2.moments(c) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) #print("("+str(cx)+","+str(cy)+")") cv2.drawContours(image, [c], -1, (0, 0, 255), 2) x, y, w, h = cv2.boundingRect(c) cv2.putText( image, "(" + str(cx) + "," + str(cy) + ")" + "-------" + str(cv2.contourArea(contours[i])), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
def _grab_board(self): time.sleep(3) samples = np.float32(np.loadtxt('vectors.data')) responses = np.float32(np.loadtxt('samples.data')) model = cv2.KNearest() model.train(samples, responses) window = gtk.gdk.get_default_root_window() x, y, width, height, _ = window.get_geometry() ss = gtk.gdk.Pixbuf.get_from_drawable(gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, width, height), gtk.gdk.get_default_root_window(), gtk.gdk.colormap_get_system(), 0, 0, x, y, width, height) ss.save(TMP_FILE, 'png') raw = cv2.imread(TMP_FILE) gray = cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY) threshold = cv2.adaptiveThreshold(gray, 255, 1, 1, 11, 15) cache = threshold.copy() contours, _ = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) squares = [] for c in contours: c = cv2.approxPolyDP(c, 4, True) if len(c) != 4: continue if not cv2.isContourConvex(c): continue squares.append(c) board_size = max(cv2.contourArea(s) for s in squares) board = [s for s in squares if cv2.contourArea(s) == board_size][0] min_x = min(s[0][0] for s in board) max_x = max(s[0][0] for s in board) min_y = min(s[0][1] for s in board) max_y = max(s[0][1] for s in board) step_x = (max_x - min_x) / 9.0 step_y = (max_y - min_y) / 9.0 values = {} centroids = {} for y in range(9): values[y] = {} for x in range(9): local_min_y = min_y + (y * step_y) + 5 local_max_y = min_y + ((y+1) * step_y) - 5 local_min_x = min_x + (x * step_x) + 5 local_max_x = min_x + ((x+1) * step_x) - 5 roi = cache[ local_min_y:local_max_y, local_min_x:local_max_x] centroids[(y, x)] = ( int((local_min_y + local_max_y) / 2.0), int((local_min_x + local_max_x) / 2.0)) cache = cache.copy() roi_cache = roi.copy() contours, _ = cv2.findContours(roi, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) if not contours: values[y][x] = (0, 0) continue item = max(contours, key=lambda c: cv2.contourArea(c)) _x, _y, _w, _h = cv2.boundingRect(item) digit = roi_cache[_y:_y+_h, _x:_x+_w] small_digit = cv2.resize(digit, (10, 10)) vector = small_digit.reshape((1, 100)).astype(np.float32) _, results, _, err = model.find_nearest(vector, k=1) value = int(results.ravel()[0]) values[y][x] = (value, err[0][0]) errs = [values[y][x][1] for y in range(9) for x in range(9)] err_threshold = np.percentile(errs, 90) # "TODO": relearn 1 :( for y in range(9): for x in range(9): val, err = values[y][x] if err > err_threshold and val == 7: values[y][x] = 1 else: values[y][x] = val return values, centroids
# compute the absolute difference between the current frame and # first frame frameDelta = cv2.absdiff(firstFrame, gray) thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] # dilate the thresholded image to fill in holes, then find contours # on thresholded image thresh = cv2.dilate(thresh, None, iterations=2) (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # loop over the contours for c in cnts: # if the contour is too small, ignore it if cv2.contourArea(c) < minarea: continue # compute the bounding box for the contour, draw it on the frame, # and update the text (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) text = "Occupied" # draw the text and timestamp on the frame # cv2.putText(frame, "Room Status: {}".format(text), (10, 20), # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"), # (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1) # show the frame and record if the user presses a key
# Difference between static background # and current frame(which is GaussianBlur) diff_frame = cv2.absdiff(static_back, gray) # If change in between static background and # current frame is greater than 30 it will show white color(255) thresh_frame = cv2.threshold(diff_frame, 30, 255, cv2.THRESH_BINARY)[1] thresh_frame = cv2.dilate(thresh_frame, None, iterations=2) # Finding contour of moving object (_, cnts, _) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in cnts: if cv2.contourArea(contour) < 10000: continue motion = 1 (x, y, w, h) = cv2.boundingRect(contour) # making green rectangle arround the moving object cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) # Appending status of motion motion_list.append(motion) motion_list = motion_list[-2:] # Appending Start time of motion if motion_list[-1] == 1 and motion_list[-2] == 0: time.append(datetime.now())
def removeBackground(image_file, out_folder): img = cv2.imread(image_file) # Crop out white borders from image so the edge detection algorithm doesnt detect the border as an edge im = Image.fromarray(img) bg = Image.new(im.mode, im.size, (255, 255, 255)) diff = ImageChops.difference(im, bg) diff = ImageChops.add(diff, diff, 2.0, -100) bbox = diff.getbbox() if bbox: im = im.crop(bbox) img = np.array(im) height, width, _ = img.shape gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) # Calculate otsu threshold for edge detection ret, threshed_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) high = ret low = high * 0.5 # Edge detection edges = cv2.Canny(gray, low, high) edges = cv2.dilate(edges, None) edges = cv2.erode(edges, None) # Find all contours and get the one with the biggest area _, contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) max_contour = sorted(contours, key=lambda c: cv2.contourArea(c), reverse=True)[0] x, y, w, h = cv2.boundingRect(max_contour) # Add a 5px buffer to bounding box x1 = x if x - 0 > 5 else 5 x2 = x + w if abs(width - (x + w)) > 5 else (x + w) - 5 y1 = y if y - 0 > 5 else 5 y2 = y + h if abs(height - (y + h)) > 5 else (y + h) - 5 # Create bounding box based on the largest contour of the image rect = (x1, y1, x2, y2) mask = np.zeros(img.shape[:2], dtype=np.uint8) # Dummy placeholders bgdmodel = np.zeros((1, 65), np.float64) fgdmodel = np.zeros((1, 65), np.float64) # Iteratively extract foreground object from background cv2.grabCut(img, mask, rect, bgdmodel, fgdmodel, 10, cv2.GC_INIT_WITH_RECT) cv2.grabCut(img, mask, rect, bgdmodel, fgdmodel, 10, cv2.GC_INIT_WITH_MASK) # Remove background from image mask2 = np.where((mask == 1) + (mask == 3), 255, 0).astype('uint8') output = cv2.bitwise_and(img, img, mask=mask2) # Convert black background to white output[np.where((output == [0, 0, 0]).all(axis=2))] = [255, 255, 255] cv2.imwrite(os.path.join(out_folder, os.path.basename(image_file)), output)
ret, thresh_img = cv2.threshold(gray,200,255,cv2.THRESH_BINARY) # create binary (BW) mask of grayscale image clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(4,4)) # perform adaptive histogram equalization hist = clahe.apply(gray) # apply histogram to grayscale frame blur = cv2.bilateralFilter(hist,9,75,75) # noise removal, keep edges intact edges = cv2.Canny(blur,150,220) # apply canny edge detection algorithm status = "eyes closed" # declare current status, default is closed # detect contours within canny edge result, only display contours above predefined area threshold # calculate centerpoints of each contour moment, display with black circle on grayscale frame # this achieves accurate segmentation the eyelid and eyelashes, and extracts x,y-position data contours = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2] for c in contours: if cv2.contourArea(c) > 13: end = time.time() # declare time of EC end status = "eyes OPEN" # declare current status, eyes open M = cv2.moments(c) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) cv2.drawContours(gray, [c], -1, 0, 2) cv2.circle(gray, (cX, cY), 7, 0, -1) # contours calculated from binary mask when eyelid folds and eyelashes are detected # each contiguous contour filled with white for visualizing sclera segmentation sclera = cv2.findContours(thresh_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2] for s in sclera:
def de_shrink(poly, r=0.5): d_i = cv2.contourArea(poly) * r / cv2.arcLength(poly, True) pco = pyclipper.PyclipperOffset() pco.AddPath(poly, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON) shrinked_poly = np.array(pco.Execute(d_i)) return shrinked_poly
def prediccion(): while camera.isOpened(): ret, frame = camera.read() frame = cv2.bilateralFilter(frame, 5, 50, 100) # smoothing filter frame = cv2.flip(frame, 1) # flip the frame horizontally cv2.rectangle(frame, (int(cap_region_x_begin * frame.shape[1]), 0), (frame.shape[1], int(cap_region_y_end * frame.shape[0])), (255, 0, 0), 2) cv2.imshow('original', frame) # Run once background is captured if isBgCaptured == 1: img = remove_background(frame) img = img[0:int(cap_region_y_end * frame.shape[0]), int(cap_region_x_begin * frame.shape[1]):frame.shape[1]] # clip the ROI # cv2.imshow('mask', img) # convert the image into binary image gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (blurValue, blurValue), 0) # cv2.imshow('blur', blur) ret, thresh = cv2.threshold(blur, threshold, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # Add prediction and action text to thresholded image # cv2.putText(thresh, f"Prediction: {prediction} ({score}%)", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255)) # cv2.putText(thresh, f"Action: {action}", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255)) # Draw the text # Draw the text cv2.putText(thresh, f"Prediction: {prediction} ({score}%)", (50, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255)) cv2.putText(thresh, f"Action: {action}", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255)) # Draw the text cv2.imshow('ori', thresh) # get the contours thresh1 = copy.deepcopy(thresh) _, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) length = len(contours) maxArea = -1 if length > 0: for i in range( length ): # find the biggest contour (according to area) temp = contours[i] area = cv2.contourArea(temp) if area > maxArea: maxArea = area ci = i res = contours[ci] hull = cv2.convexHull(res) drawing = np.zeros(img.shape, np.uint8) cv2.drawContours(drawing, [res], 0, (0, 255, 0), 2) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3) cv2.imshow('output', drawing) # Keyboard OP k = cv2.waitKey(10) if k == 27: # press ESC to exit all windows at any time break elif k == ord('b'): # press 'b' to capture the background bgModel = cv2.createBackgroundSubtractorMOG2(0, bgSubThreshold) b.set_light(6, on_command) time.sleep(2) isBgCaptured = 1 print('Background captured') elif k == ord('r'): # press 'r' to reset the background time.sleep(1) bgModel = None triggerSwitch = False isBgCaptured = 0 print('Reset background') # pasará algo si lo quitamos? elif k == 32: # If space bar pressed cv2.imshow('original', frame) # copies 1 channel BW image to all 3 RGB channels target = np.stack((thresh, ) * 3, axis=-1) target = cv2.resize(target, (224, 224)) target = target.reshape(1, 224, 224, 3) prediction, score = predict_rgb_image_vgg(target) if prediction == 'Palm': # no lo detecta bien pass elif prediction == 'Fist': action = 'Adelante' elif prediction == 'L': action = 'Izquierda' elif prediction == 'Okay': action = 'Derecha' elif prediction == 'Peace': action = 'Atrás' else: pass if save_images: img_name = f"./frames/drawings/drawing_{selected_gesture}_{img_counter}.jpg".format( img_counter) cv2.imwrite(img_name, drawing) print("{} written".format(img_name)) img_name2 = f"./frames/silhouettes/{selected_gesture}_{img_counter}.jpg".format( img_counter) cv2.imwrite(img_name2, thresh) print("{} written".format(img_name2)) img_name3 = f"./frames/masks/mask_{selected_gesture}_{img_counter}.jpg".format( img_counter) cv2.imwrite(img_name3, img) print("{} written".format(img_name3)) img_counter += 1 elif k == ord('t'): print('Tracker turned on.') cap = cv2.VideoCapture(0) ret, frame = cap.read() # Select Region of Interest (ROI) r = cv2.selectROI(frame) # Crop image imCrop = frame[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])] # setup initial location of window r, h, c, w = 250, 400, 400, 400 track_window = (c, r, w, h) # set up the ROI for tracking roi = imCrop hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.))) roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180]) cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) # Setup the termination criteria, either 10 iteration or move by at least 1 pt term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) while (1): ret, frame = cap.read() if ret == True: hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) # apply meanshift to get the new location ret, track_window = cv2.CamShift(dst, track_window, term_crit) # Draw it on image pts = cv2.boxPoints(ret) pts = np.int0(pts) img2 = cv2.polylines(frame, [pts], True, (0, 255, 0), 2) cv2.imshow('img2', img2) k = cv2.waitKey(60) & 0xff if k == 27: # if ESC key break else: cv2.imwrite(chr(k) + ".jpg", img2) else: break cv2.destroyAllWindows() return action
# continue # elif value == 1 and cv2.contourArea(contour) > 1000: # # status = 1 # value = 0 # in_time.append(datetime.now()) # print("The in time is ") # print(datetime.now()) # print(value) # print("pofsdfdsfdsfdsfsdfsdfdsfdsfsdfdsfdsfds") # else: # continue # print("pol") # print(value) if cv2.contourArea(contour) < 2000: continue status=1 # if status == 1 (x,y,w,h) = cv2.boundingRect(contour) cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0),3) status_list.append(status) if status_list[len(status_list)-1]==1 and status_list[len(status_list)-2]==0: times.append(datetime.now()) print("object appeared") elif status_list[len(status_list)-1]==0 and status_list[len(status_list)-2]==1:
frameDelta = cv2.absdiff(blur, lastBlur) thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] thresh = cv2.dilate(thresh, None, iterations=2) if (args["picamera"] > 0): (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) else: (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) motion = 0 for c in cnts: # if the contour is too small, ignore it if cv2.contourArea(c) < MINAREA: continue motion = motion + 1 (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) print motion if (motion > 0): cv2.putText(frame, time.asctime(time.localtime(time.time())), (5, HEIGHT - 20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255)) out.write(frame) print time.asctime(time.localtime(time.time())) #bot.sendMessage(285007767, 'Motion Detected')
def CircularBlink(labels,circularity = 0.5,minarea = 2): """ Based on the blinks separated from watershed (labels), determine if the blink is big enough (larger than minarea in pixels) and circular enough (circularity is larger than some user defined threshold). Output the x-y coordiate of the top-left corner of the bounding box for blinks that pass both criteria Parameters ---------- labels: ndarray Indexed separted blinks circularity: float user defined threshold of circularity (1 means circle), deviate from circle means non-circle, default value is 0.6 minarea: int user defined threshold of minimum blink size in pixels, default value is 4 pixels Returns ------- x, y: ndarray x and y coordinates ofthe top-left corner of the bounding box for blinks that pass both criteria Notes ----- This function uses circularity to select only circular blinks for output """ # create dynamic list of x and y to store coordinates x=[] y=[] # loop through all the separated blinks and filter them by size and # circularity for t in range(1,len(np.unique(labels))): # select individual blink and store the mask in blink blink = np.zeros(labels.shape) label = np.unique(labels)[t] blink[labels == label]=1 # detect contour of this blink contours =cv2.findContours(blink.astype('uint8'),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) # filter by blink area area = cv2.contourArea(contours[1][0]) if area>=minarea: # find perimeters of the blink perimeter = cv2.arcLength(contours[1][0],True) # calculate circularity circ based on the ratio of area and # perimeter^2 circ= (4*np.pi*(area/(perimeter*perimeter))) # filter out blinks that are not circular if circularity<circ<2-circularity: # get centroid, recalculate the position of the molecule using # the moments M = cv2.moments(contours[1][0]) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) # calculate the top-left corner coordinates # the blink size is 6x6 x.append(cx-3) y.append(cy-3) return x,y
dilated = cv2.dilate(thresh, kernel, iterations=1) # plot dilated image plt.imshow(dilated, cmap='gray') plt.show() plt.imshow(dilated) cv2.line(dilated, (0, 80), (256, 80), (100, 0, 0)) plt.show() # find contours contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) valid_cntrs = [] for i, cntr in enumerate(contours): x, y, w, h = cv2.boundingRect(cntr) if (x <= 200) & (y >= 80) & (cv2.contourArea(cntr) >= 25): valid_cntrs.append(cntr) # count of discovered contours len(valid_cntrs) dmy = col_images[13].copy() cv2.drawContours(dmy, valid_cntrs, -1, (127, 200, 0), 2) cv2.line(dmy, (0, 80), (256, 80), (100, 255, 255)) plt.imshow(dmy) plt.show()
firstFrame = gray continue # compute the absolute difference between the current frame and first frame frameDelta = cv2.absdiff(firstFrame, gray) thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] # dilate the thresholded image to fill in holes, then find contours on thresholded image thresh = cv2.dilate(thresh, None, iterations=2) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # loop over the contours for c in cnts: # if the contour is too small, ignore it if cv2.contourArea(c) < args["min_area"]: continue # compute the bounding box for the contour, draw it on the frame, # and update the text (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) text = "Occupied" # draw the text and timestamp on the frame cv2.putText(frame, "Room Status: {}".format(text), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1) # show the frame and record if the user presses a key
b = max(conts, key=cv2.contourArea) west = tuple(b[b[:, :, 0].argmin()][0]) #gives the co-ordinate of the left extreme of contour east = tuple(b[b[:, :, 0].argmax()][0]) #above for east i.e right north = tuple(b[b[:, :, 1].argmin()][0]) south = tuple(b[b[:, :, 1].argmax()][0]) centre_x = (west[0]+east[0])/2 centre_y = (north[0]+south[0])/2 cv2.drawContours(frame, b, -1, (0,255,0), 3) cv2.circle(frame, west, 6, (0,0,255), -1) cv2.circle(frame, east, 6, (0,0,255), -1) cv2.circle(frame, north, 6, (0,0,255), -1) cv2.circle(frame, south, 6, (0,0,255), -1) cv2.circle(frame, (int(centre_x),int(centre_y)), 6, (255,0,0), -1)#plots centre of the area bint = int(cv2.contourArea(b)) if (bint in range(8000,18000)): #hand is open mouse.release(Button.left) cv2.circle(frame, (int(centre_x),int(centre_y)), 6, (255,0,0), -1)#plots centre of the area mouse.position = (screenx-(centre_x*screenx/capturex),screeny-(centre_y*screeny/capturey)) elif(bint in range(2000,7000)): #hand is closed cv2.circle(frame, (int(centre_x),int(centre_y)), 10, (255,255,255), -1)#plots centre of the area mouse.position = (screenx-(centre_x*screenx/capturex), screeny-(centre_y*screeny/capturey)) mouse.press(Button.left) cv2.imshow('video', frame) if cv2.waitKey(1) & 0xFF == ord(' '):#exiting break
def processImage(image): # Apply Gaussian blur image = cv.GaussianBlur(image, (9, 9), 4) # Convert to HSV color-space hsvImage = cv.cvtColor(image, cv.COLOR_BGR2HSV) # Calculate the lower and upper HS values minH = 0 maxH = 50 minS = 255 * 0.21 maxS = 255 * 0.68 lower = (minH, minS, 0) upper = (maxH, maxS, 255) # Mask HS Values to create a binary image mask = cv.inRange(hsvImage, lower, upper) # Morphological transformations to filter erode = cv.erode(mask, np.ones((3, 3)), iterations=2) dilation = cv.dilate(erode, np.ones((3, 3)), iterations=3) #filtered = cv.GaussianBlur(dilation, (5, 5), 4) #ret, threshold = cv.threshold(filtered, 120, 255, 0) # Find the contours ret, contours, hierarchy = cv.findContours(dilation, cv.RETR_TREE, cv.CHAIN_APPROX_NONE) # Find contour with maximum area contour = max(contours, key=lambda x: cv.contourArea(x)) # Create bounding rectangle around the contour x, y, w, h = cv.boundingRect(contour) cv.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), 0) # Find convex hull hull = cv.convexHull(contour) # Draw contour drawing = np.zeros(image.shape, np.uint8) cv.drawContours(drawing, [contour], -1, (0, 255, ), 0) cv.drawContours(drawing, [hull], -1, (0, 0, 255), 0) # Find convexity defects hull = cv.convexHull(contour, returnPoints=False) defects = cv.convexityDefects(contour, hull) count_defects = 0 for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] start = tuple(contour[s][0]) end = tuple(contour[e][0]) far = tuple(contour[f][0]) cv.line(image, start, end, [0, 255, 0], 2) a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2) b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2) c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2) angle = (math.acos((b**2 + c**2 - a**2)/(2*b*c))*180)/3.14 # far point is below the start and end points if far[1] < start[1] or far[1] < end[1]: continue # if angle > 90 draw a circle at the far point if angle <= 90: count_defects += 1 cv.circle(image, far, 5, [0, 0, 255], -1) cv.circle(image, end, 5, [255, 0, 255], -1) cv.circle(image, start, 5, [255, 0, 255], -1) # Show each mask used image = cv.resize(image, (500, 500)) mask = cv.resize(mask, (500, 500)) cv.imshow("Hand", image) cv.imshow("Mask", mask) #cv.imshow("HSV", hsvImage) #cv.imshow("Thresholded", threshold) while(cv.waitKey(0) != 27): continue num_fingers = count_defects + 1 hands = [] hands.append(num_fingers) return hands
def _mask_post_processing(self, mask): target_mask = (mask > cfg.TRACK.MASK_THERSHOLD) target_mask = target_mask.astype(np.uint8) if cv2.__version__[-5] == '4': contours, _ = cv2.findContours(target_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) else: _, contours, _ = cv2.findContours(target_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cnt_area = [cv2.contourArea(cnt) for cnt in contours] if len(contours) != 0 and np.max(cnt_area) > 100: contour = contours[np.argmax(cnt_area)] polygon = contour.reshape(-1, 2) ## the following code estimate the shape angle with ellipse ## then fit a axis-aligned bounding box on the rotated image ellipseBox = cv2.fitEllipse(polygon) # get the center of the ellipse and the angle angle = ellipseBox[-1] #print(angle) center = np.array(ellipseBox[0]) axes = np.array(ellipseBox[1]) # get the ellipse box ellipseBox = cv2.boxPoints(ellipseBox) #compute the rotation matrix rot_mat = cv2.getRotationMatrix2D((center[0],center[1]), angle, 1.0) # rotate the ellipse box one = np.ones([ellipseBox.shape[0],3,1]) one[:,:2,:] = ellipseBox.reshape(-1,2,1) ellipseBox = np.matmul(rot_mat, one).reshape(-1,2) # to xmin ymin xmax ymax xs = ellipseBox[:,0] xmin, xmax = np.min(xs), np.max(xs) ys = ellipseBox[:,1] ymin, ymax = np.min(ys), np.max(ys) ellipseBox = [xmin, ymin, xmax, ymax] # rotate the contour one = np.ones([polygon.shape[0],3,1]) one[:,:2,:] = polygon.reshape(-1,2,1) polygon = np.matmul(rot_mat, one).astype(int).reshape(-1,2) # remove points outside of the ellipseBox logi = polygon[:,0]<=xmax logi = np.logical_and(polygon[:,0]>=xmin, logi) logi = np.logical_and(polygon[:,1]>=ymin, logi) logi = np.logical_and(polygon[:,1]<=ymax, logi) polygon = polygon[logi,:] x,y,w,h = cv2.boundingRect(polygon) bRect = [x, y, x+w, y+h] # get the intersection of ellipse box and the rotated box x1, y1, x2, y2 = ellipseBox[0], ellipseBox[1], ellipseBox[2], ellipseBox[3] tx1, ty1, tx2, ty2 = bRect[0], bRect[1], bRect[2], bRect[3] xx1 = min(max(tx1, x1, 0), target_mask.shape[1]-1) yy1 = min(max(ty1, y1, 0), target_mask.shape[0]-1) xx2 = max(min(tx2, x2, target_mask.shape[1]-1), 0) yy2 = max(min(ty2, y2, target_mask.shape[0]-1), 0) rotated_mask = cv2.warpAffine(target_mask, rot_mat,(target_mask.shape[1],target_mask.shape[0])) #refinement alpha_factor = cfg.TRACK.FACTOR while True: if np.sum(rotated_mask[int(yy1):int(yy2),int(xx1)]) < (yy2-yy1)*alpha_factor: temp = xx1+(xx2-xx1)*0.02 if not (temp >= target_mask.shape[1]-1 or xx2-xx1 < 1): xx1 = temp else: break else: break while True: if np.sum(rotated_mask[int(yy1):int(yy2),int(xx2)]) < (yy2-yy1)*alpha_factor: temp = xx2-(xx2-xx1)*0.02 if not (temp <= 0 or xx2-xx1 < 1): xx2 = temp else: break else: break while True: if np.sum(rotated_mask[int(yy1),int(xx1):int(xx2)]) < (xx2-xx1)*alpha_factor: temp = yy1+(yy2-yy1)*0.02 if not (temp >= target_mask.shape[0]-1 or yy2-yy1 < 1): yy1 = temp else: break else: break while True: if np.sum(rotated_mask[int(yy2),int(xx1):int(xx2)]) < (xx2-xx1)*alpha_factor: temp = yy2-(yy2-yy1)*0.02 if not (temp <= 0 or yy2-yy1 < 1): yy2 = temp else: break else: break prbox = np.array([[xx1,yy1],[xx2,yy1],[xx2,yy2],[xx1,yy2]]) # inverse of the rotation matrix M_inv = cv2.invertAffineTransform(rot_mat) # project the points back to image coordinate one = np.ones([prbox.shape[0],3,1]) one[:,:2,:] = prbox.reshape(-1,2,1) prbox = np.matmul(M_inv, one).reshape(-1,2) rbox_in_img = prbox else: # empty mask location = cxy_wh_2_rect(self.center_pos, self.size) rbox_in_img = np.array([[location[0], location[1]], [location[0] + location[2], location[1]], [location[0] + location[2], location[1] + location[3]], [location[0], location[1] + location[3]]]) return rbox_in_img
ret, raw = camera.read() # Make it grayscale img = cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY) # TO SHOW JUST TRESHHOLD STUFF #img = calculate_thresh(first_frame, img) # Get a list of contours cnts = calculate_thresh(first_frame, img) if cnts is not None: # Draw them on the image cv2.drawContours(raw, cnts, -1, (100, 100, 100), 3) for c in cnts: # if the contour is not too small or too big if cv2.contourArea(c) < THRESH_MAX and cv2.contourArea( c) > THRESH_MIN: (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(raw, (x, y), (x + w, y + h), (0, 0, 0), 2) ball_center = (x + w / 2, y + h / 2) cv2.circle(raw, ball_center, 20, (0, 0, 0), 5) pts.appendleft(ball_center) #print("Contour size: " + str(cv2.contourArea(c))) if cv2.pointPolygonTest(left_flip, ball_center, False) > 0 and not left_flipping: left_flipping = True left_last_flip_time = rospy.get_rostime().to_sec() publish_flipper.publish(1, flip_delta) print("FLIP LEFT!!!") if cv2.pointPolygonTest(right_flip, ball_center,
# # converting to grayscale image gray_img = cv2.cvtColor(adjusted_img, cv2.COLOR_BGR2GRAY) cv2.imshow("Grayscale_image", gray_img) cv2.waitKey(0) # applying threshold to grayscale image threshold_img = cv2.threshold(gray_img, 50, 255, cv2.THRESH_BINARY)[1] cv2.imshow("Threshold", threshold_img) cv2.waitKey(0) # contours to threshold cnts = cv2.findContours(threshold_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cnts = imutils.grab_contours(cnts) output_img = threshold_img.copy() # loop over the contours i = 0 for c in cnts: if cv2.contourArea(c) > 637: # print(cv2.contourArea(c)) i=i+1 cv2.drawContours(adjusted_img, [c], -1, (0,0, 255), 5) # #get the min enclosing circle # (x, y), radius = cv2.minEnclosingCircle(c) # # convert all values to int # center = (int(x), int(y)) # radius = int(radius) # # draw the circle in blue # img = cv2.circle(image, center, radius, (255, 0, 0), 2) # if radius > 24: # # print(cv2.minEnclosingCircle(c)) # i=i+1 print(i) # cv2.drawContours(image, cnts, -1, (0, 255, 0), 1)
def detect_motion(file_name): max_rect = 0 num1 = 0 vs = cv2.VideoCapture(file_name) firstFrame = None while True: frame = vs.read() frame = frame[1] text = "Unoccupied" if frame is None: break frame = imutils.resize(frame, width=500) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (21, 21), 0) if firstFrame is None: firstFrame = gray continue frameDelta = cv2.absdiff(firstFrame, gray) thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] thresh = cv2.dilate(thresh, None, iterations=2) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) for c in cnts: if cv2.contourArea(c) < DEF_AREA: continue # if cv2.contourArea(c) <= max_rect: # continue max_rect += 1 (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) if max_rect > 0: max_rect = 0 frameOrig = frame.copy() folder1 = path_to_in+file_name.split('-')[0] if not os.path.exists(folder1): os.mkdir(folder1) folder1 = folder1+"/"+file_name.split('-')[1]+"-"+file_name.split('-')[2].split('.')[0] if not os.path.exists(folder1): os.mkdir(folder1) if not os.path.exists(folder1+"/1"): os.mkdir(folder1+"/1") numdir=1 while os.path.exists(folder1+"/"+str(numdir)): numdir += 1 numdir -=1 listfile = os.listdir(folder1+"/"+str(numdir)) number_files = len(listfile) if number_files > 200: numdir += 1 os.mkdir(folder1+"/"+str(numdir)) folder1 = folder1+"/"+str(numdir) filejpg=folder1+"/"+file_name.split('.')[0]+"_"+str(num1)+"_.jpg" while os.path.exists(filejpg): num1 += 1 filejpg=folder1+"/"+file_name.split('.')[0]+"_"+str(num1)+"_.jpg" cv2.imwrite(filejpg, frameOrig) vs.release() return num1