gpt4 book ai didi

opencv - OpenCV:检索轮廓中心的颜色

转载 作者:行者123 更新时间:2023-12-02 17:51:31 25 4
gpt4 key购买 nike

我试图使用OpenCV检测黑色图像中一组形状的颜色,为此我使用了Canny检测。但是,颜色输出总是返回为黑色。

std::vector<std::pair<cv::Point, cv::Vec3b> > Asteroids::DetectPoints(const cv::Mat &image)
{
cv::Mat imageGray;
cv::cvtColor( image, imageGray, CV_BGR2GRAY );
cv::threshold(imageGray, imageGray, 1, 255, cv::THRESH_BINARY);
cv::Mat canny_output;

std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;

int thresh = 10;
// Detect edges using canny
cv::Canny( imageGray, canny_output, thresh, thresh*2, 3 );
// Find contours
cv::findContours( canny_output, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cv::Point(0, 0) );

std::vector<std::pair<cv::Point, cv::Vec3b> > points;

for(unsigned int i = 0; i < contours.size(); i++ )
{
cv::Rect rect = cv::boundingRect(contours[i]);
std::pair<cv::Point, cv::Vec3b> posColor;
posColor.first = cv::Point( rect.tl().x + (rect.size().width / 2), rect.tl().y + (rect.size().height / 2));

posColor.second = image.at<cv::Vec3b>( posColor.first.x, posColor.first.y );

//Dont add teh entry to the list if one with the same color and position is already pressent,
//The contour detection sometimes returns duplicates
bool isInList = false;
for(unsigned int j = 0; j < points.size(); j++)
if(points[j].first == posColor.first && points[j].second == posColor.second)
isInList = true;

if(!isInList)
points.push_back( posColor );
}
return points;
}

我知道位置或沿这些方向的东西一定是一个问题,但我不知道是什么

最佳答案

我可能是错的,但是浮现在脑海:

这不应该读

posColor.second = image.at<cv::Vec3b>(posColor.first.y, posColor.first.x);

而不是像你那样反过来吗?

矩阵符号,不是笛卡尔符号?

关于opencv - OpenCV:检索轮廓中心的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20303551/

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