image - 用非整数因子重新缩放图像的最佳方法是什么?
问题描述
我正在开发一个项目,该项目使用在 MNIST 数据集上训练的神经网络来识别图像上的多个数字。第一步是检测使用 CCL 算法制作的二进制图像上的数字。但问题是所有检测到的数字都应使用抗锯齿技术进行尺寸标准化,以适应 20x20 像素框,同时保持其纵横比 ( http://yann.lecun.com/exdb/mnist/ )。那么,我该如何解决这个问题呢?
干杯。
解决方案
我在以下链接上找到了使用双线性插值调整灰度大小的有用代码:http ://tech-algorithm.com/articles/bilinear-image-scaling/ 调整图像大小后,长边为 20 像素长,图像根据数字的质心以28x28图像为中心。用于计算应居中的图像质心的python代码:
centroid = np.ones(2)
summ = 0
img2 = np.array(img2)
for i in range (img2.shape[0]):
for j in range (img2.shape[1]):
if img2[i][j]:
summ+=img2[i][j]
centroid[0]+=i*img2[i][j]
centroid[1]+=j*img2[i][j]
centroid /= summ
centroid = np.rint(centroid)
推荐阅读
- python - 绘制鼠标移动 Python
- python - 根据生成的列数从数据帧写入 csv 列
- node.js - 用 pg 构建长 SQL 语句 - nodeJS
- sql-server - 如何将行转换为列
- css - 缺少 Wordpress 子主题 css
- orientdb - ODatabasePool 与 OPartitionedDatabasePool 没有关系
- html - HTML 中的 Unicode 规范化:Firefox 与 Chrome
- javascript - 在谷歌可视化线上绘制 x 轴日期
- windows - 如何在 Windows 上启用 postgresql 自动备份?
- python - 将属性分配给 Scrapy 中的错误项目