算术运算
逻辑运算
1.像素相加
import cv2 as cv
import numpy as np
def add_demo(m1, m2):
dst = cv.add(m1, m2) # 将图像m1和图像m2的像素相加
cv.imshow("add_demo", dst)
src1 = cv.imread("C:/Users/ASUS/Desktop/11/LinuxLogo.jpg") # 从目录中读取图片
src2 = cv.imread("C:/Users/ASUS/Desktop/11/WindowsLogo.jpg")
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.namedWindow("image2", cv.WINDOW_AUTOSIZE)
cv.imshow("image1", src1) # 在窗口中将图片显示出来,通过名字“src”找到图片
cv.imshow("image2", src2)
add_demo(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
def subtract_demo(m1, m2):
dst = cv.subtract(m1, m2) # 将图像m1和图像m2的像素相加
cv.imshow("subtract_demo", dst)
src1 = cv.imread("C:/Users/ASUS/Desktop/11/LinuxLogo.jpg") # 从目录中读取图片
src2 = cv.imread("C:/Users/ASUS/Desktop/11/WindowsLogo.jpg")
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.namedWindow("image2", cv.WINDOW_AUTOSIZE)
cv.imshow("image1", src1) # 在窗口中将图片显示出来,通过名字“src”找到图片
cv.imshow("image2", src2)
subtract_demo(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
def divide_demo(m1, m2):
dst = cv.divide(m1, m2) # 将图像m1和图像m2的像素相加
cv.imshow("divide_demo", dst)
src1 = cv.imread("C:/Users/ASUS/Desktop/11/LinuxLogo.jpg") # 从目录中读取图片
src2 = cv.imread("C:/Users/ASUS/Desktop/11/WindowsLogo.jpg")
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.namedWindow("image2", cv.WINDOW_AUTOSIZE)
cv.imshow("image1", src1) # 在窗口中将图片显示出来,通过名字“src”找到图片
cv.imshow("image2", src2)
divide_demo(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
def multiply_demo(m1, m2):
dst = cv.multiply(m1, m2) # 将图像m1和图像m2的像素相加
cv.imshow("divide_demo", dst)
src1 = cv.imread("C:/Users/ASUS/Desktop/11/LinuxLogo.jpg") # 从目录中读取图片
src2 = cv.imread("C:/Users/ASUS/Desktop/11/WindowsLogo.jpg")
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.namedWindow("image2", cv.WINDOW_AUTOSIZE)
cv.imshow("image1", src1) # 在窗口中将图片显示出来,通过名字“src”找到图片
cv.imshow("image2", src2)
multiply_demo(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
def others(m1, m2): # 计算每个通道的均值
M1 = cv.mean(m1)
M2 = cv.mean(m2)
print(M1)
print(M2)
src1 = cv.imread("C:/Users/ASUS/Desktop/11/LinuxLogo.jpg") # 从目录中读取图片
src2 = cv.imread("C:/Users/ASUS/Desktop/11/WindowsLogo.jpg")
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.namedWindow("image2", cv.WINDOW_AUTOSIZE)
cv.imshow("image1", src1) # 在窗口中将图片显示出来,通过名字“src”找到图片
cv.imshow("image2", src2)
others(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()
输出
三个通道分别对应:blue green red
(15.0128125, 15.0128125, 15.0128125, 0.0)
(128.05269531250002, 109.60858072916668, 62.55748697916667, 0.0)
6.计算图像均值和方差
import cv2 as cv
import numpy as np
def others(m1, m2): # 计算每个通道的均值
M1, dev1 = cv.meanStdDev(m1)
M2, dev2 = cv.meanStdDev(m2)
print(M1)
print(M2)
print(dev1)
print(dev2)
src1 = cv.imread("C:/Users/ASUS/Desktop/11/LinuxLogo.jpg") # 从目录中读取图片
src2 = cv.imread("C:/Users/ASUS/Desktop/11/WindowsLogo.jpg")
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.namedWindow("image2", cv.WINDOW_AUTOSIZE)
cv.imshow("image1", src1) # 在窗口中将图片显示出来,通过名字“src”找到图片
cv.imshow("image2", src2)
others(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()
输出
[[15.0128125]
[15.0128125]
[15.0128125]]
[[128.05269531]
[109.60858073]
[ 62.55748698]]
[[58.14062149]
[58.14062149]
[58.14062149]]
[[54.60093646]
[45.52335089]
[50.01800277]]