Detection of the outer extreme edge of an image and plotting a graph based on it

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

This is currently being done in Python using Intel RealSense R200 (although it can be assumed that I am using image input).

I am trying to detect the edges of the left image in order to get the center image, trying to extract the outer contour (right image):

Gmhqq.pngVQrvO.pngXYza8.png

Knowing that the sides of the two pipes exiting the corner will be parallel (two orange sides and two green sides are parallel to the same color).

ViF85.png

... I'm trying to build 2 loci of points equidistant from two pairs of colors, and then "extrapolate to the middle" to calculate the angle:

Gtema.png

, , . .

+4
6

, , .

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))

- white padded image 3.

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))

Black Augmented Image

4. , -

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

closed image 5. Canny -

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

channel border image 6. openCV HoughLinesP -

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)

enter image description here 7. . , (x2, x1), . . , , .

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)

image of horizontal lines 8. , y1 , .

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)

image of vertical lines 9. -

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

final lines

, 4 .

+1

, Hough transform ( Theta-Rho) , . , .

OpenCV HoughLines

enter image description here

( ) 0..180 count>1. -, 86-87 175-176 ( 90 )

line 
angle : count
84: 3
85: 3
86: 8
87: 12
88: 3
102: 3
135: 3
140: 2
141: 2
165: 2
171: 4
172: 2
173: 2
175: 7
176: 17
177: 3

. Delphi HoughLines . Python

+2

, , . HoughLine a bad choice .


, (3*w/4, h*2/3), :

enter image description here

img = cv2.imread("img04.jpg", 0)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
H,W = img.shape[:2]
threshed[:H*2//3,:W*3//4] = 0

cv2.imwrite("regions.png", threshed)

, .

+2

, ​​ .

, , .

. , . .

: , .

enter image description here

+1

, , . - , , , . , - . , , .

  • ( . FAST)
  • -
  • (?)

.

90.868604
42.180990
46.950407

c++. , .

triangle

// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
static double angle( Point2f pt1, Point2f pt2, Point2f pt0 )
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}

int _tmain(int argc, _TCHAR* argv[])
{
    Mat rgb = imread("GmHqQ.jpg");

    Mat im;
    cvtColor(rgb, im, CV_BGR2GRAY);

    Ptr<FeatureDetector> detector = FastFeatureDetector::create();
    vector<KeyPoint> keypoints;
    detector->detect(im, keypoints);

    drawKeypoints(im, keypoints, rgb, Scalar(0, 0, 255));

    vector<Point2f> points;
    for (KeyPoint& kp: keypoints)
    {
        points.push_back(kp.pt);
    }

    vector<Point2f> triangle(3);
    minEnclosingTriangle(points, triangle);

    for (size_t i = 0; i < triangle.size(); i++)
    {
        line(rgb, triangle[i], triangle[(i + 1) % triangle.size()], Scalar(255, 0, 0), 2);
        printf("%f\n", acosf( angle(triangle[i], 
            triangle[(i + 1) % triangle.size()], 
            triangle[(i + 2) % triangle.size()]) ) * 180 / CV_PI);
    }

    return 0;
}
+1

, , , , , . , , , . , .

-1

Source: https://habr.com/ru/post/1690622/


All Articles