简体   繁体   English

使用opencv跟踪边界

[英]tracking boundary using opencv

I am trying to track/ trace boundary points in sequence from the following binary image: 我正在尝试从以下二进制图像中顺序跟踪/跟踪边界点: 在此处输入图片说明

I am using OpenCV (python). 我正在使用OpenCV(python)。 I am using three ways: 我正在使用三种方式:

  1. Apply Canny edge detection to detect edge. 应用Canny边缘检测来检测边缘。 Problem is how to get the sequence of points? 问题是如何获得积分顺序? It's work fine but it's very hard to get the sequence of boundary points 很好,但是很难获得边界点的顺序
  2. Ideal option is to detect contours on the binary image. 理想的选择是检测二进制图像上的轮廓。 Because contours return the boundary points in sequence. 由于轮廓按顺序返回边界点。 But openCV contour method is not detecting the boundary as shown in the results. 但是openCV等高线方法未检测到边界,如结果所示。 Why is this happening? 为什么会这样呢?
  3. Detect contours on the Canny edge. 检测Canny边缘上的轮廓。 Still some boundary is missed ?? 仍然缺少一些边界?

在此处输入图片说明

Can anyone help me what's going on with OpenCV contours? 谁能帮我解决OpenCV轮廓的问题吗? Why it is not tracking the complete boundary. 为什么它没有追踪完整的边界。 I am detecting contours as follows: 我正在检测轮廓,如下所示:

 contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE ,cv2.CHAIN_APPROX_SIMPLE) 

where thresh1 is the binary image thresh1是二进制映像

Given your simple contour, I'm not sure why you are using RETR_TREE since there are no nested contours. 给定您简单的轮廓,由于不确定嵌套轮廓,因此我不确定您为什么要使用RETR_TREE。 Have you tried using RETR_EXTERNAL instead ? 您是否尝试过使用RETR_EXTERNAL?

From OpenCV docs: 从OpenCV文档:

CV_RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of nested contours. CV_RETR_TREE检索所有轮廓,并重建嵌套轮廓的完整层次。

CV_RETR_EXTERNAL retrieves only the extreme outer contours. CV_RETR_EXTERNAL仅检索极端的外部轮廓。

Also, note that CHAIN_APPROX_SIMPLE will not enumerate all points on the boundary, it will try to simplify the contour in particular it won't return multiple, sequential horizontal, vertical or diagonal points. 另外,请注意CHAIN_APPROX_SIMPLE 不会枚举边界上的所有点,而是会尝试简化轮廓,特别是它不会返回多个连续的水平,垂直或对角点。 If you want all points, use CV_CHAIN_APPROX_NONE which will force the contours algorithm to find all boundary points. 如果需要所有点,请使用CV_CHAIN_APPROX_NONE,这将强制轮廓算法找到所有边界点。

CV_CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal seg- ments and leaves only their end points. CV_CHAIN_APPROX_SIMPLE压缩水平,垂直和对角线段,仅保留其端点。 For example, an up-right rectangular contour is encoded with 4 points. 例如,一个直立的矩形轮廓编码有4个点。

CV_CHAIN_APPROX_NONE stores absolutely all the contour points. CV_CHAIN_APPROX_NONE绝对存储所有轮廓点。

The following code works on your image and finds 132 points: 以下代码在您的图像上起作用,并找到132点:

// Load original image as grey scale
Mat image = imread("imagename.png", IMREAD_GRAYSCALE);

vector<vector<Point>> contours;
Mat hierarchy;
findContours(image, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

Swapping CV_CHAIN_APPROX_SIMPLE for CV_CHAIN_APPROX_NONE results in one contour with 737 points being returned. 将CV_CHAIN_APPROX_SIMPLE交换为CV_CHAIN_APPROX_NONE会导致返回一个轮廓,并返回737点。

You haven't included the full context of your code but note that findContours does modify the source image so if you are making multiple calls in succession using the same source image this may be something to watch out for. 您尚未包括代码的完整上下文,但是请注意,findContours确实会修改源图像,因此,如果您使用同一源图像连续进行多个调用,则可能需要注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM