Esempio n. 1
0
    def draw_walls(self):
        left_wall_points = np.array([self.transform(point) for point in self.left_wall_points])
        right_wall_points = np.array([self.transform(point) for point in self.right_wall_points])

        rect = cv2.minAreaRect(left_wall_points[:, :2].astype(np.float32))
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(self.grid, [box], 0, 128, -1)

        rect = cv2.minAreaRect(right_wall_points[:, :2].astype(np.float32))
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(self.grid, [box], 0, 128, -1)

        # So I dont have to comment abunch of stuff out for debugging
        dont_display = True
        if dont_display:
            return

        # Bob Ross it up (just for display)
        left_f, right_f = self.transform(self.left_f), self.transform(self.right_f)
        left_b, right_b = self.transform(self.left_b), self.transform(self.right_b)

        boat = self.transform(self.boat_pos)
        target = self.transform(self.target)

        cv2.circle(self.grid, tuple(boat[:2].astype(np.int32)), 8, 255)
        cv2.circle(self.grid, tuple(target[:2].astype(np.int32)), 15, 255)
        cv2.circle(self.grid, tuple(self.transform(self.mid_point)[:2].astype(np.int32)), 5, 255)
        cv2.circle(self.grid, tuple(left_f[:2].astype(np.int32)), 10, 255)
        cv2.circle(self.grid, tuple(right_f[:2].astype(np.int32)), 10, 255)
        cv2.circle(self.grid, tuple(left_b[:2].astype(np.int32)), 3, 125)
        cv2.circle(self.grid, tuple(right_b[:2].astype(np.int32)), 3, 128)
        cv2.imshow("test", self.grid)
        cv2.waitKey(0)
Esempio n. 2
0
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
Esempio n. 3
0
    def applyForegroundExtractionMask_Square(self, img, mask, centroid, rotatedRect, marginAdjust):
        imgAnd = dicerfuncs.makeBinaryImageMaskForImg(mask)
        color = 255
        rotatedRect2 = (rotatedRect[0], (rotatedRect[1][0] * marginAdjust, rotatedRect[1][1] * marginAdjust), rotatedRect[2])
        boxpoints = cv2.boxPoints(rotatedRect2)
        boxpoints = boxpoints.astype(int)
        cv2.fillConvexPoly(imgAnd, boxpoints, color)
        mask = cv2.bitwise_and(imgAnd, mask)

        # mask crop image itself
        if (True):
            imgAnd = dicerfuncs.makeBinaryImageMaskForImg(mask)
            rotatedRect2 = (rotatedRect[0], (rotatedRect[1][0] * marginAdjust + 2, rotatedRect[1][1] * marginAdjust + 2), rotatedRect[2])
            boxpoints = cv2.boxPoints(rotatedRect2)
            boxpoints = boxpoints.astype(int)
            cv2.fillConvexPoly(imgAnd, boxpoints, color)
            img = cv2.bitwise_and(imgAnd, img)
            # actually crop? Note:this causes problems i think because of size changes to shape params
            if (False):
                # now lets find where we can crop
                x, y, w, h = cv2.boundingRect(boxpoints)
                img = img[y:y + h, x:x + w]
                mask = mask[y:y + h, x:x + w]

        return (img, mask)
Esempio n. 4
0
def processCam(cap):
    bx = -1
    by = -1



    
    # Capture frame-by-frame
    ret, frame = cap.read()
    
    # Our operations on the frame come here
    #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)#HSV
    #white led ring: 45,2,240 - 130,40,255
    lower_lim = np.array([37,10,180])#80,23,235
    upper_lim = np.array([106,63,255])#102,167,255
    mask = cv2.inRange(hsv, lower_lim, upper_lim)
    img, contours, heirarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    img = cv2.inRange(hsv, lower_lim, upper_lim)
    img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

    contours = findBigContours(contours)
    # find big contours
    #biggestContourIndex = findBiggestContour(contours)
    biggestContourIndex, secondBiggestIndex = findSecondBiggestContour(contours)
    #bigContours = findBigContours(contours)
    #biggestContourIndex = findBestAR(bigContours)
    if(len(contours) != 0):
        # find box around contour and it's center
        recta = cv2.minAreaRect(contours[biggestContourIndex])
        rectb = cv2.minAreaRect(contours[secondBiggestIndex])
        boxa = cv2.boxPoints(recta)
        boxb = cv2.boxPoints(rectb)
        rect = cv2.minAreaRect(np.concatenate([boxa,boxb]))
        box = cv2.boxPoints(rect)
        bx = int((box[0][0] + box[2][0])/2)
        by = int((box[0][1] + box[2][1])/2)
        #x,y,w,h = cv2.boundingRect(contours[biggestContourIndex])
        #if(h != 0):
        #    print("aspect ratio: " + str(h/float(w)))
        #print("center: " + str(bx) + ', ' + str(by))
        box = np.int0(box)
        img = cv2.drawContours(img,[box],0,(0,0,255),1)
        img = cv2.circle(img,(bx,by),4,(0,255,255),-1)

        # find centroid from moments
        #M = cv2.moments(contours[biggestContourIndex])
        #if(M['m00'] != 0):
        #    cx = int(M['m10']/M['m00'])
        #    cy = int(M['m01']/M['m00'])
        #    img = cv2.circle(img,(cx,cy),4,(255,255,0),-1)
    
    #img = cv2.drawContours(img, contours, biggestContourIndex, (255,255,0), 3)
    #img = cv2.drawContours(img, contours, secondBiggestIndex, (255,0,0), 3)
    for i in range(len(contours)):
        col = cv2.contourArea(contours[i]) / 20
        img = cv2.drawContours(img, contours, i, (0,255-col,col), 1)
    return img, bx, by
Esempio n. 5
0
	def callback(self,data):
		try:
			img = self.bridge.imgmsg_to_cv2(data, "bgr8")
		except CvBridgeError as e:
			print(e)

		#imageHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    		contours = ThreshAndContour(img, self.upper, self.lower)
		contours = contours[1]
    		#output = cv2.bitwise_and(img, img, mask=mask)

    		if len(contours) == 0:
        		return None

		rects = []
		#cv2.drawContours(img,contours,-1, (0,255,0), 3)
		for contour in contours: #adapted from https://github.com/opencv/opencv/blob/master/samples/python/squares.py
			epsilon = cv2.arcLength(contour, True)*0.05
	        	contour = cv2.approxPolyDP(contour, epsilon, True)
	       		if len(contour) == 4 and cv2.isContourConvex(contour):
	            		contour = contour.reshape(-1, 2)
	            		max_cos = np.max([angle_cos( contour[i], contour[(i+1) % 4], contour[(i+2) % 4] ) for i in range(4)])
	            		if max_cos < 0.1:
	                		rects.append(contour)
	        
	        	if len(rects) > 1:
				rects = sorted(contours, key=cv2.contourArea, reverse=True)
				rect1 = cv2.minAreaRect(rects[0])
	            		rect2 = cv2.minAreaRect(rects[1])

	            		if(rect1[1][0] < rect1[1][1]): #Fix wonky angles from opencv (I think)
	                		rect1 = (rect1[0], rect1[1], (rect1[2] + 180) * 180/3.141)
	            		else:
	                		rect1 = (rect1[0], rect1[1], (rect1[2] + 90) * 180/3.141)
	                
	            		if(rect2[1][0] < rect2[1][1]):
	                		rect2 = (rect2[0], rect2[1], (rect2[2] + 180) * 180/3.141)
	            		else:
	                		rect2 = (rect2[0], rect2[1], (rect2[2] + 90) * 180/3.141)

                                box = cv2.boxPoints(rect1)
                                box = np.int0(box)
                                #cv2.drawContours(img,[box],-1,(0,0,255),2)
                                box = cv2.boxPoints(rect2)
                                box = np.int0(box)
                                #cv2.drawContours(img,[box],-1,(0,0,255),2)

				gateLocation = None
	            		gateAxis = None
	            		gateAngle = None                     
	            		gateCenter = (int((rect1[0][0] + rect2[0][0])/2), int((rect1[0][1] + rect2[0][1])/2))
				cv2.circle(img,gateCenter,5,(0,255,0),3)

		try:
			self.image_pub.publish(self.bridge.cv2_to_imgmsg(img,"bgr8"))
		except CvBridgeError as e:
			print(e)
Esempio n. 6
0
def getTagImg(tag, rect, img):
    """
    Extracts the image of the tag from the main image, and rotates it
    appropriately.
    """
    bottom, left, top, right = cv2.boxPoints(rect)
    # drawCorners(bottom, left, top, right, imageTrack)
    try:
        if dist(left, top) < dist(left, bottom):
            pos_slope = False
            theta = math.atan((left[1] - bottom[1]) / (left[0] - bottom[0]))
        else:
            pos_slope = True
            theta = math.atan((right[1] - bottom[1]) / (right[0] - bottom[0]))

    except ZeroDivisionError:
        theta = math.atan(float('inf'))  # slope is pi/2

    height = dist(right, bottom)
    width = dist(right, top)
    if pos_slope:
        width, height = height, width

    f_center = rect[0][0], rect[0][1]
    return subimage(img, f_center, theta, width, height)
Esempio n. 7
0
  def update(self, frame):
    # print "updating %d " % self.id
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    back_project = cv2.calcBackProject([hsv],[0], self.roi_hist,[0,180],1)
    
    if args.get("algorithm") == "c":
      ret, self.track_window = cv2.CamShift(back_project, self.track_window, self.term_crit)
      pts = cv2.boxPoints(ret)
      pts = np.int0(pts)
      self.center = center(pts)
      cv2.polylines(frame,[pts],True, 255,1)
      
    if not args.get("algorithm") or args.get("algorithm") == "m":
      ret, self.track_window = cv2.meanShift(back_project, self.track_window, self.term_crit)
      x,y,w,h = self.track_window
      self.center = center([[x,y],[x+w, y],[x,y+h],[x+w, y+h]])  
      cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 255, 0), 2)

    self.kalman.correct(self.center)
    prediction = self.kalman.predict()
    cv2.circle(frame, (int(prediction[0]), int(prediction[1])), 4, (255, 0, 0), -1)
    # fake shadow
    cv2.putText(frame, "ID: %d -> %s" % (self.id, self.center), (11, (self.id + 1) * 25 + 1),
        font, 0.6,
        (0, 0, 0),
        1,
        cv2.LINE_AA)
    # actual info
    cv2.putText(frame, "ID: %d -> %s" % (self.id, self.center), (10, (self.id + 1) * 25),
        font, 0.6,
        (0, 255, 0),
        1,
        cv2.LINE_AA)
Esempio n. 8
0
    def filter(seg,area,label):
        """
        Apply the filter.
        The final list is ranked by area.
        """
        good = label[area > TextRegions.minArea]
        area = area[area > TextRegions.minArea]
        filt,R = [],[]
        for idx,i in enumerate(good):
            mask = seg==i
            xs,ys = np.where(mask)

            coords = np.c_[xs,ys].astype('float32')
            rect = cv2.minAreaRect(coords)          
            box = np.array(cv2.boxPoints(rect))
            h,w,rot = TextRegions.get_hw(box,return_rot=True)

            f = (h > TextRegions.minHeight 
                and w > TextRegions.minWidth
                and TextRegions.minAspect < w/h < TextRegions.maxAspect
                and area[idx]/w*h > TextRegions.pArea)
            filt.append(f)
            R.append(rot)

        # filter bad regions:
        filt = np.array(filt)
        area = area[filt]
        R = [R[i] for i in range(len(R)) if filt[i]]

        # sort the regions based on areas:
        aidx = np.argsort(-area)
        good = good[filt][aidx]
        R = [R[i] for i in aidx]
        filter_info = {'label':good, 'rot':R, 'area': area[aidx]}
        return filter_info
Esempio n. 9
0
def get_filled_contours(img_path,min_rect_size = 30):
    filled_coordinates = []
    img = cv2.imread(img_path)
    #Make it gray
    imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #reducing noise
    ret,thresh = cv2.threshold(imgray,127,255,0)
    _,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    #hierarchy fix and skipping outer container
    hierarchy = hierarchy[0][1:]
    #the first one is always the outer container skipping it 
    contours = contours[1:]
    for i,cnt in enumerate(contours):
            #[0] = next contour at the same hierarchical level
            #[1] = previous contour at the same hierarchical level
            #[2] = denotes its first child contour
            #[3] = denotes index of its parent contour
            child_index = hierarchy[i][2]
            isClosedShape = child_index != -1
            if isClosedShape:
                # -1 because we skipped the outer contour
                firtstChildHierarchy = hierarchy[child_index-1]
                isFilled = firtstChildHierarchy[0]!=-1
                if isFilled:
                    #Finds the minimun area rectangle for the contour
                    rect = cv2.minAreaRect(cnt)
                    box = cv2.boxPoints(rect)
                    width = box[3][0] - box[0][0]
                    if width< min_rect_size:
                        continue
                    contour_list=list(map(lambda x: x[0], cnt))
                    if not is_boxy(contour_list):
                        continue
                    filled_coordinates.append(formatBox(box))                 
    return filled_coordinates
def barcode_detect(img):
    image = img
    # load the image and convert it to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    sobelgx = cv2.Sobel(gray,cv2.CV_64F,1,0,ksize=3)
    sobelgy = cv2.Sobel(gray,cv2.CV_64F,0,1,ksize=3)

    # subtract the y-gradient from the x-gradient
    gradient = sobelgx - sobelgy
    gradient = cv2.convertScaleAbs(gradient)

    # blur and threshold the image
    blurred = cv2.blur(gradient, (3, 3))
    _, thresh = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)

    # construct a closing kernel and apply it to the thresholded image
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
    closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

    # perform a series of erosions and dilations
    closed = cv2.erode(closed, None, iterations=4)
    closed = cv2.dilate(closed, None, iterations=4)

    # find the contours in the thresholded image, then sort the contours
    # by their area, keeping only the largest one
    (_, contours, _) = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    c = sorted(contours, key=cv2.contourArea, reverse=True)[0]

    # compute the rotated bounding box of the largest contour
    rect = cv2.minAreaRect(c)
    box = np.int0(cv2.boxPoints(rect))

    return box
