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:

如我们所见,这一步骤有助于实现以下目标:

  1. the table object (white) is clearly distinguishable from the image background (black)表格对象(白色)可与图像背景(黑色)明显区分开
  2. the balls (black) are clearly distinguishable from the table surface (white)球(黑色)与桌面(白色)明显区分开

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


http://www.taodudu.cc/news/show-863659.html

相关文章:

  • NLP的特征工程
  • 无监督学习 k-means_无监督学习-第1部分
  • keras时间序列数据预测_使用Keras的时间序列数据中的异常检测
  • 端口停止使用_我停止使用
  • opencv 分割边界_电影观众:场景边界分割
  • 监督学习无监督学习_无监督学习简介
  • kusto使用_Python查找具有数据重复问题的Kusto表
  • 使用GridSearchCV和RandomizedSearchCV进行超参数调整
  • rust面向对象_面向初学者的Rust操作员综合教程
  • 深度学习术语_您应该意识到这些(通用)深度学习术语和术语
  • 问题解决方案_问题
  • airflow使用_使用AirFlow,SAS Viya和Docker像Pro一样自动化ML模型
  • 迁移学习 nlp_NLP的发展-第3部分-使用ULMFit进行迁移学习
  • 情感分析朴素贝叶斯_朴素贝叶斯推文的情感分析
  • 梯度下降优化方法'原理_优化梯度下降的新方法
  • DengAI —数据预处理
  • k 最近邻_k最近邻与维数的诅咒
  • 使用Pytorch进行密集视频字幕
  • 5g与edge ai_使用OpenVINO部署AI Edge应用
  • 法庭上认可零和博弈的理论吗_从零开始的本征理论
  • 极限学习机和支持向量机_极限学习机I
  • 如何在不亏本的情况下构建道德数据科学系统?
  • ann人工神经网络_深度学习-人工神经网络(ANN)
  • 唐宇迪机器学习课程数据集_最受欢迎的数据科学和机器学习课程-2020年8月
  • r中如何求变量的对数转换_对数转换以求阳性。
  • 美团脱颖而出的经验_使数据科学项目脱颖而出的6种方法
  • aws rds同步_将数据从Python同步到AWS RDS
  • 扫描二维码读取文档_使用深度学习读取和分类扫描的文档
  • 电路分析导论_生存分析导论
  • 强化学习-第3部分

使用OpenCV和Python从图像中提取形状相关推荐

  1. 提取图像感兴趣区域_从图像中提取感兴趣区域

    提取图像感兴趣区域 Welcome to the second post in this series where we talk about extracting regions of intere ...

  2. 使用Python,OpenCV+OCR检测护照图像中的机器可读区域(MRZ Machine-Readable Zones)

    使用Python,OpenCV+OCR检测护照图像中的机器可读区域(MRZ Machine-Readable Zones) 1. 效果图 2. 原理 3. 源码 参考 这篇博客将介绍如何只使用基本的图 ...

  3. 使用 PyTesseract 和 OpenCV 从表格图像中提取文本

    Text Extraction from a Table Image, using PyTesseract and OpenCV – Fazlur Rahmanhttps://fazlurnu.com ...

  4. 使用 OpenCV 在 Python 中检测图像中的形状

    OpenCV 是一个开源库,主要用于处理图像和视频以识别形状.对象.文本等.它主要与 python 一起使用.在本文中,我们将了解如何检测图像中的形状.为此,我们需要OpenCV 的cv2.findC ...

  5. 使用 OpenCV 和 Tesseract 对图像中的感兴趣区域 (ROI) 进行 OCR

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 在这篇文章中,我们将使用 OpenCV 在图像的选定区域上应用 O ...

  6. 编程实战(4)——python识别图像中的坐标点并保存坐标数据

    编程实战(4)--python识别图像中的坐标点并保存坐标数据 文章目录 编程实战(4)--python识别图像中的坐标点并保存坐标数据 综述 代码思路 库的安装 图片预处理 图像细化 图像二极化 提 ...

  7. python从字符串中提取数字并转换为相应数据类型_python从PDF中提取数据的示例

    01 前言 数据是数据科学中任何分析的关键,大多数分析中最常用的数据集类型是存储在逗号分隔值(csv)表中的干净数据.然而,由于可移植文档格式(pdf)文件是最常用的文件格式之一,因此每个数据科学家都 ...

  8. OpenCV for Python之图像RIO与泛洪填充

    OpenCV for Python之图像RIO与泛洪填充 1 ROI与泛函填充 2 ROI 3 泛洪填充 Opencv4 官方文档 : https://docs.opencv.org/4.2.0/ O ...

  9. python一帧一帧读取视频_用Python从视频中提取每一帧的图片

    大家应该都有这样的情况:在看到某些视频的画面时感觉美如画,想截取下来却又烦于截图的繁琐,现在我就教大家使用Python提取视频中每一帧的画面,让大家不错过每一个精彩的瞬间! •语言:Python •所 ...

最新文章

  1. 吴军:既能得诺贝尔奖,又能生产高科技产品,美国的科研机制是如何运行的?...
  2. linux定时刷新窗口,Linux的屏幕刷新率问题 窗口调整问题
  3. 浅谈 SSD,eMMC,UFS
  4. ensp路由器无法启动_品胜云路由器Breed刷入详细教程,技巧和注意事项,功能大增...
  5. Apache + Tomcat集群配置详解(1)
  6. ubuntu(jdk配置)
  7. 好用的5款火狐浏览器必备插件,每一款都很实用
  8. 2021内职班的高考成绩怎么查询,2021山西地区高考查分时间
  9. 从头开始学习->JVM(九):垃圾收集(上)
  10. Leecode第九天,广度优先搜索之矩阵,腐烂的橘子
  11. 《Linux 高级路由与流量控制手册(2012)》第九章
  12. 使用SQL实现广告的精准投放
  13. 调侃计算机专业的句子,调侃生活的幽默句子
  14. 企企通助力浙江哈尔斯,构建智慧供应链管理企业
  15. 基于爬行动物搜索RSA优化LSTM的时间序列预测
  16. STM32F412擦除内部FLASH时间过长
  17. 开博第一篇,申请博客的理由
  18. vue-json-excel 使用方法 导出excel 详解 以及实际操作 ,通俗易懂
  19. 计算机函数求娶总和的函数是,奇穴重数究竟靠什么判定?
  20. Android webview 69,android-9.0-pie-env(safe-area-inset-top)无法在Android Pie WebView 69上运行

热门文章

  1. Python基本类型-字典
  2. 安装配置好openstack环境的虚拟机,须要改动ip时,在数据库中同步改动ip的方法...
  3. 【自定义控件】自定义属性
  4. A problem while linking c++ to python
  5. Visual Studio 文件没发布出来
  6. CKFinder根据用户设置权限,不同用户有自己的私有的、独立的文件夹
  7. java tomcat原理图,浅谈tomcat工作原理
  8. python画一个祝福别人生日快乐_分享快乐给朋友的生日快乐祝福语生日贺卡句子...
  9. 绿米空调伴侣接入iobroker_普通空调用它变成互联网智能空调,你觉得怎么样?...
  10. java web后端技能树_后端技能树修炼:CAP 定理