OpenCV基础知识入门
OpenCV
先决条件
https://code.visualstudio.com/download
env\scripts\activate
激活环境,你会在C:\Users\username\Desktop\opencv
之前看到小括号(env)出现。我们会在本文中涵盖7个主题
读,写和显示图像
Example
img = imread('images/dog0.jpg')
Example
imshow('Dog Image',img)
filename: A string representing the file name. The filename must include image format like .jpg, .png, etc.
image: It is the image that is to be saved.
Example
cv2.imwrite('images/img',img)
读取视频并与网络摄像头集成
Example
video = cv2.VideoCapture('video/dog/dog.mp4')
success , img = cap.read()
cv2.imshow('Video',img)
if cv2.waitKey(1) & 0xff==ord('q'):##key 'q' will break the loop
break
cap.set(3,640) ## Frame width
cap.set(4,480) ## Frame Height
cap.set(10,100) ## Brightness
while True:
success, img = cap.read()
cv2.imshow('Video',img)
if cv2.waitKey(1) & 0xff == ord('q'):
break
调整大小和裁剪图像
IMG: image which we want to resize
WIDTH: new width of the resize image
HEIGHT: new height of the resize image
Example
cv2.resize(img,(224,224))
shape
来找到任何图像的形状,然后根据图像形状,可以增加或减小图像的大小。让我们看看示例。img = cv2.imread('images/img0.jpg') ##Choose any image
print(img.shape)
imgResize = cv2.resize(img,(224,224)) ##Decrease size
imgResize2 = cv2.resize(img,(1024,1024)) ##Increase size
cv2.imshow('Image',img)
cv2.imshow('Image Resize',imgResize)
cv2.imshow('Image Increase size',imgResize2)
print(imgResize.shape)
cv2.waitKey(0)
img = cv2.imread('images/img0.jpg') ##Choose any image
print(img.shape)
shape = img.shape
imgResize = cv2.resize(img,(shape[0]//2,shape[1]//2))##Decrease size
imgResize2 = cv2.resize(img,(shape[0]*2,shape[1]*2)) ##Increase size
cv2.imshow('Image',img)
cv2.imshow('Image Resize',imgResize)
cv2.imshow('Image Increase size',imgResize2)
print(imgResize.shape)
cv2.waitKey(0)
(x1,y1): top-left vertex
(x2,y2): bottom-right vertex
Example
imgCropped = img[0:100,200:200]
img = cv2.imread('images/img0.jpg')
imgCropped = img[50:250,120:330]
cv2.imshow('Image cropped',imgCropped)
cv2.imshow('Image',img)
cv2.waitKey(0)
基本的图像过滤器使用的函数
cvtColor
,这里我们将cv2.COLOR_BGR2GRAY
作为参数传递。IMG: Original image
CODE: Conversion code for Gray(COLOR_BGR2GRAY)
Example
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cvtColor
,这里我们将cv2.COLOR_BGR2HSV
作为参数传递。它主要用于对象跟踪。IMG: Original image
CODE: Conversion code for Gray(COLOR_BGR2HSV)
Example
imgHsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
kernalsize − A Size object representing the size of the kernel.
sigmaX − A variable representing the Gaussian kernel standard deviation in X direction.
sigmaY - same as sigmaX
Exmaple
imgBlur = cv2.GaussianBlur(img,(3,3),0)
threshold1,threshold2:Different values of threshold different for every images
Example
imgCanny = cv2.Canny(img,100,150)
imgDialation = cv2.dilate(imgCanny,kernel,iterations=1) ##DIALATION
imgDialation = cv2.erode(imgCanny,kernel,iterations=1) ##EROSION
绘制不同的形状
w: width
h: height
x: distance from x axis
y: distance from y axis
R,G,B: color in RGB form (255,255,0)
THICKNESS: thickness of rectangel(integer)
Example
cv2.rectangle(img,(100,300),(200,300),(255,0,255),2)
x: distance from x axis
y: distance from y axis
radius: size of radius(integer)
R,G,B: color in RGB form (255,255,0)
THICKNESS: thickness of rectangel(integer)
Example
cv2.circle(img,(200,130),90,(255,255,0),2)
x1,y1: start point of line (integer)
x2,y2: end point of line (integer)
R,G,B: color in RGB form (255,255,0)
THICKNESS: thickness of rectangel(integer)
Example
cv2.line(img,(110,260),(300,260),(0,255,0),3)
在图像上书写文字
img: image to put text on
text: text to put on image
X: text distance from X axis
Y: text distance from Y axis
FONT: Type of FONT (ALL FONT TYPES)
FONT_SCALE: Scale of Font(Integer)
R,G,B: color in RGB form (255,255,0)
THICKNESS: thickness of rectangel(integer)
Example
cv2.putText(img,'HELLO',(120,250),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
检测并裁剪脸部
https://github.com/opencv/opencv/tree/master/data/haarcascades
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('images/img0.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Cropping Face
crop_face = img[y:y + h, x:x + w]
#Saving Cropped Face
cv2.imwrite(str(w) + str(h) + '_faces.jpg', crop_face)
cv2.imshow('img', img)
cv2.imshow('imgcropped',crop_face)
cv2.waitKey()
赞 (0)