我可以从带正方形的二进制图像中获取角度

发布于 2025-02-08 10:57:10 字数 2080 浏览 4 评论 0原文

我想从Python的二进制图像中获取角度,但我不知道如何将此CPP转换为Python。

如果有其他方法可以从此“检查的图像”中检测角度,例如使用BLOB检测,请告诉我。

示例图像:

检查的图像

示例CPP代码:我

cv::Mat input = cv::imread("../inputData/rectangles.png");

cv::Mat gray;
cv::cvtColor(input,gray,CV_BGR2GRAY);

// since your image has compression artifacts, we have to threshold the image
int threshold = 200;
cv::Mat mask = gray > threshold;

cv::imshow("mask", mask);

// extract contours
std::vector<std::vector<cv::Point> > contours;
cv::findContours(mask, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

for(int i=0; i<contours.size(); ++i)
{
    // fit bounding rectangle around contour
    cv::RotatedRect rotatedRect = cv::minAreaRect(contours[i]);

    // read points and angle
    cv::Point2f rect_points[4]; 
    rotatedRect.points( rect_points );

    float  angle = rotatedRect.angle; // angle

    // read center of rotated rect
    cv::Point2f center = rotatedRect.center; // center

    // draw rotated rect
    for(unsigned int j=0; j<4; ++j)
        cv::line(input, rect_points[j], rect_points[(j+1)%4], cv::Scalar(0,255,0));

    // draw center and print text
    std::stringstream ss;   ss << angle; // convert float to string
    cv::circle(input, center, 5, cv::Scalar(0,255,0)); // draw center
    cv::putText(input, ss.str(), center + cv::Point2f(-25,25), 
    cv::FONT_HERSHEY_COMPLEX_SMALL,1, cv::Scalar(255,0,255)); // print angle
}

的代码:我的代码:我的代码:

contours_yellow, hierarchy = cv2.findContours(mask_yellow, cv2.RETR_TREE, 
cv2.CHAIN_APPROX_SIMPLE)

yellow_contours = cv2.drawContours(mask_yellow, contours_yellow, -1, (0,255,0), 3)

angle = 0

for contour in yellow_contours :
{
    #fit bounding rectangle around contour
    RotatedRect = minAreaRect(contours_yellow[value])

    #read point and angle
    point2f rect_points[4]
    rotatedRect.points(rect_points)

    angle = rotatedRect.angle; # angle


}

    print (angle)

I want to get the angles from the binary image in python, but I don't know how to convert this cpp to python.

If there is a other way to detect angles from this 'inspected image' for example with blob detection please let me know.

example image:

inspected image

example cpp code:

cv::Mat input = cv::imread("../inputData/rectangles.png");

cv::Mat gray;
cv::cvtColor(input,gray,CV_BGR2GRAY);

// since your image has compression artifacts, we have to threshold the image
int threshold = 200;
cv::Mat mask = gray > threshold;

cv::imshow("mask", mask);

// extract contours
std::vector<std::vector<cv::Point> > contours;
cv::findContours(mask, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

for(int i=0; i<contours.size(); ++i)
{
    // fit bounding rectangle around contour
    cv::RotatedRect rotatedRect = cv::minAreaRect(contours[i]);

    // read points and angle
    cv::Point2f rect_points[4]; 
    rotatedRect.points( rect_points );

    float  angle = rotatedRect.angle; // angle

    // read center of rotated rect
    cv::Point2f center = rotatedRect.center; // center

    // draw rotated rect
    for(unsigned int j=0; j<4; ++j)
        cv::line(input, rect_points[j], rect_points[(j+1)%4], cv::Scalar(0,255,0));

    // draw center and print text
    std::stringstream ss;   ss << angle; // convert float to string
    cv::circle(input, center, 5, cv::Scalar(0,255,0)); // draw center
    cv::putText(input, ss.str(), center + cv::Point2f(-25,25), 
    cv::FONT_HERSHEY_COMPLEX_SMALL,1, cv::Scalar(255,0,255)); // print angle
}

my code:

contours_yellow, hierarchy = cv2.findContours(mask_yellow, cv2.RETR_TREE, 
cv2.CHAIN_APPROX_SIMPLE)

yellow_contours = cv2.drawContours(mask_yellow, contours_yellow, -1, (0,255,0), 3)

angle = 0

for contour in yellow_contours :
{
    #fit bounding rectangle around contour
    RotatedRect = minAreaRect(contours_yellow[value])

    #read point and angle
    point2f rect_points[4]
    rotatedRect.points(rect_points)

    angle = rotatedRect.angle; # angle


}

    print (angle)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-02-15 10:57:10

函数cv2.minarearect()返回3个元素的元组:前两个是旋转矩形的坐标和旋转角度。我们需要为每个轮廓打印此最后一个元素。

在python中,要在listarraytuple中获取最后一个元素,您可以做myList [-1]

img = cv2.imread('white_boxes.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold the image
th = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]

# find contours 
contours, _ = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
  rect = cv2.minAreaRect(c)
  print('Angle:', rect[-1])

The function cv2.minAreaRect() returns a tuple of 3 elements: the first two are the coordinates of the rotated rectangle and the angle of rotation. We need to print this last element for each contour.

In Python, to obtain the last element in a list, array or tuple you can do myList[-1].

img = cv2.imread('white_boxes.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold the image
th = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]

# find contours 
contours, _ = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
  rect = cv2.minAreaRect(c)
  print('Angle:', rect[-1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文