图像中的图像

仿射扭曲的一个简单例子是,将图像或者图像的一部分放置在另一幅图像中,使得它们能够和指定的区域或者标记物对齐。

1.以下是实现代码:

 # -*- coding: utf-8 -*-
from PCV.geometry import homography, warp
from PIL import Image
from pylab import *
from scipy import ndimage# example of affine warp of im1 onto im2im1 = array(Image.open('F:/VSCode/pcv-book-code-master/ch03/36.jpg').convert('L'))
im2 = array(Image.open('F:/VSCode/pcv-book-code-master/ch03/35.jpeg').convert('L'))
# set to points
tp = array([[240,520,520,240],[600,620,1200,1230],[1,1,1,1]])
#tp = array([[675,826,826,677],[55,52,281,277],[1,1,1,1]])
im3 = warp.image_in_image(im1,im2,tp)
figure()
gray()
subplot(141)
axis('off')
imshow(im1)
subplot(142)
axis('off')
imshow(im2)
subplot(143)
axis('off')
imshow(im3)# set from points to corners of im1
m,n = im1.shape[:2]
fp = array([[0,m,m,0],[0,0,n,n],[1,1,1,1]])
# first triangle
tp2 = tp[:,:3]
fp2 = fp[:,:3]
# compute H
H = homography.Haffine_from_points(tp2,fp2)
im1_t = ndimage.affine_transform(im1,H[:2,:2],
(H[0,2],H[1,2]),im2.shape[:2])
# alpha for triangle
alpha = warp.alpha_for_triangle(tp2,im2.shape[0],im2.shape[1])
im3 = (1-alpha)*im2 + alpha*im1_t
# second triangle
tp2 = tp[:,[0,2,3]]
fp2 = fp[:,[0,2,3]]
# compute H
H = homography.Haffine_from_points(tp2,fp2)
im1_t = ndimage.affine_transform(im1,H[:2,:2],
(H[0,2],H[1,2]),im2.shape[:2])
# alpha for triangle
alpha = warp.alpha_for_triangle(tp2,im2.shape[0],im2.shape[1])
im4 = (1-alpha)*im3 + alpha*im1_t
subplot(144)
imshow(im4)
axis('off')
show()

2.实现如图:


对于其中的这一行代码tp = array([[240,520,520,240],[600,620,1200,1230],[1,1,1,1]])
前面的[240,520,520,240]是四个角点的纵坐标,[600,620,1200,1230]是横坐标,四个角点坐标顺序是左上角[600,240],左下角[620,520],右下角[1200,520],右上角[1230,240]。最后四个1就表示四个角点的透明度为不透明,以此实现图像的完全覆盖。修改数据展示如下:
修改数据为tp = array([[120,260,260,120],[16,16,305,305],[1,1,1,1]]) 时候:

修改数据为tp = array([[120,520,520,120],[600,620,1200,1230],[1,1,1,1]]) 时候:

3.其中warp.py的代码如下