Esempio n. 11
0
def get_motions(f, fMask, thickness=1, color=(170, 170, 170)):
    '''
    Iterates over the contours in a mask and draws a bounding box
    around the ones that encompas an area greater than a threshold.
    This will return an image of just the draw bock (black bg), and
    also an array of the box points.
    '''
    rects_mot = []
    f_rects = np.zeros(f.shape, np.uint8)
    # get contours
    if imutils.is_cv3():
        _, cnts, hierarchy = cv2.findContours(
            fMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    elif imutils.is_cv2():
        cnts, hierarchy = cv2.findContours(
            fMask.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) < contourThresh:
            continue

        if imutils.is_cv3():
            box = cv2.boxPoints(cv2.minAreaRect(c))
        elif imutils.is_cv2():
            box = cv2.cv.BoxPoints(cv2.minAreaRect(c))

        box = np.int0(box)
        cv2.drawContours(f_rects, [box], 0, color, thickness)
        rects_mot.append(cv2.boundingRect(c))
    return f_rects, rects_mot
Esempio n. 12
0
def get_hp_bars(binary):
    _, contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        center, (height, width), angle = cv2.minAreaRect(cnt)
        if 2 < height < 4 and 5 < width < 80 and 89 < -angle < 91:
            box = cv2.boxPoints((center, (height, width), angle))
            yield np.int0(box)
Esempio n. 13
0
def show_boxes_in_img(img, boxes_and_label):
    '''

    :param img:
    :param boxes: must be int
    :return:
    '''
    boxes_and_label = boxes_and_label.astype(np.int64)
    img = np.array(img, np.float32)
    img = np.array(img*255/np.max(img), np.uint8)
    for box in boxes_and_label:
        x_c, y_c, w, h, theta, label = box[0], box[1], box[2], box[3], box[4], box[5]

        category = LABEl_NAME_MAP[label]

        color = (np.random.randint(255), np.random.randint(255), np.random.randint(255))

        rect = ((x_c, y_c), (w, h), theta)
        rect = cv2.boxPoints(rect)
        rect = np.int0(rect)
        cv2.drawContours(img, [rect], -1, color, 3)

        cv2.putText(img,
                    text=category,
                    org=(x_c, y_c),
                    fontFace=1,
                    fontScale=1,
                    color=(0, 0, 255))

    cv2.imshow('img_', img)
    cv2.waitKey(0)
Esempio n. 14
0
  def run(self):
    while True:
      f, orig_img = self.capture.read()
      orig_img = cv2.flip(orig_img, 1)
      img = cv2.GaussianBlur(orig_img, (5,5), 0)
      img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2HSV)
      img = cv2.resize(img, (len(orig_img[0]) / self.scale_down, len(orig_img) / self.scale_down))
      red_lower = np.array([0, 150, 0],np.uint8)
      red_upper = np.array([5, 255, 255],np.uint8)
      red_binary = cv2.inRange(img, red_lower, red_upper)
      dilation = np.ones((15, 15), "uint8")
      red_binary = cv2.dilate(red_binary, dilation)
      image, contours, hierarchy = cv2.findContours(red_binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
      max_area = 0
      largest_contour = None
      for idx, contour in enumerate(contours):
	area = cv2.contourArea(contour)
	if area > max_area:
	  max_area = area
	  largest_contour = contour
      if not largest_contour == None:
	moment = cv2.moments(largest_contour)
	if moment["m00"] > 1000 / self.scale_down:
  	  rect = cv2.minAreaRect(largest_contour)
	  rect = ((rect[0][0] * self.scale_down, rect[0][1] * self.scale_down), (rect[1][0] * self.scale_down, rect[1][1] * self.scale_down), rect[2])
	  box = cv2.boxPoints(rect)
	  box = np.int0(box)
	  cv2.drawContours(orig_img,[box], 0, (0, 0, 255), 2)
	  cv2.imshow("ColourTrackerWindow", orig_img)
	  if cv2.waitKey(20) == 27:
		cv2.destroyWindow("ColourTrackerWindow")
		self.capture.release()
		break
Esempio n. 15
0
def findPlateNumberRegion(img):
    region = []
    # 查找外框轮廓
    contours_img, contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    print("contours lenth is :%s" % (len(contours)))
    # 筛选面积小的
    for i in range(len(contours)):
        cnt = contours[i]
        print('cnt=',cnt)
        #print(cnt)
        # 计算轮廓面积
        area = cv2.contourArea(cnt)
        print('area=',area)
        # 面积小的忽略
        if area < 2000:
            continue

        # 转换成对应的矩形(最小)
        rect = cv2.minAreaRect(cnt)
        print("rect is:%s" % {rect})

        # 根据矩形转成box类型,并int化
        box = np.int32(cv2.boxPoints(rect))
        print('box=',box)
        # 计算高和宽
        height = abs(box[0][1] - box[2][1])
        width = abs(box[0][0] - box[2][0])
        # 正常情况车牌长高比在2.7-5之间,那种两行的有可能小于2.5,这里不考虑
        ratio = float(width) / float(height)
        if ratio > maxPlateRatio or ratio < minPlateRatio:
            continue
        # 符合条件,加入到轮廓集合
        region.append(box)
    return region
def _get_magic_card_crop(original_image):
    scale_mult = 0.15
    mat = cv2.resize(original_image, (0, 0), fx=scale_mult, fy=scale_mult)
    contour = _get_magic_card_contour(mat)

    # Obtain shape approximation
    card = contour[0]
    peri = cv2.arcLength(card, True)
    approx = cv2.approxPolyDP(card, 0.02 * peri, True)

    x, y, w, h = cv2.boundingRect(card)
    w /= scale_mult
    h /= scale_mult

    w = int(w)
    h = int(h)

    # Test to see if our approximation is a quadrilateral
    if len(approx) != 4:
        rect = rectify(cv2.boxPoints(cv2.minAreaRect(card)))
    else:
        # Great! We can safely apply our perspective correction technique
        rect = rectify(approx)

    for i in range(4):
        # Compute all 4 corners on original image (re-apply scaling)
        rect[i][0] /= scale_mult
        rect[i][1] /= scale_mult
        
    cv2.imwrite(os.path.join(LATEST_DIR, "steps_contours_bon.jpg"), original_image)

    # Output perspective correction to an image of about the same size
    output = np.array([[0, 0], [w-1, 0], [w-1, h-1], [0, h-1]], np.float32)
    transform = cv2.getPerspectiveTransform(rect, output)
    return cv2.warpPerspective(original_image, transform, (w, h))
def draw_window(frame):
    # setup initial location of window
    r,h,c,w = 250,90,400,125  # simply hardcoded the values
    track_window = (c,r,w,h)    

    # set up the ROI for tracking
    roi = frame[r:r+h, c:c+w]
    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 atleast 1 pt
    term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )

    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, 255,2)
    io.imshow(img2)
Esempio n. 18
0
def convex_corners_cross(pil_img, show_plot=False):
    mask_img = alpha_fill(pil_img)

    _,contours,_ = cv2.findContours(mask_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    contour = contours[0]

    rect = cv2.minAreaRect(contour)
    box = cv2.boxPoints(rect)
    box = numpy.int0(box)

    hull = cv2.convexHull(contour,returnPoints = False)
    print(hull)
    defects = cv2.convexityDefects(contour,box)


    mask_img = cv2.cvtColor(mask_img, cv2.COLOR_GRAY2RGB)
    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])
        cv2.line(mask_img,start,end,[0,255,0],2)
        cv2.circle(mask_img,far,5,[0,0,255],-1)
    cv2.imshow('img',mask_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Esempio n. 19
0
def crop_cardboard_old(image):
    """
    :param image: A numpy array representing the full scanned document extracted from the RAW file
    :return: A subsection of the document representing the cardboard only
    """
    ratio = image.shape[0] / RESIZE_HEIGHT
    orig = image.copy()
    image = cv2.resize(image, (int(image.shape[1] / ratio), int(RESIZE_HEIGHT)))

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    (_, contours, _) = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    max_area = 0
    max_box = None
    for contour in contours:
        rect = cv2.minAreaRect(contour)
        (_, (w, h), _) = rect
        area = w * h
        if area > max_area:
            max_area = area
            max_box = cv2.boxPoints(rect)
    max_box = max_box.astype(np.int)

    cardboard = utils.crop_rectangle_warp(orig, max_box.reshape(4, 2), ratio)
    return cardboard
Esempio n. 20
0
def createImage(image_contour, image, counter):
    rectangle = cv2.minAreaRect(image_contour)
    boxed = cv2.boxPoints(rectangle)
    boxed = np.int0(boxed)
    print(boxed)
    print("----------------")
    mins = (np.amin(boxed, axis=0))
    max = (np.amax(boxed, axis=0))
    """
    print(mins[0])
    print(mins[1])
    print(mins[0],max[0],
          mins[1],max[0],
          mins[0],max[1],
          mins[1],max[1],)
    """
    width = max[0] - mins[0]
    height = max[1] - mins[1]
    print('-------------')
    print(width, height)
    if (width > 10) & (height > 10):
        cropped_image = image[mins[1]:mins[1]+height, mins[0]:mins[0]+width]
        counter += 1
        print(counter)
        """
        try:
            displayImage(cropped_image)
        except:
            print('odd nematode')
        """
    return counter
Esempio n. 21
0
def do_camshift(vec=0):
    global frame, roiBox
    
    # convert the current frame to the HSV color space and perform mean shift
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)

    # apply cam shift to the back projection, convert the points to a bounding box, and then draw them
    (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
    pts = np.int0(cv2.boxPoints(r))
    cv2.polylines(frame, [pts], True, (0,255,0), 2)

    centerScr = get_centerScreen(frame)
    x = centerScr[0]
    y = centerScr[1]
    
    # print "+" in the center of screen
    cv2.line(frame,(x,y-10),(x,y+10),(0,0,255),2) # print line
    cv2.line(frame,(x-10,y),(x+10,y),(0,0,255),2) # print line
   # cv2.line(frame,(x,y),(x+80,y),(0,255,0),2) # print line
    
    
    centerRoi = get_centerRoi(pts)    
    x = centerRoi[0]
    y = centerRoi[1]
    
    # print "+" in the center of ROI
    cv2.line(frame,(x,y-10),(x,y+10),(0,255,0),2) # print line
    cv2.line(frame,(x-10,y),(x+10,y),(0,255,0),2) # print line
    
    
    gimbel_move(pts,frame,vec)
    lineRoiTocenter(centerRoi,centerScr)
    print_data(vec)
    tragetAcquired(pts)
Esempio n. 22
0
def get_rotated_rec(cnt):
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    angle_of_rotation = rect[2]
    box = np.int0(box)

    return box, angle_of_rotation
Esempio n. 23
0
def detect_landmark(color_min, color_max):
    # Algorithm To Detect the Contour
    # implemented here

    # Find the Landmark wrt to Color
    mask = detect_in_range(color_min, color_max)

    # Find the Contour to Estimate the bounding box (for pixel estimation)
    # cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    _, cnts, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    # Cannot Find Contour => Bye GG
    if len(cnts) == 0:
        return 0, 0, 0, 0

    # Find Max
    cnt = max(cnts, key=cv2.contourArea)

    # Normal Bounding Box [OpenCV 2.4.X]
    # xx, yy, ww, hh = cv2.boundingRect(cnt)

    # Rotating Box [OpenCV 3+]
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    return box
    def frame_track(self, cur_frame):
        vis = cur_frame.copy()
        hsv = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))

        prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
        prob &= mask
        term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
        # get rotated bounding box
        track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit)
        # get bounding box
        track_rect_rotate = cv2.boxPoints(track_box)
        # get rectangle 
        track_rect = cv2.boundingRect(track_rect_rotate)

        # store them in yaml compatible format
        track_rect_yaml=track_rect

        if self.show_backproj:
            vis[:] = prob[...,np.newaxis]

        pt1 = (track_rect_yaml[0],track_rect_yaml[1])
        pt2 = (track_rect_yaml[0] + track_rect_yaml[2], track_rect_yaml[1] + track_rect_yaml[3])
        cv2.rectangle(vis, pt1, pt2, (0, 255, 0), 2)
        cv2.ellipse(vis, track_box, (0, 0, 255), 2)

        self.add_frame_to_dataset_based_on_difference(cur_frame, track_rect_yaml)
        return vis
Esempio n. 25
0
def test():
    # p1 = Point(5, 1)
    # p2 = Point(1, 4)
    # l1 = Line(p2, p1)
    # print(l1.k)
    #
    # p1 = Point(5, 4)
    # p2 = Point(1, 1)
    # l1 = Line(p1, p2)
    # print(l1.k)

    pnts = [(40, 40), (140, 85), (140, 160), (50, 100)]
    img = np.zeros((200, 200, 3))
    a = cv2.minAreaRect(np.asarray(pnts))

    img = cv2.line(img, pnts[0], pnts[1], (0, 255, 0), thickness=1)
    img = cv2.line(img, pnts[1], pnts[2], (0, 255, 0), thickness=1)
    img = cv2.line(img, pnts[2], pnts[3], (0, 255, 0), thickness=1)
    img = cv2.line(img, pnts[3], pnts[0], (0, 255, 0), thickness=1)

    box = cv2.boxPoints(a)

    def tt(p):
        return (p[0], p[1])

    img = cv2.line(img, tt(box[0]), tt(box[1]), (255, 255, 0), thickness=1)
    img = cv2.line(img, tt(box[1]), tt(box[2]), (255, 255, 0), thickness=1)
    img = cv2.line(img, tt(box[2]), tt(box[3]), (255, 255, 0), thickness=1)
    img = cv2.line(img, tt(box[3]), tt(box[0]), (255, 255, 0), thickness=1)

    cv2.imshow('test', img.astype(np.uint8))
    cv2.waitKey(0)
Esempio n. 26
0
def estimate_bbox(cnt, img):
	# calculate bounding box
	rect = cv2.minAreaRect(cnt)
	bbox = cv2.boxPoints(rect)
	bbox = np.int0(bbox)
	#cv2.drawContours(img, [bbox], 0, (0,255,0), 2)

	# rotate bounding box to get a vertical rectangle
	M = cv2.getRotationMatrix2D(rect[0], rect[2], 1)
	pts = np.ones((4, 3))
	pts[:,:-1] = bbox
	bbox_rot = np.int0(np.dot(pts, M.T))

	# resize bounding box to cover the whole document
	bbox_rot[0][0] -= 15
	bbox_rot[0][1] += 120
	bbox_rot[1][0] -= 15
	bbox_rot[2][0] += 5
	bbox_rot[3][0] += 5
	bbox_rot[3][1] += 120

	# rotate back bounding box to original orientation
	p = (bbox_rot[1][0], bbox_rot[1][1])
	M = cv2.getRotationMatrix2D(p, -rect[2], 1)
	pts = np.ones((4, 3))
	pts[:,:-1] = bbox_rot
	bbox = np.int0(np.dot(pts, M.T))
	return bbox
