本文旨在让你快速入门opencv。
python -m venv env 现在,使用
envscriptsactivate
激活环境,你会在C:UsersusernameDesktopopencv
之前看到小括号(env)出现。现在,只需使用pip安装OpenCV。pip install opencv-python
img = cv2.imread("PATH_TO_IMAGE.jpg/png") Example img = imread("images/dog0.jpg") imshow():
cv2.imshow("WINDOW NAME",IMG_VAR) Example imshow("Dog Image",img) imwrite():
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") 视频是许多帧结合在一起的集合,每帧都是一幅图像。要使用OpenCV观看视频,我们只需要使用while循环显示视频的每一帧。
while True: success , img = cap.read() cv2.imshow("Video",img) if cv2.waitKey(1) & 0xff==ord('q'):##key 'q' will break the loop break 要与网络摄像头集成,我们需要传递网络摄像头的端口值而不是视频路径。如果你使用的是笔记本电脑,但没有连接任何外部网络摄像头,则只需传递参数0;如果你有外部网络摄像头,则传递参数1。
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) 裁剪图像裁剪是获取图像的一部分过程。在OpenCV中,我们可以通过定义裁剪后的矩形坐标来执行裁剪。句法
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) 你也可以使用paint来找到(x1,y1),(x2,y2)的正确坐标。右键单击图像并保存,尝试从图像中获取王卡。提示:使用paint来找到正确的坐标,最后使用调整大小来增加裁剪图像的大小。“在寻求解决方案之前,请尝试自己动手做。”解决方案- https://gist.github.com/Abhayparashar31/9b01473431de765c0a73e81271233d91
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) 将图像转为HSV要将图像转换为HSV,我们可以使用函数
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) 图像模糊模糊用于去除图像中的多余噪声,也称为平滑,这是对图像应用低通滤波器的过程。要在Opencv中使用模糊,我们有一个函数GaussianBlur。
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) 边缘检测在OpenCV中,我们使用Canny边缘检测器来检测图像中的边缘,也有不同的边缘检测器,但最著名的是Canny边缘检测器。Canny边缘检测器是一种边缘检测算子,它使用多阶段算法来检测图像中的大范围边缘,它由John F. Canny在1986年开发。
imgCanny = cv2.Canny(img,threshold1,threshold2) threshold1,threshold2:Different values of threshold different for every images Example imgCanny = cv2.Canny(img,100,150) 膨胀膨胀是用来增加图像中边缘的大小。首先,我们定义一个大小为奇数(5,5)的核矩阵,然后利用核函数对图像进行放大。我们对Canny边缘检测器的输出图像进行了放大处理。
kernel = np.ones((5,5),np.uint8) ## DEFINING KERNEL OF 5x5 imgDialation = cv2.dilate(imgCanny,kernel,iterations=1) ##DIALATION 腐蚀腐蚀是扩张的反面,它用于减小图像边缘的尺寸。首先,我们定义一个奇数(5,5)的核矩阵大小,然后使用核对图像执行腐蚀。我们对Canny边缘检测器的输出图像施加腐蚀。
kernel = np.ones((5,5),np.uint8) ## DEFINING KERNEL OF 5x5 imgDialation = cv2.erode(imgCanny,kernel,iterations=1) ##EROSION 现在,在同一程序中将所有基础函数应用于Monalisa映像。
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。我们传递x,y,半径大小,RGB形式的颜色,厚度作为参数。
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,使用起点(x1,y1),终点(x2,y2),RGB形式的颜色,厚度作为参数。
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) 下载Monalisa图片。任务:使用形状和文本为左侧图像中所示的Monalisa脸创建框架。提示:首先是一个圆形,然后是矩形,然后根据圆形和矩形放置文本,最后根据文本放置一行。解决方案- https://gist.github.com/Abhayparashar31/af36bf25ce61345266db4b54aba33be1
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()
全部0条评论
快来发表一下你的评论吧 !