def _apply_(self, *image):

        deg = self.params['deg']
        enlarge = self.params['enlarge']
        res = ()
        n_img = 0
        for img in image:
            rows, cols = img.shape[0], img.shape[1]
            flags = self.SetFlag(n_img)
            if enlarge:
                # this part could be better adjusted
                x = int(rows * (2 - 1.414213) / 1.414213)
                y = int(cols * (2 - 1.414213) / 1.414213)

                z = max(x, y)
                big_image = self.enlarge(img, z, z)

                b_rows, b_cols = big_image.shape[0], big_image.shape[1]
                M = cv2.getRotationMatrix2D((b_cols / 2, b_rows / 2), deg, 1)
                dst = cv2.warpAffine(big_image, M, (b_cols, b_rows), flags=flags)

                res += (self.OutputType(dst[z:(z + rows), z:(z + cols)]), )
            else:
                M = cv2.getRotationMatrix2D((cols / 2, rows / 2), deg, 1)
                sub_res = cv2.warpAffine(img, M, (cols, rows), flags=flags)
                res += (self.OutputType(sub_res),)
            n_img += 1

        return res
Example #2
0
    def rotate(self):
        for index, (x, y, w, h) in enumerate(self.boundary):
            roi = self.img[y: y + h, x: x + w]
            thresh = roi.copy()

            angle = 0
            smallest = 999
            row, col = thresh.shape

            for ang in range(-60, 61):
                M = cv2.getRotationMatrix2D((col / 2, row / 2), ang, 1)
                t = cv2.warpAffine(thresh.copy(), M, (col, row))

                r, c = t.shape
                right = 0
                left = 999

                for i in range(r):
                    for j in range(c):
                        if t[i][j] == 255 and left > j:
                            left = j
                        if t[i][j] == 255 and right < j:
                            right = j

                if abs(right - left) <= smallest:
                    smallest = abs(right - left)
                    angle = ang

            M = cv2.getRotationMatrix2D((col / 2, row / 2), angle, 1)
            thresh = cv2.warpAffine(thresh, M, (col, row))
            thresh = cv2.resize(thresh, (20, 20))

            cv2.imwrite('tmp/' + str(index) + '.png', thresh)
Example #3
0
def readdataset(train,test,augment=True):
    trainfiles = [ os.path.join(train,'frame%d.tif' % i)\
                   for i in range(30) ]
    testfiles = [ os.path.join(test,'frame%d.tif' % i)\
                  for i in range(30) ]

    for tr,ts in zip(trainfiles,testfiles):
        imgtr = cv2.imread(tr,0)
        imgts = cv2.imread(ts,0)
        #yield imgtr, imgts
        for angle in range(1,91,1):
            cos = math.cos(math.radians(angle))
            sin = math.sin(math.radians(angle))
            size = int(512*(cos+sin))
            sl = (size - 512)/2
            print angle,size,sl
            imgr = cv2.resize(imgtr,(size,size))
            rows, cols = imgr.shape
            M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1)
            rotr = cv2.warpAffine(imgr,M,(cols,rows))[sl:sl+512,sl:sl+512]
            
            imgr = cv2.resize(imgts,(size,size))
            rows, cols = imgr.shape
            M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1)
            rots = cv2.warpAffine(imgr,M,(cols,rows))[sl:sl+512,sl:sl+512]

            yield rotr, rots[62:-62,62:-62]
            yield cv2.flip(rotr,1), cv2.flip(rots,1)[62:-62,62:-62]
            yield cv2.flip(rotr,0), cv2.flip(rots,0)[62:-62,62:-62]
            yield cv2.flip(cv2.flip(rotr,1),0), cv2.flip(cv2.flip(rots,1),0)[62:-62,62:-62]
def rotate(img, angle, crop):
  h,w = img.shape[:2]

  if crop:
    # Calculate the rotation matrix then apply it
    M = cv2.getRotationMatrix2D((w/2,h/2), angle, 1) # (center(x,y),angle,scale)
    rtImg = cv2.warpAffine(img,M,(w,h))    
    return rtImg
  
  else:
    # Calculate the size of the canvas
    r = int(math.sqrt(h*h + w*w)) + 1
    imgC = addCanvas(img,r*2)
    hC,wC = imgC.shape[:2]
    
    # Calculate the rotation matrix then apply it
    M = cv2.getRotationMatrix2D((wC/2,hC/2), angle, 1) # (center(x,y),angle,scale)
    rtImg = cv2.warpAffine(imgC,M,(wC,hC))
    
    relativeCorners = getVertices(h,w, math.radians(angle))
    center = (wC/2,hC/2)
    realCorners = [(corner[0]+center[0] , corner[1]+center[1]) for corner in relativeCorners]
    print realCorners
    print relativeCorners
    
    box = surroundingBox(realCorners[0], realCorners[1], realCorners[2], realCorners[3])
    
#     cv2.rectangle(rtImg,(box[0],box[2]), (box[1],box[3]), (0,255,0),3) 
#     for vertex in realCorners:
#         cv2.circle(rtImg, vertex, 20, (0, 255, 0),5)   
    
    # crop the redundant canvas
    rtImg = rtImg[box[2]:box[3],box[0]:box[1]]
    return rtImg
Example #5
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
Example #6
0
	def rotate(self):
		# Check if both points are set
		if self.pt_one != None and self.pt_two != None:
			cv2.line(self.curr_bg, self.pt_one, self.pt_two, GREEN)

			# Calculate delta y and delta x
			deltay = (max(self.pt_one[1], self.pt_two[1]) - min(self.pt_one[1], self.pt_two[1]))
			deltax = (max(self.pt_one[0], self.pt_two[0]) - min(self.pt_one[0], self.pt_two[0]))
			
			# Calculate angle using arctan
			tanval = deltay/float(deltax)
			angle = math.degrees(np.arctan(tanval))
			print angle
			rows, cols = self.orig_bg.shape

			# If the left is higher than the right rotate ccw
			if self.pt_one[1] < self.pt_two[1]:
				self.matrix= cv2.getRotationMatrix2D((cols/2, rows/2), angle, 1)
			else:
				self.matrix= cv2.getRotationMatrix2D((cols/2, rows/2), -angle, 1)
			
			# Set background to newly rotated image
			self.curr_bg = cv2.warpAffine(self.curr_bg, self.matrix, (cols, rows))

			# Draw lines to let users check rotation is sufficient
			cv2.line(self.curr_bg, (0, self.pt_one[1]), (cols, self.pt_one[1]), GREEN)
			cv2.line(self.curr_bg, (0, self.pt_two[1]), (cols, self.pt_two[1]), GREEN)
			cv2.line(self.curr_bg, (0, self.pt_one[1]+10), (cols, self.pt_one[1]+10), GREEN)
Example #7
0
def show_image(idx):
    global cur_img, cur_img_path, if_path, files
    if idx < 0 or idx >= len(files):
        return

    fpath = files[idx]
    new_img_path = "%s/%s" % (if_path, fpath)
    if new_img_path == cur_img_path:
        return

    cur_img_path = new_img_path
    cur_img = cv.imread(cur_img_path)

    # Try to get rotation data and if set, rotate image correctly
    try:
        pil_img = PIL.Image.open(cur_img_path)
        rot_code = pil_img._getexif()[274]
        (h, w, _) = cur_img.shape
        if rot_code == 3:
            cur_img = cv.warpAffine(cur_img, cv.getRotationMatrix2D((w / 2.0, h / 2.0), 180, 1.0), (w, h))
        elif rot_code == 6:
            m = min(h, w)
            cur_img = cv.warpAffine(cur_img, cv.getRotationMatrix2D((m / 2.0, m / 2.0), -90, 1.0), (h, w))
        elif rot_code == 8:
            cur_img = cv.warpAffine(cur_img, cv.getRotationMatrix2D((w / 2.0, w / 2.0), 90, 1.0), (h, w))
        pil_img.close()
    except:
        pass

    imshow(fpath, cur_img)

    if fpath in data:
        print("[%03d/%03d] Showing %s (%s)" % (idx, len(files), fpath, "GOOD" if data[fpath] else "BAD"))
    else:
        print("[%03d/%03d] Showing %s" % (idx, len(files), fpath))
Example #8
0
def warp(frame,pitch=0):
    if pitch == 0: 
        M = cv2.getRotationMatrix2D((COLS/2, ROWS/2), 1, 1)
        return cv2.warpAffine(frame, M, (COLS, ROWS))
    else:
        M = cv2.getRotationMatrix2D((COLS/2, ROWS/2), 0, 1)
        return cv2.warpAffine(frame, M, (COLS, ROWS))
Example #9
0
	def drawAndRotate(self):
		cv2.namedWindow(self.winName)
		img = cv2.imread(self.imgName)

		# create trackbars for line change
		cv2.createTrackbar('Left', self.winName, 0, self.imgHeight, self.nothing)
		cv2.createTrackbar('Right', self.winName, 0, self.imgHeight, self.nothing)

		# create switch for ON/OFF functionality for horizon line
		switch = 'Horizon Line'
		cv2.createTrackbar(switch, self.winName, 0, 1, self.nothing)

		#create switch for rotate ON/OFF
		rotSwitch = 'Rotation'
		cv2.createTrackbar(rotSwitch, self.winName, 0, 1, self.nothing)

		while(1):
		    cv2.imshow(self.winName,img)
		    k = cv2.waitKey(1) & 0xFF
		    if k == 27:
		        break

		    # get current positions of trackbars
		    self.l = cv2.getTrackbarPos('Left', self.winName)
		    self.r = cv2.getTrackbarPos('Right', self.winName)
		    self.s = cv2.getTrackbarPos(switch, self.winName)
		    self.rot = cv2.getTrackbarPos(rotSwitch, self.winName)

		    if self.s == 1:
		    	cv2.namedWindow('Line')
		    	lineImg = cv2.imread(self.imgName)
		    	cv2.imshow('Line', lineImg)
		        cv2.line(lineImg, (0, self.l), (self.imgWidth, self.r), (255,255,255), 5)
		    else:
		    	cv2.destroyWindow('Line')

			if self.rot == 1:
				#create window
				cv2.namedWindow('Rotate')
				rotImg = cv2.imread(self.imgName)
				#find theta and hypotenuse
				self.findHypotenuse()
				self.findAngle()
				#rotate
				if self.l < self.r:
					M = cv2.getRotationMatrix2D((self.imgWidth/2, self.imgHeight/2), (90-self.theta), 1)
					dst = cv2.warpAffine(rotImg, M, (self.imgWidth, self.imgHeight))
					cv2.imshow('Rotate', dst)
				else:
					M = cv2.getRotationMatrix2D((self.imgWidth/2, self.imgHeight/2), -(90-self.theta), 1)
					dst = cv2.warpAffine(rotImg, M, (self.imgWidth, self.imgHeight))
					cv2.imshow('Rotate', dst)
			else:
				cv2.destroyWindow('Rotate')


		cv2.destroyAllWindows()
Example #10
0
def rotate_90_degree(image, is_inv=False):
    angle = 90 if not is_inv else -90
    center = image.shape[1]/2, image.shape[0]/2

    affine_mat = cv2.getRotationMatrix2D(center, angle, 1)
    affine_inv_mat = cv2.getRotationMatrix2D(center, -angle, 1)
    image_affine = cv2.warpAffine(image, affine_mat, (image.shape[1], image.shape[0]))
    # util.imshow_as_uint8_cv(image_affine)

    return image_affine, affine_mat, affine_inv_mat
def RotateImg(img, ang):
    if (len(img.shape) == 2):
        rows,cols = img.shape
        M = cv2.getRotationMatrix2D((cols/2,rows/2),ang,1)
        dst = cv2.warpAffine(img,M,(cols,rows))
    else:
        rows,cols,_ = img.shape
        M = cv2.getRotationMatrix2D((cols/2,rows/2),ang,1)
        dst = cv2.warpAffine(img,M,(cols,rows))
    return dst
