在《python计算机视觉》这本书的学习中,按照书中的代码敲完运行会发现这个报错:

运行代码如下:

#  Warp_Triangle_Image01.pyfrom pylab import *
import warp
from PIL import Image
from numpy import *# 打开图像,并将其扭曲
fromim = array(Image.open('sunset_tree.jpg'))
x, y = meshgrid(range(5), range(6))
x = (fromim.shape[1]/4) * x.flatten()
y = (fromim.shape[0]/5) * y.flatten()# 三角部分
tri = warp.triangulate_points(x, y)# 打开图像和目标点
im = array(Image.open('turningtorso1.jpg'))
tp = loadtxt('turningtorso1_points.txt')   # dextination points# 将点转换成齐次坐标
fp = vstack((y, x, ones((1, len(x)))))
tp = vstack((tp[:, 1], tp[:, 0], ones((1, len(tp)))))# 扭曲三角形
im = warp.pw_affine(fromim, im, fp, tp, tri)# 绘制图像
figure()
imshow(im)
warp.plot_mesh(tp[1], tp[0], tri)
axis('off')
show()
# warp.py
import homography
from scipy import ndimage
from scipy.spatial import Delaunay
from pylab import *
from numpy import *def image_in_image(im1, im2, tp):""" 使用仿射变换将im1放置在im2上,使用im1图像的角和tp尽可能的靠近tp是齐次表示的,并且是按照从左上角逆时针计算的"""# 扭曲的点m, n = im1.shape[:2]fp = array([[0, m, m, 0], [0, 0, n, n], [1, 1, 1, 1]])# 计算仿射变换,并且将其应用于图下你过im1H = 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 alpha_for_triangle(points, m, n):""" 对于带有由points定义角点的三角形,创建大小为(m,n)的alpha图(在归一化的齐次坐标意义下)"""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:alpha[i, j] = 1return alphadef triangulate_points(x, y):""" 二维点的Delaunay三角剖分 """# centers, edges, tri, neighbors = Delaunay(x, y)tri = Delaunay(np.c_[x, y]).simplicesreturn tridef pw_affine(fromim, toim, fp, tp, tri):""" 从一幅图像中扭曲矩形图模块fromim = 将要扭曲的图像toim = 目标图像fp = 齐次坐标表示下,扭曲前的点tp = 齐次坐标表示下,扭曲后的点tri = 三角剖分 """im = toim.copy()# 检查图像是灰度图像还是彩色图像is_color = len(fromim.shape) == 3# 创建扭曲后的图像(如果需要对彩色图像的每个颜色通道进行迭代操作,那么有必要这样做)im_t = zeros(im.shape, 'uint8')for t in tri:# 计算仿射变换H = 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])# 三角形的alphaalpha = alpha_for_triangle(tp[:, t], im.shape[0], im.shape[1])# 将三角形加入到图像中im[alpha > 0] = im_t[alpha > 0]return imdef plot_mesh(x, y, tri):""" 绘制三角形 """for t in tri:t_ext = [t[0], t[1], t[2], t[0]]   # 将第一个点加入到最后plot(x[t_ext], y[t_ext], 'r')

根据错误我们看到 warp.py 中的 alpha_for_triangle(points, m, n) 函数,报错说明min()和max()的参数是float64类型,应该将之转换成int类型。

 for i in range(min(points[0]), max(points[0])):for j in range(min(points[1]), max(points[1])):

修改后的代码如下所示:

  for i in range(int(min(points[0])), int(max(points[0]))):for j in range(int(min(points[1])), int(max(points[1]))):

修改后运行结果如下图所示:


----  今天不学习,明天变废物。 ----

