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

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

问题描述

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

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天全站免登陆