Example #1
0
def get_krt(im1, im2):
    ims = [im1, im2]
    sifts = []
    for x in range(2):
        sifts.append(ims[x][:-3]+"sift")

    # compute features                                                        
    #sift.process_image('../../data/book_frontal.JPG','../../data/im0.sift')
    sift.process_image(ims[0],sifts[0])

    l0,d0 = sift.read_features_from_file(sifts[0])
    #sift.process_image('../../data/book_perspective.JPG','../../data/im1.sift')
    sift.process_image(ims[1],sifts[1])
    l1,d1 = sift.read_features_from_file(sifts[1])
    # match features and estimate homography                                        
    matches = sift.match_twosided(d0,d1)
    ndx = matches.nonzero()[0]
    fp = homography.make_homog(l0[ndx,:2].T)
    ndx2 = [int(matches[i]) for i in ndx]
    print len(ndx2)
    tp = homography.make_homog(l1[ndx2,:2].T)
    model = homography.RansacModel()
    H,ransac_data = homography.H_from_ransac(fp,tp,model)


    # camera calibration
    #K = camera.my_calibration((747,1000))
    K = camera.my_calibration((Image.open(im2).size))
    # 3D points at plane z=0 with sides of length 0.2
    box = cube.cube_points([0,0,0.1],0.1)
    # project bottom square in first image
    cam1 = camera.Camera( hstack((K,dot(K,array([[0],[0],[-1]])) )) )
    # first points are the bottom square
    box_cam1 = cam1.project(homography.make_homog(box[:,:5]))
    # use H to transfer points to the second image
    print dot(H,box_cam1)
    box_trans = homography.normalize(dot(H,box_cam1))
    # compute second camera matrix from cam1 and H
    cam2 = camera.Camera(dot(H,cam1.P))
    A = dot(linalg.inv(K),cam2.P[:,:3])
    A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T
    cam2.P[:,:3] = dot(K,A)
    # project with the second camera
    box_cam2 = cam2.project(homography.make_homog(box))
    # test: projecting point on z=0 should give the same
    point = array([1,1,0,1]).T
    print homography.normalize(dot(dot(H,cam1.P),point))
    print cam2.project(point)

    import pickle
    with open('%s.pkl' % ims[1][:-4],'w') as f:
        pickle.dump(K,f)
        pickle.dump(dot(linalg.inv(K),cam2.P),f)
    sys.stderr.write("K and Rt dumped to %s.pkl\n" % ims[1][:-4])
Example #2
0
def get_krt(im1, im2):
    ims = [im1, im2]
    sifts = []
    for x in range(2):
        sifts.append(ims[x][:-3] + "sift")

    # compute features
    #sift.process_image('../../data/book_frontal.JPG','../../data/im0.sift')
    sift.process_image(ims[0], sifts[0])

    l0, d0 = sift.read_features_from_file(sifts[0])
    #sift.process_image('../../data/book_perspective.JPG','../../data/im1.sift')
    sift.process_image(ims[1], sifts[1])
    l1, d1 = sift.read_features_from_file(sifts[1])
    # match features and estimate homography
    matches = sift.match_twosided(d0, d1)
    ndx = matches.nonzero()[0]
    fp = homography.make_homog(l0[ndx, :2].T)
    ndx2 = [int(matches[i]) for i in ndx]
    print len(ndx2)
    tp = homography.make_homog(l1[ndx2, :2].T)
    model = homography.RansacModel()
    H, ransac_data = homography.H_from_ransac(fp, tp, model)

    # camera calibration
    #K = camera.my_calibration((747,1000))
    K = camera.my_calibration((Image.open(im2).size))
    # 3D points at plane z=0 with sides of length 0.2
    box = cube.cube_points([0, 0, 0.1], 0.1)
    # project bottom square in first image
    cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))
    # first points are the bottom square
    box_cam1 = cam1.project(homography.make_homog(box[:, :5]))
    # use H to transfer points to the second image
    print dot(H, box_cam1)
    box_trans = homography.normalize(dot(H, box_cam1))
    # compute second camera matrix from cam1 and H
    cam2 = camera.Camera(dot(H, cam1.P))
    A = dot(linalg.inv(K), cam2.P[:, :3])
    A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T
    cam2.P[:, :3] = dot(K, A)
    # project with the second camera
    box_cam2 = cam2.project(homography.make_homog(box))
    # test: projecting point on z=0 should give the same
    point = array([1, 1, 0, 1]).T
    print homography.normalize(dot(dot(H, cam1.P), point))
    print cam2.project(point)

    import pickle
    with open('%s.pkl' % ims[1][:-4], 'w') as f:
        pickle.dump(K, f)
        pickle.dump(dot(linalg.inv(K), cam2.P), f)
    sys.stderr.write("K and Rt dumped to %s.pkl\n" % ims[1][:-4])
