h5响应式网站做动画,绿色推广盒子app,怎么用wordpress写文章,wordpress去掉google字体目录
介绍
本机环境
安装
常用函数
使用方法
图像平移
图像缩放
图像旋转
骨架提取
通道转换
OPenCV版本的检测
综合测试 目录
介绍
本机环境
安装
常用函数
使用方法
图像平移
图像缩放
图像旋转
骨架提取
通道转换
OPenCV版本的检测 介绍 imutils 是一…目录
介绍
本机环境
安装
常用函数
使用方法
图像平移
图像缩放
图像旋转
骨架提取
通道转换
OPenCV版本的检测
综合测试 目录
介绍
本机环境
安装
常用函数
使用方法
图像平移
图像缩放
图像旋转
骨架提取
通道转换
OPenCV版本的检测 介绍 imutils 是一个用于图像处理和计算机视觉任务的 Python 工具包。它提供了一系列方便实用的函数可以简化常见的图像处理任务imutils 库的发展始于 2015 年作者是 Adrian Rosebrock。实际上imutils是在OPenCV基础上的一个简单封装从而达到更为简结的调用OPenCV接口的目的来轻松的实现图像的平移旋转缩放骨架化等一系列的操作。
本机环境
windows10 64位 企业版
python 3.6.8X64
imutils 0.5.3
opencv-python3.4.2.16 库文件讲解及下载地址https://github.com/PyImageSearch/imutils
安装
pip install imutils 在安装前应确认已安装numpy,scipy,matplotlib和opencv如果出现缺失包错误可以使用下面安装命令,会把所有包安装
pip install NumPy SciPy opencv-python matplotlib imutils
如果安装速度过慢可以使用 国内镜像连接下载来提高速度
pip install imutils -i https://pypi.tuna.tsinghua.edu.cn/simple
常用函数
1.resize(image, widthNone, heightNone, intercv2.INTER_LINEAR)调整图像大小。可以通过指定 width 或 height 来设置新的图像尺寸也可以同时指定两者。inter 参数用于指定插值方法默认为 cv2.INTER_LINEAR。
2.rotate(image, angle)旋转图像。angle 参数指定旋转角度正值表示逆时针旋转负值表示顺时针旋转。
3.translate(image, x, y)平移图像。image参数是要移动的图像x 和 y 参数指定在 x 和 y 轴上的平移距离。
4.grab_contours(cnts)解决 OpenCV 版本兼容性问题的函数用于从 cv2.findContours() 返回的结果中提取轮廓。
5.rotate_bound(image, angle)安全地旋转图像确保旋转后的图像完整。
6.auto_canny(image, sigma0.33)自动计算 Canny 边缘检测的阈值。sigma 参数用于控制阈值的高低。
7.is_cv2() 和 is_cv3()用于检测当前使用的 OpenCV 版本。
8.in_range(image, lower, upper)将图像中的像素值限制在给定的范围内。 这些函数可以组合使用以便进行更复杂的图像处理任务。例如可以使用 resize() 函数将图像调整为指定大小然后使用 rotate() 函数对图像进行旋转最后使用 translate() 函数平移图像。
使用方法
图像平移 OpenCV中也提供了图像平移的实现要先计算平移矩阵然后利用仿射变换实现平移在imutils中可直接进行图像的平移相对于原来的cv使用imutiles可以直接指定平移的像素不用构造平移矩阵。
import numpy as np
import cv2 as cv
import imutils
import matplotlib.pyplot as plt
plt.rcParams[font.sans-serif][SimHei] #用来正常显示中文标签
plt.rcParams[axes.unicode_minus]False #用来正常显示负号img cv.imread(image1.jpg) # 更改图片地址
translated imutils.translate(img,100,50) # 平移函数plt.figure()
plt.subplot(121)
plt.imshow(img[:,:,::-1]) # img[:,:,::-1]转换是为了转回RGB格式这样才可以正常显示彩色图像
plt.title(原图)
plt.subplot(122)
plt.imshow(translated[:,:,::-1])
plt.title(平移结果)
plt.show()
图像缩放 图片的缩放在OPenCV中要注意确保保持宽高比。而在imutils中自动保持原有图片的宽高比只指定宽度weight和height即可缩放函数imutils.resize(img,width100)
import numpy as np
import cv2 as cv
import imutils
import matplotlib.pyplot as plt
plt.rcParams[font.sans-serif][SimHei] #用来正常显示中文标签
plt.rcParams[axes.unicode_minus]False #用来正常显示负号img cv.imread(image1.jpg)
# 说明一般如果有width参数就会安装width参数进行缩放不会理会height参数如果两个参数不成比例也是安装width进行缩放
resized imutils.resize(img,width100) # 指定宽度会自动计算相应比例高度还有参数heightprint(原图大小,img.shape)
print(缩放后大小, resized.shape)
plt.figure()
plt.subplot(121)
plt.imshow(img[:,:,::-1])
plt.title(原图)
plt.subplot(122)
plt.imshow(resized[:,:,::-1])
plt.title(缩放图)
plt.show()
图像旋转 在OpenCV中进行旋转时使用的是仿射变换在这里图像旋转方法是imutils.rotate()跟2个参数第一个是图片数据第二个是旋转的角度旋转是朝逆时针方向。同时imutils还提供了另一个相似的方法, rotate_round()它就是按顺时针旋转的。
import numpy as np
import cv2 as cv
import imutils
import matplotlib.pyplot as plt
plt.rcParams[font.sans-serif][SimHei] #用来正常显示中文标签
plt.rcParams[axes.unicode_minus]False #用来正常显示负号image cv.imread(image1.jpg)
# 逆时针旋转
rotated imutils.rotate(image, 90)
# 顺时针旋转
rotated_round imutils.rotate_bound(image, 90) # 画图
plt.figure(figsize[10, 10])
plt.subplot(1,3,1)
plt.imshow(img[:,:,::-1])
plt.title(原图)
plt.axis(off)
plt.subplot(1,3,2)
plt.imshow(rotated[:,:,::-1])
plt.title(逆时针旋转90度)
plt.axis(off)
plt.subplot(1,3,3)
plt.imshow(rotated_round[:,:,::-1])
plt.title(顺时针旋转90度)
plt.axis(off)
plt.show()
骨架提取 骨架提取边缘提取是指对图片中的物体进行拓扑骨架(topological skeleton)构建的过程imutils提供的方法是skeletonize()第二个参数是结构参数的尺寸(structuring element)相当于是一个粒度越小需要处理的时间越长。注意不是所有图片都能求出骨架
import cv2 as cv
import imutils
import matplotlib.pyplot as plt
plt.rcParams[font.sans-serif][SimHei] #用来正常显示中文标签
plt.rcParams[axes.unicode_minus]False #用来正常显示负号# 1 图像读取
image2 cv.imread(earth.png)
# 2 灰度化
gray cv.cvtColor(image2, cv.COLOR_BGR2GRAY)
# 3 骨架提取
skeleton imutils.skeletonize(gray, size(7, 7))# 4 图像展示
plt.figure()
plt.subplot(121),plt.imshow(image2[:,:,::-1]),plt.title(原图)plt.subplot(122),plt.imshow(skeleton,cmapgray),plt.title(骨架提取结果) # 显示灰度图要声明 grayplt.show()
通道转换
在OpenCV的Python绑定中图像以BGR顺序表示为NumPy数组。使用该cv2.imshow功能时效果很好。但是如果打算使用Matplotlib该plt.imshow函数将假定图像按RGB顺序排列。调用cv2.cvtColor解决此问题也可以使用opencv2matplotlib便捷功能。
img cv.imread(lion.jpeg)
plt.figure()
plt.imshow(imutils.opencv2matplotlib(img))
OPenCV版本的检测 OpenCV 4发布之后随着主要版本的更新向后兼容性问题尤为突出。在使用OPenCV时应检查当前正在使用哪个版本的OpenCV然后使用适当的函数或方法。在imutils中的is_cv2()、is_cv3()和is_cv4()是可用于自动确定当前环境的OpenCV的版本简单的功能。
print(OPenCV版本: {}.format(cv2.__version__))
综合测试 融合以上函数通过修改代码中的flag对应的不同数值可以得到不同的函数演示效果。代码和素材下载地址如下https://mp.csdn.net/mp_download/manage/download/UpDetailed
import numpy as np
import cv2
import imutilsif __name__ __main__:img cv2.imread(./image/apple.png)logo cv2.imread(./image/3.png)flag 5if flag 0:# 把dir路径下的所有图片名称变成一个列表,支持dir文件夹下多个子文件夹图片名称提取from imutils import pathsdir r.\imageimagePaths list(paths.list_images(dir))print(imagePaths)if flag 1:#查看imutils的相关信息print(dir(imutils))if flag 2:#图像旋转for angle in range(0,360,90): #rotate the image and display itrotated_im imutils.rotate(img,angleangle)cv2.imshow(Angle%d % (angle),rotated_im)cv2.waitKey()cv2.destroyAllWindows()if flag 3:# 图片缩放for width in (400,300,200,100):# resize the image and display itresized imutils.resize(img,widthwidth)cv2.imshow(Width%dpx%(width),resized)cv2.waitKey()cv2.destroyAllWindows()if flag 4:#图像平移# translate the image x25 pixels to the right and y 75 pixels uptranslated imutils.translate(img,25,-75)cv2.imshow(translate, translated)cv2.waitKey()cv2.destroyAllWindows()if flag 5:#白图像黑背景画出图像轮廓结构#skeletonize the imagegray cv2.cvtColor(logo,cv2.COLOR_BGR2GRAY)skeleton imutils.skeletonize(gray,size(3,3))cv2.imshow(Skeleton,skeleton)cv2.waitKey()cv2.destroyAllWindows()