博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python-siftgpu(OpenCL版本)--并且改为ASIFT-GPU
阅读量:2050 次
发布时间:2019-04-28

本文共 7572 字,大约阅读时间需要 25 分钟。

 

sudo pip install silx 

from silx.image import siftimport cv2img = cv2.imread("./11_IMG_Texture_8Bit.png", cv2.IMREAD_GRAYSCALE)siftp = sift.SiftPlan(img.shape, img.dtype, devicetype="GPU")kp = siftp.keypoints(img)

1原始SIFT-GPU (OpenCL版本)

# Python 2/3 compatibilityfrom __future__ import print_functionimport sysimport numpy as npimport cv2 as cv# local modulesfrom common import Timerfrom descriptor import init_feature, filter_matches, explore_matchfrom silx.image import siftimport numpyimport pylabfrom matplotlib.patches import ConnectionPatchimport cv2exp = 0if exp == 0:    fn1 = "./save_ply/OtherSampleFrame_IMG_Texture_8Bit_48.png"    fn2 = "./save_ply/OtherSampleFrame_IMG_Texture_8Bit_52.png"    depfn1 = "./save_ply/OtherSampleFrame_IMG_DepthMap_48.tif"    depfn2 = "./save_ply/OtherSampleFrame_IMG_DepthMap_52.tif"else:    fn1 = "./save_ply/11_IMG_Texture_8Bit.png"    fn2 = "./save_ply/22_IMG_Texture_8Bit.png"    depfn1 = "./save_ply/11_IMG_DepthMap.tif"    depfn2 = "./save_ply/22_IMG_DepthMap.tif"img1 = cv.imread(fn1, cv.IMREAD_GRAYSCALE)img2 = cv.imread(fn2, cv.IMREAD_GRAYSCALE)depImage1 = cv.imread(depfn1, -1)depImage2 = cv.imread(depfn2, -1)if img1 is None:    print('Failed to load fn1:', fn1)    sys.exit(1)if img2 is None:    print('Failed to load fn2:', fn2)    sys.exit(1)siftp = sift.SiftPlan(img1.shape, img1.dtype, devicetype="GPU")keypoints1 = siftp.keypoints(img1)keypoints2 = siftp.keypoints(img2)print("After keypoints1 %i" % keypoints1.shape[0])print("After keypoints2 %i" % keypoints2.shape[0])fig = pylab.figure()sp1 = fig.add_subplot(122)sp2 = fig.add_subplot(121)im1 = sp1.imshow(img1)im2 = sp2.imshow(img2)siftmatch = sift.MatchPlan(devicetype="GPU")matching = siftmatch.match(keypoints1, keypoints2)h1, w1 = img1.shape[:2]h2, w2 = img2.shape[:2]vis = np.zeros((max(h1, h2), w1 + w2), np.uint8)vis[:h1, :w1] = img1vis[:h2, w1:w1 + w2] = img2vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)green = (0, 255, 0)for left, right in zip(matching[:, 0], matching[:, 1]):    cv2.line(vis, (int(left.x), int(left.y)), (int(right.x+w1), int(right.y)), green, thickness=4)cv2.imwrite("./save_ply/No2_FeaturePointElimination.png", vis)

 

2改为ASIFT-GPU(OpenCL版本)

 

