使用OpenCV和Python从图像中提取形状
Welcome to the first post in this series of blogs on extracting features from images using OpenCV and Python.
歡迎使用本系列博客的第一篇有關使用OpenCV和Python從圖像中提取特征的文章。
Feature extraction from images and videos is a common problem in the field of Computer Vision. In this post, we will consider the task of identifying balls and table edges on a pool table.
從圖像和視頻中提取特征是計算機視覺領域的普遍問題。 在這篇文章中,我們將考慮識別臺球桌上的球和桌子邊緣的任務。
Consider the example image below from an online pool game.
考慮下面的在線撞球游戲示例圖像。
Let's say we want to mark the positions of every ball in this image and also the four inner edges of the table. There are multiple ways in which this can be done and some methods work better than others for a given image. However, a useful approach is to try and separate out the contents of an image based on their color composition.
假設我們要標記此圖像中每個球的位置以及桌子的四個內邊緣。 有多種方法可以完成此操作,對于給定的圖像,某些方法比其他方法效果更好。 但是,一種有用的方法是嘗試根據圖像的顏色成分來分離圖像的內容。
For example, in the above image, we can see that the tabletop, the balls and the image background all have different colors.
例如,在上圖中,我們可以看到桌面,球和圖像背景都有不同的顏色。
Hence if we can separate out the colors in the image, we would be closer to solving our problem.
因此,如果我們可以分離出圖像中的顏色,那么我們將更接近解決問題。
An easy way to do this is to convert the RBG image into HSV format and then find out the range of H, S and V values corresponding to the object of interest.
一種簡單的方法是將RBG圖像轉換為HSV格式,然后找出與感興趣對象相對應的H,S和V值的范圍。
For details on this step refer to my blog (coming soon) on HSV based extraction.
有關此步驟的詳細信息,請參閱我的博客(即將發布)有關基于HSV的提取。
Once we have the HSV color map for the table top, we can use the OpenCV “inRange()” function to obtain a visualization of the extracted mask as below.
一旦獲得了桌面的HSV顏色圖,就可以使用OpenCV“ inRange()”函數來獲取提取的蒙版的可視化效果,如下所示。
Image mask generated using OpenCV (image source author)使用OpenCV生成的圖像蒙版(圖像源作者)As we can see, this step has helped achieve the following objectives:
如我們所見,這一步驟有助于實現以下目標:
As a first step, we need to extract the table object from the image in order to focus on the table and its contents and ignore other objects in the image e.g. background, external objects etc.
第一步,我們需要從圖像中提取表格對象,以便專注于表格及其內容,而忽略圖像中的其他對象,例如背景,外部對象等。
Now is the correct time to apply Edge Detection techniques to identify and extract desired components from the image. There are multiple options available such as Canny and Sobel functions and each has its merits and demerits.
現在是應用邊緣檢測技術從圖像中識別和提取所需成分的正確時機。 有多個選項,例如Canny和Sobel函數,每個都有其優點和缺點。
We will use the OpenCV “findContours()” function for edge detection to extract all contours in the mask image. The contour with the largest area is the one corresponding to the table itself.
我們將使用OpenCV“ findContours()”函數進行邊緣檢測,以提取蒙版圖像中的所有輪廓。 面積最大的輪廓就是與表格本身相對應的輪廓。
In order to implement a smooth extraction of the table, we will find the bounding rectangle (OpenCV “boundingRect()” function) of the table contour and use its coordinates to extract the sub-image from the original image containing only the object of interest, in this case, the table surface and balls as shown in the image below.
為了實現表格的平滑提取,我們將找到表格輪廓的邊界矩形(OpenCV “ boundingRect()”函數),并使用其坐標從僅包含感興趣對象的原始圖像中提取子圖像。 ,在這種情況下,工作臺表面和球如下圖所示。
Extracted only the region of interest using OpenCV (image source author)使用OpenCV僅提取感興趣的區域(圖像源作者)Now the remaining task is to extract the individual balls and identify the inner edges of the table.
現在剩下的任務是提取單個球并識別桌子的內邊緣。
To achieve this, we will again obtain the mask using HSV based extraction method used earlier, first focusing on the balls and then on the table edges.
為此,我們將再次使用之前使用的基于HSV的提取方法來獲得蒙版,首先將注意力集中在球上,然后再關注桌面邊緣。
檢測球 (Detecting balls)
The mask image for the balls will look the same as the one we used earlier for the table. From the obtained mask image, we will extract the ball contours using the OpenCV “findContours()” function once again. This time we are interested in only those contours which resemble a circle and are of a given size.
球的蒙版圖像將與我們之前在表格中使用的蒙版圖像相同。 從獲得的蒙版圖像中,我們將再次使用OpenCV “ findContours()”函數提取球的輪廓。 這次,我們僅對那些類似于圓形且具有給定尺寸的輪廓感興趣。
Again there are many ways to detect the ball contours, but one method which works best is to find the minimum bounding rectangle for each detected contour and chose the ones which best resemble a square and also lie within the desired range of area. We will use the OpenCV function “minAreaRect()” in this case.
同樣,有很多方法可以檢測到球的輪廓,但是最有效的一種方法是為每個檢測到的輪廓找到最小的邊界矩形,然后選擇最類似于正方形并且也位于所需區域范圍內的矩形。 在這種情況下,我們將使用OpenCV函數“ minAreaRect()” 。
On the selected set of contours, we will further apply the OpenCV “minEnclosingCircle()” function to obtain uniform sized circles over each of the balls.
在所選的輪廓集上,我們將進一步應用OpenCV “ minEnclosingCircle() ”函數,以在每個球上獲得大小一致的圓。
Now we just need to use OpenCV “circle()” function to draw over each of the detected balls with any color of our choice.
現在,我們只需要使用OpenCV “ circle()”函數以我們選擇的任何顏色繪制每個檢測到的球。
檢測桌子邊緣 (Detecting table edges)
This is a two-step approach since the table has both an outer and inner edge and we are interested in only the latter.
這是一種兩步方法,因為桌子同時具有外邊緣和內邊緣,我們僅對后者感興趣。
The first step is to get a mask for the table edges using the HSV based approach. The obtained mask looks like below in which all four sides can be easily distinguished.
第一步是使用基于HSV的方法為表格邊緣獲取遮罩。 所獲得的掩模如下所示,在其中可以容易地區分所有四個側面。
Mask for table edges detection obtained using OpenCV (image source author)使用OpenCV(圖像源作者)獲得的用于表格邊緣檢測的遮罩With this mask we can now extract the inner edges by locating the two horizontal and two vertical lines which are closest from the center of the image.
使用此蒙版,我們現在可以通過定位距圖像中心最近的兩條水平線和兩條垂直線來提取內部邊緣。
We will use the OpenCV “HoughLines()” function to find all lines in the image and select only the 4 of our interest.
我們將使用OpenCV“ HoughLines()”函數查找圖像中的所有行,并僅選擇我們感興趣的4條。
Once the 4 lines are detected we just need to use the OpenCV “line()” function to draw the corresponding table edges.
一旦檢測到4條線,我們只需要使用OpenCV “ line()”函數繪制相應的表格邊緣。
The obtained image can then be overlaid on top of the original image to complete the task as shown below.
然后,可以將獲得的圖像疊加在原始圖像上以完成任務,如下所示。
Identified all the balls and four table edges with high accuracy (image source author)高精度識別所有球和四個桌子邊緣(圖片來源)Hope you have enjoyed reading this post.
希望您喜歡閱讀這篇文章。
In my next post, I will cover another interesting example of feature extraction so stay tuned.
在我的下一篇文章中,我將介紹另一個有趣的特征提取示例,請繼續關注。
Important Disclaimer:
重要免責聲明:
The method used in this blog post especially the HSV values used for detecting balls and table edges will not necessarily work for every image. Every image is unique in its characteristics and needs the right set of parameters in order for feature extraction to work as desired. This is precisely what makes Computer Vision such an interesting and challenging field.
本博客文章中使用的方法,尤其是用于檢測球和桌子邊緣的HSV值,不一定適用于所有圖像。 每張圖像的特征都是獨特的,并且需要正確的參數集,以便按需進行特征提取。 這正是使Computer Vision成為一個有趣而充滿挑戰的領域的原因。
翻譯自: https://towardsdatascience.com/extracting-circles-and-long-edges-from-images-using-opencv-and-python-236218f0fee4
總結
以上是生活随笔為你收集整理的使用OpenCV和Python从图像中提取形状的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比亚迪、奔驰、理想全躺枪 博主赞特斯拉研
- 下一篇: kusto使用_Python查找具有数据