c – OpenCV:读取矩阵值
发布时间:2020-12-16 10:55:00 所属栏目:百科 来源:网络整理
导读:我想计算背景图像中只有黑色和白色的白点数.我有这样的代码: int count = 0; for ( int j = 0; j Image.rows; j ++ ) { for ( int i = 0; i Image.cols; i ++ ) { if ( Image.atint(i,j) = 150 ) { count ++ ; } } } 出于某种原因,上面的代码不起作用,它只
我想计算背景图像中只有黑色和白色的白点数.我有这样的代码:
int count = 0; for ( int j = 0; j < Image.rows; j ++ ) { for ( int i = 0; i < Image.cols; i ++ ) { if ( Image.at<int>(i,j) >= 150 ) { count ++ ; } } } 出于某种原因,上面的代码不起作用,它只是停止反应.我检查了,“if(Image.at(i,j)> = 150)”这一行导致了问题.我的“图像”是“cv :: Mat”,带有“CV_8UC3”类型.有人可以帮助我吗?谢谢. 解决方法
除了我对Robin的回答的评论之外,您的错误是您尝试以int为单位访问CV_8UC3类型的图像.如果你想检查灰度级别,可以这样做(注意“unsigned char”而不是“int”,如Robin的回答).
cv::Mat greyscale; cv::cvtColor(image,grayscale,CV_RGB2GRAY); // either,most elegant: int count = cv::countNonZero(greyscale >= 150); // or,copied from Robin's answer: int count = 0; for(int i = 0; i < greyscale.rows; ++i) { const unsigned char* row = greyscale.ptr<unsigned char>(i); for(int j = 0; j < greyscale.cols; j++) { if (row[j] >= 150) ++count; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容