from scipy.spatial import Delaunay
from scipy import ndimage
from pylab import *
from numpy import *from PCV.geometry import homographydef image_in_image(im1,im2,tp):""" Put im1 in im2 with an affine transformationsuch that corners are as close to tp as possible.tp are homogeneous and counter-clockwise from top left. """ # points to warp fromm,n = im1.shape[:2]fp = array([[0,m,m,0],[0,0,n,n],[1,1,1,1]])# compute affine transform and applyH = homography.Haffine_from_points(tp,fp)im1_t = ndimage.affine_transform(im1,H[:2,:2],(H[0,2],H[1,2]),im2.shape[:2])alpha = (im1_t > 0)return (1-alpha)*im2 + alpha*im1_tdef combine_images(im1,im2,alpha):""" Blend two images with weights as in alpha. """return (1-alpha)*im1 + alpha*im2    def alpha_for_triangle(points,m,n):""" Creates alpha map of size (m,n) for a triangle with corners defined by points(given in normalized homogeneous coordinates). """alpha = zeros((m,n))for i in range(min(points[0]),max(points[0])):for j in range(min(points[1]),max(points[1])):x = linalg.solve(points,[i,j,1])if min(x) > 0: #all coefficients positivealpha[i,j] = 1return alphadef triangulate_points(x,y):""" Delaunay triangulation of 2D points. """tri = Delaunay(np.c_[x,y]).simplicesreturn tridef plot_mesh(x,y,tri):""" Plot triangles. """ for t in tri:t_ext = [t[0], t[1], t[2], t[0]] # add first point to endplot(x[t_ext],y[t_ext],'r')def pw_affine(fromim,toim,fp,tp,tri):""" Warp triangular patches from an image.fromim = image to warp toim = destination imagefp = from points in hom. coordinatestp = to points in hom.  coordinatestri = triangulation. """im = toim.copy()# check if image is grayscale or coloris_color = len(fromim.shape) == 3# create image to warp to (needed if iterate colors)im_t = zeros(im.shape, 'uint8') for t in tri:# compute affine transformationH = homography.Haffine_from_points(tp[:,t],fp[:,t])if is_color:for col in range(fromim.shape[2]):im_t[:,:,col] = ndimage.affine_transform(fromim[:,:,col],H[:2,:2],(H[0,2],H[1,2]),im.shape[:2])else:im_t = ndimage.affine_transform(fromim,H[:2,:2],(H[0,2],H[1,2]),im.shape[:2])# alpha for trianglealpha = alpha_for_triangle(tp[:,t],im.shape[0],im.shape[1])# add triangle to imageim[alpha>0] = im_t[alpha>0]return imdef panorama(H,fromim,toim,padding=2400,delta=2400):""" Create horizontal panorama by blending two images using a homography H (preferably estimated using RANSAC).The result is an image with the same height as toim. 'padding' specifies number of fill pixels and 'delta' additional translation. """ # check if images are grayscale or coloris_color = len(fromim.shape) == 3# homography transformation for geometric_transform()def transf(p):p2 = dot(H,[p[0],p[1],1])return (p2[0]/p2[2],p2[1]/p2[2])if H[1,2]<0: # fromim is to the rightprint ('warp - right')# transform fromimif is_color:# pad the destination image with zeros to the righttoim_t = hstack((toim,zeros((toim.shape[0],padding,3))))fromim_t = zeros((toim.shape[0],toim.shape[1]+padding,toim.shape[2]))for col in range(3):fromim_t[:,:,col] = ndimage.geometric_transform(fromim[:,:,col],transf,(toim.shape[0],toim.shape[1]+padding))else:# pad the destination image with zeros to the righttoim_t = hstack((toim,zeros((toim.shape[0],padding))))fromim_t = ndimage.geometric_transform(fromim,transf,(toim.shape[0],toim.shape[1]+padding)) else:print ('warp - left')# add translation to compensate for padding to the leftH_delta = array([[1,0,0],[0,1,-delta],[0,0,1]])H = dot(H,H_delta)# transform fromimif is_color:# pad the destination image with zeros to the lefttoim_t = hstack((zeros((toim.shape[0],padding,3)),toim))fromim_t = zeros((toim.shape[0],toim.shape[1]+padding,toim.shape[2]))for col in range(3):fromim_t[:,:,col] = ndimage.geometric_transform(fromim[:,:,col],transf,(toim.shape[0],toim.shape[1]+padding))else:# pad the destination image with zeros to the lefttoim_t = hstack((zeros((toim.shape[0],padding)),toim))fromim_t = ndimage.geometric_transform(fromim,transf,(toim.shape[0],toim.shape[1]+padding))# blend and return (put fromim above toim)if is_color:# all non black pixelsalpha = ((fromim_t[:,:,0] * fromim_t[:,:,1] * fromim_t[:,:,2] ) > 0)for col in range(3):toim_t[:,:,col] = fromim_t[:,:,col]*alpha + toim_t[:,:,col]*(1-alpha)else:alpha = (fromim_t > 0)toim_t = fromim_t*alpha + toim_t*(1-alpha)return toim_t