Example #12
0
 def _rotate90(img, ground_truth, u=0.5, v=1.0):
     if v < u:
         angle = 90
         img_rows, img_cols = img.shape
         img_m = cv2.getRotationMatrix2D((img_cols / 2, img_rows / 2), angle, 1)
         img = cv2.warpAffine(img, img_m, (img_cols, img_rows))
         ground_truth_rows, ground_truth_cols = ground_truth.shape
         ground_truth_m = cv2.getRotationMatrix2D((ground_truth_cols / 2, ground_truth_rows / 2), angle, 1)
         ground_truth = cv2.warpAffine(ground_truth, ground_truth_m, (ground_truth_cols, ground_truth_rows))
     return img, ground_truth
Example #13
0
def adjustface(face):
    FACTOR = 0.2

    if face is None or face=='':
        return None

    eyepos = []
    img = cv2.imread(face)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    height,width = img.shape[:2]

    eyes = eye_cascade.detectMultiScale(img)
    eyes = [item for item in eyes if item[1] > height/4 \
                                and (2*item[1]+item[3])/2 < height*2/3]
    eyes = sorted(eyes,key=lambda item:item[1])[:2]
    eyes = sorted(eyes,key=lambda item:item[0])

    if len(eyes) != 0:
        for index,(ex,ey,ew,eh) in enumerate(eyes):
            eyepos.append((ex+ew/2,ey+eh/2))

    if len(eyepos)==2:
        eyepos = sorted(eyepos,key=lambda item:item[0])
        leye = eyepos[0]
        reye = eyepos[1]
        # cv2.line(img,(leye[0],leye[1]),(reye[0],reye[1]),(0,0,255),2)

        yy = math.fabs(leye[1] - reye[1])
        xx = math.fabs(leye[0] - reye[0])
        degree = math.degrees(math.atan(yy/xx))

        if degree > 5:
            if reye[1] > leye[1]:
                M = cv2.getRotationMatrix2D((width/2,height/2),degree,1)
            else:
                M = cv2.getRotationMatrix2D((width/2,height/2),-degree,1)
        else:
            M = cv2.getRotationMatrix2D((width/2,height/2),0,1)

        dst = cv2.warpAffine(img,M,(width,height))

        real = cv2.cvtColor(dst,cv2.COLOR_BGR2GRAY)

        distance = leye[0]-reye[0] if leye[0] > reye[0] else reye[0] - leye[0]
        proportion = float(distance)/width

        if FACTOR > proportion:
            scale = (width-distance/FACTOR)/(2*width)
            real = enlargeimage(real,scale)

        return real
    else:
        print "can not match the eyes."
    return gray
Example #14
0
def genRotateImage(img, r, center):
    img_invert = (255 - img)
    transMat = cv2.getRotationMatrix2D(center, 0, 1)
    transMat[0][2] += max(img.shape[1], img.shape[0])/2 - img.shape[1]/2
    transMat[1][2] += max(img.shape[1], img.shape[0])/2 - img.shape[0]/2
    img_invert = cv2.warpAffine(img_invert, transMat, (max(img.shape[1], img.shape[0]),max(img.shape[1], img.shape[0])))
    center = (max(img.shape[1], img.shape[0])/2, max(img.shape[1], img.shape[0])/2)
    rotMat = cv2.getRotationMatrix2D(center, r, 1)
    img_r = cv2.warpAffine(img_invert, rotMat, (max(img.shape[1], img.shape[0]),max(img.shape[1], img.shape[0])))
    img_r = (255 - img_r)
    return img_r
Example #15
0
def rotateCrops(crop1, crop2, angle, secondAngle, broken):
    if broken == False:
        rows,cols,ch = crop1.shape
        rows2,cols2,ch2 = crop2.shape
        Rotate = cv2.getRotationMatrix2D((cols/2,rows/2), (angle * -1),1)
        Rotate2 = cv2.getRotationMatrix2D((rows2/2,cols2/2),(secondAngle * -1),1)
        rotatedCrop = cv2.warpAffine(crop1, Rotate,(cols, rows))
        rotatedCrop2 = cv2.warpAffine(crop2, Rotate2,(cols2,rows2))
        cv2.imshow("rotate1", rotatedCrop)
        cv2.imshow("rotate2", rotatedCrop2)
        cv2.waitKey(0)
def Redraw(save=False, newImg=False):
    global center, a, b, r, angle, fig, img, personId, direction, w, h, imgIdx
    ax.cla()
    ax_cropped.cla()
    
    M = cv2.getRotationMatrix2D((w/2, h/2), angle, 1)
    dst = cv2.warpAffine(img, M, (w,h))
    cropped = dst[int(h/2.-r-b/2.): int(h/2.-r+b/2.), int((w-a)/2.): int((w+a)/2.)]
    draw = cv2.resize(cropped, (200,200))
    if save:
        try:        
            data = "%d,%d,%d,%d,%d,%d,%d" % (imgIdx+idxOffset, personId, center[0]-pads[0], center[1]-pads[1], a, b, direction)
            writer.writerow(data.split(','))
        except csv.Error as e:
            sys.exit('File %s, line %d: %s') % (labelFile, writer.line_num, e)
        picName = "/home/veerachart/Datasets/PIROPO_annotated/omni_1A/omni1A_training/with_directions/%06d_%02d.jpg" % (imgIdx+idxOffset, personId)
        plt.imsave(fname=picName,arr=cropped)
    if newImg:
        line = reader.next()
        imgIdx += 1
        while int(line[1]) == 0 and int(line[2]) == 0:
            try:
                data = "%d,%d,%d,%d,%d,%d,%d" % (imgIdx+idxOffset, -1, int(line[1]), int(line[2]), -1, -1, -1)
                writer.writerow(data.split(','))
            except csv.Error as e:
                sys.exit('File %s, line %d: %s') % (labelFile, writer.line_num, e)
            line = reader.next()
            imgIdx += 1
        img = plt.imread(imgDir+'/'+imgList[imgIdx])
        center = [int(line[1])+pads[0], int(line[2])+pads[1]]
        r = np.sqrt((h/2-center[1])**2 + (center[0]-w/2)**2)
        angle = np.arctan2(center[0]-w/2, h/2-center[1]) * 180./np.pi
        h,w,ch = img.shape
        if h>w:
            img = np.lib.pad(img,((0,0),(pad_width,pad_width),(0,0)),'constant',constant_values=0)
        elif w>h:
            img = np.lib.pad(img,((pad_width,pad_width),(0,0),(0,0)),'constant',constant_values=0)
        h,w,ch = img.shape
        M = cv2.getRotationMatrix2D((w/2, h/2), angle, 1)
        dst = cv2.warpAffine(img, M, (w,h))
        cropped = dst[int(h/2.-r-b/2.): int(h/2.-r+b/2.), int((w-a)/2.): int((w+a)/2.)]
        draw = cv2.resize(cropped, (200,200))
    ax.axis('off')
    ax_cropped.axis('off')
    cv2.rectangle(dst,(int((w-a)/2.), int(h/2.-r-b/2.)), (int((w+a)/2.), int(h/2.-r+b/2.)), (0,255,0), 2)
    M = cv2.getRotationMatrix2D((w/2, h/2), -angle, 1)
    dst = cv2.warpAffine(dst, M, (w,h))
    cv2.line(dst, (center[0], center[1]), (int(round(center[0]+50*np.cos((angle-direction+90.)*np.pi/180.))),int(round(center[1]+50*np.sin((angle-direction+90)*np.pi/180.)))),(255,0,0))
    ax.imshow(dst)
    fig.canvas.draw_idle()
    cv2.line(draw, (100,100), (int(round(100+100*np.sin(direction*np.pi/180.))),int(round(100+100*np.cos(direction*np.pi/180.)))),(255,0,0))
    ax_cropped.imshow(draw)
    fig_cropped.canvas.draw_idle()
def RotateImg(img, ang):
    """
    Rotate the image usign the given ang
    """
    if (len(img.shape) == 2):
        rows,cols = img.shape
        M = cv2.getRotationMatrix2D((cols/2,rows/2),ang,1)
        dst = cv2.warpAffine(img,M,(cols,rows))
    else:
        rows,cols,_ = img.shape
        M = cv2.getRotationMatrix2D((cols/2,rows/2),ang,1)
        dst = cv2.warpAffine(img,M,(cols,rows))
    return dst
Example #18
0
        def _rotate(img, ground_truth, angle=45, u=0.5, v=1.0):
            if v < u:
                # test = "augment image shape: {}".format(img.shape)
                # log.info(test)
                img_rows, img_cols, channels = img.shape
                img_m = cv2.getRotationMatrix2D((img_cols / 2, img_rows / 2), angle, 1)
                img = cv2.warpAffine(img, img_m, (img_cols, img_rows))

                ground_truth_rows, ground_truth_cols, channels_gt = ground_truth.shape
                ground_truth_m = cv2.getRotationMatrix2D((ground_truth_cols / 2, ground_truth_rows / 2), angle, 1)
                ground_truth = cv2.warpAffine(ground_truth, ground_truth_m, (ground_truth_cols, ground_truth_rows))

            return img, ground_truth
Example #19
0
    def __init__(self):
        bridge = CvBridge()

        self.current_phase = Game.PHASE_I
        self.time_remaining = None
        self.score = None
        self.opponent_score = None
        self.update_time = rospy.Time.now()

        # load 300x300 logo

        # logo = np.zeros((300,300,3), np.uint8)
        # logo[:,0:150] = (255,0,0)
        # logo[:,150:300] = (0,255,0) # dummy logo

        rospack = rospkg.RosPack()
        path = rospack.get_path("bigredrobot_final")
        # raise ValueError(path+'/lib/logo.png')
        logo = cv.imread(path + "/lib/logo.png", cv.IMREAD_COLOR)
        if np.random.random(1) < 0.5:
            logo = cv.flip(logo, 1)
        if np.random.random(1) < 0.5:
            logo = cv.flip(logo, 0)
        rand = np.random.random(1)
        rows, cols, _ = logo.shape
        if rand < 0.25:
            R = cv.getRotationMatrix2D((cols / 2, rows / 2), 0, 1)
        elif rand < 0.5:
            R = cv.getRotationMatrix2D((cols / 2, rows / 2), 90, 1)
        elif rand < 0.75:
            R = cv.getRotationMatrix2D((cols / 2, rows / 2), 180, 1)
        else:
            R = cv.getRotationMatrix2D((cols / 2, rows / 2), 270, 1)
        logo = cv.warpAffine(logo, R, (cols, rows))

        # Convert to imgmsg
        logo_msg = bridge.cv2_to_imgmsg(logo, encoding="bgr8")

        # Call init service of game_server and get arm
        if TEST_DEBUG:
            self.arm = TEST_ARM
        else:
            init = rospy.ServiceProxy("/game_server/init", Init)
            response = init("bigredrobot", logo_msg)
            self.arm = response.arm

            # Subscribe to game_state updates
            rospy.Subscriber("/game_server/game_state", GameState, self.game_state_callback)

        # Initialise a service for use by other nodes to get arm (returns True=right, False=left)
        rospy.Service("/bigredrobot/arm", Trigger, self.arm_callback)