Example #3
0
def get_camera_params(sz, H):
    """
    Get camere parametes: K, Rts.
    """
    # camera calibration
    K = my_calibration((sz))
    #print(K)
    # 3D points at plane z=0 with sides of length 0.2
    box = cube_points([0, 0, 0.1], 0.1)

    # project bottom square in first image
    cam1 = Camera(np.hstack((K, np.dot(K, np.array([[0], [0], [-1]])))))

    # first points are the bottom square
    box_cam1 = cam1.project(homography.make_homog(box[:, :5]))

    # use H to transfer points to the second image
    box_trans = homography.normalize(np.dot(H, box_cam1))

    # compute second camera metrx from cam1 and H
    cam2 = Camera(np.dot(H, cam1.P))
    A = np.dot(linalg.inv(K), cam2.P[:, :3])
    A = np.array([A[:, 0], A[:, 1], np.cross(A[:, 0], A[:, 1])]).T
    cam2.P[:, :3] = np.dot(K, A)

    Rt = np.dot(linalg.inv(K), cam2.P)

    return K, Rt
Example #4
0
File: test.py Project: ak352/pycv
def compute_camera_matrix_1(K, H):
    # 3D points at plane z=0 with sides of length 0.2
    box = cube.cube_points([0,0,0.1],0.1)
    # project bottom square in first image
    cam1 = camera.Camera( hstack((K,dot(K,array([[0],[0],[-1]])) )) )
    # first points are the bottom square
    box_cam1 = cam1.project(homography.make_homog(box[:,:5]))
    # use H to transfer points to the second image

    box_trans = homography.normalize(dot(H,box_cam1))
    return cam1, box
Example #5
0
def compute_camera_matrix_1(K, H):
    # 3D points at plane z=0 with sides of length 0.2
    box = cube.cube_points([0, 0, 0.1], 0.1)
    # project bottom square in first image
    cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))
    # first points are the bottom square
    box_cam1 = cam1.project(homography.make_homog(box[:, :5]))
    # use H to transfer points to the second image

    box_trans = homography.normalize(dot(H, box_cam1))
    return cam1, box
Example #6
0
File: test.py Project: ak352/pycv
def compute_camera_matrix_2(cam1, K, box, H):
    # compute second camera matrix from cam1 and H
    cam2 = camera.Camera(dot(H,cam1.P))
    A = dot(linalg.inv(K),cam2.P[:,:3])
    A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T
    cam2.P[:,:3] = dot(K,A)
    # project with the second camera
    box_cam2 = cam2.project(homography.make_homog(box))
    # test: projecting point on z=0 should give the same
    point = array([1,1,0,1]).T
    print(homography.normalize(dot(dot(H, cam1.P),point)))
    print(cam2.project(point))

    #H is correct as seen by the plots but cam2.P does not eem to be correct
    #test cam2.P
    return cam2
