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

opencv3

文章目录

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

1.图像加权和

即算上每幅图像的权重

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

2.图像加密与解密

通过按位异或

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

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

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

import cv2
import numpy as np
lena = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG",0)
cv2.imshow("lena", lena)
cv2.waitKey()
print(lena)
r, c = lena.shape
key = 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 cv2
import 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[:, :]>0
watermark[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[:, :]>0
wm[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 cv2
import numpy as np
img = 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 cv2
import numpy as np
img = 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 cv2
lena = 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 cv2
lena = 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 cv2
import numpy as np
img = np.random.randint(0,256,size=[500,500],dtype=np.uint8)
min = 100
max = 200
mask = cv2.inRange(img, min,max)
cv2.imshow("1",img)
cv2.imshow("2",mask)
cv2.waitKey()
cv2.destroyAllWindows()

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

import cv2
import numpy as np
opencv = 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 cv2
img = 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 cv2
img = 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]]])

你可能感兴趣的:(#,OpenCV函数详解,opencv,计算机视觉,python,算法)