C++ OpenCV:读取矩阵值
我想计算背景图像中只有黑白的白点数量。我有这样一个代码:C++ OpenCV:读取矩阵值,c++,opencv,matrix,background-image,pixel,C++,Opencv,Matrix,Background Image,Pixel,我想计算背景图像中只有黑白的白点数量。我有这样一个代码: 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 ++ ;
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 ++ ;
}
}
}
int count=0;
对于(int j=0;j=150)
{
计数++;
}
}
}
出于某种原因,上面的代码不起作用,只是停止了反应。我检查了,行“if(Image.at(I,j)>=150)”导致了问题。我的“图像”是一个“cv::Mat”,带有“cv_8UC3”类型。有人能帮我吗?多谢各位 Write
Image.at(j,i)
notImage.at(i,j)
我认为您必须访问列前面的行,这意味着您应该交换I和j。
将
if(Image.at(i,j)>=150)
替换为if(Image.at(j,i)>=150)
不过,有更简单的方法可以使用垫子OpenCV提供了一个类似STL的迭代器,它易于使用,并且如果您想访问所有元素,它也非常易于使用。例如:
int count = 0;
MatConstIterator_<int> it = Image.begin<int>(), it_end = Image.end<int>();
for(; it != it_end; ++it)
if ((*it) >= 150)
++count;
int count=0;
matconditor_uu=Image.begin(),it_uend=Image.end();
for(;it!=it_end;++it)
如果((*it)>=150)
++计数;
最后但并非最不重要的一点是,您还可以获取指向每一行的指针,并通过plain[]运算符访问数据:
int count = 0;
for(int i = 0; i < Image.rows; ++i) {
const int* Ii = Image.ptr<int>(i);
for(int j = 0; j < Image.cols; j++) {
if (Ii[j] >= 150)
++count;
}
}
int count=0;
对于(int i=0;i=150)
++计数;
}
}
我认为这要整洁得多:
Mat result;
threshold(Image,result,150,255,THRESH_BINARY);
int white_count = countNonZero(result);
除了我对Robin的回答的评论之外,您的错误是尝试访问CV_8UC3类型的图像作为ints。如果你想检查灰度,可以这样做(注意“unsigned char”而不是“int”,如Robin的回答)
cv::Mat灰度;
cv::CVT颜色(图像、灰度、cv_RGB2GRAY);
//无论是哪一种,都是最优雅的:
int count=cv::countNonZero(灰度>=150);
//或者,抄袭罗宾的答案:
整数计数=0;
对于(int i=0;i=150行)
++计数;
}
}
您可以使用opencv字节向量(无符号字符像素)访问CV_8UC3像素!
在这种情况下,您可以进行以下操作(现在还可以使用一些特殊的颜色阈值)
int通道=0;
Image.at(行、列)[频道]
有很多方法可以访问cv::Mat图像,
如果要直接访问彩色图像(CV_8UC3),
可通过以下方式实施:
int count = 0;
int threshold = 150;
for(int j = 0; j < img.rows; j++) {
for(int i = 0; i < img.cols; i++) {
//white point which means that the point in every channel(BGR)
//are all higher than threshold!
if(img.ptr<cv::Vec3b>(j)[i][0] > threshold &&
img.ptr<cv::Vec3b>(j)[i][1] > threshold
img.ptr<cv::Vec3b>(j)[i][2] > threshold ) {
count++;
}
}
}
cv::Mat img;
cv::cvtColor(src,img,CV_BGR2RGB);
int count = 0;
int threshold = 150;
for(int j = 0; j < img.rows; j++) {
for(int i = 0; i < img.cols; i++) {
if(img.ptr<uchar>(j)[i] > threshold) {
count++;
}
}
}
int count=0;
int阈值=150;
对于(int j=0;j阈值和
img.ptr(j)[i][1]>阈值
img.ptr(j)[i][2]>阈值){
计数++;
}
}
}
但我建议,若你们只想计算白点,你们可以把图像转换成灰度
(CV_8UC1),并执行以下操作:
int count = 0;
int threshold = 150;
for(int j = 0; j < img.rows; j++) {
for(int i = 0; i < img.cols; i++) {
//white point which means that the point in every channel(BGR)
//are all higher than threshold!
if(img.ptr<cv::Vec3b>(j)[i][0] > threshold &&
img.ptr<cv::Vec3b>(j)[i][1] > threshold
img.ptr<cv::Vec3b>(j)[i][2] > threshold ) {
count++;
}
}
}
cv::Mat img;
cv::cvtColor(src,img,CV_BGR2RGB);
int count = 0;
int threshold = 150;
for(int j = 0; j < img.rows; j++) {
for(int i = 0; i < img.cols; i++) {
if(img.ptr<uchar>(j)[i] > threshold) {
count++;
}
}
}
cv::Mat img;
cv::CVT颜色(src、img、cv_BGR2RGB);
整数计数=0;
int阈值=150;
对于(int j=0;j阈值){
计数++;
}
}
}
最后,请注意,通过img访问cv::Mat图像。ptr<>不会检查访问点是否正确,因此,如果您确实知道图像的范围,则通过ptr访问图像就可以了,否则,您可以通过img访问图像。在所以,如果存在无效的访问点,它将断言您 做一组
图像。at(i,j)
不应该是图像。at(j,i)
?非常感谢您的回复。cout部分结果是这样的:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,01109311264111178459749668574984,0,0,0,0,0,0,0,0,0,0,0,0251468121101542121415421,0,0,0,0,0,0,0,0,0,0,0。。。我把它改为“Image.at(j,I)”,cout的输出是这样的:7745580006,-18699705621835821674,-1987475062,。。。有什么想法吗?谢谢你的回答。我尝试了迭代器,但对于“if((*it)>=15)”行,它给出了一个错误,表示“没有运算符>=匹配这些操作数”;cv::threshold(图像,tmp,150,1,cv::THRESH_二进制);计数=cv::计数非零(tmp)亲爱的伊塔里奥、罗宾和所有回答我问题的人,非常感谢你们的帮助。现在它工作得很好。我使用了最后提供的代码。致以最良好的问候。谢谢你推荐我是的,这确实非常有效。谢谢:)这很可能是最快的方法-threshold和countNonZero比for循环优化得更好approach@ederman不客气。我不明白,如果有帮助,为什么要否决投票?(对谁来说!)@Kamyar,我没有投反对票,我选择罗宾的答案作为参考,然后还投票认为你的答案有用。我错过什么了吗?请不要误解任何事情。谢谢你。@ederman,我知道!:)我说的是做这件事的人。我注意到:这与谁有关。无论如何,我很高兴如果它能帮助任何人…我把它改成(j,I)最后,谢谢你为我指出这个错误。然后我使用了其他朋友在这里提供的“countNonZero”方法。谢谢你的回答。萨米,你知道吗麦比恩·达米·斯密恩:起来在加利福尼亚州乌查尔市的达卡-希姆比国际贸易中心,这是一家新的合资企业;)谢谢你的回答,现在我学会了如何使用颜色阈值:)