Esempio n. 27
0
    def find_cutout_candidates(self, red_contours):
        self.candidates_drawing = np.copy(self.mat)
        
        # Eliminate some contours based on how much they look like cutouts
        cutout_candidates = []
        for contour in red_contours:
            cutout_bounding_rect = cv2.boundingRect(contour)
            area = cv2.contourArea(contour)
        
            min_area_rect = cv2.minAreaRect(contour)
            cutout_center = (int(min_area_rect[0][0]), int(min_area_rect[0][1]))
        
            box = np.int0(cv2.boxPoints(min_area_rect))
            rectangularity = area / cv2.contourArea(box)
            
            epsilon = max([min_area_rect[1][1], min_area_rect[1][0]]) * 0.1
            polygon = cv2.approxPolyDP(contour, epsilon, True)
         
            if self.mode == TorpedoesMode.track_target.value:
                min_rect = 0.5
            else:
                min_rect = self.options["min_cutout_rect"]

            if rectangularity > min_rect: # and area / self.img_area > self.options["min_cutout_area"]:
            # if polygon.shape[0] == 4:
                cutout_candidates.append(Cutout(contour=contour, area=area,
                    bounding_rect=cutout_bounding_rect, center=cutout_center, 
                    min_area_rect=min_area_rect, polygon=polygon))
                cv2.drawContours(self.candidates_drawing, [contour], -1, (255, 255, 255), 3)
                # cv2.drawContours(self.candidates_drawing, [polygon], -1, (255,255,255), 2)

        self.post_if_enabled('cutout candidates', self.candidates_drawing)

        return cutout_candidates
    def findContours(self):
        n, self.contours, h = cv.findContours(self.thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        shot = False
        for cntr in self.contours:
            self.rect = cv.minAreaRect(cntr)
            self.box = cv.boxPoints(self.rect)
            self.box = np.int0(self.box)

            if set(self.box[0]) != set(self.box[1]):
                cv.drawContours(self.frame, [self.box], 0, (0, 0, 255), 2)

                self.box = sorted(self.box, key=lambda point: point[1])
                topLeft = self.box[0]
                topRight = self.box[1]
                print(topLeft)
                print(topRight)
                print(abs(topLeft[0] - topRight[0]))

                midpoint = (int((topLeft[0] + topRight[0]) / 2), int((topLeft[1] + topRight[1]) / 2))

                if not shot:
                    shot = self.checkShot(midpoint)

                cv.circle(self.frame, midpoint, 5, (0, 255, 0), thickness=2, lineType=8, shift=0)

                self.getTheta(midpoint)

        return shot
def select_note_heads_whole(image_orig, image_bin, groups,dicts,regions):

    img, contours, hierarchy = cv2.findContours(image_bin.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    regions_dict = {}
    for contour in contours:
        rect = cv2.minAreaRect(contour)
        box = cv2.boxPoints(rect)
        box = np.int0(box)

        (x,y),radius = cv2.minEnclosingCircle(contour)
        center = (int(x),int(y))
        radius = int(radius)

        if radius==9:
            print 'rad ',radius
            cv2.circle(image_orig,center,radius,(0,255,0),2)

            # cv2.drawContours(image_orig,[box],0,(0,0,255),2)
            (x,y),(w,h),angle = rect
            #if angle < -40 and angle > -50 and w > 6 and w<8 and h > 5:
            # if w<8 and w > 4  and h> 9 and angle > -1:
            #     print '////////////////'
            #     print w,h,angle
            x,y,w,h = cv2.boundingRect(contour)
            region = image_bin[y:y+h+1,x:x+w+1]
            regions_dict[x] = [img_fun.resize_region(region), (x,y,w,h)]
            # cv2.rectangle(image_orig,(x,y),(x+w,y+h),(0,255,0),2)
            row = find_row(groups,y)
            dicts[row][x] = [img_fun.resize_region(region),(x,y,w,h)]
            r = reg.Region(x,y,w,h)
            regions.add_region(r)

    return image_orig,dicts,regions
def create_rectangle(thresh,cnt,cx,cy):
    thresh = cv2.circle(thresh,(cx,cy), 1, (0,0,0), -1)
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    thresh = cv2.drawContours(thresh,[box],0,(0,0,255),2)
    return thresh
Esempio n. 31
0
def draw_inflorescence(res, zoom_ratio):
    # gray scale

    gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
    # Gaussian filter

    gray = cv2.GaussianBlur(gray, (7, 7), 0)
    # detect the edge

    edged = cv2.Canny(gray, 50, 100)
    # close the gap between edges

    edged = cv2.dilate(edged, None, iterations=1)
    edged = cv2.erode(edged, None, iterations=1)
    # find contour of the object

    cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    # sort the contour from left to right

    (cnts, _) = contours.sort_contours(cnts)

    # initialize 'pixels per metric'

    pixelsPerMetric = None

    # Loop through each contour

    for c in cnts:
        # If the area of the current contour is too small, consider it may be noise, and ignore it

        if cv2.contourArea(c) < 50:
            continue
        # Calculate the outcut rectangle according to the contour of the object

        orig = image.copy()
        box = cv2.minAreaRect(c)
        box = \
            (cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box))
        box = np.array(box, dtype='int')

        # Sort the contour points according to the order of top-left, top-right, bottom-right and bottom-left,
        # and draw the BB of outer tangent, which is represented by green line

        box = perspective.order_points(box)
        cv2.drawContours(orig, [box.astype('int')], -1, (0, 0, 255), 1)

        # Draw the four vertices of BB, represented by small red circles

        for (x, y) in box:
            cv2.circle(orig, (int(x), int(y)), 1, (0, 0, 255), -1)

        # Calculate the center point coordinates of top-left
        # and top-right and bottom-left and bottom-right respectively

        (tl, tr, br, bl) = box
        (tltrX, tltrY) = midpoint(tl, tr)
        (blbrX, blbrY) = midpoint(bl, br)

        # Calculate the center point coordinates of top-left and top-right and top-righ and bottom-right respectively

        (tlblX, tlblY) = midpoint(tl, bl)
        (trbrX, trbrY) = midpoint(tr, br)

        # Draw the center point of the four edges of BB, represented by a small blue circle
        # cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
        # cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
        # cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
        # cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)

        # Draw a line between the center points, indicated by a magenta line
        # cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
        # ....(255, 0, 255), 2)
        # cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
        # ....(255, 0, 255), 2)

        # Calculate the Euclidean distance between two center points, that is, the distance of the picture

        height = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
        width = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))

        # Initialize the measurement index value, the width of the reference object in the picture
        #  has been calculated by Euclidean distance, and the actual size of the reference object is known

        # if pixelsPerMetric is None:
        # ....pixelsPerMetric = dB / args["width"]

        # Calculate the actual size (width and height) of the target, expressed in feet

        real_height = round(height * zoom_ratio, 2)
        real_width = round(width * zoom_ratio, 2)

        # Draw the result in the image

        cv2.putText(
            orig,
            '{:.1f}'.format(real_width),
            (int(tltrX - 15), int(tltrY - 10)),
            cv2.FONT_HERSHEY_SIMPLEX,
            0.65,
            (0, 0, 0),
            2,
        )
        cv2.putText(
            orig,
            '{:.1f}'.format(real_height),
            (int(trbrX + 10), int(trbrY)),
            cv2.FONT_HERSHEY_SIMPLEX,
            0.65,
            (0, 0, 0),
            2,
        )

        # show result
        cv2.imshow('Orig', orig)
        cv2.waitKey(0)
    return (real_height, real_width)
hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
mask = cv.inRange(hsv_roi, np.array((0., 60., 32.)), np.array(
    (180., 255., 255)))
roi_hist = cv.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINMAX)

term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)
cv.imshow('roi', roi)
while (1):
    ret, frame = cap.read()
    if ret == True:

        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
        dst = cv.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
        ret, track_window = cv.CamShift(dst, track_window, term_crit)

        pts = cv.boxPoints(ret)
        print(pts)
        pts = np.int0(pts)
        final_image = cv.polylines(frame, [pts], True, (0, 255, 0), 2)

        cv.imshow('dst', dst)
        cv.imshow('final_image', final_image)
        k = cv.waitKey(30) & 0xff
        if k == 27:
            break
    else:
        break

cap.release()
cv.destroyAllWindows()
Esempio n. 33
0
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

cv2.imshow("Kernel", kernel)
cv2.imshow("Closed", closed)

#进行腐蚀和膨胀

closed = cv2.erode(closed, None, iterations=4)
closed = cv2.dilate(closed, None, iterations=4)

cv2.imshow("Closed", closed)

#找到轮廓

# find the contours in the thresholded image, then sort the contours
# by their area, keeping only the largest one
(_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]

# compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect))

# draw a bounding box arounded the detected barcode and display the
# image
cv2.drawContours(image, [box], -1, (0, 255, 0), 3)
cv2.imshow("Image", image)

cv2.waitKey(0)
Esempio n. 34
0
        # Look for children with exactly one parent

        while (hierarchy[0][k][3] != -1):
            # As long as k has a first_child [2], find that child and look for children again.
            # http://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html
            k = hierarchy[0][k][3]
            c = c + 1

        if hierarchy[0][k][3] != -1:
            c = c + 1

        if c == 1 and MIN_CONTOUR_AREA < area < MAX_CONTOUR_AREA:
            # Fit a rectangle around the found contour and draw it.
            rotated_rect = cv2.minAreaRect(contour)  #(center, size, angle)
            rotation = rotated_rect[2]
            box = cv2.boxPoints(rotated_rect).astype(int)
            cv2.drawContours(img, [box], -1, (0, 255, 0))

            # Fit a circle and draw it
            circle = cv2.minEnclosingCircle(contour)  # (center, size, angle)
            radius = int(circle[1])
            center = tuple(np.array(circle[0], int))
            offset_center_x = center[0] + int(
                (center[0] - width / 2) / width * h_crop_offset)
            offset_center_y = center[1] + int(
                (center[1] - height) / height * v_crop_offset)
            # print()
            cv2.circle(img, center, radius, (0, 255, 0))
            cv2.circle(img, (offset_center_x, offset_center_y), radius // 2,
                       (0, 255, 0))
    print('Invalid algorithm choice')
    sys.exit()

imagePaths = sorted(list(paths.list_images(input_dir)))
for imagePath in imagePaths:
    iteration += 1
    originimage = cv2.imread(imagePath)
    image = imutils.resize(originimage, width=400, height=400)

    image = cv2.bilateralFilter(image, 3, 105, 105)
    anpr.debug_imshow("Bilateral Filter", image, waitKey=True)

    (lpText, lpCnt) = anpr.find_and_ocr(iteration,
                                        image,
                                        psm=args["psm"],
                                        clearBorder=args["clear_border"] > 0)

    if lpText is not None and lpCnt is not None:
        box = cv2.boxPoints(cv2.minAreaRect(lpCnt))
        box = box.astype("int")
        cv2.drawContours(image, [box], -1, (0, 255, 0), 2)

        (x, y, w, h) = cv2.boundingRect(lpCnt)
        cv2.putText(image, cleanup_text(lpText), (x, y - 15),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2)

        print("[INFO] Registration number: {}".format(lpText))
        anpr.debug_imshow("Output ANPR", image, waitKey=True)

    anpr.save_result("Final{}.jpg".format(iteration), image)
    cv2.destroyAllWindows()
Esempio n. 36
0
cnt = contours[k]
dst2 = src.copy()
cv2.drawContours(dst2, [cnt], 0, (255,0,0), 3)
##cv2.imshow('dst2',  dst2)

#3
area = cv2.contourArea(cnt)
print('area=', area)
x, y, width, height = cv2.boundingRect(cnt)
dst3 = dst2.copy()
cv2.rectangle(dst3, (x, y), (x+width, y+height), (0,0,255), 2)
cv2.imshow('dst3',  dst3)

#4
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int32(box)
print('box=', box)
dst4 = dst2.copy()
cv2.drawContours(dst4,[box],0,(0,0,255),2)
cv2.imshow('dst4',  dst4)

#5
(x,y),radius = cv2.minEnclosingCircle(cnt)
dst5 = dst2.copy()
cv2.circle(dst5,(int(x),int(y)),int(radius),(0,0,255),2)
cv2.imshow('dst5',  dst5)

cv2.waitKey()
cv2.destroyAllWindows()
Esempio n. 37
0
def rotate_nms_gpu(dets, iou_thr, device_id=None):
    """Dispatch to either CPU or GPU NMS implementations.

    The input can be either a torch tensor or numpy array. GPU NMS will be used
    if the input is a gpu tensor or device_id is specified, otherwise CPU NMS
    will be used. The returned type will always be the same as inputs.

    Arguments:
        dets (torch.Tensor or np.ndarray): bboxes with scores.
        iou_thr (float): IoU threshold for NMS.
        device_id (int, optional): when `dets` is a numpy array, if `device_id`
            is None, then cpu nms is used, otherwise gpu_nms will be used.

    Returns:
        tuple: kept bboxes and indice, which is always the same data type as
            the input.

    Example:
        >>> dets = np.array([[49.1, 32.4, 51.0, 35.9, 0.9],
        >>>                  [49.3, 32.9, 51.0, 35.3, 0.9],
        >>>                  [49.2, 31.8, 51.0, 35.4, 0.5],
        >>>                  [35.1, 11.5, 39.1, 15.7, 0.5],
        >>>                  [35.6, 11.8, 39.3, 14.2, 0.5],
        >>>                  [35.3, 11.5, 39.9, 14.5, 0.4],
        >>>                  [35.2, 11.7, 39.7, 15.7, 0.3]], dtype=np.float32)
        >>> iou_thr = 0.7
        >>> suppressed, inds = nms(dets, iou_thr)
        >>> assert len(inds) == len(suppressed) == 3
    """

    assert dets.shape[-1] == 6
    # convert dets (tensor or numpy array) to tensor
    if isinstance(dets, torch.Tensor):
        is_numpy = False
        dets_th = dets[:, :5]  #ignore score
    elif isinstance(dets, np.ndarray):
        is_numpy = True
        device = 'cpu' if device_id is None else 'cuda:{}'.format(device_id)
        dets_th = torch.from_numpy(dets).to(device)[:, :5]  #ignore score
    else:
        raise TypeError(
            'dets must be either a Tensor or numpy array, but got {}'.format(
                type(dets)))

    # execute cpu or cuda nms
    if dets_th.shape[0] == 0:
        inds = dets_th.new_zeros(0, dtype=torch.long)
    else:
        if dets_th.is_cuda:

            dets_th = dets_th.cpu().numpy()

            # WARNING:  convert predicted [top_left, bottom_right] to [center w h]
            d_w = dets_th[:, 2:3] - dets_th[:, 0:1]
            d_h = dets_th[:, 3:4] - dets_th[:, 1:2]
            d_ctr_x = dets_th[:, 0:1] + d_w * 0.5
            d_ctr_y = dets_th[:, 1:2] + d_h * 0.5
            d_theta = dets_th[:, 4:5]
            dets_th = np.concatenate([d_ctr_x, d_ctr_y, d_w, d_h, d_theta],
                                     axis=-1)

            for idx in range(dets_th.shape[0]):
                rot_box = ((dets_th[idx, 0], dets_th[idx, 1]),
                           (dets_th[idx, 2], dets_th[idx, 3]),
                           np.degrees(dets_th[idx, 4]))

                corner_pt = cv2.boxPoints(rot_box)
                rot_box_edge = [
                    corner_pt[1] - corner_pt[0], corner_pt[3] - corner_pt[0]
                ]
                np.linalg.norm(rot_box_edge)
                wh_idx = sorted((np.linalg.norm(edge), idx)
                                for idx, edge in enumerate(rot_box_edge))
                long_axis = rot_box_edge[wh_idx[1][1]]
                theta = np.arctan2(long_axis[1], long_axis[0])
                if theta > np.pi / 2.0:
                    theta -= np.pi
                if theta < -np.pi / 2.0:
                    theta += np.pi

                dets_th[idx, 2] = float(wh_idx[1][0])
                dets_th[idx, 3] = float(wh_idx[0][0])
                dets_th[idx, 4] = float(-theta * 180 / np.pi)

            dets_th = torch.from_numpy(dets_th).to('cuda')
            # [top_left, bottom_right] as input rotate along box center
            inds = nms_cuda.rotate_nms(dets_th, iou_thr)

        else:
            raise ValueError('RoateNMS Not Support CPU')

    if is_numpy:
        inds = inds.cpu().numpy()
    return dets[inds, :], inds