Example #20
0
def pre_deskew(img_rgb, img_gray, img_scale, border):
    mask = cv2.inRange(img_gray, 240, 255)

    for m in (img_gray, 255-mask):
        n = cv2.moments(m)
        #print(n['mu11']/n['mu02'])
        contours,_ = cv2.findContours(m, cv2.RETR_EXTERNAL, 2)
        cnt = sorted(contours, key = cv2.contourArea, reverse = True)[0]
        rect = cv2.minAreaRect(cnt)
        if abs(rect[2]) > 45:
            rect = (rect[0], rect[1], 90.0 + rect[2])
        if abs(rect[2]) < 20 and abs(rect[2]) != 0: # != -90.0 and rect[2] != 0.0:
            print('Skew: %.3f°' % rect[2])
            break

    #cv2.imshow('normgr4', m)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    #im = img_scale.copy()
    #peri = cv2.arcLength(box, True)
    #approx = cv2.approxPolyDP(box, 0.02 * peri, True)
    #img_cnt = np.array([ [[0,0]], [[rows,0]], [[rows, cols]], [[0, cols]] ])

    #cv2.drawContours(im,[approx],0,(0,0,255),5)
    #cv2.fillPoly(im, [box], (0,0,255))
    #cv2.drawContours(im, [img_cnt], 0,(0,255,0),2)
    #im = cv2.flip(im, 1)

    if abs(rect[2]) < 20:
        img_scale = img_scale.copy()
        M = cv2.getRotationMatrix2D(rect[0],rect[2],1)
        Mo = cv2.getRotationMatrix2D((rect[0][0]/2.0, rect[0][1]/2.0), rect[2],1)
        # img_scale = cv2.warpAffine(img_scale,M,(rows,cols))

        dst = cv2.cv.fromarray(img_scale.copy())
        cv2.cv.WarpAffine(cv2.cv.fromarray(img_scale),dst,cv2.cv.fromarray(M),flags=cv2.INTER_LINEAR+8,fillval=(255,255,255))
        img_scale = np.asarray(dst)

        dst = cv2.cv.fromarray(img_rgb.copy())
        cv2.cv.WarpAffine(cv2.cv.fromarray(img_rgb),dst,cv2.cv.fromarray(Mo),flags=cv2.INTER_LINEAR+8,fillval=(255,255,255))
        img_rgb = np.asarray(dst)

        #img_lev = pre_levels(img_scale, 200, 255)
#        img_lev = pre_levels(img_scale, 0, 170)

        img_scale[:border]    = pre_whiteness(img_scale[:border])
        img_scale[-border:]   = pre_whiteness(img_scale[-border:])
        img_scale[:,-border:] = pre_whiteness(img_scale[:,-border:])
        img_scale[:,:border]  = pre_whiteness(img_scale[:,:border])

    return img_rgb, img_scale
Example #21
0
def rotation_list(numberofrotation,degree_bottom,degree_top,image_w,image_h):
    rotation_list = []
    for step in range(numberoftrans):
        
        degree = random.randint(degree_bottom, degree_top)
        transM = cv2.getRotationMatrix2D((image_w/2,image_h/2),degree,1)
        rotation_list.append(transM)
    
    for step in range(numberoftrans):
       
        degree = random.randint(degree_bottom, degree_top)
        transM = cv2.getRotationMatrix2D((image_w/2,image_h/2),360 - degree,1)
        rotation_list.append(transM)
    
    return rotation_list
def read_img_train_Y_channel(cur_dir, down_time=20, scale=0.97):
    """
    读取目录下的图片:提取全部图片,并提取成单通道图像,并归一化数值[0,1]
    :param cur_dir:
    :return:
    """
    img_file_list = os.listdir(cur_dir)  # 读取目录下全部图片文件名

    img_lib = []
    for file_name in img_file_list:
        if file_name[-3:] != "jpg" and file_name[-3:] != "bmp":
            continue
        full_file_name = os.path.join(cur_dir, file_name)
        image = cv2.imread(full_file_name)  # 读取一张图片

        image = image[0:image.shape[0] - image.shape[0] % 3, 0:image.shape[1] - image.shape[1] % 3,:]
        image_low = imresize(image, 1/3.0, interp='bicubic')
        image_r = imresize(image_low, 3.0, interp='bicubic')

        image_h = np.array(image/255.0, dtype=np.float32)
        image_l = np.array(image_r / 255.0, dtype=np.float32)

        Y_H = cv2.cvtColor(image_h, cv2.COLOR_BGR2YCR_CB, None)[:, :, 0]
        Y_L = cv2.cvtColor(image_l, cv2.COLOR_BGR2YCR_CB, None)[:, :, 0]

        m1 = cv2.getRotationMatrix2D((20,20),90,1)
        m2 = cv2.getRotationMatrix2D((20,20),180,1)
        m3 = cv2.getRotationMatrix2D((20,20),270,1)

        img_lib.append((np.array(Y_H, dtype=np.float32), np.array(Y_L, dtype=np.float32)))
        # img_lib.append((cv2.warpAffine(np.array(Y_H, dtype=np.float32),m1,Y_H.shape)
        #                , cv2.warpAffine(np.array(Y_L, dtype=np.float32),m1,Y_H.shape)))
        # img_lib.append((cv2.warpAffine(np.array(Y_H, dtype=np.float32),m2,Y_H.shape)
        #                , cv2.warpAffine(np.array(Y_L, dtype=np.float32),m2,Y_H.shape)))
        # img_lib.append((cv2.warpAffine(np.array(Y_H, dtype=np.float32),m3,Y_H.shape)
        #                , cv2.warpAffine(np.array(Y_L, dtype=np.float32),m3,Y_H.shape)))

        fac = 1.0
        # 多次下采样作为样本:
        for i in range(down_time):
            if fac < 0.6 :
                break
            _Y_H = cv2.resize(Y_H,(0, 0), None, fac, fac)
            _Y_L = cv2.resize(Y_L, (0, 0), None, fac, fac)
            fac = fac * scale
            img_lib.append((np.array(_Y_H, dtype = np.float32), np.array(_Y_L, dtype = np.float32)))
    print("图像字典完毕,共%d对图片" % len(img_lib))
    return img_lib
Example #23
0
def main():
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-1", "--first", required=True,
        help="path to the first image")
    ap.add_argument("-2", "--second", required=True,
        help="path to the second image")
    ap.add_argument("-3", "--third", required=False,
        help="path to the third image")
    ap.add_argument("-4", "--fourth", required=False,
        help="path to the fourth image")
    args = vars(ap.parse_args())

    imageA = cv2.imread(args["first"])
    imageB = cv2.imread(args["second"])

    result1 = stitch_images(imageA, imageB)

    if (args["third"] != None and args["fourth"] != None):
        imageC = cv2.imread(args["third"])
        imageD = cv2.imread(args["fourth"])
        result2 = stitch_images(imageC, imageD)

        #rotate resulting images
        rows, cols, _ = result1.shape
        M = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 1)
        result1 = cv2.warpAffine(result1, M, (cols, rows))

        cv2.imwrite('result1.jpg', result1)

        rows, cols, _ = result2.shape
        M = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 1)
        result2 = cv2.warpAffine(result2, M, (cols, rows))

        cv2.imwrite('result2.jpg', result2)

        result = stitch_images(result1, result2)
        cv2.imwrite('rotatedresult.jpg', result)
        #rotate back
        rows, cols, _ = result.shape
        M = cv2.getRotationMatrix2D((cols/2, rows/2), 270, 1)
        result = cv2.warpAffine(result, M, (cols, rows))

        # save the image
        cv2.imwrite('result.jpg', result)
    else:
        # save the image
        cv2.imwrite('result1.jpg', result1)
Example #24
0
    def rotate(self, degrees):
        # see http://stackoverflow.com/a/23990392
        if degrees == 90:
            self.image = cv2.transpose(self.image)
            cv2.flip(self.image, 0, self.image)
        elif degrees == 180:
            cv2.flip(self.image, -1, self.image)
        elif degrees == 270:
            self.image = cv2.transpose(self.image)
            cv2.flip(self.image, 1, self.image)
        else:
            # see http://stackoverflow.com/a/37347070
            # one pixel glitch seems to happen with 90/180/270
            # degrees pictures in this algorithm if you check
            # the typical github.com/recurser/exif-orientation-examples
            # but the above transpose/flip algorithm is working fine
            # for those cases already
            width, height = self.size
            image_center = (width / 2, height / 2)
            rot_mat = cv2.getRotationMatrix2D(image_center, degrees, 1.0)

            abs_cos = abs(rot_mat[0, 0])
            abs_sin = abs(rot_mat[0, 1])
            bound_w = int((height * abs_sin) + (width * abs_cos))
            bound_h = int((height * abs_cos) + (width * abs_sin))

            rot_mat[0, 2] += ((bound_w / 2) - image_center[0])
            rot_mat[1, 2] += ((bound_h / 2) - image_center[1])

            self.image = cv2.warpAffine(self.image, rot_mat, (bound_w, bound_h))
def rotateImage(image, angle):
  if len(image.shape) == 3:
        image = image[0]
  image_center = tuple(np.array(image.shape)/2)
  rot_mat = cv2.getRotationMatrix2D(image_center,angle,1.0)
  result = cv2.warpAffine(image, rot_mat, image.shape,flags=cv2.INTER_LINEAR)
  return np.array(result[:, :], dtype = np.float32)
 def load_left_frame(self, filename):
     image = cv2.imread(filename)
     rows, cols = image.shape[:2]
     M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 24, 1)
     self.left_frame = cv2.warpAffine(image, M, (cols, rows))
     self.left_hsv, self.left_gray, self.left_laplacian = self.process_frame(
         self.left_frame)
Example #27
0
def extract_roi(image, roi):
  '''
  '''
  image = np.copy(image)

  # determine angle and center of rotation
  center, angle = get_roi_position(roi)

  # rotate image to align it's baseline to the X-axis
  matrix           = cv2.getRotationMatrix2D(center, math.degrees(angle), 1.0)
  height, width, _ = image.shape
  image            = cv2.warpAffine(image, matrix, (width,height))

  # rotate the ROI rectangle also to easily get width and height
  tooth   = rotate_points(roi, -angle)
  width   = abs(tooth[0][0] - tooth[3][0])
  height  = abs(tooth[0][1] - tooth[1][1])

  corner = roi[0].astype(np.int)

  x1 = max(0, int(corner[0]))
  y1 = max(0, int(corner[1]))
  x2 = max(0, int(x1 + width))
  if is_upper(roi):
    y2 = max(0, int(y1 - height))
  else:
    y2 = max(0, int(y1 + height))

  part = image[min(y1,y2):max(y1,y2), min(x1,x2):max(x1,x2)]
  
  return part  
Example #28
0
def crop_color_face(item, img, base_dir, out_dir, fn):
    rows, cols, colors = img.shape
    hypot = int(math.hypot(rows, cols))
    frame = np.zeros((hypot, hypot, 3), np.uint8)
    frame[int((hypot - rows) * 0.5):int((hypot + rows) * 0.5), int((hypot - cols) * 0.5):int((hypot + cols) * 0.5)] = img

    deg = item['deg']
    M = cv2.getRotationMatrix2D((hypot * 0.5, hypot * 0.5), -deg, 1.0)
    rotated = cv2.warpAffine(frame, M, (hypot, hypot))

    #"""
    #out_file = '%s/7_color_deg_%s.jpg' % (img_dir, deg)
    #cv2.imwrite(out_file, rotated)
    #"""

    x,y,w,h = item['frame']
    face = rotated[y:y+h, x:x+w]
    face = cv2.resize(face, (IMAGE_SIZE, IMAGE_SIZE))
    #"""
    web_path = '%s/%s_%s.jpg' % (out_dir, fn, item['face_id'])
    out_file = '%s/%s' % (base_dir, web_path)
    print 'web_path', web_path
    print 'out_file', out_file
    cv2.imwrite(out_file, face)
    #"""

    return web_path
