检测图像的最外边缘并基于它进行绘图 [英] Detecting outer most-edge of image and plotting based on it

查看:256
本文介绍了检测图像的最外边缘并基于它进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个可以通过图像计算肘关节角度的项目。我正在努力的部分是图像处理。

I'm working on a project that can calculate the angle of an elbow joint by image. The part I'm struggling on is the image processing.

目前使用英特尔实感R200在Python中执行此操作(尽管可以认为我使用的是图像输入)。

Currently doing this in Python using an Intel RealSense R200 (although it can be taken that I'm using an image input).

我正在尝试检测左图像的边缘,这样我就可以得到中心图像,旨在提取外部轮廓(右图像):

I'm attempting to detect the edges of the left image, such that I can get the center image, aiming to extract the outer contour (right image):

知道这一点从角度出来的两个管道的两侧将是平行的(两个橙色侧面和两个绿色侧面平行于相同的颜色)...

Knowing that the sides of the two pipes coming out of the angle will be parallel (two orange sides and two green sides are parallel to the same colour)...

...我是试图从两对颜色等距离构建2个点的位点然后'推断到中间'以计算角度:

... I'm trying to construct 2 loci of points equidistant from the two pairs of colours and then 'extrapolate to the middle' in order to calculate the angle:

我已经到了第二张图像,并且不可靠,直到第三张图像。我非常乐意接受建议,非常感谢任何帮助。

I've got as far as the second image and, unreliably, as far as the third image. I'm very open to suggestions and would be hugely grateful of any assistance.

推荐答案

我会用以下方法试试找到问题中提供的四行。

I would use the following approach to try and find the four lines provided in the question.

1。阅读图片,并将其转换为灰度

import cv2
import numpy as np
rgb_img = cv2.imread('pipe.jpg')
height, width = gray_img.shape
gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY)

2。在图像顶部添加一些白色填充(只是为了有一些额外的背景)

white_padding = np.zeros((50, width, 3))
white_padding[:, :] = [255, 255, 255]
rgb_img = np.row_stack((white_padding, rgb_img))

结果图片 -
3.反转灰度图像并应用黑色填充到顶部

Resultant image - 3. Invert the gray scale image and apply black padding to the top

gray_img = 255 - gray_img
gray_img[gray_img > 100] = 255
gray_img[gray_img <= 100] = 0
black_padding = np.zeros((50, width))
gray_img = np.row_stack((black_padding, gray_img))

4.使用形态学收盘来填补图像中的漏洞 -

4.Use Morphological closing to fill the holes in the image -

kernel = np.ones((30, 30), np.uint8)
closing = cv2.morphologyEx(gray_img, cv2.MORPH_CLOSE, kernel)


5.使用Canny边缘检测在图像中找到边缘 -

5. Find edges in the image using Canny edge detection -

edges = cv2.Canny(closing, 100, 200)


6.现在,我们可以使用openCV的 HoughLinesP 函数来查找给定图像中的行 -

6. Now, we can use openCV's HoughLinesP function to find lines in the given image -

minLineLength = 500
maxLineGap = 10
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 50, None, 50, 100)
all_lines = lines[0]
for x1,y1,x2,y2 in lines[0]:
    cv2.line(rgb_img,(x1,y1),(x2,y2),(0,0,255),2)


7.现在,我们必须找到最右边的两条水平线和两条最底部的垂直线。对于水平线,我们将使用两个(x2,x1)按降序对线进行排序。此排序列表中的第一行将是最右边的垂直线。跳过这一点,如果我们采用接下来的两行,它们将是最右边的水平线。

7.Now, we have to find the two rightmost horizontal lines, and the two bottommost vertical lines. For the horizontal lines, we will sort the lines using both (x2, x1), in descending order. The first line in this sorted list will be the rightmost vertical line. Skipping that, if we take the next two lines, they will be the rightmost horizontal lines.

all_lines_x_sorted = sorted(all_lines, key=lambda k: (-k[2], -k[0]))
for x1,y1,x2,y2 in all_lines_x_sorted[1:3]:
    cv2.line(rgb_img,(x1,y1),(x2,y2),(0,0,255),2)


8.同样,行可以使用y1坐标按降序排序,排序列表中的前两行将是最下面的垂直行。

8. Similarly, the lines can be sorted using the y1 coordinate, in descending order, and the first two lines in the sorted list will be the bottommost vertical lines.

all_lines_y_sorted = sorted(all_lines, key=lambda k: (-k[1]))
for x1,y1,x2,y2 in all_lines_y_sorted[:2]:
    cv2.line(rgb_img,(x1,y1),(x2,y2),(0,0,255),2)


9.两行图像 -

9. Image with both lines -

final_lines = all_lines_x_sorted[1:3] + all_lines_y_sorted[:2]

因此,获得这4行可以帮助您完成剩下的任务。

Thus, obtaining these 4 lines can help you finish the rest of your task.

这篇关于检测图像的最外边缘并基于它进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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