Example #7
0
def compute_camera_matrix_2(cam1, K, box, H):
    # compute second camera matrix from cam1 and H
    cam2 = camera.Camera(dot(H, cam1.P))
    A = dot(linalg.inv(K), cam2.P[:, :3])
    A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T
    cam2.P[:, :3] = dot(K, A)
    # project with the second camera
    box_cam2 = cam2.project(homography.make_homog(box))
    # test: projecting point on z=0 should give the same
    point = array([1, 1, 0, 1]).T
    print(homography.normalize(dot(dot(H, cam1.P), point)))
    print(cam2.project(point))

    #H is correct as seen by the plots but cam2.P does not eem to be correct
    #test cam2.P
    return cam2
# Not enough matches for H_from_ransac(), but all matches happen to be correct
# anyways, so no need for that.
H = homography.H_from_points(fp, tp)


K = camera.my_calibration(im0.shape[:2])

# How big this appears depends on the z translation in cam1 in the cam1 line.
box = cube_points([0, 0, 0.1], 0.1)

# project bottom square in first image
cam1 = camera.Camera(numpy.hstack((K, numpy.dot(K, array([[0], [0], [-1]])))))
box_cam1 = cam1.project(homography.make_homog(box[:, :5]))

# transfer points to second image
box_trans = homography.normalize(numpy.dot(H, box_cam1))

# compute second camera matrix
cam2 = camera.Camera(numpy.dot(H, cam1.P))

# H * P transforms points in z = 0 correctly, so its first two colums are
# correct. The upper left 3x3 submatrix should have orthogonal vectors, use
# this fact to reconstruct the third (H contains a scale, so this is only
# well-defined up to scale of the third axis. Fudge something up.)
A = numpy.dot(numpy.linalg.inv(K), cam2.P[:, :3])
lenx = numpy.sqrt(numpy.dot(A[:, 0], A[:, 0]))
leny = numpy.sqrt(numpy.dot(A[:, 1], A[:, 1]))
#print lenx, leny  # Should be similar.
zscale = (lenx + leny) / (2 * lenx * leny)
A = numpy.array([A[:, 0], A[:, 1], zscale * numpy.cross(A[:, 0], A[:, 1])]).T
cam2.P[:, :3] = numpy.dot(K, A)
Example #9
0
#H = homography.H_from_ransac(fp, tp, model)
# Not enough matches for H_from_ransac(), but all matches happen to be correct
# anyways, so no need for that.
H = homography.H_from_points(fp, tp)

K = camera.my_calibration(im0.shape[:2])

# How big this appears depends on the z translation in cam1 in the cam1 line.
box = cube_points([0, 0, 0.1], 0.1)

# project bottom square in first image
cam1 = camera.Camera(numpy.hstack((K, numpy.dot(K, array([[0], [0], [-1]])))))
box_cam1 = cam1.project(homography.make_homog(box[:, :5]))

# transfer points to second image
box_trans = homography.normalize(numpy.dot(H, box_cam1))

# compute second camera matrix
cam2 = camera.Camera(numpy.dot(H, cam1.P))

# H * P transforms points in z = 0 correctly, so its first two colums are
# correct. The upper left 3x3 submatrix should have orthogonal vectors, use
# this fact to reconstruct the third (H contains a scale, so this is only
# well-defined up to scale of the third axis. Fudge something up.)
A = numpy.dot(numpy.linalg.inv(K), cam2.P[:, :3])
lenx = numpy.sqrt(numpy.dot(A[:, 0], A[:, 0]))
leny = numpy.sqrt(numpy.dot(A[:, 1], A[:, 1]))
#print lenx, leny  # Should be similar.
zscale = (lenx + leny) / (2 * lenx * leny)
A = numpy.array([A[:, 0], A[:, 1], zscale * numpy.cross(A[:, 0], A[:, 1])]).T
cam2.P[:, :3] = numpy.dot(K, A)
Example #10
0
    p.append([c[0] + wid, c[1] - wid, c[2] + wid])
    p.append([c[0] + wid, c[1] - wid, c[2] - wid])
    return np.array(p).T


