欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

OpenCV函数用法详解21~30,含代码示例,可直接运行

时间:2023-04-23
opencv3 文章目录

opencv3

1.图像加权和2.图像加密与解密3.数字水印4.色彩空间类型转换5.dst = cv2.inRange(src, lowerb, upperb)6.将指定颜色查找出来,然后利用基于掩码的按位与运算将指定颜色提取出来7.alpha通道,表示透明度。RGBA色彩空间,有四个通道8.缩放9.翻转10.仿射 1.图像加权和

即算上每幅图像的权重

import cv2import numpy as npimg1 = np.ones((3,4), dtype=np.uint8)*100# 生成一个3*4的大小,元索值都是100的二维数组img2 = np.ones((3,4), dtype=np.uint8)*10gamma = 3 # 亮度调节参数img3 = cv2.addWeighted(img1, 0.6, img2, 5,gamma)print(img3)

2.图像加密与解密

通过按位异或

运算加密过程:将明文a与密钥b进行按位异或,完成加密,得到密文c。

解密过程:将密文c与密钥b进行按位异或,完成解密,得到明文a。

示例:随机生成一副图像作为密钥

import cv2import numpy as nplena = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG",0)cv2.imshow("lena", lena)cv2.waitKey()print(lena)r, c = lena.shapekey = np.random.randint(0,256,size=[r,c],dtype=np.uint8)encryption = cv2.bitwise_xor(lena, key)decryption = cv2.bitwise_xor(encryption,key)cv2.imshow("lena", lena)cv2.imshow("key",key)cv2.imshow("encryption",encryption)cv2.imshow("decryption",decryption)cv2.waitKey()cv2.destroyAllWindows()

3.数字水印

将载体图像的最低有效位层替换为当前需要隐藏的二值图像,从而实现将二值图像隐藏的目的。

import cv2import numpy as np# 读取原始载体图像lena = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp",0)# 读取水印图像watermark = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp", 0)# 将水印图像内的值255处理为1,以方便嵌入w = watermark[:, :]>0watermark[w] = 1# 读取原始载体图像是shape值r, c = lena.shape# =================嵌入过程===============# 生成元素值都是254的数组t254 = np.ones((r, c), dtype=np.uint8)*254# 获取lena图像的高七位lenaH7 = cv2.bitwise_and(lena, t254)# 将watermark嵌入lenaH7内e = cv2.bitwise_or(lenaH7, watermark)# =================提取过程===============# 生成元素值都是1的数组t1 = np.ones((r, c), dtype=np.uint8)# 从载体图像内提取水印图像wm = cv2.bitwise_and(e, t1)print(wm)# 将水印图像内的值1 处理为255,以方便显示w = wm[:, :]>0wm[w] = 255# ==================显示================cv2.imshow("lena", lena)cv2.imshow("watermark", watermark*255)cv2.imshow("e", e)cv2.imshow("wm", wm)cv2.waitKey()cv2.destroyAllWindows()

4.色彩空间类型转换

将图像从一个色彩空间转换到另一个色彩空间。例如,将RGB色彩空间和HSV色彩空间之间进行转换

cv2.cvtColor()

代码示例

import cv2import numpy as npimg = np.random.randint(0, 256,size=[2, 4, 3], dtype=np.uint8)rst = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)print("img = n", img)print("rst = n", rst)print("像素点(1,0)直接计算得到的值=",img[1,0,0]*0.114+img[1,0,1]*0.587+img[1,0,2]*0.299)print("像素点(1,0)使用公式cv2.cvtColor()转化值=" ,rst[1,0])

将灰度图像转化为BGR图像

import cv2import numpy as npimg = np.random.randint(0, 256,size=[20, 40],dtype=np.uint8)rst = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)cv2.imshow("i",img)cv2.imshow("r",rst)cv2.waitKey()cv2.destroyAllWindows()

将图像在BGR模式和灰度图像之间相互转化