《python计算机视觉》关于‘numpy.float64‘ object cannot be interpreted as an integer错误的解决办法相关推荐

  1. 训练数据出现TypeError: 'numpy.float64' object cannot be interpreted as an integer错误

    问题背景: 用tensorflow训练自己的数据的时候,训练一段时间后,出现TypeError: 'numpy.float64' object cannot be interpreted as an ...

  2. 'numpy.float64' object cannot be interpreted as an integer

    'numpy.float64' object cannot be interpreted as an integer fp=open(filename,'rb') blk_size = prod(di ...

  3. ‘numpy.float64‘ object cannot be interpreted as an integer

    能运行的代码: import numpy as npaaa=np.linspace(0.2, 1, 9)print(aaa) 报错的代码: import numpy as npccc=np.round ...

  4. Yolov6解决常见报错(1)TypeError numpy.float64 object cannot be interpreted as an index

    这几天偶然看见Yolov6出来,迫不及待的试了一下,结果看见网上评论说bug太多了,我作为使用者,想着积极做出贡献,把一些我解决的bug分享一下,方便大家也能看到顺便解决. 先看报错 Training ...

  5. frcnn系列错误: TypeError: 'numpy.float64' object cannot be interpreted as an index 的解决方案

    1.TypeError: 'numpy.float64' object cannot be interpreted as an index 的解决方案 看了很多博客都说要调整numpy版本到1.11. ...

  6. 生成浮点数列表:Python range():TypeError: ‘float‘ object cannot be interpreted as an integer

    生成浮点数列表:Python range():TypeError: 'float' object cannot be interpreted as an integer 目录 Python range ...

  7. 成功解决TypeError: 'float' object cannot be interpreted as an integer

    成功解决TypeError: 'float' object cannot be interpreted as an integer 目录 解决问题 解决思路 解决方法 解决问题 TypeError: ...

  8. ‘numpy.float64‘ object is not callable

    重点放最前面:检查函数名是否和变量名重复,或者被重新定义了. 我自己定义的一个函数如下. def rmspe(y, yhat):return np.sqrt(np.mean((yhat/y-1) ** ...

  9. 【python】解决TypeError: ‘str‘ object cannot be interpreted as an integer

    当用python的input输入一个数字时 其格式默认为string格式 所以要用int()转换为int格式 比如 import math def opgg():     # x = input(&q ...

最新文章

  1. 利用IIS作为宿主 发布你的WCF Service(转)
  2. 疫情之下,这些公司开始给员工发菜了!
  3. Apriori算法实例
  4. 2017-11-17 为Python添加中文关键字
  5. 无招胜有招之语言基础
  6. jpa 查询 列表_终极JPA查询和技巧列表–第1部分
  7. 双色球python十种算法_python : 蒙特卡罗算法 应用于双色球
  8. Spring,SpringMvc初始化监听配置
  9. 社区奖品之USB电动迷你碎纸机
  10. 【校招】SHL 的 General Ability (GA, 通用能力测试)
  11. usb转4路rs485、4路rs232原理图
  12. NOI题库练习1.4(08)
  13. 解析MATLAB短时傅里叶变换函数spectrogram()
  14. dnf剑魂buff等级上限_DNF:剑魂最强武器,比星之海伤害更高,无神话红10都能8000亿...
  15. android系统定制添加分辨率,density设置
  16. 螺旋传动设计系统lisp_螺旋传动的设计计算.pdf
  17. android 发广播屏蔽home键,如何在Android App中屏蔽(拦截)Home按键及其他按键
  18. 拦截器和过滤器的同异
  19. STM32F411核心板固件库开发(三) 按键检测
  20. STM32学习--低功耗

热门文章

  1. 积分兑换商城运营的出发点是什么
  2. 获取不同长度的UUID
  3. loraserver 源码解析 (四) lora-gateway-bridge
  4. EOJ 3346 皇后问题
  5. 功率谱,相位谱,频谱分析
  6. TensorFlow 编程模型
  7. php常用函数(第一版)
  8. 大结果集SQL引发的ClickHouse空闲超时
  9. 如何C#使用HMAC-SHA1算法
  10. 后端 Long类型,超过 js 的number类型最大值的解决办法