gpt4 book ai didi

c++ - OpenCV获取红色矩形区域的坐标

转载 作者:搜寻专家 更新时间:2023-10-31 01:39:43 26 4
gpt4 key购买 nike

我通过以下算法完成仅红色过滤的输出如下:

cv::Mat findColor(const cv::Mat & inputBGRimage, int rng=20)
{
// Make sure that your input image uses the channel order B, G, R (check not implemented).
cv::Mat mt1, mt2;
cv::Mat input = inputBGRimage.clone();
cv::Mat imageHSV; //(input.rows, input.cols, CV_8UC3);
cv::Mat imgThreshold, imgThreshold0, imgThreshold1; //(input.rows, input.cols, CV_8UC1);

assert( ! input.empty() );

// blur image
cv::blur( input, input, Size(11, 11) );

// convert input-image to HSV-image
cv::cvtColor( input, imageHSV, cv::COLOR_BGR2HSV );

// In the HSV-color space the color 'red' is located around the H-value 0 and also around the
// H-value 180. That is why you need to threshold your image twice and the combine the results.
cv::inRange( imageHSV, cv::Scalar( H_MIN, S_MIN, V_MIN ), cv::Scalar( H_MAX, S_MAX, V_MAX ), imgThreshold0 );

if ( rng > 0 )
{
// cv::inRange(imageHSV, cv::Scalar(180-rng, 53, 185, 0), cv::Scalar(180, 255, 255, 0), imgThreshold1);
// cv::bitwise_or( imgThreshold0, imgThreshold1, imgThreshold );
}
else
{
imgThreshold = imgThreshold0;
}

// cv::dilate( imgThreshold0, mt1, Mat() );

// cv::erode( mt1, mt2, Mat() );

return imgThreshold0;

}

这是输出:

enter image description here

我想检测矩形的四个坐标。如您所见,输出并不完美,我之前将 cv::findContourscv::approxPolyDP 结合使用,但它不再正常工作了。

是否有任何过滤器可以应用于输入图像(模糊、膨胀、腐 eclipse 除外)以使图像更好地进行处理?

有什么建议吗?

更新:

当我像这样使用 findContours 时:

findContours( src, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

double largest_area = 0;

for( int i = 0; i < contours.size(); i++) { // get the largest contour
area = fabs( contourArea( contours[i] ) );
if( area >= largest_area ) {
largest_area = area;
largestContours.clear();
largestContours.push_back( contours[i] );
}
}

if( largest_area > 5000 ) {
cv::approxPolyDP( cv::Mat(largestContours[0]), approx, 100, true );
cout << approx.size() << endl; /* ALWAYS RETURN 2 ?!? */
}

approxPolyDP 没有按预期工作。

最佳答案

我认为你的结果非常好,也许如果你使用 Image Moments 选择面积最大的轮廓然后找到较大轮廓的最小旋转矩形。

vector<cv::RotatedRect> cv::minRect( contours.size() );

for( size_t = 0; i < contours.size(); i++ )
{
minRect[i] = minAreaRect( cv::Mat(contours[i]) );
}

Rotated Rect类已经有一个 Point2f vector 来存储点。

RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);
Point2f vertices[4];
rRect.points(vertices);

for(int i = 0; i < 4; i++){
std::cout << vertices[i] << " ";
}

关于c++ - OpenCV获取红色矩形区域的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30780668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com