1、加减运算的例子
图像的加减乘除运算运算在很多地方用到,比如说,在一幅杂论的图中减去背景,比如说运动检测。比如说多个检测的合成。
#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#图像相加
def img_add(img1,img2):
img_result = cv.add(img1,img2)
return img_result
#图像相减
def img_sub(img1,img2):
img_result = cv.subtract(img1,img2)
return img_result
#图像相乘
def img_mul(img1,img2):
img_result = cv.multiply(img1,img2)
return img_result
#图像相除
def img_div(img1,img2):
img_result = cv.divide(img1,img2)
return img_result
#获取图像均值和方差
def img_mean_dev(img):
mean , dev = cv.meanStdDev(img)
print("mean: %s , dev: %s"%(mean,dev))
#取与运算
def img_logic_and(img1,img2):
img_result = cv.bitwise_and(img1,img2)
return img_result
#取或运算
def img_logic_or(img1,img2):
img_result = cv.bitwise_or(img1,img2)
return img_result
#取反操作,也就是反色
def img_logic_not(img):
img_result = cv.bitwise_not(img)
return img_result
def img_test():
img1 = cv.imread('E:/chenopencvblogimg/lena150W200H.jpg')
img2 = cv.imread('E:/chenopencvblogimg/chen150W200H.bmp')
#判断是否读取成功
if img1 is None or img2 is None:
print("Could not read the image,may be path error")
return
#图像原图
cv.namedWindow("origin img1",cv.WINDOW_NORMAL)
cv.imshow("origin img1",img1)
cv.namedWindow("origin img2",cv.WINDOW_NORMAL)
cv.imshow("origin img2",img2)
#图像相加
img = img_add(img1,img2)
cv.namedWindow("img_add",cv.WINDOW_NORMAL)
cv.imshow("img_add",img)
#图像相减
img = img_sub(img1,img2)
cv.namedWindow("img_sub",cv.WINDOW_NORMAL)
cv.imshow("img_sub",img)
#图像相乘
img = img_mul(img1,img2)
cv.namedWindow("img_mul",cv.WINDOW_NORMAL)
cv.imshow("img_mul",img)
#图像相除
img = img_div(img1,img2)
cv.namedWindow("img_div",cv.WINDOW_NORMAL)
cv.imshow("img_div",img)
#取与运算
img = img_logic_and(img1,img2)
cv.namedWindow("img_logic_and",cv.WINDOW_NORMAL)
cv.imshow("img_logic_and",img)
#取或运算
img = img_logic_or(img1,img2)
cv.namedWindow("img_logic_or",cv.WINDOW_NORMAL)
cv.imshow("img_logic_or",img)
#取反操作,也就是反色
img = img_logic_not(img1)
cv.namedWindow("img_logic_not1",cv.WINDOW_NORMAL)
cv.imshow("img_logic_not1",img)
img = img_logic_not(img2)
cv.namedWindow("img_logic_not2",cv.WINDOW_NORMAL)
cv.imshow("img_logic_not2",img)
#获取图像均值和方差
img_mean_dev(img1)
img_mean_dev(img2)
#让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
cv.waitKey(0)
#销毁窗口
cv.destroyAllWindows()
if __name__ == '__main__':
sys.exit(img_test() or 0)
2、基于位预算获取魔方蓝色的图
在第7个教程的基础上修改一句语句即可
用到了函数bitwise_and,定义如下
#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#颜色空间测试
def color_space_detect(img):
#转换为hsv
hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)
#黄色检测
yellow_lower_hsv = np.array([26,43,46])
yellow_uper_hsv = np.array([34,255,255])
yellowmask = cv.inRange(hsv,lowerb=yellow_lower_hsv,upperb=yellow_uper_hsv)
cv.namedWindow("yellow detected",cv.WINDOW_NORMAL)
cv.imshow("yellow detected",yellowmask)
#蓝色检测
blue_lower_hsv = np.array([100,43,46])
blue_uper_hsv = np.array([124,255,255])
bluewmask = cv.inRange(hsv,lowerb=blue_lower_hsv,upperb=blue_uper_hsv)
#教程8添加的语句
showblueimg = cv.bitwise_and(img,img,mask=bluewmask)
cv.namedWindow("blue detected",cv.WINDOW_NORMAL)
cv.imshow("blue detected",bluewmask)
#显示蓝色的区域
cv.namedWindow("showblueimg",cv.WINDOW_NORMAL)
cv.imshow("showblueimg",showblueimg)
#颜色空间测试
def color_space_test(img):
#转换为gray
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.namedWindow("gray",cv.WINDOW_NORMAL)
cv.imshow("gray",gray)
#转换为hsv
hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)
cv.namedWindow("hsv",cv.WINDOW_NORMAL)
cv.imshow("hsv",hsv)
#转换为yuv
yuv = cv.cvtColor(img,cv.COLOR_BGR2YUV)
cv.namedWindow("yuv",cv.WINDOW_NORMAL)
cv.imshow("yuv",yuv)
#转换为ycrcb
ycrcb = cv.cvtColor(img,cv.COLOR_BGR2YCrCb)
cv.namedWindow("ycrcb",cv.WINDOW_NORMAL)
cv.imshow("ycrcb",ycrcb)
def img_test():
img = cv.imread('E:/chenopencvblogimg/mofan.jpg')
#判断是否读取成功
if img is None:
print("Could not read the image,may be path error")
return
cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
cv.imshow("origin Pic",img)
#检测颜色
color_space_detect(img)
#让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
cv.waitKey(0)
#销毁窗口
cv.destroyAllWindows()
if __name__ == '__main__':
sys.exit(img_test() or 0)