如何识别与我的物体相关的轮廓并找到其几何质心 [英] How to identify contours associated with my objects and find their geometric centroid

查看:200
本文介绍了如何识别与我的物体相关的轮廓并找到其几何质心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明和背景信息:

编辑:约束:法兰上的红色会随时间而变化,因此,除非它很坚固,否则我暂时不尝试使用颜色识别来识别我的对象.另外,外部照明也是一个因素,因为将来它将在室外区域使用.

EDIT: Constraints: The red coloring on the flange changes over time, so I'm not trying to use color recognition to identify my object at this moment unless it can be robust. Addition,ally external illumination my be a factor since this will be in an outdoor area in the future.

我有RGB深度相机,有了它,我就能捕捉到这个场景.每个像素(x,y)都有一个深度值.

I have RGB-Depth camera and with it, I'm able to capture this scene. Where each pixel (x,y) has a depth value.

在与我的图像相关联的深度图上应用渐变幅度过滤器,我可以得到以下边缘图.

Applying a gradient magnitude filter to the depth map associated with my image I'm able to get the following edge map.

如果梯度幅度的大小不为零,则将其值为0.黑色(255)表示与0(均匀深度或平坦表面)相关的幅度值.

The gradient magnitudes are given the value of 0 if they had a magnitude that wasn't zero. Black (255) is for magnitude values associated with 0 (homogenous depth or a flat surface).

从该边缘贴图中我拨出了边缘,因此拾取轮廓将更加容易.

From this edge map I dialated the edges so picking up the contours would be easier.

然后,我在图像中找到了轮廓,并尝试仅绘制5个最大轮廓.

Then I found the contours in the image and tried to plot just the 5 biggest contours.

问题

是否有办法可靠地找到与我的物体(红色框和金属固定装置)相关的轮廓,然后找到其几何质心?我一直遇到我可以在图像中找到轮廓的问题,但是我无法选择性地筛选出那些是我的物体而不是噪声的轮廓.

Is there a way to reliably find the contours associated with my objects (the red box and metal fixture) and then find their geometric centroid? I keep running into the issue that I can find contours in the image, but I have no way of selectively screening for the contours that are my objects and not noise.

我已经提供了用于图像处理的图像,但是由于某些原因, OpenCV将图像另存为黑色图像,并且在您使用...阅读时将其保存...

I have provided the image I used for the image processing, but for some reason, OpenCV saves the image as a black image, and when you read it in using...

gray = cv2.imread('GRAYTEST.jpeg', cv2.IMREAD_GRAYSCALE)

它显示为蓝色,而不是我显示的二进制的黑白图像.对此感到抱歉.

it appears blue-ish and not a binary white/black image as I show. So sorry about that.

这是图像:

对不起,我不知道为什么将它保存为黑色图像,但是如果您在OpenCV中阅读它,它应该以与渐变幅度"图相同的线条显示.

Sorry, I don't know why it saved just as a black image, but if you read it in OpenCV it should show up with the same lines as "magnitude of gradients" plot.

我的密码

    gray = cv2.imread('GRAYTEST.jpeg', cv2.IMREAD_GRAYSCALE)
    plt.imshow(gray)
    plt.title('gray start image')
    plt.show()

    blurred = cv2.bilateralFilter(gray, 8, 25, 25)  # blurr image while preserving edges
    kernel = np.ones((3, 3), np.uint8)  # define a kernel (block) to apply filters to

    dialated = cv2.dilate(blurred, kernel, iterations=1)
    plt.title('dialated')
    plt.imshow(dialated)
    plt.show()

    #Just performs a canny edge dectection on an image
    edges_empty = self.Commons.CannyE_Auto(dialated)  # Canny edge image for some sigma
    #makes an empty image using the same diemensions of the given image
    empty2 = self.Commons.make_empty(gray)

    _, contours, _ = cv2.findContours(edges_empty, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:5]  # get largest five contour area
    cv2.drawContours(empty2, cnts, -1, (255, 0, 0), thickness=1)

    plt.title('contours')
    plt.imshow(empty2)
    plt.show()

推荐答案

  1. 我没有对已阈值化的图像执行模糊操作,拨号,Canny边缘检测,而是对原始图像进行了轮廓检测.

  1. Instead of performing the blurring operation, dialation, canny edge detection on my already thresholded image, I just performed the contour detection on my original image.

然后我可以通过修改find​​Contour命令为图像轮廓找到合适的轮廓.

Then I was able to find a decent contour for the outline of my image by modifying findContour command.

_,轮廓,_ = cv2.findContours(灰色,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

_, contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

将cv2.RETR_TREE替换为cv2.RETR_EXTERNAL ,我只能获取与对象轮廓关联的轮廓,而不是尝试在对象内获取轮廓.切换到cv2.CHAIN_APPROX_NONE并没有显示任何明显的改进,但是它可以为更复杂的几何图形提供更好的轮廓.

replacing cv2.RETR_TREE with cv2.RETR_EXTERNAL I was able to get only the contours that were associated with an object's outline, rather than trying to get contours within the object. Switching to cv2.CHAIN_APPROX_NONE didn't show any noticeable improvements, but it may provide better contours for more complex geometries.

        for c in cnts:
        # compute the center of the contour
        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])

        # draw the contour and center of the shape on the image
        cv2.drawContours(empty2, [c], -1, (255, 0, 0), thickness=1)
        perimeter = np.around(cv2.arcLength(c, True), decimals=3)
        area = np.around(cv2.contourArea(c), decimals=3)

        cv2.circle(empty2, (cX, cY), 7, (255, 255, 255), -1)
        cv2.putText(empty2, "center", (cX - 20, cY - 20),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

        cv2.putText(empty2, "P:{}".format(perimeter), (cX - 50, cY - 50),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

        cv2.putText(empty2, "A:{}".format(area), (cX - 100, cY - 100),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

使用上面的代码,我能够标记每个轮廓的质心以及有关每个轮廓的周长和面积的信息.

Using the above code I was able to label centroid of each contour as well as information about each contours perimeter and area.

但是,我无法执行选择哪个轮廓作为所需轮廓的测试.我有一个想法,可以在更理想的环境中捕获我的物体,并找到其质心,周长和相关区域.这样,当我找到一个新的轮廓时,可以将其与我的已知值相距多近.

However, I was unable to perform a test that would select which contour was my desired contour. I have an idea to capture my object in a more ideal setting and find its centroid, perimeter, and associated area. This way when I find a new contour I can compare it with how close it is to my known values.

我认为这种方法可以去除太大或太小的轮廓.

I think this method could work to remove contours that are too large or too small.

如果有人知道更好的解决方案,那就太好了!

If anyone knows of a better solution that would be fantastic!

这篇关于如何识别与我的物体相关的轮廓并找到其几何质心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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