PS:本文純粹本身想學習OpenCV來複現一遍,隨意翻譯,可能用詞大量不許確,若有異議錯誤歡迎指正html
須要下載的文件能夠在這裏找到(免費)
做者原文連接:https://www.pyimagesearch.com/2018/07/19/opencv-tutorial-a-guide-to-learn-opencv/python
This OpenCV tutorial is for beginners just getting started learning the basics. Inside this guide, you’ll learn basic image processing operations using the OpenCV library using Python.
爲初學者提供參考
將瞭解:git
And by the end of the tutorial you’ll be putting together a complete project to count basic objects in images using contours.
最後,你會創建一個完整的工程,可以用框,框出圖中的物體,並獲得個數。
While this tutorial is aimed at beginners just getting started with image processing and the OpenCV library, I encourage you to give it a read even if you have a bit of experience.github
A quick refresher in OpenCV basics will help you with your own projects as well.後端
The first step today is to install OpenCV on your system (if you haven’t already).
請先安裝OpenCV
他提供了在樹莓派,Ubuntu和macOS中安裝的教程
I maintain anOpenCV Install Tutorialspage which contains links to previous OpenCV installation guides for Ubuntu, macOS, and Raspberry Pi.數組
你最好先看那些教程,並執行親自執行一遍(我以爲...)
You should visit that page and find + follow the appropriate guide for your system.bash
Once your fresh OpenCV development environment is set up,install the imutils package via pip/. I have created and maintained imutils
(source on GitHub) for the image processing community and it is used heavily on my blog. You should install imutils
in the same environment you installed OpenCV into — you’ll need it to work through this blog post as it will facilitate basic image processing operations:app
$ pip install imutils
_Note:_ If you are using Python virtual environments don’t forget to use the workon
command to enter your environment before installing imutils
!
若是你用的是python虛擬環境,還要用workon
命令進入你本身配的環境來安裝imutils
框架
OpenCV工程的結構less
Before going too far down the rabbit hole, be sure to grab the code + images from the 「Downloads」section of today’s blog post.rabbit hole
:比喻進入未知領域的入口,或者一時半會難以搞定的事
開始以前記得下載好做者給的代碼+圖片
From there, navigate to where you downloaded the .zip in your terminal (cd
). And then we canunzip
the archive, change working directories (cd
) into the project folder, and analyze the project structure via tree
:
咱們進目錄看一下下載文件的結構,用tree
命令(一個可視化文件結構的,安裝命令:sudo apt-get install tree
)
$ cd ~/Downloads $ unzip opencv-tutorial.zip $ cd opencv-tutorial $ tree . ├── jp.png ├── opencv_tutorial_01.py ├── opencv_tutorial_02.py └── tetris_blocks.png 0 directories, 4 files
In this tutorial we’ll be creating two Python scripts to help you learn OpenCV basics:
在這個教程中,咱們將建立兩個python腳原本幫助你們學OpenCV:
1.Our first script,opencv_tutorial_01.py
will cover basic image processing operations using an image from the movie,Jurassic Park( jp.png ).
opencv_tutorial_01.py
,包括基本的圖像處理2.From there,opencv_tutorial_02.py
will show you how to use these image processing building blocks to create an OpenCV application to count the number of objects in a Tetris image (tetris_blocks.png ).
opencv_tutorial_02.py
,向咱們展現用這些圖像處理技術來構建一個OpenCV工程,在目標圖片中數物體數目。導入並展現圖片
![]()
Figure 1:Learning OpenCV basics with Python begins with loading and displaying an image — a simple process that requires only a few lines of code.
下面就是加載圖片的簡單代碼
Let’s begin by opening upopencv_tutorial_01.py
in your favorite text editor or IDE:
選個你喜歡的編譯器打開upopencv_tutorial_01.py
# import the necessary packages #1 import imutils #2 import cv2 #3 #4 # load the input image and show its dimensions, keeping in mind that #5 # images are represented as a multi-dimensional NumPy array with 圖片用多維數組表示 #6 # shape no.rows (height) x no.columns (width) x no. channels (depth)顏色通道 #7 image = cv2.imread("jp.png") #8 (h, w, d) = image.shape #9 print("width={}, height={}, depth={}".format(w, h, d)) #10 #11 # display the image to our screen -- we will need to click the window #12 # open by OpenCV and press a key on our keyboard to #11 continue execution #13 cv2.imshow("Image", image) #14 cv2.waitKey(0) #15
On Lines 2 and 3we import both imutils
and cv2
. The cv2
package is OpenCV and despite the 2 embedded, it can actually be OpenCV 3 (or possibly OpenCV 4 which may be released later in 2018). The imutils
package is my series of convenience functions.
第二三行導入包
Now that we have the required software at our fingertips via imports, let’s load an image from disk into memory.
如今從磁盤讀圖片
To load our_Jurassic Park_ image (from one of my favorite movies), we callcv2.imread("jp.png")
. As you can see on Line 8image = cv2.imread("jp.png")
, we assign the result to image . Our image is actually just a NumPy array.
咱們將結果定義爲圖像,而圖像在計算機中其實是當作三維數組處理的,其維度是height*width*顏色通道數(彩色就是RGB3
通道,黑白只有灰度,因此是2
)
Later in this script, we’ll need the height and width. So on Line 9, I call image.shape
to extract the height, width, and depth.
用來提取圖片三維 長、寬、深(就是顏色通道數)
It may seem confusing that the height comes before the width, but think of it this way:
Therefore, the dimensions of an image represented as a NumPy array are actually represented as _(height, width, depth)._
因此啊,圖片表示爲一個NumPy
數組,它的維度數就表明(長 寬 深)
Depth is the number of channels — in our case this is three since we’re working with 3 color channels: Blue, Green, and Red.
Depth 深度就是(顏色)通道數,彩色的就是藍
綠
紅
三個通道.
The print command shown on Line 10will output the values to the terminal:
print("width={}, height={}, depth={}".format(w, h, d))
命令框中輸出
width =600, height =322, depth =3
To display the image on the screen using OpenCV we employ cv2.imshow("Image",image)
on Line 14.
第14行的代碼cv2.imshow("Image",image)
讓代碼用OpenCV顯示出來
The subsequent line waits for a keypress (Line 15)cv2.waitKey(0)
.
第15行代碼讓界面等到咱們鍵盤按下一個鍵的時候關閉.
This is important otherwise our image would display and disappear faster than we’d even see the image.
_Note:_ You need to actually click the active window opened by OpenCV and press a key on your keyboard to advance the script. OpenCV cannot monitor your terminal for input so if you a press a key in the terminal OpenCV will not notice. Again, you will need to click the active OpenCV window on your screen and press a key on your keyboard.
大概就是你用命令框打開了OpenCV窗口,想鍵盤被按時,OpenCV窗口有響應要點擊OpenCV窗口.
處理獨立像素
![]()
Figure 2:_Top_: grayscale gradient(灰度梯度?) where brighter pixels are closer to 255 and darker pixels are closer to 0._Bottom_: RGB venn diagram where brighter pixels are closer to the center.
明亮的顏色趨於255 暗淡的顏色0
RGB維恩圖中,越明亮的顏色離中心越近
First, you may ask:
What is a pixel(像素)?
All images consist of pixels which are the raw building blocks of images.
全部的圖片都是由最基礎的像素組成的.
Images are made of pixels in a grid.
一格格的像素組成了咱們看到的圖片.
A 640 x 480 image has 640 columns (the width) and 480 rows (the height). There are 640*480=307200 pixels in an image with those dimensions.
Each pixel in a grayscale image has a value representing the shade of gray.In OpenCV, there are 256 shades of gray — from 0 to 255. So a grayscale image would have a grayscale value associated with each pixel.
每一個像素在灰度圖中都按照深淺對應一個0-255 的數值
Pixels in a color image have additional information. There are several color spaces that you’ll soon become familiar with as you learn about image processing. For simplicity let’s only consider the RGB color space.
彩圖中的像素有額外的信息.咱們等會只考慮RGB三個顏色空間
In OpenCV color images in the RGB (Red, Green, Blue) color space have a 3-tuple associated with each pixel:(B,G,R) .
RGB三色度空間中,每一個像素關聯一個三元組.
Notice the ordering is BGR
rather than RGB. This is because when OpenCV was first being developed many years ago the standard was BGR ordering. Over the years, the standard has now become RGB but OpenCV still maintains this 「legacy」 BGR ordering
to ensure no existing code breaks.
OpenCV中色度順序是bgr不是rgb.
Each value in the BGR 3-tuple has a range of[0,255] . How many color possibilities are there for each pixel in an RGB image in OpenCV? That’s easy : 256*256*256=16777216 .
BGR 3元組中,每一個值都在範圍[0,255]內
有組合種類:256*256*256=16777216
Now that we know exactly what a pixel is, let’s see how to retrieve the value of an individual pixel in the image:
# access the RGB pixel located at x=50, y=100, keepind in mind that #17 # OpenCV stores images in BGR order rather than RGB #18 (B, G, R) = image[100, 50] #19 print("R={}, G={}, B={}".format(R, G, B)) #20
As shown previously, our image dimensions arewidth = 600, height = 322, depth = 3. We can access individual pixel values in the array by specifying the coordinates so long as they are within the max width and height.
在圖片長寬範圍內,咱們能夠經過座標找到對應像素.
The code,image[100,50] , yields a 3-tuple of BGR values from the pixel located at x =50 and y =100 (again, keep in mind that the_height_is the number of_rows_and the_width_is the number of_columns_— take a second now to convince yourself this is true). 高是行
寬是列
As stated above, OpenCV stores images in BGR ordering (unlike Matplotlib, for example). Check out how simple it is to extract the color channel values for the pixel on Line 19.(B, G, R) = image[100, 50]
第19行提取color channel values
The resulting pixel value is shown on the terminal here:結果
R = 41, G =49, B =37
處理圖片數組
Extracting 「regions of interest」
(ROIs
) is an important skill for image processing.
處理圖片中咱們感興趣的部分.
Say, for example, you’re working on recognizing faces in a movie.
好比你要在電影中進行人臉識別
First, you’d run a face detection algorithm
to find the coordinates of faces in all the frames you’re working with.
首先,你要用face detection
函數,在全部框架中找到臉的座標.
Then you’d want to extract the face ROIs and either save them or process them.
提取面部ROIs
,保存或者處理它們
Locating all frames containing Dr. Ian Malcolm(某我的)
in_Jurassic Park_
(某部電影)would be a great face recognition mini-project to work on.
For now, let’s just_manually_extract an ROI. This can be accomplished with array slicing
.
咱們來親自執行一下,試着去提取ROIs
咱們用這個操做array slicing
去完成.
Figure 3:Array slicing with OpenCV allows us to extract a region of interest (ROI) easily.
# extract a 100x100 pixel square ROI (Region of Interest) from the #22 # input image starting at x=320,y=60 at ending at x=420,y=160 #23 roi = image [60:160, 320:420] #24 cv2.imshow("ROI", roi) #25 cv2.waitKey(0) #26
Array slicing is shown on Line 24roi = image [60:160, 320:420] #24
with the format格式: image[start_Y:end_Y,startX:endX] .
This code grabs an roi
which we then display on Line 25. cv2.imshow("ROI", roi)
Just like last time, we display until a key is pressed (Line 26).cv2.waitKey(0)
As you can see in Figure 3, we’ve extracted the face of Dr.Ian Malcolm.
I actually predetermined the(x, y)-coordinates using Photoshop for this example,
做者提早直接用Photoshop獲得那我的臉的座標了
but if you stick with me on the blog you could[detect and extract face ROI’s automatically(https://www.pyimagesearch.com...
emmm我等會也寫個博客記錄一下吧
改變圖像大小
Resizing images is important for a number of reasons.
1.you might want to resize a large image to fit on your screen. Image processing is also faster
on smaller images because there are fewer pixels
to process. In the case of deep learning, we often resize images, ignoring aspect ratio
橫縱比, so that the volume fits into a network which requires that an image be square and of a certain dimension.
Let’s resize our original image to 200 x 200 pixels:
# resize the image to 200x200px, ignoring aspect ratio #28 resized = cv2.resize(image, (200, 200)) #29 cv2.imshow("Fixed Resizing", resized) #30 cv2.waitKey(0) #31
On Line 29resized = cv2.resize(image, (200, 200))
we have resized an image ignoring aspect ratio. Figure 4(_right_) shows that the image is resized but is now distorted because we didn’t take into account the aspect ratio.由於沒考慮橫縱比,因此那個小圖片有點扭曲
Figure 4:Resizing an image with OpenCV and Python can be conducted withcv2.resize
however aspect ratio is not preserved automatically.
Let’s calculate the aspect ratio of the original image and use it to resize an image so that it doesn’t appear squished and distorted:
計算一下原圖的橫縱比,而後再用這個橫縱比來縮小圖片,這樣就不會扭曲了.
# fixed resizing and distort aspect ratio so let's resize the width 33 # to be 300px but compute the new height based on the aspect ratio 34 r = 300.0 / w # 35 dim = (300, int(h * r)) #36 resized = cv2.resize(image, dim) #37 cv2.imshow("Aspect Ratio Resize", resized) #38 cv2.waitKey(0) #39
Recall back to Line 9 (h, w, d) = image.shape
of this script where we extracted the width and height of the image.獲得了圖片的寬和高
Let’s say that we want to take our 600-pixel wide image and resize it to 300 pixels wide while_maintaining aspect ratio_.
咱們想把600像素的圖片壓縮到300像素,而且保持橫縱比
On Line 35r = 300.0 / w
we calculate the ratio of the _new width_to the _old width_(which happens to be 0.5).
From there, we specify our dimensions of the new image,dim.
We know that we want a 300-pixel wide image, but we must calculate the height using the ratio by multiplying h
by r
(the original height and our ratio respectively).
Feeding dim
(our dimensions) into the cv2.resize function, we’ve now obtained a new image named resized
which is not distorted (Line 37).resized = cv2.resize(image, dim)
To check our work, we display the image using the code on Line 38:cv2.imshow("Aspect Ratio Resize", resized)
Figure 5: Resizing images while maintaining aspect ratio with OpenCV is a three-step process:
保持橫縱比的壓縮圖片,以下三步走
(1) extract the image dimensions 提取圖片維度
(2) compute the aspect ratio 計算橫縱比
(3) resize the image (cv2.resize
) along one dimension先在一個維度上改變
and multiply the other dimension by the aspect ratio. 再用橫縱比乘其餘維度
SeeFigure 6for an even easier method.
But can we make this process of preserving aspect ratio during resizing even easier?
Yes!
簡化一下
Computing the aspect ratio each time we want to resize an image is a bit tedious, so I wrapped the code in a function within imutils
.
在這個包裏imutils
,把這項功能寫成函數.
Here is how you may use imutils.resize
:
# manually computing the aspect ratio can be a pain so let's use the 41 # imutils library instead 42 resized = imutils.resize(image, width =300) #43 cv2.imshow("Imutils Resize", resized) #44 cv2.waitKey(0) #45
In a single line of code, we’ve preserved aspect ratio and resized the image.只要一行代碼就能完成咱們的任務
Simple right?
All you need to provide is your target width
or target height
as a keyword argument (Line 43).
你只須要提供 寬
和 高
就行
Here’s the result:
Figure 6:If you’d like to maintain aspect ratio while resizing images with OpenCV and Python, simply useimutils.resize
. Now your image won’t risk being 「squished」 as inFigure 4.
旋轉圖像
Let’s rotate our_Jurassic Park_image for our next example:
# let's rotate an image 45 degrees clockwise using OpenCV by first 47 # computing the image center, then constructing the rotation matrix, 48 # and then finally applying the affine warp 49 center = (w // 2, h // 2) #50 M = cv2.getRotationMatrix2D(center, -45, 1.0) #51 rotated = cv2.warpAffine(image, M, (w, h)) #52 cv2.imshow("OpenCV Rotation", rotated) #53 cv2.waitKey(0) #54
Rotating an image about the center point requires that we first calculate the center (x, y)-coordinates of the image (Line 50).
經過中心點旋轉圖片,首先要找到中心座標
_Note: We use//
to perform integer math (i.e., no floating point values)_.整除?
From there we calculate a rotation matrix,M
獲得旋轉矩陣M
(Line 51). M = cv2.getRotationMatrix2D(center, -45, 1.0)
The -45 means that we’ll rotate the image 45 degrees clockwise. 順時針轉45度
Recall from your middle/high school geometry class about the unit circle and you’ll be able to remind yourself thatpositive angles
(正角度>0) are counterclockwise(逆時針
)andnegative angles(負角度<0) are clockwise(順時針
).
From there we warp the image using the matrix (effectively rotating it) on Line 52.rotated = cv2.warpAffine(image, M, (w, h))
The rotated image is displayed to the screen on Line 52and is shown in Figure 7:
Figure 7:Rotating an image with OpenCV about the center point requires three steps:
(1) compute the center point using the image width and height用寬和高,計算中心座標
(2) compute a rotation matrix withcv2.getRotationMatrix2D
計算旋轉矩陣
(3) use the rotation matrix to warp the image withcv2.warpAffine
. 用旋轉矩陣旋轉圖片
Now let’s perform the same operation in just a single line of code usingimutils :
# rotation can also be easily accomplished via imutils with less code rotated = imutils.rotate(image, -45) cv2.imshow("Imutils Rotation", rotated) cv2.waitKey(0)
Since I don’t have to rotate image as much as resizing them (comparatively) I find the rotation process harder to remember.
Therefore, I created a function in imutils
to handle it for us. In a single line of code, I can accomplish rotating the image 45 degrees clockwise (Line 57) as inFigure 8:
一樣,在做者的包imutils
中,提供直接旋轉的函數.
Figure 8:Withimutils.rotate
, we can rotate an image with OpenCV and Python conveniently with a single line of code.
At this point you have to be thinking:
Why in the world is the image clipped?(有一部分咋沒了)
The thing is, OpenCV_doesn’t care_if our image is clipped and out of view after the rotation. I find this to be quite bothersome, so here’s my imutils
version which will keep the entire image in view. I call it rotate_bound
:
# OpenCV doesn't "care" if our rotated image is clipped after rotation # so we can instead use another imutils convenience function to help # us out rotated = imutils.rotate_bound(image, 45) cv2.imshow("Imutils Bound Rotation", rotated) cv2.waitKey(0)
There’s a lot going on behind the scenes of rotate_bound
. If you’re interested in how the method on Line 64works, be sure to check outthis blog post.
emm我有時間再來研究
The result is shown in Figure 9:
Figure 9:Therotate_bound
function ofimutils
will prevent OpenCV from clipping the image during a rotation. Seethis blog postto learn how it works!
Perfect! The entire image is in the frame and it is correctly rotated 45 degrees clockwise.
平滑圖像處理
In many image processing pipelines, we must blur(模糊) an image to reduce high-frequency noise(高頻噪聲), making it easier for our algorithms to detect and understand the actual_contents_of the image rather than just _noise_that will 「confuse」 our algorithms
.
大概就是,模糊化圖片能夠減小一些不肯定因素的影響,可讓大部分函數更好的去處理它周圍的像素,若是一個圖片很高清,一些像素點可能對總體或者說咱們關注的重點,其實做用不但不大,反而起干擾做用,而模糊化(大概讓那些特異點和周圍趨同了)會減緩這種干擾.
Blurring an image is very easy in OpenCV and there are a number of ways to accomplish it.
Figure 10:This image has undergone(經歷) a Gaussian blur
(高斯模糊) with an11 x 11
kernel using OpenCV. Blurring is an important step of many image processing pipelines to reduce high-frequency noise.
I often use the GaussianBlur
function:
高斯模糊 函數
# apply a Gaussian blur with a 11x11 kernel to the image to smooth it, 68 # useful when reducing high frequency noise 69 blurred = cv2.GaussianBlur(image, (11, 11), 0) #70 cv2.imshow("Blurred", blurred) #71 cv2.waitKey(0) #72
On Line 70blurred = cv2.GaussianBlur(image, (11, 11), 0)
we perform a Gaussian Blur with an 11 x 11 kernel
the result of which is shown inFigure 10.
Larger kernels
would yield a more blurry image. Smaller kernels will create less blurry images. To read more about kernels, refer tothis blog postor thePyImageSearch Gurus course.
具體kernels
越大,圖片模糊度越高,反之亦然.
在圖片上畫畫
In this section, we’re going to draw a rectangle正方形, circle, and line on an input image. We’ll also overlay
覆蓋 text on an image as well.
Before we move on with drawing on an image with OpenCV, take note that_drawing operations on images are performed in-place
就地執行的_.
Therefore at the beginning of each code block, we make a copy of the original image storing the copy as output
.
所以在每塊代碼的開頭,咱們都保留原始圖像的副本,並將副本存儲爲做爲輸出
。
We then proceed to draw on the image called output
in-place so we do not destroy our original image.
這樣咱們繪圖時只是在副本上操做,不會破壞原始圖像.
Let’s draw a rectangle around Ian Malcolm’s face:
讓咱們在Ian Malcolm
的臉上畫個框
# draw a 2px thick red rectangle surrounding the face output = image.copy() 74 cv2.rectangle(output, (320, 60), (420, 160), (0, 0,255), 2) #75 cv2.imshow("Rectangle", output) #76 cv2.waitKey(0) #77
First, we make a copy of the image onLine 75cv2.rectangle(output, (320, 60), (420, 160), (0, 0,255), 2)
在75行 複製下圖片
for reasons just explained.
Then we proceed to draw the rectangle.
Drawing rectangles in OpenCV couldn’t be any easier. Using pre-calculated coordinates,
用提早計算好的座標
I’ve supplied the following parameters to the cv2.rectangle
function onLine 76:
已經提供給76行的代碼當參數cv2.imshow("Rectangle", output)
img
: The destination image
to draw upon. We’re drawing on output
.咱們要做畫的目標圖片
pt1
: Our starting pixel coordinate which is the top-left. In our case, the top-left is(320,60) .咱們從左上角的第一個像素開始,在本例中座標爲(320,60)
pt2
: The ending pixel — bottom-right. The bottom-right pixel is located at (420,160) .結束與右下角的像.本例中爲(420,160).
color
: BGR tuple. To represent red, I’ve supplied(0,0,255) .顏色,好比本例中是紅色,表示爲(0,0,255)
thickness
: Line thickness (a negative value will make a solid rectangle). I’ve supplied a thickness of 2 .線的粗細(給負數,會畫實心矩陣),本例中畫的是2px(像素)粗的線.
Since we are using OpenCV’s functions rather than NumPy operations we can supply our coordinates in _(x, y)_order rather than _(y, x)_since we are not manipulating(操做) or accessing the NumPy array directly — OpenCV is taking care of that for us.
咱們是使用OpenCV進行操做的,所以不用按照默認的numpy數組儲存序列(y,x)進行操做,用咱們習慣的(x,y)OpenCV提供
便可.
Here’s our result in Figure 11:
Figure 11:Drawing shapes with OpenCV and Python is an easy skill to pick up. In this image, I’ve drawn a red box usingcv2.rectangle
. I pre-determined the coordinates around the face for this example, but you could use a face detection method to automatically find the face coordinates.
以前的連接,做者教面部識別的
[detect and extract face ROI’s automatically(https://www.pyimagesearch.com...
And now let’s place a solid blue circle in front of Dr. Ellie Sattler’s face:
# draw a blue 20px (filled in) circle on the image centered at 80 # x=300,y=150 81 output = image.copy() #82 cv2.circle(output, (300, 150), 20, (255, 0, 0), -1) #83 cv2.imshow("Circle", output) #84 cv2.waitKey(0) #85
To draw a circle, you need to supply following parameters to cv2.circle
:
畫圖用這個函數v2.circle
如下步驟走
img
: The output image.center
: Our circle’s center coordinate. I supplied(300,150) which is right in front of Ellie’s eyes.radius
半徑: The circle radius in pixels. I provided a value of20 pixels.color
: Circle color. This time I went with blue as is denoted by 255 in the B and 0s in the G + R components of the BGR tuple,(255,0,0) .thickness
: The line thickness. Since I supplied a negative value (-1 ), the circle is solid/filled in.Here’s the result in Figure 12:
Figure 12:OpenCV’scv2.circle
method allows you to draw circles anywhere on an image. I’ve drawn a solid circle for this example as is denoted by the-1
line thickness parameter (positive values will make a circular outline with variable line thickness).
It looks like Ellie is more interested in the dinosaurs than my big blue dot, so let’s move on(把咱們畫的藍點給挪了)!
Next, we’ll draw a red line. This line goes through Ellie’s head, past her eye, and to Ian’s hand.
If you look carefully at the method parameters and compare them to that of the rectangle, you’ll notice that they are identical基本一致:
# draw a 5px thick red line from x=60,y=20 to x=400,y=200 87 output = image.copy() #88 cv2.line(output, (60, 20), (400, 200), (0, 0, 255), 5) #89 cv2.imshow("Line", output) #90 cv2.waitKey(0) #91
Just as in a rectangle, we supply two points, a color, and a line thickness. OpenCV’s backend
後端 does the rest.
Figure 13shows the result of Line 89from the code block:cv2.line(output, (60, 20), (400, 200), (0, 0, 255), 5)
Figure 13:Similar to drawing rectangles and circles, drawing a line in OpenCV usingcv2.line
only requires a starting point, ending point, color, and thickness.
Oftentimes you’ll find that you want to overlay text on an image for display purposes. If you’re working on face recognition
you’ll likely want to draw the person’s name above their face. Or if you advance in your computer vision career you may build an image classifier
or object detector
. In these cases, you’ll find that you want to draw text containing the class name and probability.
Let’s see how OpenCV’s putText
function works:
# draw green text on the image 93 output = image.copy() #94 cv2.putText(output, "OpenCV + Jurassic Park!!!", (10, 25), #95 cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) #96 cv2.imshow("Text", output) #97 cv2.waitKey(0) #98
The putText
function of OpenCV is responsible for drawing text on an image. Let’s take a look at the required parameters:顯示文字須要的參數
img
: The output image.text
: The string of text we’d like to write/draw on the image.pt
起始點: The starting point for the text.font
字體: I often use the cv2.FONT_HERSHEY_SIMPLEX
. The available fonts arelisted here.The code onLines 95 and 96will draw the text,_「OpenCV + Jurassic Park!!!」_in green on ouroutput image inFigure 14:
cv2.putText(output, "OpenCV + Jurassic Park!!!", (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) #96
Figure 14:Oftentimes, you’ll find that you want to display text on an image for visualization purposes. Using thecv2.putText
code shown above you can practice overlaying text on an image with different colors, fonts, sizes, and locations.
運行第一個python 腳本 OpenCV教程吧
In my blog posts, I generally provide a section detailing how you can run the code on your computer. At this point in the blog post, I make the following assumptions:
假設如下條件你都知足
To execute our first script, open a terminal or command window and navigate to the files or extract them if necessary.
From there, enter the following command:
$ python opencv_tutorial_01.py width = 600, height = 322, depth =3 R=41, G=49, B=37
The command is everything after the bash prompt $
character.
Just type python opencv_tutorial_01.py
in your terminal and then the first image will appear.
先在你的命令框中輸入python opencv_tutorial_01.py
To cycle through each step that we just learned, make sure an image window is active, and press any key.
Our first couple code blocks above told Python to print information in the terminal. If your terminal is visible, you’ll see the terminal output (Lines 2 and 3) shown.
I’ve also included a GIF animation demonstrating all the image processing steps we took sequentially, one right after the other:
Figure 15:Output animation displaying the OpenCV fundamentals we learned from this first example Python script.
數物體
Now we’re going to shift gears換擋 and work on the second script included in the_「Downloads」_associated with this blog post.做者有提供代碼
In the next few sections we’ll learn how to use create a simple Python + OpenCV script to count the number of Tetris
俄羅斯方塊 blocks in the following image:
Figure 16:If you’ve ever played Tetris (who hasn’t?), you’ll recognize these familiar shapes. In the 2nd half of this OpenCV fundamentals tutorial, we’re going to find and count the shape contours.
Along the way we’ll be:
edge detection
邊緣檢測erosion
腐蝕 and dilation
擴張Go ahead and close the first script you downloaded and open upopencv_tutorial_02.py to get started with the second example:
# import the necessary packages 1 import argparse #2 import imutils #3 import cv2 #4 #5 # construct the argument parser and parse the arguments 6 ap = argparse.ArgumentParser() #7 ap.add_argument("-i", "--image", required =True, #8 help="path to input image") #9 args = vars(ap.parse_args()) #10
On Lines 2-4we import our packages. This is necessary at the start of each Python script. For this second script, I’ve imported argparse
— a command line arguments parsing package which comes with all installations of Python.
Take a quick glance at Lines 7-10. These lines allow us to provide additional information to our program at runtime from within the terminal.
爲咱們在容許工程是提供額外的信息
Command line arguments are used heavily on the PyImageSearch blog and in all other computer science fields as well.
做者的建議閱讀
I encourage you to read about them on this post: _Python, argparse, and command line arguments._
emmm也等我有時間來翻譯一遍
We have one required command line argument --image , as is defined on Lines 8 and 9.
ap.add_argument("-i", "--image", required =True, help ="path to input image")
We’ll learn how to run the script with the required command line argument down below. For now, just know that wherever you encounter args["image"]
in the script, we’re referring to the path to the input image.
你只要輸入 args["image"]
,表明輸入圖片的路徑
轉成灰度圖
# load the input image (whose path was supplied via command line 12 # argument) and display the image to our screen 13 image = cv2.imread(args["image"]) #14 cv2.imshow("Image", image) #15 cv2.waitKey(0) #16 # convert the image to grayscale #18 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#19 cv2.imshow("Gray", gray) #20 cv2.waitKey(0) #21
We load the image into memory on Line 14.加載圖片image = cv2.imread(args["image"])
The parameter to the cv2.imread
function is our path contained in theargs dictionary referenced with the"image" key, args["image"] .
From there, we display the image until we encounter our first keypress (Lines 15 and 16).
咱們一直展現圖片,直到你按下一個鍵(而後圖片就沒了)
We’re going to be thresholding
二值化 and detecting edges
邊緣檢測 in the image shortly. Therefore we convert the image to grayscale on Line 19by calling cv2.cvtColor
and providing the image
and cv2.COLOR_BGR2GRAY
flag.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Again we display the image and wait for a keypress (Lines 20 and 21).
The result of our conversion to grayscale is shown in Figure 17(_bottom)._
Figure 17:(_top_) Our Tetris image. (_bottom_) We’ve converted the image to grayscale — a step that comes before thresholding.
邊緣檢測
Edge detection is useful for finding boundaries of objects in an image — it is effective for segmentation分隔 purposes.
Let’s perform edge detection to see how the process works:
# applying edge detection we can find the outlines of objects in #23 # images 24 edged = cv2.Canny(gray, 30, 150) #25 cv2.imshow("Edged", edged) #26 cv2.waitKey(0) #26
Using the popular Canny algorithm
(developed by John F. Canny in 1986), we can find the edges in the image.
We provide three parameters to the cv2.Canny
function:
img
: The gray
image. minVal
: A minimum threshold
臨界值, in our case 30
.maxVal
: The maximum threshold which is 150
in our example.aperture_size
核大小? : The Sobel kernel size. By default this value is 3
and hence is not shown on Line 25.(由於是默認值,因此咱們沒有展現) edged = cv2.Canny(gray, 30, 150)
Different values for the minimum and maximum thresholds will return different edge maps.
不一樣的最大最小閾值,將獲得不一樣的邊緣映射
InFigure 18below, notice how edges of Tetris blocks themselves are revealed along with sub-blocks that make up the Tetris block:
Figure 18:To conduct edge detection with OpenCV, we make use of the Canny algorithm.
二值化
Image thresholding is an important intermediary step for image processing pipelines. Thresholding can help us to remove lighter or darker regions and contours of images.
I highly encourage you to experiment with thresholding. I tuned the following code to work for our example by trial and error (as well as experience):
# threshold the image by setting all pixel values less than 225 row29 # to 255 (white; foreground) and all pixel values >= 225 to 255 row30 # (black; background), thereby segmenting the image row31 thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1] #32 cv2.imshow("Thresh", thresh) #33 cv2.waitKey(0) #34
In a single line (Line 32) we are:
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
gray
image greater than 225 and setting them to 0 (black) which corresponds to the background
of the image把全部像素值大於255的設置爲0(表明黑),對應以前的背景
For more information on the cv2.threshold
function, including how the thresholding flags work, be sure to refer toofficial OpenCV documentation.
看上面的官方文檔深刻理解函數的做用.
Segmenting foreground from background with a binary image is_critical_to finding contours (our next step).
將(白)前景
(?)與(黑)背景
相分離就能夠數俄羅斯方塊數了
Figure 19:Prior to finding contours, 數俄羅斯方塊前we threshold the grayscale image先將圖片二值化. We performed a binary inverse threshold so that the foreground shapes become white while the background becomes black.
Notice in Figure 19that the foreground objects are white and the background is black.
Figure 20:We’re working towards finding contour shapes with OpenCV and Python in this OpenCV Basics tutorial.
Pictured in the Figure 20animation動畫, we have 6 shape contours. Let’s find and draw their outlines via code:
# find contours (i.e., outlines) of the foreground objects in the 36 # thresholded image 37 cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, #38 cv2.CHAIN_APPROX_SIMPLE) #39 cnts = imutils.grab_contours(cnts) #40 output = image.copy() #41 #42 # loop over the contours 43 for c in cnts: 44 # draw each contour on the output image with a 3px thick purple 45 # outline, then display the output contours one at a time 46 cv2.drawContours(output, [c], -1, (240, 0, 159), 3) #47 cv2.imshow("Contours", output) #48 cv2.waitKey(0) #49
On Lines 38 and 39
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
we use cv2.findContours
to detect the contours in the image. Take note of the parameter flags but for now let’s keep things simple — our algorithm is finding all foreground (white) pixels in the thresh.copy()
image.
Line 40cnts = imutils.grab_contours(cnts)
is very important accounting for the fact that cv2.findContours
implementation changed between OpenCV 2.4, OpenCV 3, and OpenCV 4. This compatibility line is present on the blog wherever contours are involved.
We make a copy of the original image on Line 41so that we can draw contours on subsequent Lines 44-49.
On Line 47we draw eachc from the cnts
list on the image using the appropriately named cv2.drawContours
. I chose purple which is represented by the tuple(240,0,159) .
Using what we learned earlier in this blog post, let’s overlay some text on the image:
在上面寫點字
# draw the total number of contours found in purple 51 text = "I found {} objects!".format(len(cnts)) #52 cv2.putText(output,text,(10,25),cv2.FONT_HERSHEY_SIMPLEX, 0.7,#53 (240, 0, 159), 2)#54 cv2.imshow("Contours", output) #55 cv2.waitKey(0) #56
Line 52builds a text
string containing the number of shape contours. Counting the total number of objects in this image is as simple as checking the length of the contours list — len(cnts)
.
The result is shown in Figure 21:
Figure 21:Counting contours with OpenCV is as easy as finding them and then callinglen(cnts)
.
Erosions and dilations are typically used to reduce noise in binary images (a side effect of thresholding).用來在二值化圖中減小噪聲影響
To reduce the size of foreground objects we can erode away pixels given a number of iterations
迭代:
# we apply erosions to reduce the size of foreground objects 58 mask = thresh.copy() #59 mask = cv2.erode(mask, None, iterations=5) #60 cv2.imshow("Eroded", mask) #61 cv2.waitKey(0) #62
On Line 59we copy the thresh
image while naming it mask
.
Then, utilizing cv2.erode
, we proceed to reduce the contour sizes with 5iterations (Line 60).mask = cv2.erode(mask, None, iterations= 5)
Demonstrated in Figure 22, the masks generated from the Tetris contours are slightly smaller:
Figure 22:Using OpenCV we can erode contours, effectively making them smaller or causing them to disappear completely with sufficient iterations. This is typically useful for removing small blobs in mask image.
Similarly, we can foreground
regions in the mask. To enlarge the regions, simply usecv2.dilate :
放大白色部分called foreground
#similarly, dilations can increase the size of the ground objects 64 mask = thresh.copy() #65 mask = cv2.dilate(mask, None, iterations=5) #66 c2.imshow("Dilated", mask) #67 cv2.waitKey(0) #68
Figure 23:In an image processing pipeline if you ever have the need to connect nearby contours, you can apply dilation to the image. Shown in the figure is the result of dilating contours with five iterations, but not to the point of two contours becoming one.注意不要讓兩個圖形重疊了
Masks allow us to 「mask out」 regions of an image we are uninterested in. We call them 「masks」 because they will _hide_regions of images we do not care about.
If we use the thresh image from Figure 18and mask it with the original image, we’re presented with Figure 23:
Figure 24: When using the thresholded image as the mask in comparison to our original image, the colored regions reappear as the rest of the image is 「masked out」. This is, of course, a simple example, but as you can imagine, masks are very powerful.
In Figure 24, the background is black now and our foreground consists of colored pixels — any pixels masked by ourmask image.
Let’s learn how to accomplish this:
# a typical operation we may want to apply is to take our mask and 70 # apply a bitwise AND to our input image, keeping only the masked 71 # regions 72 mask = thresh.copy() #73 output = cv2.bitwise_and(image, image, mask=mask)#74 cv2.imshow("Output", output) #75 cv2.waitKey(0) #76
The mask
is generated by copying the binary thresh
image (Line 73).mask = thresh.copy()
From there we bitwise
逐位 AND the pixels from both images together using cv2.bitwise_and
.
The result is Figure 24above where now we’re only showing/highlighting the Tetris blocks.
運行第二個腳本吧...
To run the second script, be sure you’re in the folder containing your downloaded source code and Python scripts. From there, we’ll open up a terminal provide the script name +command line argument:
python opencv_tutorial_02.py --image tetris_blocks.png
The argument flag is --image
and the image argument itself is tetris_blocks.png
— a path to the relevant file in the directory.
There is no terminal output for this script. Again, to cycle through the images, be sure you click on an image window to make it active, from there you can press a key and it will be captured to move forward to the nextwaitKey(0) in the script. When the program is finished running, your script will exit gracefully and you’ll be presented with a new bash prompt line in your terminal.
Below I have included a GIF animation of the basic OpenCV image processing steps in our example script:
Figure 25:Learning OpenCV and the basics of computer vision by counting objects via contours.
If you’re looking to continue learning OpenCV and computer vision, be sure to take a look at my book, _Practical Python and OpenCV._做者推薦本身的數=書
Inside the book we’ll explore the OpenCV fundamentals we discussed here today in more detail.
You’ll also learn how to use these fundamentals to build actual computer vision + OpenCV applications, including:
To learn more about_Practical Python and OpenCV_, and how it can help you learn OpenCV (in less than a single weekend),just click here.
In today’s blog post you learned the fundamentals of image processing and OpenCV using the Python programming language.
You are now prepared to start using these image processing operations as 「building blocks」 you can chain together to build an actual computer vision application — a great example of such a project is the basic object counter we created by counting contours.
I hope this tutorial helped you learn OpenCV!
To be notified when future OpenCV blog posts are published here on PyImageSearch,_just enter your email address in the form below!_