Esempio n. 38
0
def test(args):
    import torch
    data_loader = IC15TestLoader(root_dir=args.root_dir,
                                 long_size=args.long_size)
    test_loader = torch.utils.data.DataLoader(data_loader,
                                              batch_size=1,
                                              shuffle=False,
                                              num_workers=2,
                                              drop_last=True)

    # Setup Model
    if args.arch == "resnet50":
        model = models.resnet50(pretrained=False,
                                num_classes=1,
                                scale=args.scale,
                                train_mode=False)
    elif args.arch == "resnet101":
        model = models.resnet101(pretrained=True,
                                 num_classes=1,
                                 scale=args.scale)
    elif args.arch == "resnet152":
        model = models.resnet152(pretrained=True,
                                 num_classes=1,
                                 scale=args.scale)

    for param in model.parameters():
        param.requires_grad = False

    if args.gpus > 0:
        model = model.cuda()

    if args.resume is not None:
        if os.path.isfile(args.resume):
            print("Loading model and optimizer from checkpoint '{}'".format(
                args.resume))
            device = torch.device('cpu') if args.gpus < 0 else None
            checkpoint = torch.load(args.resume, map_location=device)

            # model.load_state_dict(checkpoint['state_dict'])
            d = collections.OrderedDict()
            for key, value in checkpoint['state_dict'].items():
                tmp = key[7:]
                d[tmp] = value
            model.load_state_dict(d)

            print("Loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
            sys.stdout.flush()
        else:
            print("No checkpoint found at '{}'".format(args.resume))
            sys.stdout.flush()

    if args.onnx:
        import torch.onnx.symbolic_opset9
        dummy_input = torch.autograd.Variable(torch.randn(1, 3, 640,
                                                          640)).cpu()
        torch.onnx.export(model, dummy_input, 'dbnet.onnx', verbose=False)
    model.eval()

    total_frame = 0.0
    total_time = 0.0
    for idx, (org_img, img, scale_val) in enumerate(test_loader):
        print('progress: %d / %d' % (idx, len(test_loader)))
        sys.stdout.flush()
        if args.gpus > 0:
            img = Variable(img.cuda(), volatile=True)
        org_img = org_img.numpy().astype('uint8')[0]
        text_box = org_img.copy()
        if args.gpus > 0:
            torch.cuda.synchronize()
        start = time.time()

        outputs = model(img)

        probability_map, threshold_map, binarization_map = outputs

        score = binarization_map[0, 0]
        text = torch.where(score > 0.9, torch.ones_like(score),
                           torch.zeros_like(score))
        text = text.data.cpu().numpy().astype(np.uint8)

        prob_map = probability_map.cpu().numpy()[0, 0] * 255
        thre_map = threshold_map.cpu().numpy()[0, 0] * 255
        bin_map = binarization_map.cpu().numpy()[0, 0] * 255

        out_path = 'outputs/vis_ic15/'
        image_name = data_loader.img_paths[idx].split('/')[-1].split('.')[0]
        print("im_name:", image_name)
        # cv2.imwrite(out_path + image_name + '_prob.png', prob_map.astype(np.uint8))
        # cv2.imwrite(out_path + image_name + '_thre.png' , thre_map.astype(np.uint8))
        cv2.imwrite(out_path + image_name + '_bin.png',
                    bin_map.astype(np.uint8))

        scale = (org_img.shape[1] * 1.0 / img.shape[1],
                 org_img.shape[0] * 1.0 / img.shape[0])
        print("[shape_info:]", text.shape, img.shape, org_img.shape, scale,
              scale_val)
        bboxes = []
        scale_val = scale_val.cpu().numpy()

        nLabels, labels, stats, centroids = cv2.connectedComponentsWithStats(
            text.astype(np.uint8), connectivity=4)
        img_h, img_w = text.shape

        for k in range(1, nLabels):
            # size filtering
            size = stats[k, cv2.CC_STAT_AREA]
            if size < 100:
                continue

            # make segmentation map
            segmap = np.zeros(text.shape, dtype=np.uint8)
            segmap[labels == k] = 255
            x, y = stats[k, cv2.CC_STAT_LEFT], stats[k, cv2.CC_STAT_TOP]
            w, h = stats[k, cv2.CC_STAT_WIDTH], stats[k, cv2.CC_STAT_HEIGHT]
            # niter = int(math.sqrt(size * min(w, h) / (w * h)) * 2)
            # sx, ex, sy, ey = x - niter, x + w + niter + 1, y - niter, y + h + niter + 1
            # # boundary check
            # if sx < 0 : sx = 0
            # if sy < 0 : sy = 0
            # if ex >= img_w: ex = img_w
            # if ey >= img_h: ey = img_h
            # kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(1 + niter, 1 + niter))
            # segmap[sy:ey, sx:ex] = cv2.dilate(segmap[sy:ey, sx:ex], kernel)
            np_contours = np.roll(np.array(np.where(segmap != 0)), 1,
                                  axis=0).transpose().reshape(-1, 2)
            rectangle = cv2.minAreaRect(np_contours)
            box = cv2.boxPoints(rectangle) * 4
            box = box / scale_val
            box = box.astype('int32')
            bboxes.append(box)

        # find contours
        bboxes = np.array(bboxes)
        num_box = bboxes.shape[0]
        try:
            unshrink_bboxes = unshrink(bboxes.reshape((num_box, -1, 2)))
        except:
            continue
        for i in range(unshrink_bboxes.shape[0]):
            cv2.drawContours(text_box, [unshrink_bboxes[i]], -1, (0, 255, 255),
                             2)
        if args.gpus > 0:
            torch.cuda.synchronize()
        end = time.time()
        total_frame += 1
        total_time += (end - start)
        print('fps: %.2f' % (total_frame / total_time))
        sys.stdout.flush()

        for bbox in bboxes:
            cv2.drawContours(text_box, [bbox.reshape(4, 2)], -1, (0, 255, 0),
                             2)

        image_name = data_loader.img_paths[idx].split('/')[-1].split('.')[0]
        write_result_as_txt(image_name, bboxes.reshape((-1, 8)),
                            'outputs/submit_ic15/')

        # text_box = cv2.resize(text_box, (text.shape[1], text.shape[0]))
        debug(idx, data_loader.img_paths, [[text_box]], 'outputs/vis_ic15/')
Esempio n. 39
0
def main():
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", help="path to the (optional) video file")
    args = vars(ap.parse_args())

    # grab the reference to the current frame, list of ROI
    # points and whether or not it is ROI selection mode
    global frame, roiPts, inputMode

    # if the video path was supplied, grab the reference to the camera
    if not args.get("video", False):
        camera = cv2.VideoCapture(0)

    # otherwise, load the video
    else:
        camera = cv2.VideoCapture(args["video"])

    # setup the mouse callback
    cv2.namedWindow("frame")
    cv2.setMouseCallback("frame", selectROI)

    # initialize the termination criteria for cam shift, indicating
    # a maximum of ten iterations or movement by at least one pixel
    # along with the bounding box of the ROI
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    roiBox = None

    # keep looping over the frames
    while True:
        # grab the current frame
        (grabbed, frame) = camera.read()

        # check to see if we have reached the end of the
        # video
        if not grabbed:
            break

        # see if the ROI has been computed
        if roiBox is not None:
            # convert the current frame to the HSV color space
            # and perform mean shift
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)

            # apply cam shift to the back projection, convert the
            # points to a bounding box, and then draw them
            (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
            pts = np.int0(cv2.boxPoints(r))
            cv2.polylines(frame, [pts], True, (0, 255, 0), 2)

        # show the frame and record if the user presses a key
        cv2.imshow("frame", frame)
        key = cv2.waitKey(1) & 0xFF

        # handle if the 'i' key is pressed, then go into ROI selection mode
        if key == ord('i') and len(roiPts) < 4:
            # indicate that we are in input mode and clone the frame
            inputMode = True
            orig = frame.copy()

            # keep looping until 4 reference ROI points have been selected;
            # press any key to exit ROI selection mode once 4 points have
            # been selected
            while len(roiPts) < 4:
                cv2.imshow("frame", frame)
                cv2.waitKey(0)

            # determine the top-left and bottom-right points
            roiPts = np.array(roiPts)
            s = roiPts.sum(axis=1)
            tl = roiPts[np.argmin(s)]
            br = roiPts[np.argmax(s)]

            # grab the ROI for the bounding box and convert it
            # to the HSV color space
            roi = orig[tl[1]:br[1], tl[0]:br[0]]
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

            # compute the HSV histogram for the ROI and store the
            # bounding box
            roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180])
            roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)
            roiBox = (tl[0], tl[1], br[0], br[1])

        #if the 'q' key is pressed, stop the loop
        elif key == ord("q"):
            break
while True:
    (grabbed, frame) = camera.read()

    #if not grabbed:
    #print "not grabbed"
    #break

    blue = cv2.inRange(frame, blueLower, blueUpper)
    blue = cv2.GaussianBlur(blue, (3, 3), 0)

    _, cnts, _ = cv2.findContours(blue.copy(), cv2.RETR_EXTERNAL,
                                  cv2.CHAIN_APPROX_SIMPLE)

    if len(cnts) > 0:
        cnt = sorted(cnts, key=cv2.contourArea, reverse=True)[0]

        rect = np.int32(cv2.boxPoints(cv2.minAreaRect(cnt)))
        cv2.drawContours(frame, [rect], -1, (0, 255, 0), 2)

    cv2.imshow("Original Feedback", frame)
    cv2.imshow("HSV Binary Feddback", blue)

    time.sleep(0.025)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

camera.release()
cv2.destroyAllWindows()
Esempio n. 41
0
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    # sort the contours from left-to-right and initialize the
    # 'pixels per metric' calibration variable
    (cnts, _) = contours.sort_contours(cnts)
    pixelsPerMetric = None

    # loop over the contours individually
    for c in cnts:
        # if the contour is not sufficiently large, ignore it
        if cv2.contourArea(c) < 100:
            continue
        # compute the rotated bounding box of the contour
        orig = frame.copy()
        box = cv2.minAreaRect(c)
        box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
        box = np.array(box, dtype="int")

        # order the points in the contour such that they appear
        # in top-left, top-right, bottom-right, and bottom-left
        # order, then draw the outline of the rotated bounding
        # box
        box = perspective.order_points(box)
        cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)

        # loop over the original points and draw them
        for (x, y) in box:
            cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)

        # unpack the ordered bounding box, then compute the midpoint
        # between the top-left and top-right coordinates, followed by
Esempio n. 42
0
def parse(imagePath):
    # 读取图片
    img = cv2.imread(imagePath)

    # 转化成灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 利用Sobel边缘检测生成二值图
    sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
    # 二值化
    ret, binary = cv2.threshold(sobel, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)

    # 膨胀、腐蚀
    element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 9))
    element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (24, 6))

    # 膨胀一次,让轮廓突出
    dilation = cv2.dilate(binary, element2, iterations=1)

    # 腐蚀一次,去掉细节
    erosion = cv2.erode(dilation, element1, iterations=1)

    # 再次膨胀,让轮廓明显一些
    dilation2 = cv2.dilate(erosion, element2, iterations=2)

    #  查找轮廓和筛选文字区域
    region = []
    image,contours, hierarchy = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for i in range(len(contours)):
        cnt = contours[i]

        # 计算轮廓面积,并筛选掉面积小的
        area = cv2.contourArea(cnt)
        if (area < 1000):
            continue

        cv2.drawContours(img, [cnt], 0, (0, 255, 0), 1)

        # 找到最小的矩形
        rect = cv2.minAreaRect(cnt)
        print ("rect is: ")
        print (rect)

        # box是四个点的坐标
        box = cv2.boxPoints(rect)
        box = np.int0(box)

        # 计算高和宽
        height = abs(box[0][1] - box[2][1])
        width = abs(box[0][0] - box[2][0])

        # 根据文字特征,筛选那些太细的矩形,留下扁的
        if (height > width * 1.3):
            continue

        region.append(box)


    # # 绘制轮廓
    for box in region:
        cv2.drawContours(img, [box], 0, (0, 0, 255), 2)

    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Esempio n. 43
