我可以从带正方形的二进制图像中获取角度
我想从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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数
cv2.minarearect()
返回3个元素的元组:前两个是旋转矩形的坐标和旋转角度。我们需要为每个轮廓打印此最后一个元素。在python中,要在
list
,array
或tuple
中获取最后一个元素,您可以做myList [-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
ortuple
you can domyList[-1]
.