def show_video(name):
    cap = cv2.VideoCapture(name)
    rotate = False
    M = cv2.getRotationMatrix2D((480,270), 180, 1.0)
    if not cap.isOpened():
      print("Error when reading video")
    else:
        while(True):
            try:
                # Capture frame-by-frame
                ret, frame = cap.read()
                frame = cv2.resize(frame, (960,540))
                if rotate:
                    frame = cv2.warpAffine(frame, M, (960, 540))
                cv2.putText(frame,'press the escape key when done',(20,20), cv2.FONT_HERSHEY_SIMPLEX, 1,(130,130,130),2)
                cv2.imshow(name,frame)
            except:
                cap = cv2.VideoCapture(name)
                ret, frame = cap.read()
                frame = cv2.resize(frame, (960,540))
                cv2.imshow(name,frame)
            k = cv2.waitKey(20)
            if k == 27:
                break
            elif k == 114:
                rotate = False if rotate is True else True

    # When everything is done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    return rotate
Example #30
0
def rotateImage(I, angle):
    "Rotate the image, I, angle degrees around the image center"
    size = I.shape
    image_center = tuple(np.array(size)/2)
    rot_mat = cv2.getRotationMatrix2D(image_center[0:2],angle,1)
    result = cv2.warpAffine(image, rot_mat,dsize=size[0:2],flags=cv2.INTER_LINEAR)
    return result
h, w, c = img.shape
rest = cv2.resize(img, (2 * w, 2 * h), interpolation=cv2.INTER_CUBIC)
#cv2.imshow('res',res)
#cv2.imshow('rest',rest)

#Translation
#M is the transition matrix
rows, cols, chan = img.shape
#for a shift of (100,50), generally array format is [1,0,x,],[0,1,y]
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow('dst', dst)

#Rotation
#the arguments are rotation center, angle, and magnification
M1 = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1)
dstt = cv2.warpAffine(img, M1, (cols, rows))
cv2.imshow('dstt', dstt)

#Affline Transformation

pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])

M2 = cv2.getAffineTransform(pts1, pts2)
dsr = cv2.warpAffine(img, M, (cols, rows))