0
def Demo(scaleim, cam, vector_w, normalValue=40, offset=5):
    Distance = 1000
    ob_in_area = 0
    sdetect = 0
    frball = 0
    frta = 0
    frkhoa = 0
    nhan_dang = 0
    term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    track_window = (0, 0, 95 - 75, 420 - 220)
    Image_show = cv2.imread('An/imagezero.jpg')
    out = cv2.VideoWriter('An/result_backup.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (1280, 480))

    net = cv2.dnn.readNetFromDarknet("Model/YOLOv3/yolov3.cfg", "Model/YOLOv3/yolov3_best.weights")
    net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
    net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
    classes = []
    with open("Model/YOLOv3/obj.names", "r") as f:
        classes = [line.strip() for line in f.readlines()]
    layer_names = net.getLayerNames()
    output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

    # tai anh/video
    start = time.time()
    cap = cv2.VideoCapture(cam)

    frame_id = 0
    inv_w_array = [0, 0, 0, 0, 0, 0]
    while True:
        ok, frame = cap.read()
        #frame = QuanLib.gamma_correct()
        frame_id += 1
        if not ok:
            break
        img = frame.copy()
        #gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        image = pyramid(img, scaleim)
        nearOb = -1

        height, width,channels = image.shape
        # nhandien
        blob = cv2.dnn.blobFromImage(image, 1/255, (416, 416), (0, 0, 0), True, crop=False)

        net.setInput(blob)
        outs = net.forward(output_layers)

        # hien_man_hinh
        detector_idxs = []
        confidences = []
        boxes = []
        for o in outs:
            for detection in o:
                scores = detection[5:]
                class_id = np.argmax(scores)
                confidence = scores[class_id]
                if confidence > 0:
                    center_x = int(detection[0] * width)
                    center_y = int(detection[1] * height)
                    w = int(detection[2] * width)
                    h = int(detection[3] * height)
                    # Rec coord
                    x = int(center_x - w / 2)  # top left x
                    y = int(center_y - h / 2)  # top left y

                    boxes.append([x, y, w, h])
                    confidences.append(float(confidence))
                    detector_idxs.append(class_id)
        indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0, 0.4)
        x=0
        y=0
        w=0
        h=0

        for i in range(len(boxes)):

            if i in indexes:
                nhan_dang = 1
                x,y,w,h = boxes[i]

                if (((detector_idxs[i] == 2) and (y <= 200)) or (y <= 95)):
                    ob_in_area = 1
                w_0 = vector_w[0][detector_idxs[i]]
                w_1 = vector_w[1][detector_idxs[i]]
                w_2 = vector_w[2][detector_idxs[i]]
                w_3 = vector_w[3][detector_idxs[i]]
                w_4 = vector_w[4][detector_idxs[i]]
                dis1 = 0
                dis2 = 0
                dis3 = 0
                frball = 0
                frta = 0
                frkhoa = 0
                if (detector_idxs[i] == 0):
                    dis1 = w_0 + w_1 * x + w_2 * y + w_3 * w + w_4 * h
                    frball += 1
                elif (detector_idxs[i] == 1):
                    dis2 = 1 / (w_0 + w_1 * y)
                    frta += 1
                else:
                    dis3 = w_0 + w_1 * y
                    frkhoa += 1
                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 250, 0), 2)
                cv2.putText(img, str(detector_idxs[i]) + '  ' + str(np.round(confidences[i] * 100, 2)) + '%', (x, y),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (250, 0, 0), 1, lineType=cv2.LINE_AA)

        frtotal = max(frball, frta, frkhoa)
        if (frtotal == 0):
            nearOb = -1
            Distance = 1000
        elif (frtotal == frball):
            nearOb = 0
            Distance = dis1
        elif (frtotal == frta):
            nearOb = 1
            Distance = dis2
        elif (frtotal == frkhoa):
            nearOb = 2
            Distance = dis3
        if nearOb == 0:
            NameOb = 'Marker Ball'
        elif nearOb == 1:
            NameOb = 'Ta Chong Rung'
        elif nearOb == 2:
            NameOb = 'Khoa do day'
        else:
            NameOb = 'Nothing'

        #Phan cua Tho
        '''top_left = [x,y]
        bottom_right = [x+w,y+h]
        Distance, contour, _ = Tho.distance_estimate(frame.copy(), top_left, bottom_right, 0.2, 200, 400, 14.7)
        Distance = Distance / 10
        cv2.imshow('contour', contour.copy())'''
        a = 4132.45382956
        b = 2.4475897335913714
        if w != 0:
            inv_w_array = inv_w_array[1:6]+[1/w]
            #print(inv_w_array)
        Distance = a * QuanLib.butter_lowpass_filter(data=inv_w_array,cutoff=0.7,fs=10,order=2)[5] + b

        '''
        if top_left == [0,0] or bottom_right == [0,0]:
            Distance = 1000
        else:
            Distance,_,_ = Tho.distance_estimate(img, top_left, bottom_right, 0.2, 300, 900, 14.7)
            Distance = Distance/10
        '''

        if (Distance == 1000 or nhan_dang == 0 or np.round(Distance, 2) < 21):
            dis_string = str(np.round(Distance, 2))
            #dis_string = 'Unknow'
        else:
            dis_string = str(np.round(Distance, 2))
        end = time.time()
        cv2.putText(img, 'FPS:' + str(
            np.round(1 / (end - start))) + '   Name:' + NameOb + '     Distance:' + dis_string + '  cm', (20, 20),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1, lineType=cv2.LINE_AA)
        start = time.time()

        frame1 = frame[75:95, 220:420].copy()
        cv2.rectangle(frame, (220, 75), (420, 95), (0, 255, 0), 2)
        frame2 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
        kernel = np.ones((3, 3), np.uint8)
        th = cv2.adaptiveThreshold(frame2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
                                   cv2.THRESH_BINARY_INV, 51, 4)
        opening1 = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
        opening = cv2.morphologyEx(opening1, cv2.MORPH_CLOSE, kernel)
        ret, track_window = cv2.CamShift(opening, track_window, term_crit)
        pts = np.int0(cv2.boxPoints(ret))
        y1 = pts[0][0]
        y2 = pts[1][0]
        y3 = pts[2][0]
        y4 = pts[3][0]
        if (not (y1 == 0 and y2 == 0 and y3 == 0 and y4 == 0)) or (max(y1, y2, y3, y4) >= 150) or (
                min(y1, y2, y3, y4) <= 50):
            opening[:, 0:min(y1, y2, y3, y4) - 10] = 0
            opening[:, max(y1, y2, y3, y4) + 10:200] = 0
            resu = opening[:, max(min(y1, y2, y3, y4) - 10, 0):min(max(y1, y2, y3, y4) + 10, 200)].copy()
            edges = cv2.Canny(opening, 50, 200, apertureSize=3)
            feat = np.sum(edges / 255)
            #print(feat)
            if sdetect == 1:
                if ob_in_area == 1:
                    cv2.putText(frame, 'Obstacle', (70, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2,
                                lineType=cv2.LINE_AA)
                    cv2.putText(img, 'Lightning rods: Unknow', (20, 45), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255),
                                1, lineType=cv2.LINE_AA)
                    cv2.rectangle(frame, (220, 75), (420, 95), (255, 0, 0), 2)
                elif ((feat <= 37) or (feat >= 43)):
                    cv2.putText(frame, 'Error', (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2,
                                lineType=cv2.LINE_AA)
                    cv2.putText(img, 'Lightning rods: Error', (20, 45), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255),
                                1, lineType=cv2.LINE_AA)
                    cv2.rectangle(frame, (220, 75), (420, 95), (0, 0, 255), 2)
                else:
                    cv2.putText(frame, 'Normal', (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2,
                                lineType=cv2.LINE_AA)
                    cv2.putText(img, 'Lightning rods: Normal', (20, 45), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255),
                                1, lineType=cv2.LINE_AA)
                    cv2.rectangle(frame, (220, 75), (420, 95), (0, 255, 0), 2)


        Image_show = combineImage(img, frame, Image_show, scaleim=1)
        cv2.namedWindow('result', cv2.WINDOW_NORMAL)
        cv2.imshow('result', Image_show)
        Distance = 1000
        nhan_dang = 0
        k = cv2.waitKey(5) & 0xff
        if k == 27:
            break
        elif (k == 115):
            sdetect = 1
        elif (k == 99):
            ob_in_area = 0
            frball = 0
            frta = 0
            frkhoa = 0
        out.write(Image_show)
    cap.release()
    cv2.destroyAllWindows()
hsv_roi =  cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, lower_color, upper_color)
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 atleast 1 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )

while True:
    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, bounding_window = cv2.CamShift(dst, bounding_window, term_crit)

        # Draw the poly
        pts = cv2.boxPoints(ret)
        pts = np.int0(pts)
        feed = cv2.polylines(frame,[pts],True, 255,2)
        feed = cv2.flip(feed, 1)
        cv2.imshow('video feed',feed)

        if cv2.waitKey(1) == 13: #13 is the Enter Key
            break


cv2.destroyAllWindows()
cap.release()
def findTape(contours, image, centerX, centerY):
    screenHeight, screenWidth, channels = image.shape
    #Seen vision targets (correct angle, adjacent to each other)
    targets = []

    if len(contours) >= 2:
        #Sort contours by area size (biggest to smallest)
        cntsSorted = sorted(contours,
                            key=lambda x: cv2.contourArea(x),
                            reverse=True)

        biggestCnts = []
        for cnt in cntsSorted:
            # Get moments of contour; mainly for centroid
            M = cv2.moments(cnt)
            # Get convex hull (bounding polygon on contour)
            hull = cv2.convexHull(cnt)
            # Calculate Contour area
            cntArea = cv2.contourArea(cnt)
            # calculate area of convex hull
            hullArea = cv2.contourArea(hull)
            # Filters contours based off of size
            if (checkContours(cntArea, hullArea)):
                ### MOSTLY DRAWING CODE, BUT CALCULATES IMPORTANT INFO ###
                # Gets the centeroids of contour
                if M["m00"] != 0:
                    cx = int(M["m10"] / M["m00"])
                    cy = int(M["m01"] / M["m00"])
                else:
                    cx, cy = 0, 0
                if (len(biggestCnts) < 13):
                    #### CALCULATES ROTATION OF CONTOUR BY FITTING ELLIPSE ##########
                    rotation = getEllipseRotation(image, cnt)

                    # Calculates yaw of contour (horizontal position in degrees)
                    yaw = calculateYaw(cx, centerX, H_FOCAL_LENGTH)
                    # Calculates yaw of contour (horizontal position in degrees)
                    pitch = calculatePitch(cy, centerY, V_FOCAL_LENGTH)

                    ##### DRAWS CONTOUR######
                    # Gets rotated bounding rectangle of contour
                    rect = cv2.minAreaRect(cnt)
                    # Creates box around that rectangle
                    box = cv2.boxPoints(rect)
                    # Not exactly sure
                    box = np.int0(box)
                    # Draws rotated rectangle
                    cv2.drawContours(image, [box], 0, (23, 184, 80), 3)

                    # Calculates yaw of contour (horizontal position in degrees)
                    yaw = calculateYaw(cx, centerX, H_FOCAL_LENGTH)
                    # Calculates yaw of contour (horizontal position in degrees)
                    pitch = calculatePitch(cy, centerY, V_FOCAL_LENGTH)

                    # Draws a vertical white line passing through center of contour
                    cv2.line(image, (cx, screenHeight), (cx, 0),
                             (255, 255, 255))
                    # Draws a white circle at center of contour
                    cv2.circle(image, (cx, cy), 6, (255, 255, 255))

                    # Draws the contours
                    cv2.drawContours(image, [cnt], 0, (23, 184, 80), 1)

                    # Gets the (x, y) and radius of the enclosing circle of contour
                    (x, y), radius = cv2.minEnclosingCircle(cnt)
                    # Rounds center of enclosing circle
                    center = (int(x), int(y))
                    # Rounds radius of enclosning circle
                    radius = int(radius)
                    # Makes bounding rectangle of contour
                    rx, ry, rw, rh = cv2.boundingRect(cnt)
                    boundingRect = cv2.boundingRect(cnt)
                    # Draws countour of bounding rectangle and enclosing circle in green
                    cv2.rectangle(image, (rx, ry), (rx + rw, ry + rh),
                                  (23, 184, 80), 1)

                    cv2.circle(image, center, radius, (23, 184, 80), 1)

                    # Appends important info to array
                    if not biggestCnts:
                        biggestCnts.append([cx, cy, rotation])
                    elif [cx, cy, rotation] not in biggestCnts:
                        biggestCnts.append([cx, cy, rotation])

        # Sorts array based on coordinates (leftmost to rightmost) to make sure contours are adjacent
        biggestCnts = sorted(biggestCnts, key=lambda x: x[0])
        # Target Checking
        for i in range(len(biggestCnts) - 1):
            #Rotation of two adjacent contours
            tilt1 = biggestCnts[i][2]
            tilt2 = biggestCnts[i + 1][2]

            #x coords of contours
            cx1 = biggestCnts[i][0]
            cx2 = biggestCnts[i + 1][0]

            cy1 = biggestCnts[i][1]
            cy2 = biggestCnts[i + 1][1]
            # If contour angles are opposite
            if (np.sign(tilt1) != np.sign(tilt2)):
                centerOfTarget = math.floor((cx1 + cx2) / 2)
                #ellipse negative tilt means rotated to right
                #Note: if using rotated rect (min area rectangle)
                #      negative tilt means rotated to left
                # If left contour rotation is tilted to the left then skip iteration
                if (tilt1 > 0):
                    if (cx1 < cx2):
                        continue
                # If left contour rotation is tilted to the left then skip iteration
                if (tilt2 > 0):
                    if (cx2 < cx1):
                        continue
                #Angle from center of camera to target (what you should pass into gyro)
                yawToTarget = calculateYaw(centerOfTarget, centerX,
                                           H_FOCAL_LENGTH)
                #Make sure no duplicates, then append
                if not targets:
                    targets.append([centerOfTarget, yawToTarget])
                elif [centerOfTarget, yawToTarget] not in targets:
                    targets.append([centerOfTarget, yawToTarget])
    #Check if there are targets seen
    if (len(targets) > 0):
        # pushes that it sees vision target to network tables
        networkTable.putBoolean("tapeDetected", True)
        #Sorts targets based on x coords to break any angle tie
        targets.sort(key=lambda x: math.fabs(x[0]))
        finalTarget = min(targets, key=lambda x: math.fabs(x[1]))
        # Puts the yaw on screen
        #Draws yaw of target + line where center of target is
        cv2.putText(image, "Yaw: " + str(finalTarget[1]), (40, 40),
                    cv2.FONT_HERSHEY_COMPLEX, .6, (255, 255, 255))
        cv2.line(image, (finalTarget[0], screenHeight), (finalTarget[0], 0),
                 (255, 0, 0), 2)

        currentAngleError = finalTarget[1]
        # pushes vision target angle to network tables
        networkTable.putNumber("tapeYaw", currentAngleError)
    else:
        # pushes that it deosn't see vision target to network tables
        networkTable.putBoolean("tapeDetected", False)

    cv2.line(image, (round(centerX), screenHeight), (round(centerX), 0),
             (255, 255, 255), 2)

    return image