# Python 2/3 compatibilityfrom __future__ import print_functionimport sysimport numpy as npimport cv2 as cv# local modulesfrom common import Timerfrom descriptor import init_feature, filter_matches, explore_matchfrom silx.image import siftimport numpyimport pylabfrom matplotlib.patches import ConnectionPatchimport cv2def affine_skew(tilt, phi, img, mask=None):    '''    affine_skew(tilt, phi, img, mask=None) -> skew_img, skew_mask, Ai    Ai - is an affine transform matrix from skew_img to img    '''    h, w = img.shape[:2]    if mask is None:        mask = np.zeros((h, w), np.uint8)        mask[:] = 255    A = np.float32([[1, 0, 0], [0, 1, 0]])    if phi != 0.0:        phi = np.deg2rad(phi)        s, c = np.sin(phi), np.cos(phi)        A = np.float32([[c,-s], [ s, c]])        corners = [[0, 0], [w, 0], [w, h], [0, h]]        tcorners = np.int32( np.dot(corners, A.T) )        x, y, w, h = cv.boundingRect(tcorners.reshape(1,-1,2))        A = np.hstack([A, [[-x], [-y]]])        img = cv.warpAffine(img, A, (w, h), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_REPLICATE)    if tilt != 1.0:        s = 0.8*np.sqrt(tilt*tilt-1)        img = cv.GaussianBlur(img, (0, 0), sigmaX=s, sigmaY=0.01)        img = cv.resize(img, (0, 0), fx=1.0/tilt, fy=1.0, interpolation=cv.INTER_NEAREST)        A[0] /= tilt    if phi != 0.0 or tilt != 1.0:        h, w = img.shape[:2]        mask = cv.warpAffine(mask, A, (w, h), flags=cv.INTER_NEAREST)    Ai = cv.invertAffineTransform(A)    return img, mask, Aidef affine_detect(img, mask=None):    '''    affine_detect(detector, img, mask=None, pool=None) -> keypoints, descrs    Apply a set of affine transformations to the image, detect keypoints and    reproject them into initial image coordinates.    See http://www.ipol.im/pub/algo/my_affine_sift/ for the details.    ThreadPool object may be passed to speedup the computation.    '''    params = [(1.0, 0.0)]    for t in 2**(0.5*np.arange(1,6)):        for phi in np.arange(0, 180, 72.0 / t):            params.append((t, phi))    hh,ww = img.shape[:2]    keypointa_all = []    total_size = 0    dtype_kp = numpy.dtype([('x', numpy.float32),                            ('y', numpy.float32),                            ('scale', numpy.float32),                            ('angle', numpy.float32),                            ('desc', (numpy.uint8, 128))                            ])    for i, (k, d) in enumerate(params):        t, phi = k, d        timg, tmask, Ai = affine_skew(t, phi, img)        img_disp = cv.bitwise_and(timg, timg, mask=tmask);        siftp = sift.SiftPlan(img_disp.shape, img_disp.dtype, devicetype="GPU")        keypoints = siftp.keypoints(img_disp)        for kp in keypoints:            x = kp.x            y = kp.y            pt_x,pt_y = tuple(np.dot(Ai, (x, y, 1)))            # Out of bounds judgment            if ((pt_x<0) or (pt_y<0) or (pt_x > ww-1) or (pt_y > hh-1)):                if (pt_x < 0):                    kp.x, kp.y = (0, pt_y)                if (pt_y < 0):                    kp.x, kp.y = (pt_x, 0)                if (pt_x > ww-1):                    kp.x, kp.y = (ww-1, pt_y)                if (pt_y > hh-1):                    kp.x, kp.y = (pt_x, hh-1)        keypointa_all.append(keypoints)        total_size += len(keypoints)    output = numpy.recarray(shape=(total_size,), dtype=dtype_kp)    last = 0    for ds in keypointa_all:        l = ds.shape[0]        if l > 0:            output[last:last + l].x = ds[0:l].x            output[last:last + l].y = ds[0:l].y            output[last:last + l].scale = ds[0:l].scale            output[last:last + l].angle = ds[0:l].angle            output[last:last + l].desc = ds[0:l].desc            last += l    return outputdef main():    feature_name = 'sift'    exp = 1    if exp == 0:        fn1 = "./save_ply/OtherSampleFrame_IMG_Texture_8Bit_48.png"        fn2 = "./save_ply/OtherSampleFrame_IMG_Texture_8Bit_52.png"        depfn1 = "./save_ply/OtherSampleFrame_IMG_DepthMap_48.tif"        depfn2 = "./save_ply/OtherSampleFrame_IMG_DepthMap_52.tif"    else:        fn1 = "./save_ply/P1000965.JPG"        fn2 = "./save_ply/P1000966.JPG"        depfn1 = "./save_ply/11_IMG_DepthMap.tif"        depfn2 = "./save_ply/22_IMG_DepthMap.tif"    img1 = cv.imread(fn1, cv.IMREAD_GRAYSCALE)    img2 = cv.imread(fn2, cv.IMREAD_GRAYSCALE)    depImage1 = cv.imread(depfn1, -1)    depImage2 = cv.imread(depfn2, -1)    if img1 is None:        print('Failed to load fn1:', fn1)        sys.exit(1)    if img2 is None:        print('Failed to load fn2:', fn2)        sys.exit(1)    with Timer('kp1'):        keypoints1 = affine_detect(img1)    with Timer('kp2'):        keypoints2 = affine_detect(img2)    print("keypoints1 %i" % len(keypoints1))    print("keypoints2 %i" % len(keypoints2))    siftmatch = sift.MatchPlan(devicetype="GPU")    matching = siftmatch.match(keypoints1, keypoints2)    h1, w1 = img1.shape[:2]    h2, w2 = img2.shape[:2]    vis = np.zeros((max(h1, h2), w1 + w2), np.uint8)    vis[:h1, :w1] = img1    vis[:h2, w1:w1 + w2] = img2    vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)    green = (0, 255, 0)    for left, right in zip(matching[:, 0], matching[:, 1]):        cv2.line(vis, (int(left.x), int(left.y)), (int(right.x+w1), int(right.y)), green, thickness=4)    cv2.imwrite("./save_ply/No2_FeaturePointElimination.png", vis)if __name__ == '__main__':    with Timer('all time:'):        main()

 

转载地址:http://regof.baihongyu.com/

你可能感兴趣的文章
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
Redis学习笔记(四)—— redis的常用命令和五大数据类型的简单使用
查看>>
深入分析JavaWeb技术内幕(一)—— 深入Web请求过程
查看>>
深入分析JavaWeb技术内幕(二)—— 深入分析Java I/O的工作机制
查看>>
使用Java将PDF解析成HTML页面进行展示并从页面中提取Json数据设置到Table中
查看>>
Redis学习笔记(五)—— 在Linux下搭建Redis集群
查看>>
Redis学习笔记(六)—— 解决安装ruby出现的问题:redis requires Ruby version &amp;gt;= 2.2.2.
查看>>
从原理上搞懂编码——究竟什么是编码?什么是解码?什么是字节流?
查看>>
前端上传文件组件Plupload使用指南
查看>>
单点登录原理与简单实现
查看>>
使用zxing生成彩色或带图片的二维码
查看>>
在Linux下安装JDK8
查看>>
面试题 —— HTTP请求中get请求和post请求的区别以及底层原理
查看>>
面试题 —— HashMap、HashTable、HashSet的实现原理和底层数据结构
查看>>
C语言学习笔记(三)—— 数据和C
查看>>
Java编程思想(三)—— 操作符
查看>>
梦飞 —— 述:我只是一个普通农民家的孩子,但我有一个梦想
查看>>
图解HTTP(二)—— 简单的HTTP协议
查看>>
程序员的数学(四)—— 数学归纳法,如何征服无穷数列
查看>>