Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV:读取矩阵值_C++_Opencv_Matrix_Background Image_Pixel - Fatal编程技术网

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)
not
Image.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)