Esempio n. 46
0
def test_img(img_name):
    img = cv2.imread(img_name, cv2.IMREAD_GRAYSCALE)
    img_start = cv2.imread(img_name)
    white_black(img_name, "res2.jpg", 0.85)
    img = cv2.imread("res2.jpg", cv2.IMREAD_GRAYSCALE)
    blur = cv2.blur(img, (3, 3))  # blur the image
    ret, thresh = cv2.threshold(blur, 50, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)

    hull = []
    # calculate points for each contour
    for i in range(len(contours)):
        hull.append(cv2.convexHull(contours[i], False))
    drawing = np.zeros((thresh.shape[0], thresh.shape[1], 3), np.uint8)
    canvas = np.ones((img.shape[0], img.shape[1], 3), np.uint8) * 100
    for i in range(len(contours)):
        color_contours = (0, 255, 0)
        #color = (255, 0, 0)
        cv2.drawContours(drawing, contours, i, color_contours, 1, 8, hierarchy)
        #cv2.drawContours(canvas, hull, i, color, 1, 8)
    cv2.imshow('img1', img)
    cv2.waitKey(0)
    cv2.imshow('countur', drawing)
    cv2.waitKey(0)

    # Проходя через все контуры, найденные на изображении.
    font = cv2.FONT_HERSHEY_COMPLEX
    page = []
    rotrect = cv2.minAreaRect(contours[0])
    for cnt in contours:
        if cv2.arcLength(cnt, True) > 800:
            approx = cv2.approxPolyDP(cnt, 0.012 * cv2.arcLength(cnt, True),
                                      True)
            cv2.drawContours(canvas, [approx], 0, (0, 0, 255), 5)
            n = approx.ravel()
            i = 0
            for j in n:
                if (i % 2 == 0):
                    help_arr = [n[i], n[i + 1]]
                    page.append(help_arr)
                    x = n[i]
                    y = n[i + 1]
                    string = str(x) + " " + str(y)
                    cv2.putText(canvas, string, (x, y), font, 0.5, (0, 255, 0))
                i = i + 1
            rotrect = cv2.minAreaRect(cnt)
    # РЕДАГУВАННЯ МАСИВУ ДЛЯ ВІДПОВІДНОСТІ
    # тут треба повороти

    # коробочка по фрейду
    box = cv2.boxPoints(rotrect)
    box = np.int0(box)
    cv2.drawContours(canvas, [box], 0, (0, 255, 255), 2)
    cv2.imshow('box', canvas)
    cv2.waitKey(0)

    # матриця переходу і трансформація
    (x1, y1), (x2, y2), angle = rotrect
    box1 = [[0, 0], [0, y2], [x2, y2], [x2, 0]]
    box1 = forvard_back(box1)
    box1 = max_X_sort(box1)
    box = np.array(box, np.float32)
    box1 = np.array(box1, np.float32)
    page = aprox_in_array(find_near(page, box), page)
    page = np.array(page, np.float32)
    matrix = cv2.getPerspectiveTransform(page, box1)
    result = cv2.warpPerspective(img_start, matrix, (int(x2), int(y2)))

    # Wrap the transformed image
    cv2.imshow('img transform', result)
    cv2.imwrite("res.jpg", result)
    cv2.waitKey(0)

    # висвітлення фону паперу, приберання малих косяків та підведення ліній
    # increasing the contrast 20%
    image = Image.open("res.jpg")
    new_image = ImageEnhance.Contrast(image).enhance(1.2)
    result = np.array(new_image)
    cv2.imwrite("res.jpg", result)

    white_black("res.jpg", "res2.jpg", 0.85)
    result = cv2.imread("res2.jpg")
    cv2.imshow('bw-result', result)
    cv2.waitKey(0)

    cv2.destroyAllWindows()

    # thickness
    gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
    gray = cv2.bitwise_not(gray)
    cv2.imshow('gray invert', gray)
    cv2.waitKey(0)

    edges = cv2.Canny(result, 50, 150, apertureSize=3)
    minLineLength = 1
    maxLineGap = 1
    lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 20, minLineLength,
                            maxLineGap)
    for l in lines:
        for x1, y1, x2, y2 in l:
            cv2.line(result, (x1, y1), (x2, y2), (0, 0, 0), 1)

    cv2.imshow('with countur', result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Esempio n. 47
0
for index in txt_name:
    four_points_list = load_kitti_annotation(label_folder, index)  #标注

    label_list = []
    for pts in four_points_list:
        # cv2.line(img,(pts[0],pts[1]),(pts[2],pts[3]),127,1)
        # cv2.line(img,(pts[2],pts[3]),(pts[4],pts[5]),255,1)
        # cv2.line(img,(pts[4],pts[5]),(pts[6],pts[7]),255,1)
        # cv2.line(img,(pts[0],pts[1]),(pts[6],pts[7]),255,1)

        #求最小外界矩形
        cnt = np.array([[pts[0], pts[1]], [pts[2], pts[3]], [pts[4], pts[5]],
                        [pts[6], pts[7]]])  # 必须是array数组的形式
        rect = cv2.minAreaRect(cnt)  # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度)
        # box = cv2.cv.BoxPoints(rect) # OpenCV 2. 获取最小外接矩形的4个顶点
        box = cv2.boxPoints(rect)  # OpenCV 3.x 获取最小外接矩形的4个顶点
        # cv2.line(img,(box[0][0],box[0][1]),(box[1][0],box[1][1]),127,1)
        # cv2.line(img,(box[1][0],box[1][1]),(box[2][0],box[2][1]),127,1)
        # cv2.line(img,(box[2][0],box[2][1]),(box[3][0],box[3][1]),127,1)
        # cv2.line(img,(box[3][0],box[3][1]),(box[0][0],box[0][1]),127,1)

        orient_rec_labels = str(int(box[0][0])) + ',' + str(int(box[0][1])) + ',' + \
                            str(int(box[1][0])) + ',' + str(int(box[1][1])) + ',' + \
                            str(int(box[2][0])) + ',' + str(int(box[2][1])) + ',' + \
                            str(int(box[3][0])) + ',' + str(int(box[3][1])) + ',' + \
                            str(pts[8])

        #判断矩形框是否超出图像范围(靠近边缘也删除)
        i = 0
        flag_append = True
        for i in range(0, 4):
Esempio n. 48
0
def vision(cap):
    ret, frame = cap.read()

    global h_div
    global s_div
    global v_div
    global xTarget
    global yTarget
    # show the original frame (Testing only)
    #cv2.imshow('Original',frame)

    #Convert to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)

    hue = cv2.absdiff(h, 90)
    hue = cv2.subtract(90, hue)
    hue = cv2.divide(hue, h_div)
    #	saturation = cv2.divide(s, s_div)
    value = cv2.divide(v, v_div)

    #targetyness = cv2.multiply(hue,saturation)
    targetyness = cv2.multiply(hue, value)

    ret, targetyness = cv2.threshold(targetyness, 200, 255, 0)
    mask = targetyness

    #Show the mask (Testing Only)
    #cv2.imshow('mask',mask)

    #Find the contours of the combined image
    discardImage, contours, hierarchy = cv2.findContours(
        targetyness, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    #Make sure that a contour region is found
    if len(contours) > 1:
        #find Biggest Contour region
        areas = [cv2.contourArea(c) for c in contours]
        max_index = np.argmax(areas)
        cnt = contours[max_index]

        #Find the smallest bounding box of the rectangle
        rect = cv2.minAreaRect(cnt)
        box = cv2.boxPoints(rect)
        box = np.int0(box)

        #find center of target for RoboRIO
        xCenter = (box[0][0] + box[1][0] + box[2][0] + box[3][0]) / 4
        yCenter = (box[0][1] + box[1][1] + box[2][1] + box[3][1]) / 4

        #These Calculations and draw settings are to help the driver lock onto the target
        xLocked = abs(xTarget - xCenter) < xError
        yLocked = abs(yTarget - yCenter) < yError

        #This here is just for drawing lines on the screen
        if (xLocked):
            cv2.line(frame, (xTarget, 0), (xTarget, 480), (0, 0, 255), 3)
        else:
            cv2.line(frame, (xTarget, 0), (xTarget, 480), (255, 0, 0), 3)
        if (yLocked):
            cv2.line(frame, (0, yCenter), (640, yCenter), (0, 0, 255), 3)
        else:
            cv2.line(frame, (0, yCenter), (640, yCenter), (255, 0, 0), 3)
        if (xLocked and yLocked):
            cv2.drawContours(frame, [box], 0, (0, 0, 255), 2)
        else:
            cv2.drawContours(frame, [box], 0, (0, 255, 0), 2)
        msg = '(' + str(xCenter) + ',' + str(yCenter) + ')\n'
    else:
        msg = '( 0,0)\n'

    #show final image (Testing only)
    #cv2.imshow("Show",frame)
    #Quit if q is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        print exitKey
    return msg, frame, mask
Esempio n. 49
0
def getFrontground(img_path):
    # 1.1 载入图像
    img = cv2.imread(img_path)
    height, width = img.shape[:2]  # 获取图像的高和宽
    print(height, width)
    # cv2.imshow('Origin', img)
    # 1.2 滤波降噪
    blured = cv2.blur(img, (5, 5))  # 进行滤波去掉噪声,参数二为低通滤波器的大小
    # cv2.imshow('Blur', blured)
    # 1.3 mask是掩码图像,用来确定哪些区域是背景,哪些区域是前景
    mask = np.zeros((height+2, width+2), np.uint8)  # 掩码长和宽都比输入图像多两个像素点,满水填充不会超出掩码的非零边缘
    # 为什么要加2可以这么理解:当从0行0列开始泛洪填充扫描时,mask多出来的2可以保证扫描的边界上的像素都会被处理
    # 1.4 进行泛洪填充
    cv2.floodFill(blured, mask, (width-1, height-1), (255, 255, 255), (1, 1, 1), (1, 1, 1), 8)
    # cv2.imshow('floodfill', blured)
    # 1.5 转换为灰度图
    gray = cv2.cvtColor(blured, cv2.COLOR_BGR2GRAY)
    # cv2.imshow('gray', gray)
    # 1.6 定义结构元素
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(50, 50))
    # 1.7 开闭运算,先开运算去除背景噪声,再继续闭运算填充目标内的孔洞
    opened = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
    closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, kernel)
    # cv2.imshow('closed', closed)
    # 1.8 求二值图
    ret, binary = cv2.threshold(closed, 250, 255, cv2.THRESH_BINARY)
    # cv2.imshow('binary', binary)
    # 1.9 找到前景物轮廓
    _, contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # 注意,contours[0]表示外轮廓,contours[1]表示内轮廓
    # 1.10 绘制轮廓
    draw_img = cv2.drawContours(img.copy(), contours, -1, (0, 0, 255), 3)
    cv2.imshow('result', draw_img)
    # 1.11 遍历像素点是否在轮廓内,改变轮廓外的像素点
    color = (255, 255, 255)
    for h in range(height):
        for w in range(width):
            try:
                test = cv2.pointPolygonTest(contours[1], (w, h), False)
                if test == -1 or test == 0:
                    img[h, w] = color
            except:
                test = cv2.pointPolygonTest(contours[0], (w, h), False)
                if test == 1:
                    img[h, w] = color
    cv2.imshow('handle', img)
    # 1.12 使用轮廓建立最小矩形区域
    for c in contours:
        # 计算包围目标的最小矩形区域
        rect = cv2.minAreaRect(c)
        # 计算矩形的 4 点坐标,返回结果为float数据类型
        points = cv2.boxPoints(rect)
        # 转换为int类型
        box = np.int0(points)
    # 1.13 根据box将对图片进行裁剪
    Xs = [i[0] for i in box]
    Ys = [i[1] for i in box]
    x1 = min(Xs)
    x2 = max(Xs)
    y1 = min(Ys)
    y2 = max(Ys)
    height = y2 - y1
    width = x2 - x1
    crop_img = img[y1:y1+height, x1:x1+width]
    cv2.imshow('crop_img', crop_img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return crop_img
Esempio n. 50
0
def siamese_track(state,
                  im,
                  mask_enable=False,
                  refine_enable=False,
                  device='cpu',
                  debug=False):
    p = state['p']
    net = state['net']
    avg_chans = state['avg_chans']
    window = state['window']
    target_pos = state['target_pos']
    target_sz = state['target_sz']

    wc_x = target_sz[1]  #+ p.context_amount * sum(target_sz)
    hc_x = target_sz[0]  #+ p.context_amount * sum(target_sz)
    s_x = np.sqrt(wc_x * hc_x)
    scale_x = p.exemplar_size / s_x
    # d_search = (p.instance_size - p.exemplar_size) / 2
    # pad = d_search / scale_x
    # s_x = s_x + 2 * pad
    s_x = s_x * 2
    crop_box = [
        target_pos[0] - round(s_x) / 2, target_pos[1] - round(s_x) / 2,
        round(s_x),
        round(s_x)
    ]

    if debug:
        im_debug = im.copy()
        crop_box_int = np.int0(crop_box)
        cv2.rectangle(im_debug, (crop_box_int[0], crop_box_int[1]),
                      (crop_box_int[0] + crop_box_int[2],
                       crop_box_int[1] + crop_box_int[3]), (255, 0, 0), 2)
        cv2.imshow('search area', im_debug)
        cv2.waitKey(0)

    # extract scaled crops for search region x at previous target position
    x_crop = Variable(
        get_subwindow_tracking(im, target_pos, p.instance_size, round(s_x),
                               avg_chans).unsqueeze(0))

    if mask_enable:
        score, delta, mask = net.track_mask(x_crop.to(device))
    else:
        score, delta = net.track(x_crop.to(device))

    delta = delta.permute(1, 2, 3, 0).contiguous().view(4,
                                                        -1).data.cpu().numpy()
    score = F.softmax(score.permute(1, 2, 3,
                                    0).contiguous().view(2, -1).permute(1, 0),
                      dim=1).data[:, 1].cpu().numpy()

    delta[0, :] = delta[0, :] * p.anchor[:, 2] + p.anchor[:, 0]
    delta[1, :] = delta[1, :] * p.anchor[:, 3] + p.anchor[:, 1]
    delta[2, :] = np.exp(delta[2, :]) * p.anchor[:, 2]
    delta[3, :] = np.exp(delta[3, :]) * p.anchor[:, 3]

    def change(r):
        return np.maximum(r, 1. / r)

    def sz(w, h):
        pad = (w + h) * 0.5
        sz2 = (w + pad) * (h + pad)
        return np.sqrt(sz2)

    def sz_wh(wh):
        pad = (wh[0] + wh[1]) * 0.5
        sz2 = (wh[0] + pad) * (wh[1] + pad)
        return np.sqrt(sz2)

    # size penalty
    target_sz_in_crop = target_sz * scale_x
    # if predicted size < 127, then scale penalty = 127 / predicted_size, if predicted_size > 127, then scale penalty = predicted_size / 127
    s_c = change(sz(delta[2, :], delta[3, :]) /
                 (sz_wh(target_sz_in_crop)))  # scale penalty
    # put penalty on aspect ratio change, the same way as the size change
    r_c = change((target_sz_in_crop[0] / target_sz_in_crop[1]) /
                 (delta[2, :] / delta[3, :]))  # ratio penalty

    penalty = np.exp(-(r_c * s_c - 1) * p.penalty_k)
    pscore = penalty * score
    # pscore = score

    # cos window (motion model)
    pscore = pscore * (1 - p.window_influence) + window * p.window_influence
    best_pscore_id = np.argmax(pscore)
    best_pscore = pscore[best_pscore_id]
    state['best_pscore'] = best_pscore

    pred_in_crop = delta[:, best_pscore_id] / scale_x
    lr = penalty[best_pscore_id] * score[best_pscore_id] * p.lr  # lr for OTB

    res_x = pred_in_crop[0] + target_pos[0]
    res_y = pred_in_crop[1] + target_pos[1]

    res_w = target_sz[0] * (1 - lr) + pred_in_crop[2] * lr
    res_h = target_sz[1] * (1 - lr) + pred_in_crop[3] * lr

    target_pos = np.array([res_x, res_y])
    target_sz = np.array([res_w, res_h])

    # for Mask Branch
    if mask_enable:
        best_pscore_id_mask = np.unravel_index(best_pscore_id,
                                               (5, p.score_size, p.score_size))
        delta_x, delta_y = best_pscore_id_mask[2], best_pscore_id_mask[1]

        if refine_enable:
            mask = net.track_refine(
                (delta_y, delta_x)).to(device).sigmoid().squeeze().view(
                    p.out_size, p.out_size).cpu().data.numpy()
        else:
            mask = mask[0, :, delta_y, delta_x].sigmoid(). \
                squeeze().view(p.out_size, p.out_size).cpu().data.numpy()

        def crop_back(image, bbox, out_sz, padding=-1):
            a = (out_sz[0] - 1) / bbox[2]
            b = (out_sz[1] - 1) / bbox[3]
            c = -a * bbox[0]
            d = -b * bbox[1]
            mapping = np.array([[a, 0, c], [0, b, d]]).astype(np.float)
            crop = cv2.warpAffine(image,
                                  mapping, (out_sz[0], out_sz[1]),
                                  flags=cv2.INTER_LINEAR,
                                  borderMode=cv2.BORDER_CONSTANT,
                                  borderValue=padding)
            return crop

        s = crop_box[2] / p.instance_size
        sub_box = [
            crop_box[0] + (delta_x - p.base_size / 2) * p.total_stride * s,
            crop_box[1] + (delta_y - p.base_size / 2) * p.total_stride * s,
            s * p.exemplar_size, s * p.exemplar_size
        ]
        s = p.out_size / sub_box[2]
        back_box = [
            -sub_box[0] * s, -sub_box[1] * s, state['im_w'] * s,
            state['im_h'] * s
        ]
        mask_in_img = crop_back(mask, back_box, (state['im_w'], state['im_h']))

        target_mask = (mask_in_img > p.seg_thr).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)]  # use max area polygon
            polygon = contour.reshape(-1, 2)
            # pbox = cv2.boundingRect(polygon)  # Min Max Rectangle
            prbox = cv2.boxPoints(
                cv2.minAreaRect(polygon))  # Rotated Rectangle

            # box_in_img = pbox
            rbox_in_img = prbox
        else:  # empty mask
            location = cxy_wh_2_rect(target_pos, target_sz)
            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]]])

    target_pos[0] = max(0, min(state['im_w'], target_pos[0]))
    target_pos[1] = max(0, min(state['im_h'], target_pos[1]))
    target_sz[0] = max(10, min(state['im_w'], target_sz[0]))
    target_sz[1] = max(10, min(state['im_h'], target_sz[1]))

    state['target_pos'] = target_pos
    state['target_sz'] = target_sz
    state['score'] = score[best_pscore_id]
    state['mask'] = mask_in_img if mask_enable else []
    state['ploygon'] = rbox_in_img if mask_enable else []
    return state
