OpenCV笔记+简单案例(基于Python)
2021/10/4 20:42:53
本文主要是介绍OpenCV笔记+简单案例(基于Python),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
学习时,将每块注释分别去掉即为一个模块知识点
图像数字化
import numpy as np """ #二维的ndarry #两行四列全是0的uchar类型的二维数组 z = np.zeros((2,4),np.uint8) print(type(z)) print(z) print("\n") #两行四列全是1的整型矩阵 o = np.ones((2,4),np.int32) print(o) print("\n") #浮点型矩阵 m = np.array([[4,12,3,1],[10,12,14,29]],np.float32) print(m) print("\n") """ """ #二维的ndarry #2×2×4的32为浮点型数组 m = np.array([ [[1,2,3,4],[5,6,7,8]], [[10,11,12,14],[15,16,17,18]] ],np.float32) print(m) """ """ #ndarry成员变量 m = np.array([[4,12,3,1],[10,12,14,29]],np.float32) #m的尺寸 print(m.shape) #m的数据类型 print(m.dtype) """ """ #访问ndarry中的值 #访问二维ndarry中的值 m = np.array([[14,12,3,1],[10,12,114,29],[67,23,534,2]],np.float32) #从第0行第0列开始,:表示整个,左开右闭 print(m) print(m[1,3])#第1行第3列 print(m[2,:])#整个第2行 print(m[:,3])#整个第3列 print(m[0:2,1:3])#第0行到第2行 ∩ 第1列和第3列的 """ """ #访问三维ndarry中的值 m = np.array([ [[1,2,3,4],[5,6,7,8]], [[10,11,12,14],[15,16,17,18]], [[11,12,43,32],[1,5,10,23]] ],dtype=float) print(m) #所有二维数组的第0列 print(m[:,:,0]) #第0个二维数组 print(m[0,:,:]) """ ''' Mat(Size(int cols,int rows),int type) 其中cols列(宽) rows行(高) type类型,包括通道数及其数据类型 CV_8UC(n) CV_16SC(n) CV_16UC(n) CV_32SC(n) CV_32FC(n) CV_64FC(n) 8U、8S、16S、16U、32S、32F、64F代表一个数值所占的bit数,1byte=8bit,都应该除以8再看 1U、1S、2S、2U、4S、4F、8F uchar、S为int、32F为4字节的float,64F为8字节的double C(n)表示通道数,n=1即二维矩阵,n=2即三维矩阵... ''' """ #ndarray的加法 src1 = np.array([[23,123,90],[100,250,0]],np.uint8) src2 = np.array([[125,150,60],[100,10,40]],np.uint8) print(src1) print(src2) dst = src1 + src2 print(dst) print(dst.dtype) #仔细看123+150=273,最后的结果为273%255-1=17 """ ''' src1 = np.array([[23,123,90],[100,250,0]],np.uint8) src2 = np.array([[125,150,60],[100,10,40]],np.uint8) dst = cv2.add(src1,src2,dtype=cv2.CV_32F) print(dst) ''' """ #ndarry的减法 src1 = np.array([[23,123,90],[100,250,0]],np.uint8) src2 = np.array([[125,150,60],[100,10,40]],np.uint8) dst = src1 - src2 print(dst) #23-125=-102 -102%255+1=154 """ """ #ndarray的点乘 src1 = np.array([[23,123,90],[100,250,0]],np.uint8) src2 = np.array([[125,150,60],[100,10,40]],np.uint8) dst = src1 * src2 print(dst) dst1 =np.multiply(src1,src2) print(dst1) """ """ #ndarray的点除 src1 = np.array([[23,123,90],[100,250,0]],np.uint8) src2 = np.array([[125,150,60],[100,10,40]],np.uint8) src3 = np.array([[23,123,90],[100,250,0]],np.float32) dst = src2 / src1 dst1 = src2 / src3 print(dst) print(dst1) #Numpy在处理分母为0的时候,如果两个ndarray都是uint8类型则返回0,其他情况返回inf """ """ #ndarray的乘法 src3 = np.array([[1,2,3],[4,5,6]],np.uint8) src4 = np.array([[6,5],[4,3],[2,1]],np.uint8) dst = np.dot(src3,src4) print(dst) """ """ #ndarray的指数和对数运算 src5 = np.array([[6,5],[4,3]],np.uint8) dst2 = np.log(src5) dst3 = np.exp(src5) print(dst2) print(dst2.dtype) print(dst3) print(dst3.dtype) #log对数,exp指数,矩阵类型只能是CV_32F或者CF_64F """ """ #ndarray的幂指数和开平方运算 src = np.array([[25,40],[10,100]],np.uint8) dst1 = np.power(src,2) print(dst1) print(dst1.dtype) dst2 = np.power(src,2.0) print(dst2) print(dst2.dtype) #power中指数是2和2.0的时候返回值会不同,精度不同,这点要注意 """
例子
加载一个灰度图(E:\Python-workspace\yanyu.png),显示图片
按下’s’键保存(beyond.png)(保存后的路径和该程序所在路径一致)后退出,或者按下 ESC 键退出不保存
import cv2 img = cv2.imread('E:\Python-workspace/yanyu.png',0)#读取该灰度照片0,彩色照片1 cv2.imshow("image",img)#显示该照片 k = cv2.waitKey(0)#获取所按的键盘反馈 if k == 27:#是Esc cv2.destoryAllWindows()#退出 elif k == ord('s'):#是's' cv2.imwrite("beyond.png",img)#保存该照片 cv2.destroyAllWindows()#退出
对照片(E:\Python-workspace\OpenCV/water1.png)进行边缘检测,并将边缘化的照片进行保存
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('E:\Python-workspace\OpenCV/water1.png',1) edges = cv2.Canny(img,100,200) plt.subplot(121),plt.imshow(img,cmap = 'gray') plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(edges,cmap = 'gray') plt.title('Edge Image'), plt.xticks([]), plt.yticks([]) plt.show() cv2.imwrite("beyond.png",edges)#保存照片 cv2.destroyAllWindows()#退出
找取图片上的25个特征点
import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('water1.png') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) corners = cv2.goodFeaturesToTrack(gray,25,0.01,10) # 返回的结果是 [[ 311., 250.]] 两层括号的数组。 corners = np.int0(corners) for i in corners: x,y = i.ravel() cv2.circle(img,(x,y),3,255,-1) plt.imshow(img),plt.show()
显示照片的直方图
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('water2.png',0) plt.hist(img.ravel(),256,[0,256]); plt.show()
将两照片进行加权混合对比(1.png和2.jpg的大小完全相同才行)
import cv2 import numpy as np img1=cv2.imread('E:\Python-workspace\OpenCV/1.png',0) img2=cv2.imread('E:\Python-workspace\OpenCV/2.jpg',0) Image=cv2.addWeighted(img1,0.7,img2,0.3,0) #img1的比重因子为0.7,img2的比重因子0.3,伽马为0 cv2.imshow('Image',Image) cv2.waitKey(0) cv2.destroyAllWindow()
比较俩照片的差异,返回数值
from PIL import Image import math import operator from functools import reduce def image_contrast(img1, img2): image1 = Image.open(img1) image2 = Image.open(img2) h1 = image1.histogram() h2 = image2.histogram() result = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) ) return result if __name__ == '__main__': img1 = "E:\Python-workspace\OpenCV/water1.png" # 指定图片路径 img2 = "E:\Python-workspace\OpenCV/water1.png" result = image_contrast(img1,img2) print(result) if result==0:print("OK") else: print("NO")
这篇关于OpenCV笔记+简单案例(基于Python)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享