创建与原图片大小一致的图片,add/subtract/multiply/divide
import cv2 as cv
import numpy as np
def math_demo():
image = cv.imread("D:/opencv_image/touxiang.jpg")
cv.imshow("input",image)
h,w,c = image.shape
blank = np.zeros_like(image)#创建一个和image一样大的空白图片
blank[:,:] = (80,80,80)
result = cv.subtract(image,blank)#改变了图像亮度
cv.imshow("result",result)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
math_demo()
没有代码补全提示真的很容易拼错,所以去找了下怎么打开
import cv2 as cv
import numpy as np
def nothing(x):
print(x)
def adjust_lightness_demo():
image = cv.imread("D:/opencv_image/touxiang.jpg")
cv.namedWindow("input",cv.WINDOW_AUTOSIZE)
cv.createTrackbar("lightness","input",0,100,nothing)#最后那个是回调函数
cv.imshow("input",image)
blank = np.zeros_like(image)
while 1:
pos = cv.getTrackbarPos("lightness","input")
blank[:,:] = (pos,pos,pos)
result = cv.add(image,blank)
cv.imshow("result",result)
c = cv.waitKey(1)
if c == 27:
break
cv.destroyAllWindows()
if __name__ == "__main__":
adjust_lightness_demo()
import cv2 as cv
import numpy as np
def nothing(x):
print(x)
def adjust_contrast_demo():
image = cv.imread("D:/opencv_image/touxiang.jpg")
cv.namedWindow("input",cv.WINDOW_AUTOSIZE)
cv.createTrackbar("lightness","input",0,100,nothing)
cv.createTrackbar("contrast","input",100,200,nothing)
cv.imshow("input",image)
blank = np.zeros_like(image)
while 1:
light = cv.getTrackbarPos("lightness","input")
contrast = cv.getTrackbarPos("contrast","input")/100
print("light:",light,"contrast",contrast)
result = cv.addWeighted(image,contrast,blank,0.5,light)
cv.imshow("result",result)
c = cv.waitKey(1)
if c == 27:
break;
cv.destroyAllWindows()
if __name__ == "__main__":
adjust_contrast_demo()
import cv2 as cv
import numpy as np
def keys_demo():
image = cv.imread("D:/opencv_image/touxiang.jpg") # 这里换了一张图片
cv.namedWindow("input",cv.WINDOW_AUTOSIZE)
cv.imshow("input", image)
#cv.waitKey接受一个按键事件并返回按键的ASCII码
while True:
c = cv.waitKey(1)
if c == 49: #1
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
cv.imshow("result",gray)
if c == 50: #2
hsv = cv.cvtColor(image,cv.COLOR_BGR2HSV)
cv.imshow("result",hsv)
if c == 51: #3
invert = cv.bitwise_not(image)#对二进制数据进行“非”操作
cv.imshow("result",invert)
if c == 27:
break
cv.destroyAllWindows()#关闭所有由imshow产生的窗口
if __name__ == "__main__":
keys_demo()
import cv2 as cv
import numpy as np
#applyColorMap()函数进行图片彩色变换
def color_table_demo():
#opencv颜色表
colormap = [
cv.COLORMAP_AUTUMN,
cv.COLORMAP_BONE,
cv.COLORMAP_JET,
cv.COLORMAP_WINTER,
cv.COLORMAP_RAINBOW,
cv.COLORMAP_OCEAN,
cv.COLORMAP_SUMMER,
cv.COLORMAP_SPRING,
cv.COLORMAP_COOL,
cv.COLORMAP_PINK,
cv.COLORMAP_HOT,
cv.COLORMAP_PARULA,
cv.COLORMAP_MAGMA,
cv.COLORMAP_INFERNO,
cv.COLORMAP_PLASMA,
]
image = cv.imread("D:/opencv_image/touxiang.jpg")
cv.namedWindow("input",cv.WINDOW_AUTOSIZE)
cv.imshow("input",image)
index = 0
while True:
dst = cv.applyColorMap(image,colormap[index%15])
index += 1
cv.imshow("color style",dst)
c = cv.waitKey(400)
if c == 49:#按1停止
break;
cv.destroyAllWindows()
if __name__ == "__main__":
color_table_demo()
运行结果:每隔一小段时间实现一次颜色变换
即与或非的操作,必须对两张大小和通道数完全相同的图像这样操作才合法
import cv2 as cv
import numpy as np
#applyColorMap()函数进行图片彩色变换
def bitwise_demo():
b1 = np.zeros((400,400,3),dtype=np.uint8)
b1[:,:] = (255,0,255)
b2 = np.zeros((400,400,3),dtype=np.uint8)
b2[:,:] = (0,255,0)
cv.imshow("b1",b1)
cv.imshow("b2",b2)
dst1 = cv.bitwise_and(b1,b2)
dst2 = cv.bitwise_or(b1,b2)
cv.imshow("bitwise_and",dst1)
cv.imshow("bitwise_or",dst2)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
bitwise_demo()
运行结果: