使用 OpenCV 技术检测饼图中的角度

Detecting angles in a piechart using OpenCV technique

我正在尝试使用 OpenCV 检测 piechat 图像中的角度。我使用 HoughlinesP 检测线条并尝试找到线条之间的角度,但它没有显示直角,如果饼图中有任何直线(如 180 度),就会出现问题。

我使用 findContours 尝试了另一种技术。它会给我一个饼图的边界区域,但我卡在那里,我不知道下一步应该做什么。

已编辑 这是图像示例:

PieChat

This is a result of Contours

任何帮助,您提供的任何路径都将不胜感激。 提前致谢。

我正在对切片的颜色值进行阈值处理并计算切片中的像素数。我将像素数转换为百分比,然后将百分比转换为角度。

我得到的输出是:

[54.4, 18.3, 27.3]

[195.84, 65.88, 98.28]

import cv2
import numpy as np

# get pixel value under mouse
def clicky(event, x, y, flags, params):
    if event == cv2.EVENT_LBUTTONUP:
        global h;
        print(h[y][x]);

# load image
img = cv2.imread("pie.jfif");

# resize
scale = 0.5;
h, w = img.shape[:2];
h = int(h*scale);
w = int(w*scale);
img = cv2.resize(img, (w,h));

# hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV);
h,s,v = cv2.split(hsv);
h = cv2.medianBlur(h, 5);

# find values
cv2.namedWindow("Hue");
cv2.setMouseCallback("Hue", clicky);
while True:
    cv2.imshow("Hue", h);
    if cv2.waitKey(1) == ord('q'):
        break;

# threshold (102, 60, 14)
slices = [];
ranges = [102, 60, 14];
for r in ranges:
    mask = cv2.inRange(h, r-1, r+1);
    slices.append(np.sum(mask == 255));

# convert slices to percentages
percents = [];
total = sum(slices);
for s in slices:
    percent = (s / total) * 100;
    percents.append(round(percent, 1));
print(percents);

# convert to angles
angles = [];
total = 360;
for p in percents:
    angles.append(p * total / 100);
print(angles);

在图像上按 'q' 以移过代码的颜色查找部分。