Esempio n. 51
0
    def UniversalProcess(self, inframe):
        inimg = inframe.getCvBGR()
        outimg = inimg

        #change to hsv
        hsv = cv2.cvtColor(inimg, cv2.COLOR_BGR2HSV)

        arrayForHSV = list(self.stringForHSV)

        #threshold colors to detect - Green: First value decides color, second val determines intensity, third val decides brightness
        lowerThreshold = np.array([self.lowerH, self.lowerS, self.lowerV])
        upperThreshold = np.array([self.upperH, self.upperS, self.upperV])

        if len(arrayForHSV) > 15 and arrayForHSV[4] == "h":
            stringForH = self.stringForHSV.lstrip("set hrange")
            stringHSpace = stringForH.replace("...", " ")
            stringH = stringHSpace.split(" ")
            self.lowerH = int(stringH[0])
            self.upperH = int(stringH[1])
            lowerThreshold = np.array([self.lowerH, self.lowerS, self.lowerV])
            upperThreshold = np.array([self.upperH, self.upperS, self.upperV])
            #jevois.sendSerial("hello")
        if len(arrayForHSV) > 15 and arrayForHSV[4] == "s":
            stringForS = self.stringForHSV.lstrip("set srange")
            stringSSpace = stringForS.replace("...", " ")
            stringS = stringSSpace.split(" ")
            self.lowerS = int(stringS[0])
            self.upperS = int(stringS[1])
            lowerThreshold = np.array([self.lowerH, self.lowerS, self.lowerV])
            upperThreshold = np.array([self.upperH, self.upperS, self.upperV])
        if len(arrayForHSV) > 15 and arrayForHSV[4] == "v":
            stringForV = self.stringForHSV.lstrip("set vrange")
            stringVSpace = stringForV.replace("...", " ")
            stringV = stringVSpace.split(" ")
            self.lowerV = int(stringV[0])
            self.upperV = int(stringV[1])
            lowerThreshold = np.array([self.lowerH, self.lowerS, self.lowerV])
            upperThreshold = np.array([self.upperH, self.upperS, self.upperV])
        #jevois.sendSerial(str(lowerThreshold))
        #jevois.sendSerial(str(upperThreshold))

        oKernel = np.ones((2, 2), np.uint8)
        cKernel = np.ones((4, 4), np.uint8)
        #cKernel = np.array([
        #[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        #[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
        #[0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0],
        #[0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0],
        #[0, 0, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]], dtype = np.uint8)

        #check if color in range
        mask = cv2.inRange(hsv, lowerThreshold, upperThreshold)

        result = cv2.bitwise_and(inimg, inimg, mask=mask)

        #create blur on image to reduce noise
        blur = cv2.GaussianBlur(mask, (5, 5), 0)

        ret, thresh = cv2.threshold(blur, 65, 255, cv2.THRESH_BINARY)

        #closes of noise from inside object
        closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, cKernel)

        #takes away noise from outside object
        opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, oKernel)

        #find contours
        contours, _ = cv2.findContours(opening, cv2.RETR_TREE,
                                       cv2.CHAIN_APPROX_NONE)

        cntArray = []

        for contour in contours:
            peri = cv2.arcLength(contour, True)
            approx = cv2.approxPolyDP(contour, 0.04 * peri, True)
            cntArea = cv2.contourArea(contour)
            if cntArea > 75 and cntArea < 800:
                cntArray.append(contour)

        sortedArray = self.sortContours(cntArray)

        if len(sortedArray) == 0:
            jevois.sendSerial('{"Distance":-11, "Angle":-100}')
            #outimg = cv2.cvtColor(opening, cv2.COLOR_GRAY2BGR)
            outimg = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
            return result

        boxColor = (240, 255, 255)

        target = cv2.minAreaRect(sortedArray[0])
        points_A = cv2.boxPoints(target)
        points_1 = np.int0(points_A)
        cv2.drawContours(result, [points_1], 0, boxColor, 2)
        targetX = target[0][0]
        targetY = target[0][1]

        yawAngle = (targetX - 159.5) * 0.203125
        #get actual thing later
        distance = -12

        yawAngle = str(yawAngle)
        distance = str(distance)

        JSON = '{"Distance":' + distance + ', "Angle":' + yawAngle + '}'

        jevois.sendSerial(JSON)
        #jevois.sendSerial(yawAngle)

        #outimg = cv2.cvtColor(opening, cv2.COLOR_GRAY2BGR)
        outimg = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

        return result
    def __getitem__(self, index):
        img_path = self.img_files[index]
        seg_mask_path = self.seg_mask_files[index]
        ins_mask_path = self.ins_mask_files[index]

        # First try
        # data = np.array(Image.open(img_path))
        # label_seg = np.array(Image.open(seg_mask_path))
        # label_ins = np.array(Image.open(ins_mask_path))
        # return torch.from_numpy(data).float(), torch.from_numpy(label_seg).float(), torch.from_numpy(label_ins).float()

        # Second try
        # data =  torch.Tensor(Image.open(img_path).convert('RGB'))
        # data =  self.to_tensor(Image.open(img_path).convert('RGB'))
        # label_seg =  torch.Tensor(Image.open(seg_mask_path).convert('L'))
        # label_seg =  self.to_tensor(Image.open(seg_mask_path).convert('L'))
        # label_ins =  self.to_tensor(Image.open(ins_mask_path).convert('L'))
        # label_ins =  torch.Tensor(Image.open(ins_mask_path).convert('L'))

        # Original try
        data = np.asarray(Image.open(img_path).convert('L'))
        data = torch.Tensor(data[np.newaxis])
        data_shape = np.ones((1024, 1024), dtype=np.uint8) * 255

        fullname = os.path.join(ins_mask_path)
        xmldoc = minidom.parse(fullname)
        itemlist = xmldoc.getElementsByTagName('robndbox')
        ins = np.zeros((0, 1024, 1024), dtype=np.uint8)
        for rec in itemlist[:10]:
            x = float(rec.getElementsByTagName('cx')[0].firstChild.nodeValue)
            y = float(rec.getElementsByTagName('cy')[0].firstChild.nodeValue)
            w = float(rec.getElementsByTagName('w')[0].firstChild.nodeValue)
            h = float(rec.getElementsByTagName('h')[0].firstChild.nodeValue)
            theta = float(
                rec.getElementsByTagName('angle')[0].firstChild.nodeValue)
            rect = ([x, y], [w, h], math.degrees(theta))
            box = np.int0(cv2.boxPoints(rect))
            gt = np.zeros_like(data_shape)
            gt = cv2.fillPoly(gt, [box], 1)
            ins[:, gt != 0] = 0
            ins = np.concatenate([ins, gt[np.newaxis]])

        sem = np.zeros_like(data_shape, dtype=bool)
        sem[np.sum(ins, axis=0) != 0] = True
        sem = np.stack([~sem, sem]).astype(np.uint8)

        label_ins = torch.Tensor(ins)
        label_seg = torch.Tensor(sem)

        # # Worked for report
        # data =  np.asarray(Image.open(img_path).convert('RGB')).transpose((2,0,1))
        # data = torch.Tensor(data)
        # label_ins =  np.asarray(Image.open(ins_mask_path).convert('L'))
        # label_ins = torch.Tensor(label_ins[np.newaxis])
        # label_seg =  np.asarray(Image.open(seg_mask_path).convert('L'))
        # label_seg = torch.Tensor(label_seg[np.newaxis])

        filename = self.filenames[index]

        if self.train:
            return data, label_seg, label_ins
        else:
            return data, filename
Esempio n. 53
0
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis("off")
# show the figure
plt.show()

method_1_original_image = original_image.copy()
method_2_original_image = original_image.copy()
# loop over the contours individually
for (index, contour) in enumerate(contours):
    # if the contour is not sufficiently large, ignore it
    if cv2.contourArea(contour) < 100:
        continue

    # compute the rotated bounding box of the contour
    box = cv2.minAreaRect(contour)
    box_points = cv2.boxPoints(box)
    box_points = np.array(box_points, dtype="int")
    # show the original coordinates
    print("Object #{}:".format(index + 1))
    print(box_points)

    # draw the contours
    cv2.drawContours(method_1_original_image, [box_points], -1, (0, 255, 0), 2)
    cv2.drawContours(method_2_original_image, [box_points], -1, (0, 255, 0), 2)

    # show compare contour sorting
    fig = plt.figure("Contours")
    images = ("Method 1 Contours",
              method_1_original_image), ("Method 2 Contours",
                                         method_2_original_image)
    # show the image
Esempio n. 54
0
cv2.imwrite('edges2.png', edges)
# 輪郭抽出
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)
# 面積でフィルタリング
rects = []

for cnt, hrchy in zip(contours, hierarchy[0]):
    if cv2.contourArea(cnt) < 200:
        continue  # 面積が小さいものは除く
    #if hrchy[3] == -1:
    #  continue  # ルートノードは除く
    # 輪郭を囲む長方形を計算する。
    rect = cv2.minAreaRect(cnt)
    rect_points = cv2.boxPoints(rect).astype(int)
    rects.append(rect_points)

# x-y 順でソート
rects = sorted(rects, key=lambda x: (x[0][1], x[0][0]))

# 描画する。
for i, rect in enumerate(rects):
    color = np.random.randint(0, 255, 3).tolist()
    cv2.drawContours(img, rects, i, color, 2)
    #cv2.putText(img, str(i), tuple(rect[0]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

    print('rect:\n', rect)

cv2.imwrite('img.png', img)
Esempio n. 55
0
                                                     angles=np.array(angles))
            rotated_rectangles = [
                east.get_cv_rotated_rect(bbox, angle * -1)
                for (bbox, angle) in zip(boxes, angles)
            ]

    if frame is not None:
        if in_det is not None:
            rec_received = 0
            rec_pushed = len(rotated_rectangles)
            if rec_pushed:
                print("====== Pushing for recognition, count:", rec_pushed)
            cropped_stacked = None
            for idx, rotated_rect in enumerate(rotated_rectangles):
                # Draw detection crop area on input frame
                points = np.int0(cv2.boxPoints(rotated_rect))
                cv2.polylines(frame, [points],
                              isClosed=True,
                              color=(255, 0, 0),
                              thickness=1,
                              lineType=cv2.LINE_8)

                # TODO make it work taking args like in OpenCV:
                # rr = ((256, 256), (128, 64), 30)
                rr = dai.RotatedRect()
                rr.center.x = rotated_rect[0][0]
                rr.center.y = rotated_rect[0][1]
                rr.size.width = rotated_rect[1][0]
                rr.size.height = rotated_rect[1][1]
                rr.angle = rotated_rect[2]
                cfg = dai.ImageManipConfig()
