opencv_python学习(五)----图像像素的算数操作
一、总结
前提:进行四则运算的两张图像大小需相同(H,W)相同 图像像素的加法可以提高图像的亮度 图像像素的减法可以降低图像的亮度 图像像素的乘法可以提高图像的对比度 图像像素的除法可以降低图像的对比度
二、算数操作
(1)加法
首先创建一个蒙版mask,使mask与image同大小,并给mask中所有像素赋值。
mask = np.zeros_like(image)
mask[:,:] = (50,50,50)
具体的demo如下:
def pixel_arithmetic_demo():
image = cv.imread('路径')
mask = np.zeros_like(image)
mask[:,:] = (50,50,50)
result = cv.add(image,mask)
cv.imshow('input',image)
cv.imshow('mask',mask)
cv.imshow('result',result)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
pixel_arithmetic_demo()
result
(2)减法
减法的demo
def pixel_arithmetic_demo():
image = cv.imread('D:\Desktop_data\Python_study\Opencv_tution\opencv_tutorial_data-master\images\objects.jpg')
mask = np.zeros_like(image)
mask[:,:] = (50,50,50)
result = cv.subtract(image,mask)
cv.imshow('input',image)
cv.imshow('mask',mask)
cv.imshow('result',result)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
pixel_arithmetic_demo()
(3)乘法
乘法的demo
def pixel_arithmetic_demo():
image = cv.imread('D:\Desktop_data\Python_study\Opencv_tution\opencv_tutorial_data-master\images\objects.jpg')
mask = np.zeros_like(image)
mask[:,:] = (2,2,2)
result = cv.multiply(image,mask)
cv.imshow('input',image)
cv.imshow('mask',mask)
cv.imshow('result',result)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
pixel_arithmetic_demo()
(4)除法
除法的demo
def pixel_arithmetic_demo():
image = cv.imread('D:\Desktop_data\Python_study\Opencv_tution\opencv_tutorial_data-master\images\objects.jpg')
mask = np.zeros_like(image)
mask[:,:] = (2,2,2)
result = cv.divide(image,mask)
cv.imshow('input',image)
cv.imshow('mask',mask)
cv.imshow('result',result)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
pixel_arithmetic_demo()
|