def warp(image, corners, warp_size): image_copy = image.copy() destination = np.array([[0, 0], [warp_size - 1, 0], [warp_size - 1, warp_size - 1], [0, warp_size - 1]], dtype="float32") transform_matrix = cv2.getPerspectiveTransform(corners, destination) transform_matrix_inv = cv2.getPerspectiveTransform(destination, corners) # inverse to do opposite transformation later warped = cv2.warpPerspective(image_copy, transform_matrix, (warp_size, warp_size)) return warped, transform_matrix_inv
def get_M_Minv(): """ Transfer thresholded img to view from above """ # src = np.float32([[(203, 720), (585, 460), (695, 460), (1127, 720)]]) # dst = np.float32([[(320, 720), (320, 0), (960, 0), (960, 720)]]) src = np.float32([[(20, 120), (70, 80), (90, 80), (140, 120)]]) dst = np.float32([[(20, 120), (40, 0), (100, 0), (140, 120)]]) M = cv2.getPerspectiveTransform(src, dst) Minv = cv2.getPerspectiveTransform(dst, src) return M, Minv
def four_point_transform(self, orig, contour): rect = self.order_points(contour) print(rect) tl, tr, br, bl = rect # create the new image width -- the max length of the top or bottom of the contour widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2)) widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2)) maxWidth = max(int(widthA), int(widthB)) # create the new image height -- the max of the left/right of the contour heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2)) heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2)) maxHeight = max(int(heightA), int(heightB)) # now that we have the dimensions of the new image, construct # the set of destination points to obtain a "birds eye view", # (i.e. top-down view) of the image, again specifying points # in the top-left, top-right, bottom-right, and bottom-left # order dst = np.array([ [0, 0], [maxWidth - 1, 0], [maxWidth - 1, maxHeight - 1], [0, maxHeight - 1]], dtype = "float32") # compute the perspective transform matrix and then apply it M = cv2.getPerspectiveTransform(rect, dst) warped = cv2.warpPerspective(orig, M, (maxWidth, maxHeight)) return warped
def fitToCode(image: np.ndarray, centerPoints: np.ndarray, dimension: int) -> np.ndarray: dx = 3 dy = 4 x0 = 3 y0 = 3 x1 = dimension - 4 y1 = dimension - 4 dest = np.array( [ [x0, y0], [x1, y0], [x0, y1], [x1, y1], ], np.float32, ) src = np.array(centerPoints, np.float32) matrix = cv2.getPerspectiveTransform(src, dest) warped = cv2.warpPerspective( image, matrix, (dimension, dimension), flags=cv2.INTER_NEAREST, ) return warped
def start(): frame = cv2.imread("building.jpg") height = frame.shape[0] width = frame.shape[1] aspect_ratio = width / height ph = 200 pw = int(ph * aspect_ratio) roi = [[62, 142], [358, 48], [49, 257], [396, 276]] project_pts = [[0, 0], [pw, 0], [0, ph], [pw, ph]] src_pts = np.float32(roi) dest_pts = np.float32(project_pts) transformation_matrix = cv2.getPerspectiveTransform(src_pts, dest_pts) output = cv2.warpPerspective(frame, transformation_matrix, (pw, ph)) cv2.imwrite("perspective_corrected_image.jpg", output) for pts in roi: cv2.circle(frame, tuple(pts), 5, (0, 0, 255), -1) cv2.imshow("Image", frame) cv2.imshow("Corrected", output) cv2.waitKey(0)
def circle_trans(Cicle_2D: '上下右左', points: '物体底部四点', lined_img=[]): ''' 若最后一个参数非空(调试模式),则显示图片 ''' affined_Circle_2D = np.float32([[300, 0], [300, 600], [600, 300], [0, 300]]) # 圆桌R = 300 M = cv2.getPerspectiveTransform(Cicle_2D, affined_Circle_2D) # 获取透视变换矩阵 transed_points = np.matmul(points, np.transpose(M)) for i in range(4): transed_points[i][0] = transed_points[i][0] / transed_points[i][2] transed_points[i][1] = transed_points[i][1] / transed_points[i][2] a = [0 for i in range(4)] for i in range(4): a[i] = (int(transed_points[i][0]), int(transed_points[i][1])) if len(lined_img): # Perspective_Transformation transed = cv2.warpPerspective(lined_img, M, (600, 600)) cv2.line(transed, a[0], a[1], (0, 255, 0), 1) cv2.line(transed, a[0], a[2], (0, 255, 0), 1) cv2.line(transed, a[1], a[3], (0, 255, 0), 1) cv2.line(transed, a[2], a[3], (0, 255, 0), 1) cv2.imshow('perspective', transed) return transed_points
def alineamiento(imagen,ancho,alto): imagen_alineada=None # le pasamos grises a la imagen grises=cv2.cvtColor(imagen, cv2.COLOR_BGR2GRAY) # devolvemos dos umbrales minimos y maximos tipoumbral,umbral=cv2.threshold(grises, 150,255, cv2.THRESH_BINARY) # mostramos el umbral cv2.imshow("Umbral", umbral) # contornos que devuelven dos valores, contorno y jerarquia contorno=cv2.findContours(umbral, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0] # ordenar los contornos son sorted # reverse ordena los puntos de menor a mayor, son para los eje x contorno=sorted(contorno,key=cv2.contourArea,reverse=True)[:1] # recorremos los cotornor for c in contorno: # la variable c esta recorriendo todos los contornos # epsilon ayuda a encontrar las areas # arcLenght sirve para sacar las areas epsilon=0.01*cv2.arcLength(c, True) # aca piden las curvas que va a analizar para que las curvas no tengan tando ruido approximacion=cv2.approxPolyDP(c, epsilon, True) # contamos objetos que tenemos en la lista # los 4 puntos forman un circulo if len(approximacion)==4: puntos=ordenarpuntos(approximacion) # convertimos los puntos en alto y ancho puntos1=np.float32(puntos) puntos2=np.float32([[0,0],[ancho,0],[0,alto],[ancho,alto]]) # metodo de perspectiva # se mantiene fijo M en caso que la camara rote M = cv2.getPerspectiveTransform(puntos1, puntos2) # a la imagen alineada le pasamos la informacion imagen_alineada=cv2.warpPerspective(imagen, M, (ancho,alto)) return imagen_alineada
def Perspective(img, c): src = getPerspectiveSrc(c) src.sort() maxWidth, maxHeight = src[0], src[1] rect = getMinAreaRectPoints(c) dst = srcToDstConvert(src) dst, maxWidth, maxHeight = func.GetDst(rect) rect = rect.astype(np.float32) dst = dst.astype(np.float32) M = cv2.getPerspectiveTransform(rect, dst) warp = cv2.warpPerspective(img, M, (maxWidth, maxHeight)) # 畫點的數字 # for i in range(len(dst)): # x = int(dst[i,0]) # y = int(dst[i,1]) # if x <= 0 : # x += 10 # else: # x -= 10 # if y <= 0 : # y += 20 # else: # y -= 20 # cv2.putText(warp,str(i) , (x, y),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) return warp
def square_trans(Table_2D: '桌子四角', points: '物体底部四点', lined_img=[]): ''' 若最后一个参数非空(调试模式),则显示图片 ''' affine_table_2D = np.float32([[0, 0], [0, 550], [550, 0], [550, 550]]) # 方桌边长550mm M = cv2.getPerspectiveTransform(Table_2D, affine_table_2D) # 获取透视变换矩阵 transed_points = np.matmul(points, np.transpose(M)) for i in range(4): transed_points[i][0] = transed_points[i][0] / transed_points[i][2] transed_points[i][1] = transed_points[i][1] / transed_points[i][2] a = [0 for i in range(4)] for i in range(4): a[i] = (int(transed_points[i][0]), int(transed_points[i][1])) angle = np.degrees( np.arccos((a[0][0] - a[0][1]) / (((a[0][0] - a[0][1])**2 + (a[1][0] - a[1][1])**2)**0.5))) if len(lined_img): # Perspective_Transformation transed = cv2.warpPerspective(lined_img, M, (550, 550)) cv2.line(transed, a[0], a[1], (0, 255, 0), 1) cv2.line(transed, a[0], a[2], (0, 255, 0), 1) cv2.line(transed, a[1], a[3], (0, 255, 0), 1) cv2.line(transed, a[2], a[3], (0, 255, 0), 1) cv2.imshow('perspective', transed) return transed_points, angle
def four_point_transform(image, pts): # obtain a consistent order of the points and unpack them # individually rect = order_points(pts) (tl, tr, br, bl) = rect # compute the width of the new image, which will be the # maximum distance between bottom-right and bottom-left # x-coordiates or the top-right and top-left x-coordinates widthA = np.sqrt(((br[0] - bl[0])**2) + ((br[1] - bl[1])**2)) widthB = np.sqrt(((tr[0] - tl[0])**2) + ((tr[1] - tl[1])**2)) maxWidth = max(int(widthA), int(widthB)) # compute the height of the new image, which will be the # maximum distance between the top-right and bottom-right # y-coordinates or the top-left and bottom-left y-coordinates heightA = np.sqrt(((tr[0] - br[0])**2) + ((tr[1] - br[1])**2)) heightB = np.sqrt(((tl[0] - bl[0])**2) + ((tl[1] - bl[1])**2)) maxHeight = max(int(heightA), int(heightB)) # now that we have the dimensions of the new image, construct # the set of destination points to obtain a "birds eye view", # (i.e. top-down view) of the image, again specifying points # in the top-left, top-right, bottom-right, and bottom-left # order dst = np.array([[0, 0], [maxWidth - 1, 0], [maxWidth - 1, maxHeight - 1], [0, maxHeight - 1]], dtype="float32") # compute the perspective transform matrix and then apply it M = cv2.getPerspectiveTransform(rect, dst) warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight)) # return the warped image return warped
def get_top_view(image, corners, make_square=True): # get bounding box rect = cv2.minAreaRect(corners) box = cv2.boxPoints(rect) box = np.int0(box) # cv2.drawContours(image, [box], 0, (0, 0, 255), 2) # rect = (center, shape, angle) # dimensions height = int(rect[1][1]) width = int(rect[1][0]) final = np.float32([[0,0],[width,0],[0,height],[width,height]]) # perspective transformation matrix transformation_matrix = cv2.getPerspectiveTransform(corners, final) warped = cv2.warpPerspective(image, transformation_matrix, (width, height)) side = max(width, height) if side < 200: return None, None, None # make it a square try: warped = cv2.resize(warped, (side,side), interpolation=cv2.INTER_CUBIC) warped = cv2.resize(warped, (450,450), interpolation=cv2.INTER_CUBIC) except Exception as e: print(e) return warped, transformation_matrix, (height,width)
def get_board_from_border(img): pts2 = np.float32([[0, 0], [800, 0], [0, 800], [800, 800]]) pts1 = np.float32([points[0], points[1], points[2], points[3]]) matrix = cv2.getPerspectiveTransform(pts1, pts2) result = cv2.warpPerspective(img, matrix, (800, 800)) cv2.imshow("Frame", result) cv2.waitKey(100) cv2.destroyAllWindows() return result # if __name__ == '__main__': # points = [] # calibrate_params("028.png") # # img = load_image("damki.png") # # img = resize(10, img) # # cv2.imwrite("../pictures/damkiresize.png", img) # # calibrate_image(img) # # b = generate_chessboard() # # find_circles(img, b) # # for line in b: # # print(line) # # res = calibrate_image(img) # # cv2.imwrite(path_to_src('pictures', "calib.png"), res) # # pip = load_image("calib.png") # # calibrate_params("calib.png")
def __bird_eye(self, img): h, w, _ = img.shape before = np.array([(0, h), (w / 4, h / 2), (3 * w / 4, h / 2), (w, h)], np.float32) after = np.array([(w / 4, h), (w / 4, 0), (3 * w / 4, 0), (0.79 * w, h)], np.float32) M = cv2.getPerspectiveTransform(before, after) dst = cv2.warpPerspective(img, M, (w, h)) return dst
def CorrectPerspective(img, pts): ptsVec = np.float32(pts) width = img.shape[1] height = img.shape[0] ptsVec2 = np.float32(((0, 0), (width, 0), (width, height), (0, height))) matrix = cv2.getPerspectiveTransform(ptsVec, ptsVec2) result = cv2.warpPerspective(img, matrix, (width, height)) return result
def wrap_perspective(img, edges): global imgOutput width, height = 250, 350 pts1 = np.float32([edges[0], edges[1], edges[2], edges[3]]) pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]]) matrix = cv2.getPerspectiveTransform(pts1, pts2) imgOutput = cv2.warpPerspective(img, matrix, (width, height)) cv2.imshow("Output Image ", imgOutput)
def transform(original, corners, x=450, y=450): new_size = np.float32([[0, 0], [x, 0], [0, y], [x, y]]) # puntos de las esquinas de la nueva imagen M = cv2.getPerspectiveTransform(corners, new_size) size = np.float32([x, y]) # dimensiones nueva imagen result = cv2.warpPerspective(original, M, tuple(size)) return result
def getWarp(img, biggest, widthImg, heightImg): biggest = reorder(biggest) pts1 = np.float32(biggest) pts2 = np.float32([[0,0], [widthImg, 0], [0, heightImg], [widthImg, heightImg]]) matrix = cv2.getPerspectiveTransform(pts1, pts2) imgOutput = cv2.warpPerspective(img, matrix, (widthImg, heightImg)) imgCropped = imgOutput[10:imgOutput.shape[0]-10, 10:imgOutput.shape[1]-10] imgCropped = cv2.resize(imgCropped, (widthImg, heightImg)) return imgCropped
def warpImg (img,points,w,h,pad=15): # print(points) points =reorder(points) pts1 = np.float32(points) pts2 = np.float32([[0,0],[w,0],[0,h],[w,h]]) matrix = cv2.getPerspectiveTransform(pts1,pts2) imgWarp = cv2.warpPerspective(img,matrix,(w,h)) imgWarp = imgWarp[pad:imgWarp.shape[0]-pad,pad:imgWarp.shape[1]-pad] return imgWarp
def _apply_func_perspective(image): """ Apply a perspective to an image """ rgb_image = image.convert('RGBA') img_arr = np.array(rgb_image) a = img_arr w, h = a.shape[0], a.shape[1] if h // w > 3: img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGBA2BGRA) img = cv2.copyMakeBorder(img, 20, 20, 0, 0, cv2.BORDER_CONSTANT, value=[255, 255, 255]) #img = cv2.imread(img) img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA)) img = img.resize((48, 48), Image.ANTIALIAS) return img ''' Set random vertex to target quadrilateral ''' random_flag = random.uniform(0, 2) if random_flag > 1: vertex1 = [0, 0] vertex4 = [random.uniform(1.0000, 1.1618) * (w - 1), 0] lens = vertex4[0] - vertex1[0] vertex2 = [random.uniform(0.1, 0.1618) * (w - 1), h - 1] vertex3 = [vertex2[0] + lens * random.uniform(0.932, 1), h - 1] else: vertex4 = [(w - 1) * random.uniform(1.0000, 1.1618), 0] vertex1 = [random.uniform(0.1000, 0.2618) * (w - 1), 0] lens = vertex4[0] - vertex1[0] vertex2 = [random.uniform(0.0000, 0.0618) * (w - 1), h - 1] vertex3 = [vertex2[0] + lens * random.uniform(0.932, 1), h - 1] pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]) pts1 = np.float32([vertex1, vertex2, vertex3, vertex4]) ''' get 3*3 transform martix M ''' M = cv2.getPerspectiveTransform(pts, pts1) dsize = get_perspective_offset(M, w, h) dst = cv2.warpPerspective(a, M, dsize) img_arr = np.array(dst) img = Image.fromarray(np.uint8(img_arr)) img = img.resize((48, 48), Image.ANTIALIAS) return img
def get_xform(src, dst): """get the transform between the two input arrays :param src: source points :type src: numpy array :param dst: destination points :type dst: numpy array :return: transform matrix :rtype: numpy array """ return cv2.getPerspectiveTransform(src, dst)
def wrap_perspective(path): img = cv2.imread(path) w, h = 250, 350 pts1 = np.float32([[111, 219], [287, 188], [154, 482], [352, 440]]) pts2 = np.float32([[0, 0], [w, 0], [0, h], [w, h]]) matrix = cv2.getPerspectiveTransform(pts1, pts2) imgOutput = cv2.warpPerspective(img, matrix, (w, h)) cv2.imshow('Images', img) cv2.imshow('Output', imgOutput) cv2.waitKey(0)
def rotRandrom(img, factor, shape): src_points = np.float32([[0, 0], [0, shape[0]], [shape[1], 0], [shape[1], shape[0]] ]) #Original Four Point From input image dst_points = np.float32([[R(factor), R(factor)], [R(factor), shape[0] - R(factor)], [shape[1] - R(factor), R(factor)], [shape[1] - R(factor), shape[0] - R(factor)]]) Transfer = cv2.getPerspectiveTransform(src_points, dst_points) output = cv2.warpPerspective(img, Transfer, shape) return output
def projective_transformation(img, vp): rows, cols = img.shape[:2] src_points = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1], [cols - 1, rows - 1]]) # dst_points = np.float32([[int(0.33*cols),int(rows/2)], [int(0.33*cols) + 100,int(rows/2)], [0,rows-1], [cols-1,rows-1]]) dst_points = np.float32([[vp[0] - 100, vp[1]], [vp[0] + 100, vp[1]], [-cols, 2 * rows], [2 * cols, 2 * rows]]) projective_matrix = cv2.getPerspectiveTransform(src_points, dst_points) img_output = cv2.warpPerspective(img, projective_matrix, (cols, rows)) return img_output
def wrapping(img, img_approx, wplus=0): if len(img.shape) == 3: height, width, _ = img.shape else: height, width = img.shape width = width + wplus pts1 = np.float32([ img_approx[0][0], img_approx[3][0], img_approx[1][0], img_approx[2][0] ]) pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]]) matrix = cv2.getPerspectiveTransform(pts1, pts2) return cv2.warpPerspective(img, matrix, (width, height))
def applyTransformations(img): proc = Utils.convertImg2Binary(img) # finding the top-down perspective of the puzzle vertices = Utils.findSudoku(proc) vertices = vertices.astype("float32") new_vertices = np.float32([[0, 0], [450, 0], [0, 450], [450, 450]]) # converting vertices into new vertices matrix = cv2.getPerspectiveTransform(vertices, new_vertices) result = cv2.warpPerspective(img, matrix, (450, 450)) return result
def get_board_from_border(img, points): pts2 = np.float32([[0, 0], [800, 0], [0, 800], [800, 800]]) pts1 = np.float32([points[0], points[1], points[2], points[3]]) matrix = cv2.getPerspectiveTransform(pts1, pts2) result = cv2.warpPerspective(img, matrix, (800, 800)) # cv2.imwrite("../pictures/wiadomka.png", result) # cv2.imshow("Frame", result) # cv2.waitKey(0) # cv2.destroyAllWindows() return result
def warpPerspective(): img = cv2.imread("./cards.jpg") width, height = 250, 350 imgResize = cv2.resize(img, (550, 350)) # pts1 = np.float32([[364, 83], [469, 113], [422, 256], [317, 222]]) pts1 = np.float32([[364, 83], [317, 222], [422, 253], [469, 113]]) pts2 = np.float32([[0, 0], [0, height], [width, height], [width, 0]]) matrix = cv2.getPerspectiveTransform(pts1, pts2) imgOutput = cv2.warpPerspective(imgResize, matrix, (width, height)) cv2.imshow("Image", imgResize) cv2.imshow("Output", imgOutput) #print(img.shape) cv2.waitKey(0)
def Enquadramento(img): #mesma logica do programa para seleção dos pontos para calibração e ajustes img = cv2.resize(img, (639, 479), interpolation=cv2.INTER_AREA) pts1 = np.float32([pontos[0], pontos[1], pontos[3], pontos[2]]) ##pts2 deve seguir a propporção do retangulo base densenhado pelo braço ## o retangulo que dita a proporção da workspace, importante delimita-la com o desenho progemado em .cod no braço (exem programa joe_ret2, passivo de alterações) pts2 = np.float32([[0, 0], [int(2500 * proporcao), 0], [0, 2500], [int(2500 * proporcao), 2500]]) #ajusta a pespectiva deformando a imagem de modo a diminuir as distoções causadas pelo angulo da câmera M = cv2.getPerspectiveTransform(pts1, pts2) dst = cv2.warpPerspective(img, M, (int(2500 * proporcao), 2500)) return dst
def perspectiveManipulation(image, imagePoints): """[Takes the given image and given points and tranforms the pixels contained in the points to a new image of chosen size. warpMat gets the tranform matrix to be used by the function warpPerspective.] Arguments: image -- [the image to be transformed, it will be the image that contains all of the hazard labels.] imagePoints {[numpy array]} -- [the set of points that contain the hazard label to be transformed.] Returns: [image] -- [the new image that contains the standardised hazard label] """ endPoints = np.float32([[250, 0], [0, 250], [250, 500], [500, 250]]) warpMat = cv2.getPerspectiveTransform(imagePoints, endPoints) return cv2.warpPerspective(image, warpMat, (500, 500))
def polyline(self): # Рисование линий на холсте, фактически бесполезная вещь '''self.allBinary_copy = self.allBinary.copy() cv2.polylines(self.allBinary_copy, [self.figure_pts], True, 255) cv2.imshow("line", self.allBinary_copy)''' perspective_temp = cv2.getPerspectiveTransform(self.figure_pts_0, self.dst) self.perspective = cv2.warpPerspective( self.allBinary, perspective_temp, (self.video_width, self.video_height), flags=cv2.INTER_LINEAR) cv2.imshow("perspective", self.perspective) self.search_white_pixels()