极市导读
本文涵盖了使用OpenCV库的7个热门主题,给出了不同的图像处理任务的实现步骤和代码参考。 >>加入极市CV技术交流群,走在计算机视觉的最前沿
python -m venv env
env\scripts\activate
激活环境,你会在
C:\Users\username\Desktop\opencv
之前看到小括号(env)出现。
pip install opencv-python
读,写和显示图像
读取视频并与网络摄像头集成
调整大小和裁剪图像
基本的图像过滤器使用的函数
绘制不同的形状
在图像上书写文字
检测并裁剪脸部
img = cv2.imread\( "PATH\_TO\_IMAGE.jpg/png"\)
Example
img = imread\( "images/dog0.jpg"\)
cv2.imshow\( "WINDOW NAME",IMG\_VAR\)
Example
imshow\( "Dog Image",img\)
cv2.imwrite\(FILENAME, IMAGE\)
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\)
video = cv2.VideoCapture\( "FILEPATH.mp4"\)
Example
video = cv2.VideoCapture\( "video/dog/dog.mp4"\)
while True:
success , img = cap.read\(\)
cv2.imshow\( "Video",img\)
if cv2.waitKey\( 1\) \& 0xff\==ord\( 'q'\): ##key 'q' will break the loop
break
cap = cv2.VideoCapture\( 0\)
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
cv2.resize\(IMG,\(WIDTH,HEIGHT\)\)
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
来找到任何图像的形状,然后根据图像形状,可以增加或减小图像的大小。让我们看看示例。
import cv2
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\)
import cv2
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\)
imgCropped = img\[y1:y2, x1:x2\]
\(x1,y1\): top-left vertex
\(x2,y2\): bottom-right vertex
Example
imgCropped = img\[ 0: 100, 200: 200\]
import cv2
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
作为参数传递。
imgGray = cv2.cvtColor\(IMG,cv2.CODE\)
IMG: Original image
CODE: Conversion code for Gray\(COLOR\_BGR2GRAY\)
Example
imgGray = cv2.cvtColor\(img,cv2.COLOR\_BGR2GRAY\)
cvtColor
,这里我们将
cv2.COLOR_BGR2HSV
作为参数传递。它主要用于对象跟踪。
imgGray = cv2.cvtColor\(IMG,cv2.CODE\)
IMG: Original image
CODE: Conversion code for Gray\(COLOR\_BGR2HSV\)
Example
imgHsv = cv2.cvtColor\(img,cv2.COLOR\_BGR2HSV\)
imgBlur = cv2.GaussianBlur\(img,\(sigmaX,sigmaY\),kernalSize\)
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\)
imgCanny = cv2.Canny\(img,threshold1,threshold2\)
threshold1,threshold2:Different values of threshold different for every images
Example
imgCanny = cv2.Canny\(img, 100, 150\)
kernel = np.ones\(\( 5, 5\),np.uint8\) ## DEFINING KERNEL OF 5x5
imgDialation = cv2.dilate\(imgCanny,kernel,iterations= 1\) ##DIALATION
kernel = np.ones\(\( 5, 5\),np.uint8\) ## DEFINING KERNEL OF 5x5
imgDialation = cv2.erode\(imgCanny,kernel,iterations= 1\) ##EROSION
cv2.rectangle\(img,\(w,h\),\(x,y\),\(R,G,B\),THICKNESS\)
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\)
cv2.circle\(img,\(x,y\),radius,\(R,G,B\),THICKNESS\)
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\)
cv2.line\(img,\(x1,y1\),\(x2,y2\),\(R,G,B\),THICKNESS\)
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\)
cv2.putText\(img,text,\(x,y\),FONT,FONT\_SCALE,\(R,G,B\),THICKNESS\)
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\)
import cv2
# 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\(\)
公众号后台回复“目标检测综述”获取目标检测(2001-2021)综述PDF~
“
点击阅读原文进入CV社区
收获更多技术干货