图像基本操作
图像采集
import numpy as np
import cv2
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("D:/6.jpg",cv2.IMREAD_COLOR)
if img is None:
print("图像为空")
exit()
cv2.imwrite("D:/12.jpg",img)
print(img.shape)
print(img.type)
print(img.size)
print(img.dtype)
cv_show("img",img)
视频采集
import numpy as np
import cv2
vc = cv2.VideoCapture("D:/test.mp4")
if vc.isOpened():
open,frame = vc.read()
else:
open = False
while(open):
ret,frame = vc.read()
if frame is None:
break
if ret==True:
frame = cv2.flip(frame,1)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow("gray",gray)
if cv2.waitKey(10)&0xFF==27:
break
vc.release()
cv2.destroyAllWindows()
success0 = cameraCapture.grab()
success1 = cameraCapture.grab()
if success0 and success1:
frame0 = cameraCapture0.retrieve()
frame1 = cameraCapture1.retrieve()
获得图像的基本信息
采集视频图像并保存
import cv2
import numpy as np
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
vc = cv2.VideoCapture('D:/test.mp4')
if vc.isOpened():
open,frame = vc.read()
w = vc.get(cv2.CAP_PROP_FRAME_WIDTH)
h = vc.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(w)
print(h)
else:
open = False
index = 0
while(open):
ret,frame = vc.read()
if frame is None:
break
if ret == True:
frame = cv2.flip(frame,1)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imwrite("D:/img/" + str(index) + ".jpg", frame)
index += 1
if index > 10:
break
cv2.imshow('gray',gray)
if cv2.waitKey(10)&0xFF==ord('q'):
break
vc.release()
cv2.destroyAllWindows()
截取部分图像数据
import numpy as np
import cv2
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("D:/6.jpg")
plant = cv2.imread("D:/2.jpg")
local = img[0:50,0:200]
b,g,r = cv2.split(img)
img1 = cv2.merge((b,g,r))
cur_R = img.copy()
cur_R[:,:,0]=0
cur_R[:,:,1]=0
cv_show("R",cur_R)
cur_G = img.copy()
cur_G[:,:,0]=0
cur_G[:,:,2]=0
cv_show("G",cur_G)
cur_B = img.copy()
cur_B[:,:,1]=0
cur_B[:,:,2]=0
cv_show("B",cur_B)
边界填充
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)
数值计算
img3 = img+10
img4 = img+img3
cv2.add(img+img3)
cv_show("replicate",replicate)
cv_show("img1",img1)
cv_show("b",b)
cv_show("g",g)
cv_show("r",r)
cv_show("local",local)
图像融合
import numpy as np
import cv2
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("D:/6.jpg")
plant = cv2.imread("D:/4.jpg")
plant = cv2.resize(plant,(315,356))
print(img.shape)
print(plant.shape)
res = cv2.addWeighted(img,0.4,plant,0.6,0)
res1 = cv2.resize(img,(0,0),fx=3,fy=1)
cv_show("res1",res1)
cv_show("res",res)
import cv2
import numpy as np
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('D:/6.jpg')
plant = cv2.imread('D:4.jpg')
if img.shape or plant.shape is None:
print('could not load the pic..')
exit()
print(plant.shape)
plant = cv2.resize(plant,(img.shape[1],img.shape[0]))
res = cv2.addWeighted(img,0.4,plant,0.6,0)
res1 = cv2.resize(img,(0,0),fx=4,fy=1)
cv_show('res1',res1)
cv_show('res',res)
图像旋转
import cv2
import numpy as np
image = cv2.imread("E:/1.jpg")
img = cv2.rotate(image,cv2.ROTATE_90_COUNTERCLOCKWISE)
cv2.imshow("image",image)
cv2.imshow("img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
图像阈值处理
图像阈值
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("D:/6.jpg")
plant = cv2.imread("D:/4.jpg")
ret,src = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
cv_show("src",src)
图像对比度增强
src1 = cv2.convertScaleAbs(gray,2,34)
直方图正规化
import cv2
import numpy as np
image = cv2.imread("E:/2.jpeg")
Imax = np.max(image)
Imin = np.min(image)
Omin,Omax = 0,255
a = float(Omax-Omin)/(Imax-Imin)
b = Omin - a*Imin
O = a*image+b
O = O.astype(np.uint8)
cv2.imshow("img",image)
cv2.imshow("O",O)
cv2.waitKey(0)
cv2.destroyAllWindows()
伽马变换
import cv2
import numpy as np
image = cv2.imread("E:/2.jpeg")
fI = image/255.0
gamma = 0.5
O = np.power(fI,gamma)
cv2.imshow("img",image)
cv2.imshow("O",O)
cv2.waitKey(0)
cv2.destroyAllWindows()
图像滤波
import numpy as np
import cv2
def cv_show(image):
cv2.imshow("image",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("D:/6.jpg")
plant = cv2.imread("D:/4.jpg")
blur = cv2.blur(img,(3,3))
box = cv2.boxFilter(img,-1,(3,3),normalize=True)
aussian = cv2.GaussianBlur(img,(5,5),1)
median = cv2.medianBlur(img,5)
res= np.hstack((aussian,median))
cv2.imshow("res",res)
cv_show(blur)
cv_show(plant)
图像形态学处理
import numpy as np
import cv2
image = cv2.imread("D:/dige.png")
cv2.imshow("image",image)
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(image,kernel,iterations=1)
kernel1 = np.ones((5,5),np.uint8)
dilate = cv2.dilate(erosion,kernel1,iterations=1)
kernel2 = np.ones((11,11),np.uint8)
opening = cv2.morphologyEx(image,cv2.MORPH_OPEN,kernel2)
closing = cv2.morphologyEx(image,cv2.MORPH_CLOSE,kernel2)
gradient = cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kernel)
tophat = cv2.morphologyEx(image,cv2.MORPH_TOPHAT,kernel)
blackhat = cv2.morphologyEx(image,cv2.MORPH_BLACKHAT,kernel2)
res = np.hstack((erosion,dilate,opening))
res1 = np.hstack((tophat,blackhat))
res2 = np.hstack((gradient,closing))
cv2.imshow("res2",res2)
cv2.imshow('res1',res1)
cv2.imshow("res",res)
cv2.waitKey()
cv2.destroyAllWindows()
图像梯度计算
import numpy as np
import cv2
def cv_show(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("D:/6.jpg")
plant = cv2.imread("D:/4.jpg")
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
cv_show('sobelx',sobelx)
cv_show('sobelxy',sobelxy)
import numpy as np
import cv2
def cv_show(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("D:/6.jpg")
plant = cv2.imread("D:/4.jpg")
scharrx = cv2.Scharr(img,cv2.CV_64F,1,0)
scharry = cv2.Scharr(img,cv2.CV_64F,0,1)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.convertScaleAbs(scharry)
scharrxy = cv2.addWeighted(scharrx,0.5,scharry,0.5,0)
laplacian = cv2.Laplacian(img,cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
res = np.hstack((scharrxy,laplacian))
cv_show("res",res)
投影变换
import cv2
import numpy as np
image = cv2.imread("E:/1.jpg")
h,w,c = image.shape
src = np.array([[0,0],[w-1,0],[0,h-1],[w-1,h-1]],np.float32)
dst = np.array([[50,50],[w/3,50],[50,h-1],[w-1,h-1]],np.float32)
print(image.shape)
p = cv2.getPerspectiveTransform(src,dst)
r= cv2.warpPerspective(image,p,(w,h),borderValue=125)
cv2.imshow("img",image)
cv2.imshow("r",r)
cv2.waitKey(0)
cv2.destroyAllWindows()