#%%
box = cube_points([0, 0, 0.1], 0.2)
''' Camera 1 at position 0 '''
P1 = np.hstack((K, np.dot(K, np.array([[0], [0], [-1]]))))
cam1 = camera.Camera(P1)
''' Project cube (first points are the bottom square) '''
box_P1 = homography.make_homog(box[:, :5])
box_P1 = cam1.project(box_P1)
''' Transfer points from P1 to P2 using homography '''
box_trans = np.dot(H, box_P1)
box_trans = homography.normalize(box_trans)
''' Compute P2 '''
P2 = np.dot(H, cam1.P)
cam2 = camera.Camera(P2)
A = np.dot(linalg.inv(K), cam2.P[:, :3])
A = np.array([A[:, 0], A[:, 1], np.cross(A[:, 0], A[:, 1])]).T
cam2.P[:, :3] = np.dot(K, A)
''' Project with the second camera '''
box_P2 = homography.make_homog(box)
box_P2 = cam2.project(box_P2)

#%%
''' Plot projections '''
# 2D projection of bottom square
plt.figure(1)
plt.imshow(im1)
Example #11
0
model = homography.RansacModel()
H, inliers = homography.H_from_ransac(fp, tp, model)  #删除误匹配

# camera calibration
K = my_calibration((747, 1000))  #相机焦距光圈初始化

# 3D points at plane z=0 with sides of length 0.2
box = cube_points([0, 0, 0.1], 0.1)

# project bottom square in first image
cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))  #相机初始化
# first points are the bottom square
box_cam1 = cam1.project(homography.make_homog(box[:, :5]))  #在相机中的坐标

# use H to transfer points to the second image
box_trans = homography.normalize(dot(H, box_cam1))  #单应性矩阵的变换

# compute second camera matrix from cam1 and H
cam2 = camera.Camera(dot(H, cam1.P))  #相机的外参数
A = dot(linalg.inv(K), cam2.P[:, :3])
A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T
cam2.P[:, :3] = dot(K, A)  #照相机矩阵的变换

# project with the second camera
box_cam2 = cam2.project(
    homography.make_homog(box))  #得到cube在第二幅图中的形状 通过照相机矩阵的变换
# plotting
im0 = array(Image.open('book_frontal.JPG'))
im1 = array(Image.open('book_perspective.JPG'))

figure()
    # img3 = img2
    # img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:60], img3)

    # cv2.imshow('result', img3)
    # cv2.imwrite('result.jpg', img3)
    # cv2.waitKey()

    # 位于边长为0.2, z = 0平面上底部的正方形
    Box = cube_points([0, 0, 0.1], 0.1)

    cam1 = Cli.Camera(
        numpy.hstack((K, numpy.dot(K, numpy.array([[0], [0], [-1]])))))
    box_cam1 = cam1.project(h**o.make_homog(Box[:, :5]))

    # 使用H将点变换到第二幅图像中
    box_trans = h**o.normalize(numpy.dot(H[0], box_cam1))

    # 从cam1和H中计算第二个照相机矩阵
    cam2 = Cli.Camera(numpy.dot(H[0], cam1.P))
    A = numpy.dot(numpy.linalg.inv(K), cam2.P[:, :3])
    A = numpy.array([A[:, 0], A[:, 1], numpy.cross(A[:, 0], A[:, 1])]).T
    cam2.P[:, :3] = numpy.dot(K, A)

    # 使用第二个照相机矩阵投影
    box_cam2 = cam2.project(h**o.make_homog(Box))

    # 测试:将点投影在Z=0上,应该能够得到相同点
    point = numpy.array([[1, 1, 0, 1]]).T
    print h**o.normalize(numpy.dot(numpy.dot(H[0], cam1.P), point)).T
    print h**o.normalize(numpy.dot(H[0], cam1.project(point))).T
    print cam2.project(point).T