plt.subplot(121), plt.imshow(img), plt.title('Input')
plt.subplot(122), plt.imshow(dsr), plt.title('Output')
#plt.show()
Example #32
0
def random_perspective(img,
                       targets=(),
                       degrees=10,
                       translate=.1,
                       scale=.1,
                       shear=10,
                       perspective=0.0,
                       border=(0, 0)):
    # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
    # targets = [cls, xyxy]

    height = img.shape[0] + border[0] * 2  # shape(h,w,c)
    width = img.shape[1] + border[1] * 2

    # Center
    C = np.eye(3)
    C[0, 2] = -img.shape[1] / 2  # x translation (pixels)
    C[1, 2] = -img.shape[0] / 2  # y translation (pixels)

    # Perspective
    P = np.eye(3)
    P[2, 0] = random.uniform(-perspective,
                             perspective)  # x perspective (about y)
    P[2, 1] = random.uniform(-perspective,
                             perspective)  # y perspective (about x)

    # Rotation and Scale
    R = np.eye(3)
    a = random.uniform(-degrees, degrees)
    # a += random.choice([-180, -90, 0, 90])  # add 90deg rotations to small rotations
    s = random.uniform(1 - scale, 1 + scale)
    # s = 2 ** random.uniform(-scale, scale)
    R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)

    # Shear
    S = np.eye(3)
    S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi /
                       180)  # x shear (deg)
    S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi /
                       180)  # y shear (deg)

    # Translation
    T = np.eye(3)
    T[0, 2] = random.uniform(0.5 - translate,
                             0.5 + translate) * width  # x translation (pixels)
    T[1, 2] = random.uniform(
        0.5 - translate, 0.5 + translate) * height  # y translation (pixels)

    # Combined rotation matrix
    M = T @ S @ R @ P @ C  # order of operations (right to left) is IMPORTANT
    if (border[0] != 0) or (border[1] !=
                            0) or (M != np.eye(3)).any():  # image changed
        if perspective:
            img = cv2.warpPerspective(img,
                                      M,
                                      dsize=(width, height),
                                      borderValue=(114, 114, 114))
        else:  # affine
            img = cv2.warpAffine(img,
                                 M[:2],
                                 dsize=(width, height),
                                 borderValue=(114, 114, 114))

    # Visualize
    # import matplotlib.pyplot as plt
    # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
    # ax[0].imshow(img[:, :, ::-1])  # base
    # ax[1].imshow(img2[:, :, ::-1])  # warped

    # Transform label coordinates
    n = len(targets)
    if n:
        # warp points
        xy = np.ones((n * 4, 3))
        xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(
            n * 4, 2)  # x1y1, x2y2, x1y2, x2y1
        xy = xy @ M.T  # transform
        if perspective:
            xy = (xy[:, :2] / xy[:, 2:3]).reshape(n, 8)  # rescale
        else:  # affine
            xy = xy[:, :2].reshape(n, 8)

        # create new boxes
        x = xy[:, [0, 2, 4, 6]]
        y = xy[:, [1, 3, 5, 7]]
        xy = np.concatenate(
            (x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T

        # # apply angle-based reduction of bounding boxes
        # radians = a * math.pi / 180
        # reduction = max(abs(math.sin(radians)), abs(math.cos(radians))) ** 0.5
        # x = (xy[:, 2] + xy[:, 0]) / 2
        # y = (xy[:, 3] + xy[:, 1]) / 2
        # w = (xy[:, 2] - xy[:, 0]) * reduction
        # h = (xy[:, 3] - xy[:, 1]) * reduction
        # xy = np.concatenate((x - w / 2, y - h / 2, x + w / 2, y + h / 2)).reshape(4, n).T

        # clip boxes
        xy[:, [0, 2]] = xy[:, [0, 2]].clip(0, width)
        xy[:, [1, 3]] = xy[:, [1, 3]].clip(0, height)

        # filter candidates
        i = box_candidates(box1=targets[:, 1:5].T * s, box2=xy.T)
        targets = targets[i]
        targets[:, 1:5] = xy[i]

    return img, targets
Example #33
0
    def Rotate(self, im_dir):
        im_list = glob.glob(im_dir + "/*.jpg")
        im_count = len(im_list)
        print("Image Num = " + str(im_count))
        for im_name in im_list:
            label_name = os.path.splitext(im_name)[0] + ".txt"
            _label_name = "./rotated_image/" + os.path.basename(label_name)
            if os.path.isfile(label_name):
                print(im_name)

                img = cv2.imread(im_name)
                HEIGHT, WIDTH = img.shape[:2]

                save_flag = False
                labels = ""

                lines = []
                with open(label_name, "r") as f:
                    lines = f.readlines()

                for line in lines:
                    _line = line.split()
                    signal = _line[0]
                    x = float(_line[1]) * WIDTH
                    y = float(_line[2]) * HEIGHT
                    width = float(_line[3]) * WIDTH
                    height = float(_line[4]) * HEIGHT

                    im_tr = img[int(y - height / 2):int(y + height / 2),
                                int(x - width / 2):int(x + width / 2)]

                    angle = 0

                    while True:
                        angle = random.randint(-3, 3)
                        if angle != 0:
                            if abs(angle) == 1:
                                angle += angle
                            break

                    angle_rad = math.radians(angle)
                    S = math.sin(angle_rad)
                    C = math.cos(angle_rad)
                    _width = width * C + height * S
                    _height = width * S + height * C
                    print(str(_width) + " , " + str(_height))
                    M = cv2.getRotationMatrix2D(
                        (int(width / 2), int(height / 2)), angle, 1.0)
                    M[0][2] += (_width - width) / 2
                    M[1][2] += (_height - height) / 2

                    correct_image = False

                    if im_tr.shape[0] > 0 and im_tr.shape[1] > 0:
                        im_tr = cv2.warpAffine(im_tr, M,
                                               (int(_width), int(_height)))
                        _lty = y - _height / 2
                        _rby = _lty + _height
                        _ltx = x - _width / 2
                        _rbx = _ltx + _width

                        if _lty > 0 and _ltx > 0 and _rby < HEIGHT and _rbx < WIDTH:
                            im_tr = cv2.resize(im_tr,
                                               dsize=(int(_rbx) - int(_ltx),
                                                      int(_rby) - int(_lty)))
                            #print("("+str(int(_lty))+", "+str(int(_rby))+"), ("+str(int(_ltx))+", "+str(int(_rbx))+")")
                            img[int(_lty):int(_rby),
                                int(_ltx):int(_rbx)] = im_tr
                            labels += signal + " " + _line[1] + " " + _line[
                                2] + " " + str(_width / WIDTH) + " " + str(
                                    _height / HEIGHT) + "\n"
                            correct_image = True
                            save_flag = True

                    if not correct_image:
                        labels += _line[0] + " " + _line[1] + " " + _line[
                            2] + " " + _line[3] + " " + _line[4] + "\n"

                if save_flag:
                    cv2.imwrite("./rotated_image/" + os.path.basename(im_name),
                                img)
                    with open(_label_name, "w") as lf:
                        lf.writelines(labels)
def rotate_opencv(img, nose_center, angle):
    M = cv2.getRotationMatrix2D(nose_center, angle, 1)
    rotated = cv2.warpAffine(img,
                             M, (img.shape[1], img.shape[0]),
                             flags=cv2.INTER_CUBIC)
    return rotated
Example #35
0
        # Obtain the Threshold for the GRAY Image
        ocr_thr, ocr_img = cv2.threshold(ocr_img, 0, 255,
                                         cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        ocr3 = Image.fromarray(ocr_img)

        # Rotate the Image to DeSkew It
        coords = np.column_stack(np.where(ocr_img > 0))
        angle = cv2.minAreaRect(coords)[-1]
        if angle < -45:
            angle = -(90 + angle)
        else:
            angle = -angle
        (h, w) = ocr_img.shape[:2]
        center = (w // 2, h // 2)
        M = cv2.getRotationMatrix2D(center, angle, 1.0)
        ocr_img = cv2.warpAffine(ocr_img,
                                 M, (w, h),
                                 flags=cv2.INTER_CUBIC,
                                 borderMode=cv2.BORDER_REPLICATE)
        ocr4 = Image.fromarray(ocr_img)

        # Remove Noises
        kernel = np.ones((1, 1), np.uint8)
        # Dilating the Image
        ocr_img = cv2.dilate(ocr_img, kernel, iterations=1)
        ocr5 = Image.fromarray(ocr_img)
        # Eroding the Image
        ocr_img = cv2.erode(ocr_img, kernel, iterations=1)
        ocr6 = Image.fromarray(ocr_img)
Example #36
0
from ffpyplayer.player import MediaPlayer

cap = cv2.VideoCapture("Dog.mp4")
sourcePath = "Dog.mp4"
player = MediaPlayer(sourcePath)

width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter("dog_out_1.mp4", fourcc, 30, (width, height))

while True:
    ret, frame = cap.read()
    audio_frame, val = player.get_frame()
    # frame2_resized = cv2.resize(frame2, (500, 500))
    frame_resized = cv2.resize(frame, (width // 2, height // 2))
    rows, columns, channels = frame_resized.shape
    R = cv2.getRotationMatrix2D((columns / 2, rows / 2), 270, 0.5)
    frame2_rot = cv2.warpAffine(frame_resized, R, (columns, rows))
    # img_resize = cv2.resize(img, (width, height))
    # together = cv2.addWeighted(img_resize, 0.25, frame, 1, 0, frame)
    # cv2.imshow('test', frame)
    cv2.imshow("bvid", frame2_rot)
    if (cv2.waitKey(1) & 0xFF == ord('q')):
        break
    # out.write(frame)
    out.write(frame2_rot)

cap.release()
cv2.destroyAllWindows()
Example #37
0
        # Hack um es zu zerstoeren
        #img_org = cv2.imread('/home/dueo/data_kaggel_bowl/train_augmented/trichodesmium_puff/44350_0.jpg')
        if show:
            cv2.imshow('ORI', img_org)
        mani_num = 0;
        writeImg(outPath, img_org, kind, file, mani_num) #Original
        rots = np.random.uniform(0,360,10).astype(int) #10 random rotations
        for i, rot in enumerate(rots):
            im_size = img_org.shape[0]
            if (np.random.rand() > 0.5):
                if (np.random.rand() > 0.5):
                    img_org = cv2.flip(img_org,0)
                else:
                    img_org = cv2.flip(img_org,1)
            scale = np.random.uniform(0.7,1.3)
            mat = cv2.getRotationMatrix2D((im_size / 2, im_size / 2), rot, scale=scale)
            img_rotated = cv2.warpAffine(img_org, mat, (im_size, im_size), flags=cv2.INTER_LINEAR, borderValue=(255,255,255))

            img_out = np.zeros((img_rotated.shape[0], img_rotated.shape[1], 3), dtype=np.uint8)
            img_orig = img_rotated[:,:,0]
            img_btop = 255-black_tophat(img_orig, selem)
            img_wtop = 255-white_tophat(img_orig, selem)
            img_out[:, :, 1] = img_btop
            img_out[:, :, 2] = img_wtop

            img_rotated = img_out

            if show:
              cv2.imshow('Rot_' + str(i), img_rotated)
            writeImg(outPath, img_rotated, kind, file, i+1) #Original
        if show:
Example #38
0
# 3)旋转:调用getRotationMatrix2D()获取仿射矩阵

# center是旋转的中心点;
# angle是旋转角度(正数表示逆时针),如angle = 15
# 表示逆时针旋转15度;
# scale标量是缩放因子,0.5
# 表示缩小,2
# 表示放大一倍,-2
# 表示放大一倍后再做(上下 + 左右)
# 翻转。
# M30 = numpy.float32([[1, 0, 0], [0, 1, 0]])#
M30 = numpy.float32([[numpy.sqrt(3) / 2, 0.5, 0], [-0.5,
                                                   numpy.sqrt(3) / 2, 0]])  #
# M3 = cv2.getRotationMatrix2D(center=(0,0), angle=0, scale=0.5)
M3 = cv2.getRotationMatrix2D(
    (cols // 2, rows // 2), 45,
    scale=0.8)  # angle=45表示逆时针旋转45度。scale=0.5表示缩小到原来的一半。
# 4)倾斜
M4 = numpy.float32([[1, 0.5, 0], [0, 1, 0]])  # 沿x轴倾斜0.5倍
M5 = numpy.float32([[1, 0, 0], [0.5, 1, 0]])  # 沿y轴倾斜0.5倍
# 5)翻转/镜像
# M6 = numpy.float32([[-1, 0, cols], [0, 1, 0]])  # 绕y转翻转,沿x轴平移cols个像素单位
M6 = numpy.float32([[1, 0, 0], [0, -1, rows]])  # 绕x转翻转,沿y轴平移rows个像素单位
M7 = numpy.float32([[-1, 0, cols],
                    [0, -1,
                     rows]])  # 绕y转翻转、绕x转翻转,最后沿x轴平移cols个像素单位、沿y轴平移rows个像素单位

# 2.进行仿射变换
# M: 仿射变换矩阵
# dsize: 指定输出图片的大小
dst0 = cv2.warpAffine(img, M0, dsize=(cols, rows))  # 平移
Example #39
0
def random_affine(img,
                  targets=(),
                  degrees=10,
                  translate=.1,
                  scale=.1,
                  shear=10,
                  border=0):
    # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
    # https://medium.com/uruvideo/dataset-augmentation-with-random-homographies-a8f4b44830d4
    # targets = [cls, xyxy]

    height = img.shape[0] + border * 2
    width = img.shape[1] + border * 2

    # Rotation and Scale
    R = np.eye(3)
    a = random.uniform(-degrees, degrees)
    # a += random.choice([-180, -90, 0, 90])  # add 90deg rotations to small rotations
    s = random.uniform(1 - scale, 1 + scale)
    # s = 2 ** random.uniform(-scale, scale)
    R[:2] = cv2.getRotationMatrix2D(angle=a,
                                    center=(img.shape[1] / 2,
                                            img.shape[0] / 2),
                                    scale=s)

    # Translation
    T = np.eye(3)
    T[0,
      2] = random.uniform(-translate, translate
                          ) * img.shape[0] + border  # x translation (pixels)
    T[1,
      2] = random.uniform(-translate, translate
                          ) * img.shape[1] + border  # y translation (pixels)

    # Shear
    S = np.eye(3)
    S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi /
                       180)  # x shear (deg)
    S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi /
                       180)  # y shear (deg)

    # Combined rotation matrix
    M = S @ T @ R  # ORDER IS IMPORTANT HERE!!
    if (border != 0) or (M != np.eye(3)).any():  # image changed
        img = cv2.warpAffine(img,
                             M[:2],
                             dsize=(width, height),
                             flags=cv2.INTER_LINEAR,
                             borderValue=(114, 114, 114))

    # Transform label coordinates
    n = len(targets)
    if n:
        # warp points
        xy = np.ones((n * 4, 3))
        xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(
            n * 4, 2)  # x1y1, x2y2, x1y2, x2y1
        xy = (xy @ M.T)[:, :2].reshape(n, 8)

        # create new boxes
        x = xy[:, [0, 2, 4, 6]]
        y = xy[:, [1, 3, 5, 7]]
        xy = np.concatenate(
            (x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T

        # # apply angle-based reduction of bounding boxes
        # radians = a * math.pi / 180
        # reduction = max(abs(math.sin(radians)), abs(math.cos(radians))) ** 0.5
        # x = (xy[:, 2] + xy[:, 0]) / 2
        # y = (xy[:, 3] + xy[:, 1]) / 2
        # w = (xy[:, 2] - xy[:, 0]) * reduction
        # h = (xy[:, 3] - xy[:, 1]) * reduction
        # xy = np.concatenate((x - w / 2, y - h / 2, x + w / 2, y + h / 2)).reshape(4, n).T

        # reject warped points outside of image
        xy[:, [0, 2]] = xy[:, [0, 2]].clip(0, width)
        xy[:, [1, 3]] = xy[:, [1, 3]].clip(0, height)
        w = xy[:, 2] - xy[:, 0]
        h = xy[:, 3] - xy[:, 1]
        area = w * h
        area0 = (targets[:, 3] - targets[:, 1]) * (targets[:, 4] -
                                                   targets[:, 2])
        ar = np.maximum(w / (h + 1e-16), h / (w + 1e-16))  # aspect ratio
        i = (w > 4) & (h > 4) & (area / (area0 * s + 1e-16) > 0.2) & (ar < 10)

        targets = targets[i]
        targets[:, 1:5] = xy[i]

    return img, targets
# crop first half of image and prep for rotation
crop = one[initRow:endRow, initCol:halfCol]
cropTwo = two[initRow:endRow, initCol:halfCol]

# getSize img
(cropH, cropW) = crop.shape[:2]

# getCenter img
cropCenter = (cropW / 2, cropH / 2)
initCropRow, initCropCol, halfCropRow = int(0), int(0), int(cropH * .5)
endCropRow, endCropCol, halfCropCol = int(cropH), int(cropW), int(cropW * .5)


# get Matrix of imgs and rotate images
MOne = cv2.getRotationMatrix2D(cropCenter, 180, 1.0)
MTwo = cv2.getRotationMatrix2D(cropCenter, 180, 1.0)
rotatedOne = cv2.warpAffine(crop, MOne, (cropW, cropH))
rotatedTwo = cv2.warpAffine(cropTwo, MTwo, (cropW, cropH))

# crop/cut image one and two halves
cropOneAlt = one[initRow: endRow, halfCol: endCol]
cropTwoAlt = two[initRow: endRow, halfCol: endCol]


# Cut the Left side on img one and two, which got rotated
rotatedOneLeft = rotatedOne[initCropRow:endCropRow, initCropCol:halfCropCol]
rotatedTwoRight = rotatedTwo[initCropRow:endCropRow, halfCropCol:endCropCol]

# Cut the Right side on imgs one and two in two Halves (Left, Right)
cropTwiceOneRight = cropOneAlt[initCropRow:endCropRow, halfCropCol:endCropCol]
Example #41
0
def detect(img):

    global CARD_NUM

    CARD_NUM = ''

    notFound = True

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

    # 2. 遍历二值化阈值算法
    algos = bz.myThreshold().getAlgos()

    for i in algos:

        #形态学变换的预处理,得到可以查找矩形的图片
        dilation = preprocess(gray, algos[i])

        # 3. 查找和筛选文字区域
        region = findTextRegion(dilation)

        # 4. 用绿线画出这些找到的轮廓
        angle = 0
        for rect in region:

            angle = rect[2]

            #识别身份证号码
            a, b = rect[1]
            if a > b:
                width = a
                hight = b
                pts2 = np.float32([[0, hight], [0, 0], [width, 0],
                                   [width, hight]])
            else:
                width = b
                hight = a
                angle = 90 + angle
                pts2 = np.float32([[width, hight], [0, hight], [0, 0],
                                   [width, 0]])

            #透视变换
            box = cv2.cv.BoxPoints(rect)
            pts1 = np.float32(box)
            M = cv2.getPerspectiveTransform(pts1, pts2)
            cropImg = cv2.warpPerspective(img, M, (int(width), int(hight)))

            # 计算核大小
            kenalx = kenaly = int(math.ceil((hight / 100.0)))
            CARD_NUM = getCardNum(cropImg, (kenalx, kenaly))
            if CARD_NUM:
                notFound = False
                #找到身份证号码,然后根据号码区域的倾斜角度,对原图进行旋转变换

                if abs(angle) > 10:
                    sp = img.shape
                    H = sp[0]
                    W = sp[1]
                    M = cv2.getRotationMatrix2D((W / 2, H / 2), angle, 1)
                    cropImg = cv2.warpAffine(img, M, (W, H))
                    # cv2.namedWindow("倾斜矫正", cv2.WINDOW_NORMAL)
                    # cv2.imshow("倾斜矫正", cropImg)
                    # cv2.waitKey(0)
                    #矫正图片地址
                    global curpath
                    path = 'tilt_correction.jpg'
                    newFile = os.path.join(curpath, path)
                    cv2.imwrite(newFile, cropImg)

                    return True, '', newFile

                # 画图
                if DEBUG:
                    cv2.drawContours(img, [np.int0(box)], 0, (0, 255, 0), 3)

                # 寻找汉字区域
                # 裁剪后的图片
                box = cv2.cv.BoxPoints(rect)
                box = np.int0(box)
                cropImg, point, width, hight = func.cropImgByBox(img, box)
                box = findChineseCharArea(point, width, hight)
                #cv2.drawContours(img, [box], 0, (0, 255, 0), 3)

                chiCharArea, point, width, hight = func.cropImgByBox(img, box)
                getChineseChar(chiCharArea, (kenalx, kenaly))

                # winname = "身份证号码: %s" % (CARD_NUM)
                # cv2.namedWindow(winname, cv2.WINDOW_NORMAL)
                # cv2.imshow(winname, cropImg)
                # cv2.waitKey(0)

                break

        if notFound:
            continue
        else:
            break

    if notFound:
        #win32api.MessageBox(0, "无法识别,请换一个分辨率高点的照片~", "错误提示")
        return False, '无法识别,请换一个分辨率高点的照片~", "错误提示', ''

    # 带轮廓的图片
    if DEBUG:
        cv2.namedWindow("img", cv2.WINDOW_NORMAL)
        cv2.imshow("img", img)
        key = cv2.waitKey(0)

        cv2.destroyAllWindows()
        if key != 32:
            sys.exit()

    if CARD_NUM != '':
        # 为了获取更加精准的值,通过身份证号码规则直接取得出生年月
        CARD_YEAR, CARD_MON, CARD_DAY = func.getBirthByCardNum(CARD_NUM)

        info = """
            姓名:%s
            性别:%s     民族:%s
            出生:%s 年 %s  月 %s 日
            住址:%s
            公民身份号码:%s
        """ % (CARD_NAME, CARD_SEX, CARD_ETHNIC, CARD_YEAR, CARD_MON, CARD_DAY,
               CARD_ADDR, CARD_NUM)

        ret = [
            CARD_NAME, CARD_SEX, CARD_ETHNIC, CARD_YEAR, CARD_MON, CARD_DAY,
            CARD_ADDR, CARD_NUM
        ]

        if DEBUG:
            print info

        return True, ret, ''
    else:
        return True, '', ''
def fixBorder(frame):
    s = frame.shape
    # Scale the image 4% without moving the center
    T = cv2.getRotationMatrix2D((s[1] / 2, s[0] / 2), 0, 1.04)
    frame = cv2.warpAffine(frame, T, (s[1], s[0]))
    return frame
Example #43
0
def detect_faces(img, lines):
    results = []
    for line in lines:
        e = line.split(' ')
        size = max(float(e[0]), float(e[1])) * 1.1
        # skip if face is too small
        if size < 60.0:
            break
        # crop to detect frontalface
        center = (int(float(e[3]) + .5), int(float(e[4]) + .5))
        angle = float(e[2]) / math.pi * 180.0
        if angle < 0:
            angle += 180.0
        M = cv2.getRotationMatrix2D(center, angle - 90.0, 1)
        M[0, 2] -= float(e[3]) - size
        M[1, 2] -= float(e[4]) - size
        target = cv2.warpAffine(img, M,
                                (int(size * 2 + .5), int(size * 2 + .5)))

        # detect face and eyes
        faces = FACE_CASCADE.detectMultiScale(target)
        if len(faces) != 1:
            print('{} faces found...'.format(len(faces)))
            break
        face = faces[0]
        face_img = target[face[1]:face[1] + face[3], face[0]:face[0] + face[2]]
        eyes = []
        for eye in EYES_CASCADE.detectMultiScale(face_img):
            # reject false detection
            if eye[1] > face_img.shape[0] / 2:
                break
            eyes.append(eye)
        if len(eyes) != 2:
            print('{} eyes found...'.format(len(eyes)))
            break
        # reject invalid scale eyes
        if not (2. / 3. < eyes[0][2] / eyes[1][2] < 3. / 2.
                and 2. / 3. < eyes[0][3] / eyes[1][3] < 3. / 2.):
            break

        # calculate coordinates of the original image
        center_points = [[
            face[0] + face[2] / 2.0,
            face[1] + face[3] / 2.0,
        ],
                         [
                             face[0] + eyes[0][0] + eyes[0][2] / 2.0,
                             face[1] + eyes[0][1] + eyes[0][3] / 2.0,
                         ],
                         [
                             face[0] + eyes[1][0] + eyes[1][2] / 2.0,
                             face[1] + eyes[1][1] + eyes[1][3] / 2.0,
                         ]]
        p = np.hstack([np.array(center_points), np.ones((3, 1))])
        p = cv2.invertAffineTransform(M).dot(p.T).T
        results.append([{
            'class': 'face',
            'xmin': p[0][0] - face[2] / 2.0,
            'xmax': p[0][0] + face[2] / 2.0,
            'ymin': p[0][1] - face[3] / 2.0,
            'ymax': p[0][1] + face[3] / 2.0,
        }, {
            'class': 'eye',
            'xmin': p[1][0] - eyes[0][2] / 2.0,
            'xmax': p[1][0] + eyes[0][2] / 2.0,
            'ymin': p[1][1] - eyes[0][3] / 2.0,
            'ymax': p[1][1] + eyes[0][3] / 2.0,
        }, {
            'class': 'eye',
            'xmin': p[2][0] - eyes[1][2] / 2.0,
            'xmax': p[2][0] + eyes[1][2] / 2.0,
            'ymin': p[2][1] - eyes[1][3] / 2.0,
            'ymax': p[2][1] + eyes[1][3] / 2.0,
        }])
    return results
Example #44
0
    with open("steer.npy", "wb") as npy:
        for chunk in r.iter_content(chunk_size=1024):
            # writing one chunk at a time to pdf file
            if chunk:
                npy.write(chunk)

    try:
        degrees = np.load('./steer.npy', allow_pickle=True)
        print(degrees)
    except:
        #degrees = np.load('./steer.npy', allow_pickle=True)
        print(degrees)

    print("Delay: {}".format((time.time() - last_time)))

    # make smooth angle transitions by turning the steering wheel based on the difference of the current angle
    # and the predicted angle
    smoothed_angle += 0.2 * pow(abs((degrees - smoothed_angle)), 2.0 / 3.0) * (
        degrees - smoothed_angle) / abs(degrees - smoothed_angle)
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -smoothed_angle, 1)
    dst = cv2.warpAffine(img, M, (cols, rows))
    cv2.imshow("Client", dst)

    i += 1

    # Press "q" to quit
    if cv2.waitKey(25) & 0xFF == ord("q"):
        cv2.destroyAllWindows()
        break
Example #45
0
def barriers_for_player(barriers, reference_tank):

    img = np.zeros((IMG_SZ, IMG_SZ, 1), np.uint8)

    # constants
    wall_value = 255
    border_allowance_global = -1.0

    unity32 = UNITY_SZ * 1.5
    unityhf = UNITY_SZ * 0.5

    #draw walls
    wleft = [[-unity32, -unity32],
             [-unityhf - border_allowance_global, -unity32],
             [-unityhf - border_allowance_global, unity32],
             [-unity32, unity32]]  #in world
    wleft_rel = np.asarray([
        points_relative_point_heading(wleft, reference_tank[0:2],
                                      reference_tank[2])
    ])
    wleft_rel = (wleft_rel / UNITY_SZ) * SCALE + float(IMG_SZ) * 0.5
    cv2.fillPoly(img, wleft_rel.astype(np.int32), wall_value)

    wright = [[unityhf + border_allowance_global, -unity32],
              [unity32, -unity32], [unity32, unity32],
              [unityhf + border_allowance_global, unity32]]  #in world
    wright_rel = np.asarray([
        points_relative_point_heading(wright, reference_tank[0:2],
                                      reference_tank[2])
    ])
    wright_rel = (wright_rel / UNITY_SZ) * SCALE + float(IMG_SZ) * 0.5
    cv2.fillPoly(img, wright_rel.astype(np.int32), wall_value)

    wtop = [[-unity32, -unity32], [unity32, -unity32],
            [unity32, -unityhf - border_allowance_global],
            [-unity32, -unityhf - border_allowance_global]]  #in world
    wtop_rel = np.asarray([
        points_relative_point_heading(wtop, reference_tank[0:2],
                                      reference_tank[2])
    ])
    wtop_rel = (wtop_rel / UNITY_SZ) * SCALE + float(IMG_SZ) * 0.5
    cv2.fillPoly(img, wtop_rel.astype(np.int32), wall_value)

    wbot = [[-unity32, unityhf + border_allowance_global],
            [unity32, unityhf + border_allowance_global], [unity32, unity32],
            [-unity32, unity32]]  #in world
    wbot_rel = np.asarray([
        points_relative_point_heading(wbot, reference_tank[0:2],
                                      reference_tank[2])
    ])
    wbot_rel = (wbot_rel / UNITY_SZ) * SCALE + float(IMG_SZ) * 0.5
    cv2.fillPoly(img, wbot_rel.astype(np.int32), wall_value)

    #draw internal barriers
    res = cv2.resize(np.squeeze(barriers[:, :, 0]),
                     dsize=(int(SCALE), int(SCALE)),
                     interpolation=cv2.INTER_CUBIC)
    threshold_indices = res > 0.05
    res[threshold_indices] = 1.0
    res *= 255.0

    #draw in larger image
    scl = int(SCALE)
    barrier_img = np.ones((scl * 2, scl * 2), np.uint8)
    barrier_img[(scl // 2):3 * scl // 2, (scl // 2):3 * scl // 2] = res

    #translate
    dx = (reference_tank[0] / UNITY_SZ) * SCALE
    dy = (reference_tank[1] / UNITY_SZ) * SCALE
    M = np.float32([[1, 0, -dx], [0, 1, -dy]])
    barrier_img = cv2.warpAffine(barrier_img, M, (scl * 2, scl * 2))

    #rotate about center
    ang = reference_tank[2] * (180.0 / 3.14)
    M = cv2.getRotationMatrix2D((float(scl * 2) * 0.5, float(scl * 2) * 0.5),
                                ang, 1)
    barrier_img = cv2.warpAffine(barrier_img, M, (scl * 2, scl * 2))

    #extract central area without padding
    padd = (IMG_SZ - int(SCALE)) // 2
    barrier_img = barrier_img[(scl // 2) - padd:(scl // 2) + IMG_SZ - padd,
                              (scl // 2) - padd:(scl // 2) + IMG_SZ - padd]

    #add channel
    barrier_img = np.expand_dims(barrier_img, axis=2)

    #concat the walls and the barriers
    ch = np.maximum(img, barrier_img)

    return ch
    def get_random_data(self,
                        image,
                        input_shape,
                        jitter=.3,
                        hue=.1,
                        sat=1.5,
                        val=1.5,
                        flip_signal=False):
        image = image.convert("RGB")

        h, w = input_shape
        # resize image
        rand_jit1 = rand(1 - jitter, 1 + jitter)
        rand_jit2 = rand(1 - jitter, 1 + jitter)
        new_ar = w / h * rand_jit1 / rand_jit2

        scale = rand(0.75, 1.25)
        if new_ar < 1:
            nh = int(scale * h)
            nw = int(nh * new_ar)
        else:
            nw = int(scale * w)
            nh = int(nw / new_ar)
        image = image.resize((nw, nh), Image.BICUBIC)

        # flip image or not
        flip = rand() < .5
        if flip and flip_signal:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)

        # place image
        dx = int(rand(0, w - nw))
        dy = int(rand(0, h - nh))
        new_image = Image.new('RGB', (w, h), (255, 255, 255))

        new_image.paste(image, (dx, dy))
        image = new_image

        rotate = rand() < .5
        if rotate:
            angle = np.random.randint(-5, 5)
            a, b = w / 2, h / 2
            M = cv2.getRotationMatrix2D((a, b), angle, 1)
            image = cv2.warpAffine(np.array(image),
                                   M, (w, h),
                                   borderValue=[255, 255, 255])

        # distort image
        hue = rand(-hue, hue)
        sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
        val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
        x = cv2.cvtColor(np.array(image, np.float32) / 255, cv2.COLOR_RGB2HSV)
        x[..., 0] += hue * 360
        x[..., 0][x[..., 0] > 1] -= 1
        x[..., 0][x[..., 0] < 0] += 1
        x[..., 1] *= sat
        x[..., 2] *= val
        x[x[:, :, 0] > 360, 0] = 360
        x[:, :, 1:][x[:, :, 1:] > 1] = 1
        x[x < 0] = 0
        image_data = cv2.cvtColor(x, cv2.COLOR_HSV2RGB) * 255
        if self.channel == 1:
            image_data = Image.fromarray(np.uint8(image_data)).convert("L")
        # cv2.imshow("123",np.uint8(image_data))
        # cv2.waitKey(0)
        return image_data
Example #47
0
    def predict(self, im, boxes, landmarks):

        img_rotated, (landmark_pred_rotated, ), (angle, center) = ratate(
            boxes, landmarks, points=[landmarks], img=im)
        h, w, c = img_rotated.shape

        landmark_left_eye = landmark_pred_rotated[0]
        landmark_right_eye = landmark_pred_rotated[1]
        landmark_left_mouse = landmark_pred_rotated[3]
        W = landmark_right_eye[0] - landmark_left_eye[0] + 1
        H = landmark_left_mouse[1] - landmark_left_eye[1] + 1

        dx1, dy1, dx2, dy2 = W * 0.8, H * 0.7, W * 0.8, H * 0.4
        lx, ly = landmark_left_eye
        lx1 = max(0, int(lx - dx1 + 0.5))
        ly1 = max(0, int(ly - dy1 + 0.5))
        lx2 = min(w - 1, int(lx + dx2 + 0.5))
        ly2 = min(h - 1, int(ly + dy2 + 0.5))
        Llx = lx2 - lx1 + 1
        Lly = ly2 - ly1 + 1

        rx, ry = landmark_right_eye
        rx1 = max(0, int(rx - dx2 + 0.5))
        ry1 = max(0, int(ry - dy1 + 0.5))
        rx2 = min(w - 1, int(rx + dx1 + 0.5))
        ry2 = min(h - 1, int(ry + dy2 + 0.5))
        Lrx = rx2 - rx1 + 1
        Lry = ry2 - ry1 + 1

        left_im = img_rotated[ly1:ly2, lx1:lx2, :]
        right_im = img_rotated[ry1:ry2, rx1:rx2, :]
        right_im = cv2.flip(right_im, 1)
        # cv2.imwrite('left.jpg', left_im)
        # cv2.imwrite('right.jpg', right_im)

        cropped_im1 = np.zeros((1, self.net_size, self.net_size, 3),
                               dtype=np.float32)
        cropped_im1[0, :, :, :] = (cv2.resize(left_im,
                                              (self.net_size, self.net_size)) -
                                   127.5) / 128

        cropped_im2 = np.zeros((1, self.net_size, self.net_size, 3),
                               dtype=np.float32)
        cropped_im2[0, :, :, :] = (cv2.resize(right_im,
                                              (self.net_size, self.net_size)) -
                                   127.5) / 128

        landmark1 = self.net.predict(cropped_im1)
        landmark2 = self.net.predict(cropped_im2)

        landmark1 = landmark1.reshape(-1, 2)
        landmark2 = landmark2.reshape(-1, 2)

        # print(landmark2.shape)
        idx = [4, 3, 2, 1, 0, 8, 7, 6, 5, 10, 9, 11]
        landmark2 = landmark2[idx, :]
        landmark2[:, 0] = 1 - landmark2[:, 0]

        lwh = np.asarray([[Llx, Lly]], dtype=np.float32)
        lxy = np.asarray([[lx1, ly1]], dtype=np.float32)
        rwh = np.asarray([[Lrx, Lry]], dtype=np.float32)
        rxy = np.asarray([[rx1, ry1]], dtype=np.float32)
        landmark1 = landmark1 * lwh + lxy
        landmark2 = landmark2 * rwh + rxy

        rot_mat = cv2.getRotationMatrix2D(center, -angle, 1)
        landmark1_ = np.asarray([
            (rot_mat[0][0] * x + rot_mat[0][1] * y + rot_mat[0][2],
             rot_mat[1][0] * x + rot_mat[1][1] * y + rot_mat[1][2])
            for (x, y) in landmark1
        ])
        landmark2_ = np.asarray([
            (rot_mat[0][0] * x + rot_mat[0][1] * y + rot_mat[0][2],
             rot_mat[1][0] * x + rot_mat[1][1] * y + rot_mat[1][2])
            for (x, y) in landmark2
        ])

        return landmark1_, landmark2_
Example #48
0
# print dimensions of the image
print("Original size: " + str(image.shape))

# Output: (388, 647, 3)
# 388 rows, 647 columns and 3 channels RGB
# => 647 pixel wide and 388 pixel tall

# resizing an image
# ratio
r = 100.0 / image.shape[1]
dim = (100, int(image.shape[0] * r))

resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
cv2.imshow("resized", resized)
print("Resized: " + str(resized.shape))

# Rotating an image
# grab the dimensions of the image and calculate center
(h, w) = image.shape[:2]
center = (w / 2, h / 2)

# rotate the image 180 degrees
M = cv2.getRotationMatrix2D(center, 180, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("rotated", rotated)

# crop image
cropped = image[70:170, 440:540]
cv2.imshow("Cropped", cropped)

cv2.waitKey(0)
Example #49
0
cv2.drawContours(dst, [cnt], 0, (255, 0, 0), 3)

#2
M = cv2.moments(cnt)
hu = cv2.HuMoments(M)
print('hu.shape=', hu.shape)
print('hu=', hu)

#3
angle = 45.0
scale = 0.2
cx = M['m10'] / M['m00']
cy = M['m01'] / M['m00']
center = (cx, cy)
t = (20, 30)
A = cv2.getRotationMatrix2D(center, angle, scale)
A[:, 2] += t  # translation
print('A=', A)  # Affine 변환
cnt2 = cv2.transform(cnt, A)
cv2.drawContours(dst, [cnt2], 0, (0, 255, 0), 3)
cv2.imshow('dst', dst)

#4
M2 = cv2.moments(cnt2)
hu2 = cv2.HuMoments(M2)
print('hu2.shape=', hu2.shape)
print('hu2=', hu)

#5
##diffSum = sum(abs(hu - hu2))
diffSum = np.sum(cv2.absdiff(hu, hu2))
    def align_head(self, head):
        """Aligns a head region using affine transformations

            This method preprocesses an extracted head region by rotating
            and scaling it so that the face appears centered and up-right.

            The method returns True on success (else False) and the aligned
            head region (head). Possible reasons for failure are that one or
            both eye detectors fail, maybe due to poor lighting conditions.

            :param head: extracted head region
            :returns: success, head
        """
        height, width = head.shape[:2]

        # detect left eye
        left_eye_region = head[int(0.2 * height): int(0.5 * height),
                               int(0.1 * width): int(0.5 * width)]
        left_eye = self.left_eye_casc.detectMultiScale(
            left_eye_region,
            scaleFactor=1.1,
            minNeighbors=3,
            flags=cv2.cv.CV_HAAR_FIND_BIGGEST_OBJECT)
        left_eye_center = None
        for (xl, yl, wl, hl) in left_eye:
            # find the center of the detected eye region
            left_eye_center = np.array([0.1 * width + xl + wl / 2,
                                        0.2 * height + yl + hl / 2])
            break  # need only look at first, largest eye

        # detect right eye
        right_eye_region = head[int(0.2 * height): int(0.5 * height),
                                int(0.5 * width): int(0.9 * width)]
        right_eye = self.right_eye_casc.detectMultiScale(
            right_eye_region,
            scaleFactor=1.1,
            minNeighbors=3,
            flags=cv2.cv.CV_HAAR_FIND_BIGGEST_OBJECT)
        right_eye_center = None
        for (xr, yr, wr, hr) in right_eye:
            # find the center of the detected eye region
            right_eye_center = np.array([0.5 * width + xr + wr / 2,
                                         0.2 * height + yr + hr / 2])
            break  # need only look at first, largest eye

        # need both eyes in order to align face
        # else break here and report failure (False)
        if left_eye_center is None or right_eye_center is None:
            return False, head

        # we want the eye to be at 25% of the width, and 20% of the height
        # resulting image should be square (desired_img_width,
        # desired_img_height)
        desired_eye_x = 0.25
        desired_eye_y = 0.2
        desired_img_width = 200
        desired_img_height = desired_img_width

        # get center point between the two eyes and calculate angle
        eye_center = (left_eye_center + right_eye_center) / 2
        eye_angle_deg = np.arctan2(right_eye_center[1] - left_eye_center[1],
                                   right_eye_center[0] - left_eye_center[0]) \
            * 180.0 / cv2.cv.CV_PI

        # scale distance between eyes to desired length
        eyeSizeScale = (1.0 - desired_eye_x * 2) * desired_img_width / \
            np.linalg.norm(right_eye_center - left_eye_center)

        # get rotation matrix
        rot_mat = cv2.getRotationMatrix2D(tuple(eye_center), eye_angle_deg,
                                          eyeSizeScale)

        # shift center of the eyes to be centered in the image
        rot_mat[0, 2] += desired_img_width * 0.5 - eye_center[0]
        rot_mat[1, 2] += desired_eye_y * desired_img_height - eye_center[1]

        # warp perspective to make eyes aligned on horizontal line and scaled
        # to right size
        res = cv2.warpAffine(head, rot_mat, (desired_img_width,
                                             desired_img_width))

        # return success
        return True, res
Example #51
0
import cv2
import numpy

image = cv2.imread('images.png')
#method 1
#rotation=cv2.transpose(image)
#cv2.imshow('rotation_image',rotation)

#method 2
height, width = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((height / 2, width / 2), 90, 1)
rotation_image = cv2.warpAffine(image, rotation_matrix, (height, width))
cv2.imshow('image rotation', rotation_image)
cv2.waitKey()
cv2.destroyAllWindows()
Example #52
0
#Scaling
import cv2
import numpy as np

img = cv2.imread('messi5.jpg')
res = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
#OR
height, width = img.shape[:2]
res = cv2.resize(img, (2 * width, 2 * height), interpolation=cv2.INTER_CUBIC)

#Translation
import cv2
import numpy as np

img = cv2.imread('messi5.jpg', 0)
rows, cols = img.shape

M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv2.warpAffine(img, M, (cols, rows))

cv2.imshow('img', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

#Rotation
img = cv2.imread('messi5.jpg', 0)
rows, cols = img.shape

M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 90, 1)
dst = cv2.warpAffine(img, M, (cols, rows))
Example #53
0
def random_rotation(image):
    (r, c) = image.shape[:2]
    M = cv2.getRotationMatrix2D((c / 2, r / 2), 180, 1.0)
    dst1 = cv2.warpAffine(image, M, (c, r))
    return dst1
Example #54
0
def rotate_buffer(image, angle):
    """
    Rotates an OpenCV 2 / NumPy image about its center by the given angle
    The returned image will be large enough to hold the entire
    new image, with a black background
    modified from http://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders
    """

    # Get the image size
    image_size = (image.shape[1], image.shape[0])
    image_center = tuple(np.array(image_size) / 2)

    # Convert the OpenCV 3x2 rotation matrix to 3x3
    rot_mat = np.vstack(
        [cv2.getRotationMatrix2D(image_center, angle, 1.0), [0, 0, 1]]
    )

    rot_mat_notranslate = np.matrix(rot_mat[0:2, 0:2])

    # Shorthand for below calcs
    image_w2 = image_size[0] * 0.5
    image_h2 = image_size[1] * 0.5

    # Obtain the coordinates of the corners of the rotated image
    rotated_coords = [
        (np.array([-image_w2,  image_h2]) * rot_mat_notranslate).A[0],
        (np.array([ image_w2,  image_h2]) * rot_mat_notranslate).A[0],
        (np.array([-image_w2, -image_h2]) * rot_mat_notranslate).A[0],
        (np.array([ image_w2, -image_h2]) * rot_mat_notranslate).A[0]
    ]

    # Find the size of the new image
    x_coords = [pt[0] for pt in rotated_coords]
    x_pos = [x for x in x_coords if x > 0]
    x_neg = [x for x in x_coords if x < 0]

    y_coords = [pt[1] for pt in rotated_coords]
    y_pos = [y for y in y_coords if y > 0]
    y_neg = [y for y in y_coords if y < 0]

    right_bound = max(x_pos)
    left_bound = min(x_neg)
    top_bound = max(y_pos)
    bot_bound = min(y_neg)

    new_w = int(abs(right_bound - left_bound))
    new_h = int(abs(top_bound - bot_bound))

    # We require a translation matrix to keep the image centered
    trans_mat = np.matrix([
        [1, 0, int(new_w * 0.5 - image_w2)],
        [0, 1, int(new_h * 0.5 - image_h2)],
        [0, 0, 1]
    ])

    # Compute the tranform for the combined rotation and translation
    affine_mat = (np.matrix(trans_mat) * np.matrix(rot_mat))[0:2, :]

    # Apply the transform
    result = cv2.warpAffine(
        image,
        affine_mat,
        (new_w, new_h),
        flags=cv2.INTER_LINEAR
    )

    return result
Example #55
0
    def get_data(self):
        if self.shuffle:
            self.rng.shuffle(self.imglist)

        for img_path in self.imglist:

            img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
            #             label = np.copy(img)
            #             if self.line_noise:
            #                 xmin = np.random.randint(0, img.shape[1] - 110)
            #                 ymin = np.random.randint(0, img.shape[0] - 110)
            #                 xlength = np.random.randint(90, 110)
            #                 ylength = np.random.randint(0, xlength)
            #                 cv2.line(img,(xmin,ymin),(xmin+xlength,ymin+ylength), 100, 3)

            # wld
            if cfg.wld == True:
                img = wld(img)

            # random scale
            line_noise_length = np.random.randint(90, 110)
            scale = random.choice(cfg.scale_list)
            if scale >= 2:
                img = cv2.pyrDown(img)
                line_noise_length = line_noise_length // 2
            if scale >= 4:
                img = cv2.pyrDown(img)
                line_noise_length = line_noise_length // 2

            # random rotate
            angle = random.choice([0, 90, 180, 270])
            if self.rotate and angle != 0:
                rows, cols = img.shape
                M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
                img = cv2.warpAffine(img, M, (cols, rows))

            # random flip
            if self.flip_ver == True and np.random.rand() > 0.5:
                img = cv2.flip(img, 1)

            if self.flip_horiz == True and np.random.rand() > 0.5:
                img = cv2.flip(img, 0)

            img = np.expand_dims(img, axis=-1)
            #             label = np.expand_dims(label, axis=-1)

            # random crop
            h, w, _ = img.shape
            assert h >= cfg.img_size and w >= cfg.img_size, (h, w)
            diffh = h - cfg.img_size
            h0 = 0 if diffh == 0 else np.random.randint(diffh)
            diffw = w - cfg.img_size
            w0 = 0 if diffw == 0 else np.random.randint(diffw)

            img = img[h0:h0 + cfg.img_size, w0:w0 + cfg.img_size]

            label = np.copy(img)
            if self.line_noise:
                xmin = np.random.randint(0, img.shape[1])
                ymin = np.random.randint(0, img.shape[0])
                #                 import pdb
                #                 pdb.set_trace()
                ylength = np.random.randint(0, line_noise_length)
                cv2.line(img, (xmin, ymin),
                         (xmin + line_noise_length, ymin + ylength), 100, 4)

            yield [img, label]
    def match_template(self, frame):
        
        H,W = frame.shape[0], frame.shape[1]
        h,w = self.template.shape[0], self.template.shape[1]

        # Make sure that the template image is smaller than the source
        if W < w or H < h:
            rospy.loginfo( "Template image must be smaller than video frame." )
            return None
        
        if frame.dtype != self.template.dtype: 
            rospy.loginfo("Template and video frame must have same depth and number of channels.")
            return None
        
        # Create a copy of the frame to modify
        frame_copy = frame.copy()
        
        for i in range(self.n_pyr):
            frame_copy = cv2.pyrDown(frame_copy)
            
        template_height, template_width  = self.template.shape[:2]
        
        # Cycle through all scales starting with the last successful scale

        scales = self.scales[self.last_scale:] + self.scales[:self.last_scale - 1]

        # Track which scale and rotation gives the best match
        maxScore = -1
        best_s = 1
        best_r = 0
        best_x = 0
        best_y = 0
        
        for s in self.scales:
            for r in self.rotations:
                # Scale the template by s
                template_copy = cv2.resize(self.template, (int(template_width * s), int(template_height * s)))

                # Rotate the template through r degrees
                rotation_matrix = cv2.getRotationMatrix2D((template_copy.shape[1]/2, template_copy.shape[0]/2), r, 1.0)
                template_copy = cv2.warpAffine(template_copy, rotation_matrix, (template_copy.shape[1], template_copy.shape[0]), borderMode=cv2.BORDER_REPLICATE)
    
                # Use pyrDown() n_pyr times on the scaled and rotated template
                for i in range(self.n_pyr):
                    template_copy = cv2.pyrDown(template_copy)
                
                # Create the results array to be used with matchTempate()
                h,w = template_copy.shape[:2]
                H,W = frame_copy.shape[:2]
                
                result_width = W - w + 1
                result_height = H - h + 1
                
                try:
                    result_mat = cv.CreateMat(result_height, result_width, cv.CV_32FC1)
                    result = np.array(result_mat, dtype = np.float32)
                except:
                    continue
                
                # Run matchTemplate() on the reduced images
                cv2.matchTemplate(frame_copy, template_copy, cv.CV_TM_CCOEFF_NORMED, result)
                
                # Find the maximum value on the result map
                (minValue, maxValue, minLoc, maxLoc) = cv2.minMaxLoc(result)
                
                if maxValue > maxScore:
                    maxScore = maxValue
                    best_x, best_y = maxLoc
                    best_s = s
                    best_r = r
                    best_template = template_copy.copy()
                    self.last_scale = self.scales.index(s)
                    best_result = result.copy()
                
        # Transform back to original image sizes
        best_x *= int(pow(2.0, self.n_pyr))
        best_y *= int(pow(2.0, self.n_pyr))
        h,w = self.template.shape[:2]
        h = int(h * best_s)
        w = int(w * best_s)
        best_result = cv2.resize(best_result, (int(pow(2.0, self.n_pyr)) * best_result.shape[1], int(pow(2.0, self.n_pyr)) * best_result.shape[0]))
        display_result = np.abs(best_result)**3

        cv2.imshow("Result", display_result)
        best_template = cv2.resize(best_template, (int(pow(2.0, self.n_pyr)) * best_template.shape[1], int(pow(2.0, self.n_pyr)) * best_template.shape[0]))
        cv2.imshow("Best Template", best_template)
        
        #match_box = ((best_x + w/2, best_y + h/2), (w, h), -best_r)
        return (best_x, best_y, w, h)
Example #57
0
import numpy as np
import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original", image)

(h, w) = image.shape[:2]
center = (w / 2, h / 2)

M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated by 45 degree", rotated)

M = cv2.getRotationMatrix2D(center, -90, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated by -90 degree", rotated)

rotated = imutils.rotate(image, 180)
cv2.imshow("Rotated by 180 degree", rotated)
cv2.waitKey(0)
Example #58
0
def Equation_recognize(image_path, image_extract_path):
    if not os.path.exists(image_extract_path):
        os.makedirs(image_extract_path)
    # step1: gauss filter and two values
    img = cv2.imread(image_path, 0)
    img = cv2.GaussianBlur(img, (3, 3), 0, 0)
    weight, height = img.shape[:]
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                cv2.THRESH_BINARY, 75, 10)
    img = cv2.bitwise_not(img)
    points = np.column_stack(np.where(img > 0))
    # get the whole center and the image's width and it's rotation angel
    rec = cv2.minAreaRect(points)
    angle = rec[-1]
    # int and reverse
    box_center = tuple(np.int0(rec[0]))
    box_center = tuple(reversed(list(box_center)))
    box_size = tuple(np.int0(rec[1]))
    box_size = tuple(reversed(list(box_size)))
    if angle < -45:
        angle = -(90 + angle)
    else:
        angle = -angle
    (h, w) = img.shape[:]
    center = (w // 2, h // 2)
    # 首先反转图像,使得图像对齐
    transform_rotate = cv2.getRotationMatrix2D(center=center,
                                               angle=angle,
                                               scale=1.0)
    rotated = cv2.warpAffine(img,
                             transform_rotate, (w, h),
                             flags=cv2.INTER_CUBIC,
                             borderMode=cv2.BORDER_REPLICATE)
    # 剪切图像,这里要对box_size 和box_center进行反转
    croped = cv2.getRectSubPix(rotated, box_size, box_center)
    croped2 = croped.copy()
    croped2 = cv2.cvtColor(croped2, cv2.COLOR_GRAY2BGR)
    black = 0 * croped.copy()
    croped3 = croped.copy()
    croped2 = cv2.cvtColor(croped3, cv2.COLOR_GRAY2BGR)
    # 找到联通区域
    _, contors, _ = cv2.findContours(croped, cv2.RETR_EXTERNAL,
                                     cv2.CHAIN_APPROX_TC89_KCOS)
    contors_ply = [cv2.approxPolyDP(i, 3, True) for i in contors]
    # 对contors进行筛选
    valid_contors = []
    for i in range(len(contors_ply)):
        r = cv2.boundingRect(contors_ply[i])
        area = r[2] * r[3]
        if area < 100:
            continue
        inside = False
        for j in range(len(contors_ply)):
            if j == i:
                continue
            r2 = cv2.boundingRect(contors_ply[j])
            area2 = r2[2] * r2[3]
            if area2 < 100 or area2 < area:
                continue
            if r[0] > r2[0] and r[0] + r[2] < r2[0] + r2[2] and r[1] > r2[
                    1] and r[1] + r[3] < r2[1] + r2[3]:
                inside = True
        if inside:
            continue
        valid_contors.append(contors_ply[i])
    Bounding_box = [cv2.boundingRect(i) for i in valid_contors]
    # 对valid_contors 进行检验

    for i in range(len(valid_contors)):
        if Bounding_box[i][2] * Bounding_box[i][3] < 100:
            continue
        cv2.drawContours(black, valid_contors, i, (255, 255, 255), cv2.FILLED)
        cv2.rectangle(croped2, (Bounding_box[i][0], Bounding_box[i][1]),
                      (Bounding_box[i][0] + Bounding_box[i][2],
                       Bounding_box[i][1] + Bounding_box[i][3]), color, 2, 8,
                      0)
    cv2.imshow('boudiongbox', black)

    # 对排序之后的边框countour进行检验

    (sorted_controus, Bounding_box) = sort_contours_function(valid_contors)
    for (i, c) in enumerate(sorted_controus):
        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])

        # draw the countour number on the image
        cv2.putText(croped3, "#{}".format(i + 1), (cX - 20, cY),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
        cv2.drawContours(black, sorted_controus, i, color, 1, 8)
    cv2.imshow("Sorted", croped3)
    # cv2.waitKey(0)

    Extrac_contours(croped3, valid_contors, image_extract_path)
    cv2.waitKey(0)
Example #59
0
import cv2
import numpy

img=cv2.imread("lion.jpg")

m=numpy.float32([[1,0,100],[0,1,50]])
print (m)

h,w,c=img.shape

m=cv2.getRotationMatrix2D((h/2,w/2),90,1)
dst=cv2.warpAffine(img,m,(h,w))

cv2.imshow("image",img)
cv2.imshow("image1",dst)

cv2.waitKey(0)
cv2.destroyAllWindows()
Example #60
0
#     cv2.imshow('res',res)
#
#     k = cv2.waitKey(1) & 0xff
#     if k == 27 :
#         break
#
# cv2.destroyWindow()

##rotation

img = cv2.imread('1.jpg')

rows, cols, ch = img.shape

###给定旋转矩阵 2*3
M  = cv2.getRotationMatrix2D((rows/2,cols/2), 30, 0.8)

dst = cv2.warpAffine(img, M, (2*rows,2*cols))

cv2.imshow('img',img)
cv2.imshow('res',dst)
cv2.waitKey(0)