如何从OpenCV中已标记的图像中获取区域属性? [英] How to get region properties from image that is already labeled in OpenCV?

查看:185
本文介绍了如何从OpenCV中已标记的图像中获取区域属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在OpenCV中使用分水岭算法为图像添加标签(类似于本教程: https://docs.opencv.org/3.4/d3/db4/tutorial_py_watershed.html ),这样一来,我最终获得了一个标签数组,其中每个区域都有一个与其标签相对应的整数值.现在,我想获取每个区域的边界框和区域的坐标.

I am labeling images using the watershed algorithm in OpenCV (similar to this tutorial: https://docs.opencv.org/3.4/d3/db4/tutorial_py_watershed.html) such that at the end I obtain an array of labels where each region has an integer value corresponding to its label. Now, I want to obtain the coordinates of the bounding boxes and areas of each region.

我知道使用skimage.measure.regionprops()可以很容易地做到这一点,但是考虑到执行速度,我希望在不导入skimage的情况下实现这一点,理想情况下直接使用OpenCV.

I know this is easily done with skimage.measure.regionprops() but for considerations of speed of execution I would like to achieve this without importing skimage, ideally directly with OpenCV.

我尝试使用cv2.connectedComponentsWithStats(),但似乎仅在图像为二进制的情况下有效,而不是在已经定义标签的情况下.

I have tried using cv2.connectedComponentsWithStats() but it only seems to work if the image is binary not if the labels are already defined.

我尝试对标记的图像进行二值化处理,然后按如下所示用connectedComponentsWithStats()重新标记(请注意,在这种情况下背景的标签为1,我想将其删除):

I have tried to binarize the labeled image and then relabel it with connectedComponentsWithStats() as follows (note that the background has a label of 1 in this case and I want to remove it):

segmented = cv2.watershed(image.astype('uint8'), markers)

segmented_bin = segmented.copy()
segmented_bin[segmented < 2] = 0
segmented_bin[segmented > 1] = 255
num_labels, label_image, stats, centroids = cv2.connectedComponentsWithStats(segmented_bin.astype('uint8'), 4, cv2.CV_32S)

但是这种方法会合并没有被背景分开的区域,这不是理想的效果.

But this approach merges regions that are not separated by background which is not the desired effect.

本质上,我想知道是否有类似于connectedComponentsWithStats()的功能来处理已标记的图像?

Essentially I would like to know if there is a function similar to connectedComponentsWithStats() that deals with already labeled images?

推荐答案

如果其他人有兴趣,由于无法获取cv2.connectedComponentsWithStats(),我最终恢复为skimage.measure.regionprops().每个图像的时间开销只有几十毫秒.

If anybody else is interested, I ended up reverting to skimage.measure.regionprops() since I could not get cv2.connectedComponentsWithStats(). The time overhead is only in the tens of millisecond per image.

这篇关于如何从OpenCV中已标记的图像中获取区域属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