# 最初の点群は、底面の正方形
pts = homography.make_homog(box[:, :5])
box_cam1 = cam1.project(pts)
# -

box_cam1

img_for_edit=img_query.copy()
for i in range(5):
    circle_pt=tuple([int(pt) for pt in box_cam1[:2,i]])
    cv2.circle(img_for_edit,circle_pt,50,(255,255,255),thickness=-1)
ip.show_img(img_for_edit,show_axis=True)

# Hを使って第2の画像に点を変換する
box_trans = homography.normalize(homology_matrix @ box_cam1)

img_for_edit=img_train.copy()
for i in range(5):
    circle_pt=tuple([int(pt) for pt in box_trans[:2,i]])
    cv2.circle(img_for_edit,circle_pt,50,(255,255,255),thickness=-1)
ip.show_img(img_for_edit,show_axis=True)

# cam1とHから第2のカメラ行列を計算する
P2=homology_matrix @ cam1.P
cam2 = camera.Camera(P2)

cam2.P[:,:3]

# P=K (R|t)なので
#P[:,:3]は K R
Example #14
0
    # Note: always resize image to 747 x 1000 or change the K below
    # camera calibration
    K = my_calibration((480,640))

    # 3D points at plane z=0 with sides of length 0.2
    box = cube_points([0,0,0.1],0.1)

    # project bottom square in first image
    cam1 = camera.Camera( hstack((K,dot(K,array([[0],[0],[-1]])) )) )
    # first points are the bottom square
    box_cam1 = cam1.project(homography.make_homog(box[:,:5]))


    # use H to transfer points to the second image
    box_trans = homography.normalize(dot(H,box_cam1))

    # compute second camera matrix from cam1 and H
    cam2 = camera.Camera(dot(H,cam1.P))
    A = dot(linalg.inv(K),cam2.P[:,:3])
    A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T
    cam2.P[:,:3] = dot(K,A)

    # project with the second camera
    box_cam2 = cam2.project(homography.make_homog(box))

    print(box_cam2)

    
    
    delta_x = box_cam2[0][0] - px
Example #15
0
import homography
import numpy as np

fp = []
tp = []
f = open("corresponding_points.txt")
for l in f:
    if l.strip() == "":
        continue
    points = l.strip().split(",")
    points = map(int, points)
    fp.append([points[0], points[1], 1])
    tp.append([points[2], points[3], 1])

fp = np.array(fp).transpose()
tp = np.array(tp).transpose()

H = homography.H_from_points(fp, tp)

print H

for i in xrange(len(fp.transpose())):
    f = fp.transpose()[i]
    transformed = H * np.matrix(f).transpose()
    transformed = homography.normalize(transformed)
    print[int(j) for j in transformed.flat]
    print[int(j) for j in tp.transpose()[i].flat]
    print "==================="
ndx = matches.nonzero()[0]
fp = homography.make_homog(l0[ndx, :2].T)
ndx2 = [int(matches[i]) for i in ndx]
tp = homography.make_homog(l1[ndx2, :2].T)
model = homography.RansacModel()
H, inliers = homography.H_from_ransac(fp, tp, model)
# camera calibration
K = my_calibration((747, 1000))
# 3D points at plane z=0 with sides of length 0.2
box = cube_points([0, 0, 0.1], 0.1)
# project bottom square in first image
cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))
# first points are the bottom square
box_cam1 = cam1.project(homography.make_homog(box[:, :5]))
# use H to transfer points to the second image
box_trans = homography.normalize(dot(H, box_cam1))
# compute second camera matrix from cam1 and H
cam2 = camera.Camera(dot(H, cam1.P))
A = dot(linalg.inv(K), cam2.P[:, :3])
A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T
cam2.P[:, :3] = dot(K, A)
# project with the second camera
box_cam2 = cam2.project(homography.make_homog(box))
# test: projecting point on z=0 should give the same

point = array([1, 1, 0, 1]).T
print homography.normalize(dot(dot(H, cam1.P), point))
print cam2.project(point)
# plotting
im0 = array(Image.open('../pcv_data/data/book_frontal.JPG'))
im1 = array(Image.open('../pcv_data/data/book_frontal.JPG'))
Example #17
0
File: 0519.py Project: ta-oyama/PCV


# カメラのキャリブレーション
K = my_calibration((747,1000))

# z=0の平面上の辺の長さ0.2の立方体の3Dの点
box = cube_points([0,0,0.1],0.1)
# 第一の画像の底面の正方形を射影する
cam1 = camera.Camera( np.hstack((K,np.dot(K,np.array([[0],[0],[-1]])) )) )

# 最初の点群は、底面の正方形
box_cam1 = cam1.project(homography.make_homog(box[:,:5]))

# Hをつかって第2の画像に点を変換する
box_trans = homography.normalize(np.dot(H,box_cam1))

# cam1とHから第2のカメラ行列を計算する
cam2 = camera.Camera(np.dot(H,cam1.P))
A = np.dot(np.linalg.inv(K),cam2.P[:,:3])
A = np.array([A[:,0],A[:,1],np.cross(A[:,0],A[:,1])]).T
cam2.P[:,:3] = np.dot(K,A)

#第2のカメラ行列を使って射影する
box_cam2 = cam2.project(homography.make_homog(box))

# テスト : z=0上の点において射影を変換すると同じになるはず
point = np.array([1,1,0,1]).T
print homography.normalize(np.array([np.dot(np.dot(H,cam1.P),point)]).T)
print cam2.project(point)
Example #18
0
model = homography.RansacModel()
H = homography.H_from_ransac(fp, tp, model)

# 计算相机标定矩阵
K = my_calibration((747, 1000))

# 位于边长为0.2, z=0平面上的三维点
box = cube_points([0, 0, 0.1], 0.1)

# 投影第一幅图像上底部的正方形
cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]])))))
# 底部正方形上的点
box_cam1 = cam1.project(homography.make_homog(box[:, :5]))

# 使用H将点变换到第二幅图像上
box_trans = homography.normalize(dot(H, box_cam1))

# 从cam1和H中计算第二个照相机矩阵
cam2 = camera.Camera(dot(H, cam1.P))
A = dot(linalg.inv(K), cam2.P[:, :3])
A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T
cam2.P[:, :3] = dot(K, A)

# 使用第二个照相机矩阵投影
box_cam2 = cam2.project(homography.make_homog(box))

# 测试: 将点投影在z=0上, 应该能够得到相同的点
point = array([1, 1, 0, 1]).T
print homography.normalize(dot(dot(H, cam1.P), point))
print cam2.project(point)
#projection cube on the first bottom
cam1 = cam.Camera( hstack((K,dot(K,array([[0],[0],[-1]])) )) )
#points up the bottom
box_trans = homography,mormalize(dot(H,box_cam1))
#compute the second matrix from the cam1 and H
cam2 = cam.Camera(dot(H,cam1.P))
A = dot(linalg.inv(K),cam2.P[:,:3])
A = array([A[:,0],A[:1],cross(A[:,0],A[:,1])]).T
cam2.P[:,:3] = dot(K,A)

#use the second matrix projection
box_cam2 = cam2.project(homography.make_homog(box))

#test:to projection on z =0 ,to get the same point
point = array([1,1,0,1]).T
print homography.normalize(dot(dot(H,cam1.P),point))
print cam2.project(point)

im0 = array(Image.open('/home/wangkai/Pictures/book_frontal.jpg'))
im1 = array(Image.open('/home/wangkai/Pictures/book_prespective.jpg'))

#bottom cube 2D projection
figure()
imshow(im0)
plot(box_cam1[0,:],box_cam1[1,:],linewidht=3)
#use H to trans the 2D projection
figure()
imshow(im1)
plot(box_trans[0,:], box_trans[1,:],linewidth=3)
#3D cube
figure()