import cv2
img = cv2.imread( )
imread函数有两参数,第一个参数为读取路径,第二个参数为读取形式(可不写,默认为读取彩色图片)
读取形式有三种
1、cv2.IMREAD_COLOR:加载彩色图片,这个是默认参数,可以直接写1
2、cv2.IMREAD_GRAYSCALE:以灰度模式加载图片,可以直接写0
3、cv2.IMREAD_UNCHANGED:包括alpha,可以直接写-1
cv2.imshow('显示窗口名称',img)
cv2.waitKey(0) #按任意键继续
cv2.cv.destroyAllWindows() #清楚缓存
cv2.imshow(filename,img)
filename为存储后图像的文件名,img为需要存储的图像
import cv2
img = cv2.imread('sample.jpg')
#截取部分图像区域
img = img[0:200,0:200]
#颜色通道提取
b,g,r = cv2.split(img)
#3通道合并
img = cv2.merge((b,g,r))
#只保留r通道
r_img = img.copy()
r_img[:,:,0]=0
r_img[:,:,1]=0
#只保留g通道
g_img = img.copy()
g_img[:,:,0]=0
g_img[:,:,2]=0
#只保留b通道
b_img = img.copy()
b_img[:,:,1]=0
b_img[:,:,2]=0
cv2.imshow('initial',img)
cv2.imshow('r',r_img)
cv2.imshow('g',g_img)
cv2.imshow('b',b_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
图片的shape属性包含三个数,分别为高度、宽度、通道数
图片的大小,以像素值表示
图像的数据类型
cap = cv.VideoCapture(0)
while 1:
ret,frame = cap.read()#从视频中读取一帧图片
cv.imshow('cap', frame)#显示读取到的图片
cv.waitKey(1)
#按退出键退出
if cv.getWindowProperty('cap', cv.WND_PROP_AUTOSIZE) < 1:
break
#按退出键退出
if 0xFF == 27:
break
cap.release()#关闭
cv.destroyAllWindows()#清楚缓存
cv.VideoCapture可以读取视频或者打开摄像头,读取视频时直接填写路径即可
import cv2
img = cv2.imread('sample.jpg')
top_size,bottom_size,left_size,right_size = (50,50,50,50)#图片四周填充的长度
replicate = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType = cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType = cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType = cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType = cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType = cv2.BORDER_CONSTANT,value = 0)
cv2.imshow('Original',img)
cv2.imshow('replicate',replicate)
cv2.imshow('reflect',reflect)
cv2.imshow('reflect101',reflect101)
cv2.imshow('wrap',wrap)
cv2.imshow('constant',constant)
cv2.waitKey(0)
cv2.destroyWindow()
BORDER_REPLICATE:复制法,也就是复制最边缘像素。
BORDER_REFLECT:反射法,对感兴趣的图像中的像素在两边进行复制
BORDER_REFLECT_101:反射法,也就是以最边缘像素为轴
BORDER_WRAP:外包装法,以图像的左边界与右边界相连,上下边界相连
BORDER_CONSTANT:常量法,需要填充value值,0表示黑框填充
import cv2
img = cv2.imread('sample.jpg')
image = cv2.imread('test.jpg')
print(img[0:5,0:5,0])
print(image[0:5,0:5,0])
#直接加10
img1 = img +10
image1 = image + 10
print(img1[0:5,0:5,0])
print(image1[0:5,0:5,0])
#两图像直接相加,大小需一样
img = cv2.resize(img,(image.shape[1],image.shape[0]))
result1 = img + image
print(result1[0:5,0:5,0])
#通过cv2.add相加,大小需一样
result2 = cv2.add(img,image)
print(result2[0:5,0:5,0])
如果直接相加,会报错
#图像融合
import cv2
image = cv2.imread('sample.jpg')
img = cv2.imread('test.jpg')
result = image + img
#图像融合
import cv2
image = cv2.imread('sample.jpg')
img = cv2.imread('test.jpg')
#将两张图片调整为大小一致
img = cv2.resize(img,(image.shape[1],image.shape[0]))
result = image + img
cv2.imshow('1',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
使用函数融合
#图像融合
import cv2
image = cv2.imread('sample.jpg')
img = cv2.imread('test.jpg')
#将两张图片调整为大小一致
img = cv2.resize(img,(image.shape[1],image.shape[0]))
result = cv2.addWeighted(image, 0.5, img, 0.5, 0)
cv2.imshow('1',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
ret,dst = cv2.threshold(src,thresh,maxval,type)
src:输入图,只能输入单通道图像,一般使用灰度图
dst:输出图
thresh:阈值
maxval:当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
type:二值化操作的类型,包含以下5中类型:
cv2.THRESH_BINARY:超过阈值部分取maxval(最大值),否则取0
cv2.THRESH_BINARY_INV:cv2.THRESH_BINARY的反转
cv2.THRESH_TRUNC:大于阈值的部分设为阈值,否则不变
cv2.THRESH_TOZERO:大于阈值的部分不变,否则设为0
cv2.THRESH_TOZERO_INV:cv2.THRESH_TOZERO的反转
import cv2
image = cv2.imread('sample.jpg')
img_Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret1,thresh1 = cv2.threshold(img_Gray, 127, 255, cv2.THRESH_BINARY)
ret2,thresh2 = cv2.threshold(img_Gray, 127, 255, cv2.THRESH_BINARY_INV)
ret3,thresh3 = cv2.threshold(img_Gray, 127, 255, cv2.THRESH_TRUNC)
ret4,thresh4 = cv2.threshold(img_Gray, 127, 255, cv2.THRESH_TOZERO)
ret5,thresh5 = cv2.threshold(img_Gray, 127, 255, cv2.THRESH_TOZERO_INV)
title = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'THRESH_TOZERO_INV']
images = [image, thresh1,thresh2, thresh3, thresh4, thresh5]
for i in range(6):
cv2.imshow(title[i],images[i])
cv2.waitKey(0)
cv2.destroyAllWindows()
生成椒盐噪声
import cv2
import numpy as np
#椒盐噪声生成函数
def noise(img,snr):
h=img.shape[0]
w=img.shape[1]
img1=img.copy()
sp=h*w # 计算图像像素点个数
NP=int(sp*(1-snr)) # 计算图像椒盐噪声点个数
for i in range (NP):
randx=np.random.randint(1,h-1) # 生成一个 1 至 h-1 之间的随机整数
randy=np.random.randint(1,w-1) # 生成一个 1 至 w-1 之间的随机整数
if np.random.random()<=0.5: # np.random.random()生成一个 0 至 1 之间的浮点数
img1[randx,randy]=0
else:
img1[randx,randy]=255
return img1
image=cv2.imread("sample.jpg")
img_gray=imGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
noiseimage=noise(img_gray,0.99) # 将信噪比设定为0.99
cv2.imshow("image",image)
cv2.imshow("img_gray",img_gray)
cv2.imshow("noiseimage",noiseimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
均值滤波
#均值滤波
#简单的平均卷积操作
blur = cv2.blur(noiseimage, (3, 3)) #3*3的卷积操作,一般为奇数
cv2.imshow('blur', blur)
方框滤波
#方框滤波
#基本和均值一样,可以选择归一化
box = cv2.boxFilter(noiseimage, -1, (3,3), normalize=True)
cv2.imshow('box', box)
#方框滤波
#基本和均值一样,不选择归一化,容易超阈值
box = cv2.boxFilter(noiseimage, -1, (3,3), normalize=False)
cv2.imshow('box', box)
高斯滤波
#高斯分布
#高斯模糊的卷积核里的数值是高斯分布,相当于更重视中间的
aussian = cv2.GaussianBlur(noiseimage, (5, 5), 1 )
cv2.imshow('aussian', aussian)
中值滤波
#中值滤波
#相当于用中值代替
median = cv2.medianBlur(noiseimage, 5)
cv2.imshow('median', median)
res = np.hstack((blur, aussian, median))
cv2.imshow('blur_aussian_median',res)