import cv2lena = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp")gray = cv2.cvtColor(lena, cv2.COLOR_BGR2GRAY)rgb = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)# ===============show shape================print("lena.shape=", lena.shape)print("gray.shape=", gray.shape)print("rgb.shape=", rgb.shape)# ================show img=================cv2.imshow("lena", lena)cv2.imshow("gray", gray)cv2.imshow("rgb", rgb)cv2.waitKey()cv2.destroyAllWindows()

将图像从BGR转化到RGB模式

import cv2lena = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp")rgb = cv2.cvtColor(lena, cv2.COLOR_BGR2RGB)cv2.imshow("lena", lena)cv2.imshow("rgb", rgb)cv2.waitKey()cv2.destroyAllWindows()

5.dst = cv2.inRange(src, lowerb, upperb)

dst:表示输出结果

src:表示要检查的数组或图像

lowerb:表示范围下界

upperb:表示范围上届

(如果,src值处于该指定区间内,则dst中对应位置上的值为255,否则为0)将某图像在[100, 200]内的值标记出来

import cv2import numpy as npimg = np.random.randint(0,256,size=[500,500],dtype=np.uint8)min = 100max = 200mask = cv2.inRange(img, min,max)cv2.imshow("1",img)cv2.imshow("2",mask)cv2.waitKey()cv2.destroyAllWindows()

6.将指定颜色查找出来,然后利用基于掩码的按位与运算将指定颜色提取出来

import cv2import numpy as npopencv = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG")hsv = cv2.cvtColor(opencv,cv2.COLOR_BGR2HSV)cv2.imshow("opencv", opencv)# =====================指定蓝色值的范围====================minBlue = np.array([110, 50, 50])maxBlue = np.array([130, 255, 255])# 确定蓝色区域mask = cv2.inRange(hsv, minBlue, maxBlue)# 通过掩码控制的按位与运算,锁定蓝色区域blue = cv2.bitwise_and(opencv,opencv, mask=mask)cv2.imshow("blue", blue)# ===================指定绿色值的范围======================minGreen = np.array([50, 50, 50])maxGreen = np.array([70, 255, 255])# 确定绿色区域mask = cv2.inRange(hsv,minGreen, maxGreen)# 通过掩码控制的按位与运算,锁定绿色区域green = cv2.bitwise_and(opencv, opencv, mask=mask)cv2.imshow("green",green)# =================指定红色值的范围=======================minRed = np.array([0, 50, 50])maxRed = np.array([30, 255, 255])# 确定红色区域mask = cv2.inRange(hsv, minRed, maxRed)# 通过掩码控制的按位与运算,锁定红色区域red = cv2.bitwise_and(opencv, opencv, mask=mask)cv2.imshow("red", red)cv2.waitKey()cv2.destroyAllWindows()

7.alpha通道,表示透明度。RGBA色彩空间,有四个通道 8.缩放

#dst = cv2.resize(scr, dsize[, fx[, fy[, interpolation]]])import cv2img = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp")rows, cols = img.shape[: 2]size = (int(cols*0.4), int(rows*0.4))rst = cv2.resize(img, size)cv2.imshow("img", img)cv2.imshow("rst", rst)cv2.waitKey()cv2.destroyAllWindows()

9.翻转

#dst = cv2.flip(src, flipCode)#(旋转类型就是绕着x,y(镜像)还是xy轴旋转)import cv2img = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp")x = cv2.flip(img, 0)y = cv2.flip(img, 1)xy = cv2.flip(img, -1)cv2.imshow("img", img)cv2.imshow("x", x)cv2.imshow("y", y)cv2.imshow("xy", xy)cv2.waitKey()cv2.destroyAllWindows()

10.仿射

实现平移、旋转,其保持平直性和平行性

#cv2.warpAffine()#dst(x,y) = src(M11x+M12y+M13, M21x+M22y+M23)#dst = cv2.warpAffine(src(原图像), M, dsize[, flags[, borderMode[, borderValue]]])

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。