def getDetBoxes_core(textmap, linkmap, text_threshold, link_threshold,
                     low_text):
    # prepare data
    linkmap = linkmap.copy()
    textmap = textmap.copy()
    img_h, img_w = textmap.shape
    """ labeling method """
    ret, text_score = cv2.threshold(textmap, low_text, 1, 0)
    ret, link_score = cv2.threshold(linkmap, link_threshold, 1, 0)

    text_score_comb = np.clip(text_score + link_score, 0, 1)
    nLabels, labels, stats, centroids = cv2.connectedComponentsWithStats(
        text_score_comb.astype(np.uint8), connectivity=4)

    det = []
    mapper = []
    for k in range(1, nLabels):
        # size filtering
        size = stats[k, cv2.CC_STAT_AREA]
        if size < 10: continue

        # thresholding
        if np.max(textmap[labels == k]) < text_threshold: continue

        # make segmentation map
        segmap = np.zeros(textmap.shape, dtype=np.uint8)
        segmap[labels == k] = 255
        segmap[np.logical_and(link_score == 1,
                              text_score == 0)] = 0  # remove link area
        x, y = stats[k, cv2.CC_STAT_LEFT], stats[k, cv2.CC_STAT_TOP]
        w, h = stats[k, cv2.CC_STAT_WIDTH], stats[k, cv2.CC_STAT_HEIGHT]
        niter = int(math.sqrt(size * min(w, h) / (w * h)) * 2)
        sx, ex, sy, ey = x - niter, x + w + niter + 1, y - niter, y + h + niter + 1
        # boundary check
        if sx < 0: sx = 0
        if sy < 0: sy = 0
        if ex >= img_w: ex = img_w
        if ey >= img_h: ey = img_h
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT,
                                           (1 + niter, 1 + niter))
        segmap[sy:ey, sx:ex] = cv2.dilate(segmap[sy:ey, sx:ex], kernel)

        # make box
        np_contours = np.roll(np.array(np.where(segmap != 0)), 1,
                              axis=0).transpose().reshape(-1, 2)
        rectangle = cv2.minAreaRect(np_contours)
        box = cv2.boxPoints(rectangle)

        # align diamond-shape
        w, h = np.linalg.norm(box[0] - box[1]), np.linalg.norm(box[1] - box[2])
        box_ratio = max(w, h) / (min(w, h) + 1e-5)
        if abs(1 - box_ratio) <= 0.1:
            l, r = min(np_contours[:, 0]), max(np_contours[:, 0])
            t, b = min(np_contours[:, 1]), max(np_contours[:, 1])
            box = np.array([[l, t], [r, t], [r, b], [l, b]], dtype=np.float32)

        # make clock-wise order
        startidx = box.sum(axis=1).argmin()
        box = np.roll(box, 4 - startidx, 0)
        box = np.array(box)

        det.append(box)
        mapper.append(k)

    return det, labels, mapper
Esempio n. 57
0
_, cnts, _ = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

#cnts = imutils.grab_contours(cnts)

(cnts, _) = contours.sort_contours(cnts)

width = 2.2  ## in inches

print(len(cnts))
for c in cnts:
    if cv2.contourArea(c) < 20000:
        continue

    box = cv2.minAreaRect(
        c)  ## will get the center,width,height,angle of rotations
    box = cv2.boxPoints(box)  ## will get the corners
    box = np.array(box, dtype="int")
    box = perspective.order_points(box)

    cv2.drawContours(original, [box.astype("int")], -1, (0, 255, 0), 10)
    for (x, y) in box:
        cv2.circle(original, (int(x), int(y)), 5, (0, 0, 255),
                   -1)  ## draw the corners
    (topleft, topright, bottomright,
     bottomleft) = box  ## Top right ,top right , bottom right , bottom left

    (topleft_toprightX, topleft_toprightY) = midpoint(
        topleft, topright)  ## midpoint of top row of rectangele
    (bottomleft_bottomrightX, bottomleft_bottomrightY) = midpoint(
        bottomleft, bottomright)  ## midpoint of bottom row of rectangle
Esempio n. 58
0
def get_plant_height(image, zoom_ratio):
    image_height = get_height(image)
    image_width = get_width(image)

    # define range of orange color in HSV

    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    hsv2 = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    hsv3 = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    lower_orange = np.array([11, 43, 46])
    upper_orange = np.array([26, 255, 255])
    lower_purple = np.array([100, 50, 50])
    upper_purple = np.array([200, 200, 200])
    lower_green = np.array([30, 200, 100])
    upper_green = np.array([100, 255, 190])
    mask = cv2.inRange(hsv, lower_orange, upper_orange)
    mask2 = cv2.inRange(hsv2, lower_green, upper_green)
    mask3 = cv2.inRange(hsv3, lower_purple, upper_purple)
    res = cv2.add(mask, mask3)

    cv2.imshow('res', res)
    a = np.zeros(get_width(image))
    stem_top = []
    stem_bottom = []
    cv2.imshow('image', image)

    # corp_threshold = int(560 * (4 / 5))
    # print(a)
    # mask[0:corp_threshold] = a
    # mask[550:560] = a

    if zoom_ratio < 0.25:
        mask = cv2.erode(mask, None, iterations=3)
        mask = cv2.dilate(mask, None, iterations=2)
        j = 0
        cv2.imshow('mask', mask)
        for i in range(image_height):
            for k in range(image_width):
                if mask[i][k] == 255:
                    stem_top = [k, i]

                    # print(stem_top)

                    j = 1
                    break
            if j == 1:
                break
        stem_bottom = [145, 535]
        orig = image.copy()
    else:

        kernel = np.ones((10, 1), np.uint8)  # 1, 13

        # cv2.imshow("mask2", mask)

        res = cv2.erode(res, None, iterations=1)
        res[0:100] = a
        res[493:image_height] = a

        # print(res.shape)

        cv2.imshow('res1', res)
        res = cv2.erode(res, kernel, iterations=1)
        cv2.imshow('res2', res)
        kernel2 = np.ones((10, 1), np.uint8)
        res = cv2.dilate(res, kernel2, iterations=2)
        cv2.imshow('res2', res)
        j = 0
        for i in range(image_height):
            for k in range(image_width):
                if res[i][k] == 255:
                    stem_top = [k, i]

                    # print(stem_top)

                    j = 1
                    break
            if j == 1:
                break

        mask2[0:445] = a
        kernel = np.ones((1, 10), np.uint8)
        mask2 = cv2.erode(mask2, kernel, iterations=1)

        # cv2.imshow("mask", mask2)

        mask2 = cv2.dilate(mask2, kernel, iterations=2)

        # cv2.imshow("mask1", mask2)

        cnts = cv2.findContours(mask2.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        (cnts, _) = contours.sort_contours(cnts)
        for c in cnts:
            if cv2.contourArea(c) < 10:
                continue
            orig = image.copy()
            box = cv2.minAreaRect(c)
            box = \
                (cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box))
            box = np.array(box, dtype='int')
            box = perspective.order_points(box)
            cv2.drawContours(orig, [box.astype('int')], -1, (0, 255, 0), 2)
            (tl, tr, br, bl) = box

            midX = (tl[0] + tr[0]) / 2
            midY = (tl[1] + bl[1]) / 2
            stem_bottom = [midX, midY]

    plant_height = dist.euclidean((stem_top[0], stem_top[1]),
                                  (stem_bottom[0], stem_bottom[1]))
    real_plant_height = plant_height * zoom_ratio
    real_plant_height = round(real_plant_height, 2)
    cv2.line(orig, (int(stem_top[0]), int(stem_top[1])),
             (int(stem_bottom[0]), int(stem_bottom[1])), (0, 0, 255), 2)
    cv2.putText(
        orig,
        '{:.2f}'.format(real_plant_height),
        (int(stem_top[0]), int(stem_top[1])),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.65,
        (0, 0, 0),
        2,
    )
    cv2.imshow('orig', orig)
    cv2.waitKey(0)
    return real_plant_height
Esempio n. 59
0
def draw_emo_v2(bg, face, txt):
    """绘制表情图,利用opencv,智能识别表情插入位置
    Args:
        bg: 底图
        face: 前脸
        txt: 文本

    Returns:
        Image

    Raises:
    """
    bg_img = cv2.imread(bg)
    f_img = cv2.imread(face)
    # 读入背景的灰度图,方便进行处理
    bg_img_gray = cv2.cvtColor(bg_img, cv2.COLOR_BGR2GRAY)
    
    #Otsu 滤波,产生二值化的图片
    ret2,th_bg = cv2.threshold(bg_img_gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    #第一步,找出图片中的熊猫轮廓
    # 将熊猫弄成白色
    th_bg_inv = cv2.bitwise_not(th_bg)
    # 找轮廓
    image,cnts,hierarchy = cv2.findContours(th_bg_inv,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    areas_s_idx = contours_sort_idx(cnts)
    bg_mask = th_bg_inv.copy()
    # 将熊猫区域内填充黑色,形成mask
    cv2.drawContours(bg_mask, cnts, areas_s_idx[0], (255,255,255), cv2.FILLED)
    # bg_mask = cv2.bitwise_not(bg_mask)
    # 使用下面的方式进行抠图,把原图与一个黑色的图片叠加,mask为蒙版,蒙版中白色的地方会进行add,黑色区域的像素会被删除(变成黑色)
    face_bg_cntimg=cv2.add(th_bg, th_bg, mask=bg_mask)
    # 产生的face_cntimg中,只有表情所在的轮廓是白的,其他都是黑的,可能存在噪点,通过面积排序去除干扰
    image,face_bg_cnts,hierarchy = cv2.findContours(face_bg_cntimg,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    image = bg_img.copy()
    areas_s_idx = contours_sort_idx(face_bg_cnts)
    rect = cv2.minAreaRect(face_bg_cnts[areas_s_idx[0][0]])
    box = cv2.boxPoints(rect)
    # 将box缩小一下,让图片插入的位置更好看
    offset = 15
    box[0][0] = box[0][0] + offset
    box[0][1] = box[0][1] - offset
    box[1][0] = box[1][0] + offset
    box[1][1] = box[1][1] + offset
    box[2][0] = box[2][0] - offset
    box[2][1] = box[2][1] + offset
    box[3][0] = box[3][0] - offset
    box[3][1] = box[3][1] - offset
    roi = bg_img[int(box[1][1]):int(box[0][1]), int(box[0][0]):int(box[2][0])]
    f_img_resize = cv2.resize(f_img, roi.shape[-2::-1])
    # 单纯的add对这种黑白图片,效果很糟糕
    # roi = cv2.add(roi, f_img_resize)
    # 读入表情的灰度图,方便进行处理
    fg_img_gray = cv2.cvtColor(f_img_resize, cv2.COLOR_BGR2GRAY)
    
    #Otsu 滤波,产生二值化的图片
    ret2,mask = cv2.threshold(fg_img_gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    mask_inv = cv2.bitwise_not(mask)
    # 背景图,去掉表情中的黑色部分的像素
    img1_bg = cv2.bitwise_and(roi, roi, mask = mask)
    # 表情图,去掉图片中的白色部分
    img2_fg = cv2.bitwise_and(f_img_resize, f_img_resize, mask = mask_inv)
    dst = cv2.add(img1_bg, img2_fg)
    bg_img[int(box[1][1]):int(box[0][1]), int(box[0][0]):int(box[2][0])] = dst
    image_result = Image.fromarray(cv2.cvtColor(bg_img,cv2.COLOR_BGR2RGB))
    # 创建底图
    target = Image.new(
        'RGBA', (CONST_IMG_WIDTH, CONST_IMG_HEIGH), (255, 255, 255, 255))
    # 表情背景贴到底图上
    target.paste(image_result, (0, 0))
    target = draw_text_v4(txt, target, off_set=(5, 200), allign='center')
    return target
Esempio n. 60
0
def getBoxes(y_pred,
             detection_threshold=0.7,
             text_threshold=0.4,
             link_threshold=0.4,
             size_threshold=10):
    box_groups = []
    for y_pred_cur in y_pred:
        # Prepare data
        textmap = y_pred_cur[..., 0].copy()
        linkmap = y_pred_cur[..., 1].copy()
        img_h, img_w = textmap.shape

        _, text_score = cv2.threshold(textmap,
                                      thresh=text_threshold,
                                      maxval=1,
                                      type=cv2.THRESH_BINARY)
        _, link_score = cv2.threshold(linkmap,
                                      thresh=link_threshold,
                                      maxval=1,
                                      type=cv2.THRESH_BINARY)
        n_components, labels, stats, _ = cv2.connectedComponentsWithStats(
            np.clip(text_score + link_score, 0, 1).astype('uint8'),
            connectivity=4)
        boxes = []
        for component_id in range(1, n_components):
            # Filter by size
            size = stats[component_id, cv2.CC_STAT_AREA]

            if size < size_threshold:
                continue

            # If the maximum value within this connected component is less than
            # text threshold, we skip it.
            if np.max(textmap[labels == component_id]) < detection_threshold:
                continue

            # Make segmentation map. It is 255 where we find text, 0 otherwise.
            segmap = np.zeros_like(textmap)
            segmap[labels == component_id] = 255
            segmap[np.logical_and(link_score, text_score)] = 0
            x, y, w, h = [
                stats[component_id, key] for key in [
                    cv2.CC_STAT_LEFT, cv2.CC_STAT_TOP, cv2.CC_STAT_WIDTH,
                    cv2.CC_STAT_HEIGHT
                ]
            ]

            # Expand the elements of the segmentation map
            niter = int(np.sqrt(size * min(w, h) / (w * h)) * 2)
            sx, sy = max(x - niter, 0), max(y - niter, 0)
            ex, ey = min(x + w + niter + 1,
                         img_w), min(y + h + niter + 1, img_h)
            segmap[sy:ey, sx:ex] = cv2.dilate(
                segmap[sy:ey, sx:ex],
                cv2.getStructuringElement(cv2.MORPH_RECT,
                                          (1 + niter, 1 + niter)))

            # Make rotated box from contour
            contours = cv2.findContours(segmap.astype('uint8'),
                                        mode=cv2.RETR_TREE,
                                        method=cv2.CHAIN_APPROX_SIMPLE)[-2]
            contour = contours[0]
            box = cv2.boxPoints(cv2.minAreaRect(contour))

            # Check to see if we have a diamond
            w, h = np.linalg.norm(box[0] - box[1]), np.linalg.norm(box[1] -
                                                                   box[2])
            box_ratio = max(w, h) / (min(w, h) + 1e-5)
            if abs(1 - box_ratio) <= 0.1:
                l, r = contour[:, 0, 0].min(), contour[:, 0, 0].max()
                t, b = contour[:, 0, 1].min(), contour[:, 0, 1].max()
                box = np.array([[l, t], [r, t], [r, b], [l, b]],
                               dtype=np.float32)
            else:
                # Make clock-wise order
                box = np.array(np.roll(box, 4 - box.sum(axis=1).argmin(), 0))
            boxes.append(2 * box)
        box_groups.append(np.array(boxes))
    return box_groups