目录
1、显示/保存图像 获取图像信息/单通道图像/合并三通道
2、读取摄像头
3、翻转图像,用API接口速度快 30ms 逐像素翻转
4、提取不同颜色区域 颜色空间转换
5、图像加减乘除
6、泛洪填充
void nameWindow(const string& winname,int flags = WINDOW_AUTOSIZE) ;
参数1:新建的窗口的名称。自己随便取。
参数2:窗口的标识,一般默认为WINDOW_AUTOSIZE 。
WINDOW_AUTOSIZE 窗口大小自动适应图片大小,并且不可手动更改。(上面图1就是使用的它)
WINDOW_NORMAL 用户可以改变这个窗口大小(上面图2就是使用的它)
WINDOW_OPENGL 窗口创建的时候会支持OpenGL
import cv2
import numpy as np
#-----------------------显示/保存图像 获取图像信息 获取单通道图像 合并三通道 --------------------------
def get_img_info(img):
print(type(img))
print(img.shape)
print(img.size)
print(img.dtype)
print(np.array(img))
def channle_img(img):
b,g,r=cv2.split(img)
cv2.imshow('blue',b)
cv2.imshow('green',g)
cv2.imshow('red',r)
dst=cv2.merge([b,g,r])
dst[:,:,2]=255
cv2.imshow('changed img',dst)
if __name__ == '__main__':
src=cv2.imread('../opencv-python-img/lena.png')
cv2.namedWindow("input img",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input img",src)
get_img_info(src)
gray=cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
cv2.imwrite('test.jpg',gray)
channle_img(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
#-----------------------读取摄像头 ------------------------------
def video_demo():
capture=cv2.VideoCapture(0)
while(True):
ret,frame = capture.read()
frame=cv2.flip(frame,1)#翻转以y轴 镜像变换 1水平翻转 0 垂直翻转 -1 水平垂直翻转
cv2.imshow("video",frame)
key=cv2.waitKey(50)
if c==27:
break
if __name__ == '__main__':
video_demo()
#----------------------翻转图像,用API接口速度快 30ms 逐像素翻转1500ms--------------------
def access_pixels(img):#逐像素翻转图像
height=img.shape[0]
weight=img.shape[1]
channels=img.shape[2]
for channel in range(channels):
for row in range(height):
for col in range(weight):
pix=img[row,col,channel]
img[row,col,channel]=255-pix
cv2.imshow('pix',img)
def inverse(img):#API翻转图像
dst=cv2.bitwise_not(img)
cv2.imshow("inverse demo",dst)
def create_img():
img=np.zeros([400,400,3],np.uint8)
img[:,:,1]=np.ones([400,400])*255
cv2.imshow("new img",img)
img=np.ones([400,400,1],np.uint8)
img=img*0
print(img.shape)
cv2.imshow("new image",img)
m1=np.ones([3,3],np.uint8)
m1.fill(12.233)
print(m1)
m2=m1.reshape([1,9])
print(m2)
m3=np.array([[2,3,4],[4,5,6],[7,8,9]],np.int32)
print(m3)
if __name__ == '__main__':
src=cv2.imread('../opencv-python-img/lena.png')
cv2.namedWindow("input img",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input img",src)
t1=cv2.getTickCount()
#access_pixels(src)
#inverse(src)
create_img()
t2=cv2.getTickCount()
time=(t2-t1)/cv2.getTickFrequency()
print("time:%s ms"%(time*1000))
cv2.waitKey(0)
cv2.destroyAllWindows()
#---------------------提取不同颜色区域 颜色空间转换---------------------
def extrace_object_demo(path):
capture=cv2.VideoCapture(path)
while (True):
ret,frame=capture.read()
if ret==False:
break
hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
lower_hsv=np.array([20,20,20])
upper_hsv=np.array([200,200,200])
mask=cv2.inRange(hsv,lowerb=lower_hsv,upperb=upper_hsv)
#实现图像二值化,即检查数组元素是否在另外两个数组元素值之间。
#lowerb---upperb函数会将两个区域间的值置为255,区间外的值置为0
#比如想要看到青色的区域处于图像中的什么位置,青色的区域是[78,43,46],[99,255,255]
dst=cv2.bitwise_and(frame,frame,mask=mask)#像素相加
cv2.imshow("video",frame)
cv2.imshow('mask',dst)
key=cv2.waitKey(40)
if key==27:
break
def color_space_demo(img):
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
yuv=cv2.cvtColor(img,cv2.COLOR_BGR2YUV)
Ycrcb=cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb)
cv2.imshow('gray',gray)
cv2.imshow('hsv',hsv)
cv2.imshow('yuv',yuv)
cv2.imshow('ycrcb',Ycrcb)
cv2.waitKey(0)
if __name__ == '__main__':
path='../opencv-python-img/dinosaur.avi'
extrace_object_demo(path)
src=cv2.imread('../opencv-python-img/lena.png')
color_space_demo(src)
#---------------图像加减乘除-----------
def add_demo(m1,m2):
dst=cv2.add(m1,m2)
cv2.imshow("add_demo",dst)
def substract_demo(m1,m2):
dst=cv2.subtract(m1,m2)
cv2.imshow("substract",dst)
def multi_demo(m1,m2):
dst=cv2.multiply(m1,m2)
cv2.imshow("multiply",dst)
def divide_demo(m1,m2):
dst=cv2.divide(m1,m2)
cv2.imshow("divide",dst)
def logic_demo(m1,m2):
dst1=cv2.bitwise_and(m1,m2)
dst2=cv2.bitwise_or(m1,m2)
dst3=cv2.bitwise_not(m1)
cv2.imshow('dst1',dst1)
cv2.imshow('dst2',dst2)
cv2.imshow('dst3',dst3)
def constrast_brightness_demo(img,c,b):
h,w,c=img.shape
blank=np.zeros([h,w,c],img.dtype)
dst=cv2.addWeighted(img,c,blank,1-c,b)
#图像叠加or图像混合加权实现
#图像尺寸相同、类型相同
#cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst
#dst=src1 * alpha + src2 * beta + gamma;
cv2.imshow("con-bri-demo",dst)
def other(m1,m2):
M1,dev1=cv2.meanStdDev(m1)
M2,dev2=cv2.meanStdDev(m2)
h,w=m1.shape[:2]
print("M1",M1)#输出三个值,一个通道一个值
print("M2",M2)
print("dev1",dev1)
print("dev2",dev2)
im=np.zeros([h,w],np.uint8)
m,dev=cv2.meanStdDev(im)
print(m,dev)
if __name__ == '__main__':
src1=cv2.imread('../opencv-python-img/lena.png')
src2=cv2.imread('../opencv-python-img/test.jpg')
cv2.imshow("src1",src1)
cv2.imshow("src2",src2)
add_demo(src1,src2)
substract_demo(src1,src2)
multi_demo(src1,src2)
divide_demo(src1,src2)
other(src1,src2)
logic_demo(src1,src2)
constrast_brightness_demo(src1,1.5,0)
cv2.waitKey(0)
#----------------------------泛洪填充--------------------
#填充一个图像内部区域
#- FLOODFILL_FIXED_RANGE – 改变图像,泛洪填充
#- FLOODFILL_MASK_ONLY – 不改变图像、只填充遮罩层本身、忽略新的颜色值参数
def fill_color_demo(img):
copyImg=img.copy()
h,w=img.shape[:2]
mask=np.zeros([h+2,w+2],np.uint8)
cv2.floodFill(copyImg,mask,(30,30),(0,255,255),(100,100,100),(50,50,50),cv2.FLOODFILL_FIXED_RANGE)
#(30,30)种子点的位置
#(100,100,100)以种子点像素三通道值[b,g,r]为基准,被填充范围为原图三通道最低值为[b-100,g-100,r-100]
#(50,50,50)以种子点像素三通道值[b,g,r]为基准,被填充范围为原图三通道最高值为[b+50,g+50,r+50]
#所以在原图像只有像素三通道值[b-100,g-100,r-100]<=[b,g,r]<=[b+50,g+50,r+50],在此范围内的都会被指定(0,255,255)即黄色填充
#cv.FLOODFILL_FIXED_RANGE:待处理的像素点与种子点作比较,在范围之内,则填充此像素 。(改变图像)
cv2.imshow("fill_color_demo",copyImg)
def fill_binary():
image=np.zeros([400,400,3],np.uint8)
image[100:300,100:300,:]=255
cv2.imshow("fill_binary",image)
mask=np.ones([402,402,1],np.uint8)
mask[101:301,101:301]=0
cv2.floodFill(image,mask,(200,200),(0,23,255),cv2.FLOODFILL_MASK_ONLY)
#当flag为FLOORFILL_MAK_ONLY时,只会填充mask中数值为0的区域。
cv2.imshow("filled binary",image)
if __name__ == '__main__':
src=cv2.imread('../opencv-python-img/lena.png')
fill_color_demo(src)
fill_binary()
cv2.waitKey(0)