【问题标题】:Detecting angles in a piechart using OpenCV technique使用 OpenCV 技术检测饼图中的角度
【发布时间】:2021-02-08 15:53:25
【问题描述】:

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

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

已编辑 这是作为示例的图像:

PieChat

This is a result of Contours

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

【问题讨论】:

  • 你可以发布饼图图像吗?
  • 请检查。我在帖子中添加了图片。

标签: python opencv angle opencv-python opencv-contour


【解决方案1】:

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

我得到的输出是:

[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”以移过代码的颜色查找部分。

【讨论】:

  • 你拯救了我的一天。谢谢
  • 有什么方法可以在特定区域内自动获取此点击事件?有时我的价值为零..
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 2017-09-24
  • 1970-01-01
  • 2011-11-30
  • 2017-05-04
  • 2012-06-05
相关资源
最近更新 更多