Python+OpenCV(四)——像素运算
学习视频:python+opencv3.3视频教学 基础入门
其他学习记录:
Python+OpenCV(一)——基础操作
Python+OpenCV(二)——Numpy模块
Python+OpenCV(三)——色彩空间
源码如下:
# -*- coding = utf-8 -*-
# @Time : 2021/7/31 16:00
# @Author : 西兰花
# @File : OpenCV02.py
# @Software : PyCharm
"""
像素运算
"""
import cv2 as cv
import numpy as np
def arithmetic_demo(m1, m2): # 像素的四则运算
dst = cv.add(m1, m2) # 像素相加
cv.imshow("add_demo", dst)
dst = cv.subtract(m1, m2) # 像素相减
cv.imshow("subtract_demo", dst)
dst = cv.divide(m1, m2) # 像素相除
cv.imshow("divide_demo", dst)
dst = cv.multiply(m1, m2) # 像素相乘
cv.imshow("multiply_demo", dst)
def logic_demo(m1, m2): # 像素的逻辑运算
dst = cv.bitwise_and(m1, m2) # 像素“和”运算
cv.imshow("bitwise_and", dst)
dst = cv.bitwise_or(m1, m2) # 像素“或”运算
cv.imshow("bitwise_or", dst)
image = cv.imread("C:/Users/Administrator/Pictures/PS/3.jpg")
cv.imshow("image_3.jpg----before", image)
image = cv.bitwise_not(image) # 像素取反运算
cv.imshow("image_3.jpg----after", image)
def contrast_brightness_demo(image, c, b): # 对比度、亮度调节
h, w, c = image.shape
blank = np.zeros([h, w, c], image.dtype)
dst = cv.addWeighted(image, c, blank, 1-c, b)
cv.imshow("contrast_brightness_demo——after", dst)
def others(m1, m2):
M1, dev1 = cv.meanStdDev(m1) # 计算矩阵的均值和标准偏差
M2, dev2 = cv.meanStdDev(m2)
print(M1)
print(M2)
print(dev1)
print(dev2)
print("------ Hello OpenCV ------")
# src1 = cv.imread("C:/Users/Administrator/Pictures/PS/2.jpg") # 读取图像
# src2 = cv.imread("C:/Users/Administrator/Pictures/PS/8.png")
# print(src1.shape)
# print(src2.shape)
# cv.imshow("image1", src1) # 显示图像
# cv.imshow("image2", src2)
# others(src1, src2)
# arithmetic_demo(src1, src2) #像素的四则运算
# logic_demo(src1, src2) #像素的逻辑运算
src3 = cv.imread("C:/Users/Administrator/Pictures/PS/1.jpg")
cv.imshow("contrast_brightness_demo——before", src3)
contrast_brightness_demo(src3, 3, 50) # 对比度、亮度调节
cv.waitKey(0)
cv.destroyAllWindows() # 销毁/关闭所有窗口
输出结果:
-
像素的四则运算
①像素相加②像素相减
③像素相除
④像素相乘
-
像素的逻辑运算
①逻辑“和”、 ②逻辑“或”③逻辑“取反”
-
计算图像各通道的平均值、标准偏差