def final_pers(name, name1): # Reading the image. im_src = cv2.imread(name) im_base = cv2.imread("./input/Base.jpg") # Show image and wait for 4 clicks. pts_src = get_four_points(im_src, "Image") pts_src = np.float32(pts_src) pts_base = get_four_points(im_base, "Image") pts_base = np.float32(pts_base) #calculate dimensions minx = 9999999 miny = 9999999 maxx = 0 maxy = 0 for i in range(0, 4): x = pts_base[i][0] y = pts_base[i][1] if x > maxx: maxx = x if x < minx: minx = x if y > maxy: maxy = y if y < miny: miny = y a = 4 * int(maxx - minx) b = 4 * int(maxy - miny) #normalise co-ordinates for i in range(0, 4): pts_base[i][0] = 4 * (pts_base[i][0] - minx) pts_base[i][1] = 4 * (pts_base[i][1] - miny) # Calculate the homography M = cv2.getPerspectiveTransform(pts_src, pts_base) # Warp source image to destination im_dst = cv2.warpPerspective(im_src, M, (a, b)) # Show output cv2.imshow("Image", im_dst) cv2.imwrite(name1, im_dst) cv2.waitKey(0)
def point_ply(img): if (flag_pick_pionts == True): pts_src = get_four_points(img) flag_pick_pionts == False return pts_src
def main(size): im_base = cv2.imread("./input/Base.jpg") print('mark complete base') M = get_four_points(im_base, "Image") name = "./Matrices/Base.npy" np.save(name, M) print('Now mark for individual images') for i in range(1, size + 1): name = "./input/C" + str(i) + "/bw.jpg" name1 = "./input/C" + str(i) + "/perss2.jpg" final_pers_adj(name, name1, i)
def homography(projector_img, img_name): # Read destination image (image of the projector) im_dst = cv2.imread(projector_img) print(im_dst.shape) # Get four corners of the billboard print 'Click on four corners of a billboard and then press ENTER' pts_dst = get_four_points(im_dst) # generate layered image # crop generated image for i in range(len(img_name)): # Read source image. src_path = '/home/abhishek/Pictures/' + img_name[i] + '.png' im_src = cv2.imread(src_path) size = im_src.shape # Create a vector of source points. pts_src = np.array([[0, 0], [size[1] - 1, 0], [size[1] - 1, size[0] - 1], [0, size[0] - 1]], dtype=float) # Calculate Homography between source and destination points h, status = cv2.findHomography(pts_src, pts_dst) # Warp source image im_temp = cv2.warpPerspective(im_src, h, (im_dst.shape[1], im_dst.shape[0])) # Black out polygonal area in destination image. cv2.fillConvexPoly(im_dst, pts_dst.astype(int), 0, 16) # Add warped source image to destination image. im_dst = im_dst + im_temp path = '/home/abhishek/Pictures/warp_' + img_name[i] + '.png' cv2.imwrite(path, im_temp)
import cv2 import numpy as np from utils import get_four_points img_src = cv2.imread("data/images/book1.jpg") dst_size = (400, 300, 3) img_dst = np.zeros(dst_size, np.uint8) # cv2.imshow("dst",img_dst) # 우리가 원본 이미지로부터는 마우스 클릭으로 4개의 점을 가져올거다. cv2.imshow("Image", img_src) points_src = get_four_points(img_src) # 새로만들 이미지에서는 위의 원본 이미지 4개의 점과 매핑할 점을 잡아줘야 한다. point_dst = np.array( [0, 0, dst_size[1], 0, dst_size[1], dst_size[0], 0, dst_size[0]], dtype=float) points_dst = point_dst.reshape(4, 2) h, status = cv2.findHomography(points_src, points_dst) img_dst = cv2.warpPerspective(img_src, h, (dst_size[1], dst_size[0])) cv2.imshow("img dst", img_dst) cv2.waitKey(0) cv2.destroyAllWindows()
dst_size = img_dst.shape # 우리가 원본 이미지로부터는 이미지 포인트를 바로 가져올 수 있다. # get_four_points 포인트를 가져오는 사용자 함수를 사용할 필요가 없다 cv2.imshow('Source Image', img_src) # 시계 방향으로 점을 가져온다, 정사각형이므로 좌표를 찍을 수 있다 points_src = np.array([ 0, 0, img_src.shape[1], 0, img_src.shape[1], img_src.shape[0], 0, img_src.shape[0] ]) points_src = points_src.reshape(4, 2) # 새로 만들 이미지에서는, 위의 원본 이미지 4개의 점과 매핑할 점을 잡아줘야한다 # 좌표를 알 수 없으므로 마우스를 눌러서 찍어야함 points_dst = get_four_points(img_dst) # h가 3행 X 3열 행렬임 h, stauts = cv2.findHomography(points_src, points_dst) # dst_size[1] x축 dst_size[0] y축 #행렬을 머리속에 그려보자, 그러면 x y축이 이해가 감 img_warp_temp = cv2.warpPerspective(img_src, h, (dst_size[1], dst_size[0])) cv2.imshow('warpPerspective', img_warp_temp) # 이제 점이 찍은 다음이므로 타임스퀘어 이미지를 간판을 검정색으로 바꾼다 0 # fillConvexRoly() 에서 2번째 파라미터 포인트는 int로 바꿔서 넣어줘야한다. points_dst는 float blackedImg = cv2.fillConvexPoly(img_dst, points_dst.astype(int), 0) # 이렇게 하면 간판만 검은색 화면으로 볼 수 있다 cv2.imshow('Img to 0', blackedImg)
def obtain(im_nm, im_cam): im_name = "input/" + im_cam + "/" + im_nm im_src = cv2.imread(im_name) pts_src = get_four_points(im_src, im_cam) return pts_src
import sys import numpy as np import cv2 from utils import get_four_points upper_bound = [] lower_bound = [] rgb_color = [] im = cv2.imread('hex_t.PNG') print('First start by clicking on a red tile\n') print( 'proceed to click on a blue tile\n then green...purple\n yellow\n orange\n black' ) points = get_four_points(im) ''' blue = sys.argv[1] green = sys.argv[2] red = sys.argv[3] ''' for i in range(0, len(points)): color_point_x = np.int64(points[i][0]) color_point_y = np.int64(points[i][1]) rgb_color.append(im[color_point_x, color_point_y]) for i in range(0, len(rgb_color)): blue = rgb_color[i][0] green = rgb_color[i][1] red = rgb_color[i][2] color = np.uint8([[[blue, green, red]]])
def main(): # Parse Arguments parser = OptionParser() # If file_source is not specified, then camera will be used parser.add_option('--file_source', dest='file_source', default="None") # If no camera specified, then camera 0 will be used parser.add_option('--camera', dest='camera', default="0", type="int") # Warp is disable by default parser.add_option('--warp', dest='warp', action='store_true') parser.add_option('--no-warp', dest='warp', action='store_false') parser.set_defaults(warp=False) options, remainder = parser.parse_args() print(options.camera) print(options.warp) print(options.file_source) if options.file_source == "None": print("No input file source selected") stream = open_stream(options.camera) else: print("input desired from a file") stream = open_stream(options.file_source) if stream.isOpened() == False: print("failed to open input stream") return ret_val, img = stream.read() if ret_val == False: print("EOS input stream, exit") return #cv2.imshow('input image',img) #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #gray = cv2.bilateralFilter(gray, 11, 17, 17) #cv2.imshow('gray image',gray) #edged = cv2.Canny(gray, 30, 200) #cv2.imshow('Canny edges',edged) #(_,contours, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:40] #cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:40] #cv2.drawContours(img,cnts,-1,(0,255,0),2) #-1 fills it #cv2.imshow('final',img) #while(True): # if cv2.waitKey(1) & 0xFF == ord('q'): # break #return if options.warp == True: print("warp was set to true") # Use the very first frame to get warp calibration # get the four points to apply reprojection pts_src = get_four_points(img) # calculate width and height for the reprojected image W = int(pts_src[2][0] - pts_src[3][0]) H = int(pts_src[3][1] - pts_src[0][1]) pts_dst = np.array([[0, 0], [W - 1, 0], [W - 1, H - 1], [0, H - 1]], dtype=float) # Initialize Destination Frame im_dst = np.zeros((H, W, 3), dtype="uint8") # Calculate Homography h, status = cv2.findHomography(pts_src, pts_dst) size = im_dst.shape # warp calibration done while True: #capture a new frame from camera ret_val, im_src = stream.read() if ret_val == False: print("EOS in input stream") if options.file_source != "None": print("Reopen the Input File") stream = open_stream(options.file_source) if stream.isOpened() == False: print("Could not open input file") return ret_val, im_src = stream.read() if ret_val == False: print("Could not read from File") return #reproject_frame if options.warp == True: im_dst = cv2.warpPerspective(im_src, h, (size[1], size[0])) cv2.imshow('reprojected', im_dst) show_frame(im_src, mirror=False) # TODO: Pre-process the Input Image before conversion to gray? if options.warp == True: gray = cv2.cvtColor(im_dst, cv2.COLOR_BGR2GRAY) else: gray = cv2.cvtColor(im_src, cv2.COLOR_BGR2GRAY) # TODO: Post-process the Input Image to sharpen it? # TODO: Get filter parameters from user #gray = cv2.bilateralFilter(gray, 11, 17, 17) cv2.imshow('gray image', gray) # TODO: Get Canny parameters from user edged = cv2.Canny(gray, 30, 200) cv2.imshow('Canny edges', edged) (_, contours, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key=cv2.contourArea, reverse=True) #cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:40] new_contour = cnts[:1] # TODO: Get a Threshold Parameter from User for c in cnts: # approximate the contour #peri = cv2.arcLength(c, True) # approx = cv2.approxPolyDP(c, 0.02 * peri, True) #print(peri>100) area = cv2.contourArea(c) if area > 10: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) if (len(approx) == 3 or 4 or 5): new_contour.append(c) cnts = new_contour print("OVER TO NEXT Frame") # TODO: Get a parameter if only rectangle to be displayed # if our approximated contour has four points, then # we can assume that we have found our screen # if len(approx) == 4: # screenCnt = approx # break if options.warp == True: cv2.drawContours(im_dst, cnts, -1, (0, 255, 0), 2) #-1 fills it cv2.imshow('final', im_dst) else: cv2.drawContours(im_src, cnts, -1, (0, 255, 0), 2) #-1 fills it cv2.imshow('final', im_src) #print(str(datetime.now())) if cv2.waitKey(1) == 27: # Escape Character break # Esc to quit
pts_dst = np.array( [ [0,0], [size[0] - 1, 0], [size[0] - 1, size[1] -1], [0, size[1] - 1 ] ], dtype=float ) print ''' Click on the four corners of the book -- top left first and bottom left last -- and then hit ENTER ''' # Show image and wait for 4 clicks. cv2.imshow("Image", im_src) pts_src = get_four_points(im_src); # Calculate the homography h, status = cv2.findHomography(pts_src, pts_dst) # Warp source image to destination im_dst = cv2.warpPerspective(im_src, h, size[0:2]) # Show output cv2.imshow("Image", im_dst) cv2.waitKey(0)
# Read in the image. im_src = cv2.imread("./input/C2/pers1.jpg") # Destination image size = (300, 400, 3) im_dst = np.zeros(size, np.uint8) pts_dst = np.array([[0, 0], [size[0] - 1, 0], [size[0] - 1, size[1] - 1], [0, size[1] - 1]], dtype=float) print(''' Click on the four corners of the book -- top left first and bottom left last -- and then hit ENTER ''') # Show image and wait for 4 clicks. cv2.imshow("Image", im_src) pts_src = get_four_points(im_src, "Image") # Calculate the homography h, status = cv2.findHomography(pts_src, pts_dst) # Warp source image to destination im_dst = cv2.warpPerspective(im_src, h, size[0:2]) # Show output cv2.imshow("Image", im_dst) cv2.waitKey(0)
# Destination image size = (600, 500, 3) im_dst = np.zeros(size, np.uint8) pts_dst = np.array( [[0, 0], [size[0] - 1, 0], [size[0] - 1, size[1] - 1], [0, size[1] - 1]], dtype=float) print( 'Click on the four corners of the book -- top left first and bottom left last -- and then hit ENTER' ) # Show image and wait for 4 clicks. cv2.imshow("Image", im_src) pts_src = get_four_points(im_src) parameters['corners'] = pts_src.tolist() # Calculate the homography h, status = cv2.findHomography(pts_src, pts_dst) # Warp source image to destination im_dst = cv2.warpPerspective(im_src, h, size[0:2]) # Show output cv2.imshow("Image", im_dst) cv2.waitKey(0) print('First start by clicking on all red tiles') print( 'proceed to click on a blue tile\n then green...\n gray\n orange\n purple') parameters['color'] = [red, blue, green, gray, orange, purple] hsv = cv2.cvtColor(im_dst, cv2.COLOR_BGR2HSV)
out.write(social) else: cv.imshow("social", social) if cv.waitKey(1) & 0xff == 27: break else: break self._capture.release() if save: out.release() if __name__ == "__main__": video = VideoProc("TownCentreXVID.avi") frame = cv.imread("frame_0.jpg") get_four_points(frame) M = video.four_point_transform(frame,\ np.array([(1116,219),\ (1536,263),\ (930, 883),\ (292,735)], dtype=np.float32)) warped = video.get_warped_image(frame, M, (2000,2100)) capture = video.get_stream() if video.check_stream() == False: print("video stream unavailable") sys.exit(-1) video.run(100, False, "HOG") cv.destroyAllWindows()
pts_dst = np.array( [ [0,0], [size[0] - 1, 0], [size[0] - 1, size[1] -1], [0, size[1] - 1 ] ], dtype=float ) print ''' Click on the four corners of the book -- top left first and bottom left last -- and then hit ENTER ''' # Show image and wait for 4 clicks. cv2.imshow("Image", im_src) pts_src = get_four_points(im_src); # Calculate the homography h, status = cv2.findHomography(pts_src, pts_dst) # Warp source image to destination im_dst = cv2.warpPerspective(im_src, h, size[0:2]) # Show output cv2.imshow("Image", im_dst) cv2.waitKey(0)
# pass these in next time as a command line argument newsize = (4000,6000,3) size = (20,100,3) im_dst = np.zeros(newsize, np.uint8) # points the source will get mapped to pts_dst = np.array([ [800,800], [800+size[0]-1,800], [800+size[0]-1,800+size[1]-1], [800,800+size[1]-1]],dtype=float) # need to start from top left and end with bottom left pts_src = get_four_points(frame) h, status = cv2.findHomography(pts_src, pts_dst) # translation matrix to get points within view # calculate this based on warped perspective next time t = np.array([ [1,0,100], [0,1,3000], [0,0,1] ]) # total transformation matrix transform = np.dot(t,h) # looping over frames of video
def final_pers_adj(name, name1, xx): M2 = np.load("./Matrices/Base.npy") # Reading the image. im_src = cv2.imread(name) im_base = cv2.imread("./input/Base.jpg") # Show image and wait for 4 clicks. pts_src = get_four_points(im_src, "Image") pts_src = np.float32(pts_src) pts_base = get_four_points(im_base, "Image") pts_base = np.float32(pts_base) #print(M2) #print(pts_base) #calculate dimensions minx = 9999999 miny = 9999999 maxx = 0 maxy = 0 for i in range(0, 4): x = pts_base[i][0] y = pts_base[i][1] if x > maxx: maxx = x if x < minx: minx = x if y > maxy: maxy = y if y < miny: miny = y #size of base marked a = int(maxx - minx) b = int(maxy - miny) #finding size of complete image cols = (M2[1][0] - M2[0][0]) rows = (M2[3][1] - M2[0][0]) #translation works xtranslate = (pts_base[0][0] - M2[0][0]) ytranslate = (pts_base[0][1] - M2[0][1]) print(xtranslate, " ", ytranslate) if xtranslate <= 10.0: xtranslate = 0 if ytranslate <= 10.0: ytranslate = 0 #normalise co-ordinates for i in range(0, 4): pts_base[i][0] = (pts_base[i][0] - minx) pts_base[i][1] = (pts_base[i][1] - miny) # Calculate the homography M = cv2.getPerspectiveTransform(pts_src, pts_base) name = "./Matrices/C" + str(xx) np.save(name, M) # Warp source image to destination im_dst = cv2.warpPerspective(im_src, M, (a, b)) M1 = np.float32([[1, 0, xtranslate], [0, 1, ytranslate], [0, 0, 1]]) im_fin = cv2.warpPerspective(im_dst, M1, (int(cols), int(rows))) Mx = np.dot(M, M1) #print (str(Mx)) nnn = name + "s" np.save(nnn, Mx) cv2.imshow("Image", im_fin) cv2.imwrite(name1, im_fin) cv2.waitKey(0)