计算机视觉中的图像扭曲相关推荐

  1. 计算机视觉中的图像标注工具总结

    本文来自公众号CV技术指南资源分享系列 创建高质量的数据集是任何机器学习项目的关键部分.在实践中,这通常比实际训练和超参数优化花费的时间更长.因此,选择合适的标注工具至关重要.在这里,我们总结了一些用 ...

  2. 计算机视觉中常见图像扰动方法的Pytorch实现

    和另一种说法数据增强(data augmentation)十分类似.主要区别在于,数据增强是训练时进行的,用于提升模型的鲁棒程度:而这里说的扰动指测试时进行的,用于评估已训练模型的鲁棒程度,因此并不需 ...

  3. CV:计算机视觉技术之图像基础知识(一)—以python的cv2库来了解计算机视觉图像基础(傅里叶变换-频域-时域/各种滤波器-线性-非线性-均值-中值-高斯-双边)

    CV:计算机视觉技术之图像基础知识(一)-以python的cv2库来了解计算机视觉图像基础(傅里叶变换-频域-时域/各种滤波器-线性-非线性-均值-中值-高斯-双边) 目录 一.图像中的傅里叶变换 1 ...

  4. 【图像分割】基于计算机视觉实现视网膜图像中的血管分割附matlab代码

    1 简介 视网膜图像里的血管是可以被观察到的一类微血管,并且它是无创伤的,而其分布位置也属于深度部位[5].其分布.结构和形态特征的变化能在一定程度上反映病变的程度.而白血病.糖尿病以及高血压等疾病都 ...

  5. 计算机视觉技术在图像特征提取中的应用研究,彩色图像特征提取研究(一)

    彩色图像特征提取研究(一) 彩色图像特征提取研究 徐红霞 摘要:以普通的彩色图像为例,介绍了对彩色图像特征提取的原理.其具体过程分为原图像的预处理.图像信息分析.图像的特征提取,然后用MATLAB实现 ...

  6. matlab 通过矩阵变换使图像旋转平移_28. 图像扭曲

    本文同步发表在我的微信公众号"计算摄影学",欢迎扫码关注 你有没有想过,下面这个视频中的特效应该如何实现? Morphing Female Starshttps://www.zhi ...

  7. 计算机视觉中头部姿态估计的研究综述--Head Pose Estimation in Computer Vision - A Survey

    计算机视觉中头部姿态估计的研究综述 埃里克.莫非,IEEE的初级会员 默罕 马努拜特里维迪,IEEE高级会员 摘要---让计算机视觉系统作为一个普通人拥有识别另一个人的头部姿势的能力这一想法的提出,对 ...

  8. python计算机视觉编程——立体图像之计算视差图

    计算视差图 一.立体图像 1.1概念 1.2关于图像配准算法 二.立体重建之计算视差图 2.1归一化及算法概念 2.2匹配流程 三.实验测试 3.1实验要求 3.2实验代码 3.3实验结果分析 3.4 ...

  9. 计算机视觉-深度学习图像检测方法梳理

    计算机视觉-深度学习图像检测方法梳理 由于之后要转方向啦,趁这段时间整理手中硕士研究方向的一些阅读笔记,这是一篇关于计算机视觉的基础知识梳理 先搞清一些小知识点 首先我们要弄清楚图像分类.目标定位.语 ...

  10. 三、【python计算机视觉编程】图像到图像的映射

    图像到图像的映射 (一)单应性变换 (1)直接线性变换算法(DLT) (2)仿射变换(affine) (二)图像扭曲 (1)图像中的图像 (2)分段仿射扭曲 (3)图像配准 (三)创建全景图 (1)R ...

最新文章

  1. python语言自学-如何自学python语言
  2. fzu 2139 久违的月赛之二
  3. MYSQL:explain分析
  4. 用宝塔本地搭建php,Windows系统如何使用宝塔面板一键快速搭建本地服务器环境(LNMP/LAMP)...
  5. Python面向对象编程 __init__方法
  6. U-Boot移植(8)u-boot的流程
  7. maven jdk 8 Error while generating Javadoc 或者 Error while creating archive
  8. mysql调用时附加依赖项_c# – 无法加载“MySql.Data”或其中一个依赖项.
  9. 戴尔PowerEdge 4路服务器全面升级 实现企业应用与核心业务工作负载的优异性能...
  10. 专业FTP服务器Rumpus for Mac
  11. view绘制流程学习心得
  12. 幸运抽奖java_java10幸运抽奖
  13. Linux中如何设置静态IP和动态ip设定
  14. NeurIPS2020 Generalized Focal Loss论文翻译
  15. Craw the data of the web page and parse to pdf
  16. 内网穿透基础概念---内网外网
  17. 【JY】有限单元分析的常见问题及单元选择
  18. Object 的 equal() 、hashCode()方法说起
  19. CCF认证-201812-2-小明放学(C语言实现)
  20. 高手帮忙,菜鸟提问关于Query的基础问题!很急!就剩这些分了!好心人帮忙!

热门文章

  1. java 中facade_Java设计模式之Facade模式
  2. [渝粤教育] 西南科技大学 行政法学与行政诉讼法学 在线考试复习资料(1)
  3. 用c语言实现打印日历
  4. 面试必备:消息队列原理和选型(荣耀典藏版)
  5. python 判断字符串是否为空
  6. av终结者特征及防治方法
  7. 微信小程序开发教程:WeUI一个专为微信小程序设计的UI框架
  8. 基于stm32 ESP8266WiFi模块的基本通信
  9. matlab2016以上进行多体动力学不能用joint actuator驱动旋转关节,要用simulink-PS converter
  10. Spotfire 连接mysql数据库