C++ OpenCV访问MAT对象中的RGB值

C++ OpenCV访问MAT对象中的RGB值,c++,opencv,image-processing,C++,Opencv,Image Processing,我试图使用OpenCV从网络摄像头抓取帧,并将其转换为aHSV(色调、饱和度、值)Mat对象和阈值 当我打印阈值图像像素值时,它为所有像素提供[0,0,0],即使是黑色像素值也为[0,0,0]。 如果选择的像素是黑色的,我需要做一些计算;如何访问像素值 imgOriginal=frame from camera Mat imgHSV; cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captu

我试图使用OpenCV从网络摄像头抓取帧,并将其转换为aHSV(色调、饱和度、值)Mat对象和阈值

当我打印阈值图像像素值时,它为所有像素提供[0,0,0],即使是黑色像素值也为[0,0,0]。 如果选择的像素是黑色的,我需要做一些计算;如何访问像素值

    imgOriginal=frame from camera 


    Mat imgHSV;

    cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

    Mat imgThresholded;
    inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

    //morphological opening (remove small objects from the foreground)
    erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
    dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 

    //morphological closing (fill small holes in the foreground)
    dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 
    erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );


    //************************************



    std::vector<cv::Vec3b> pixels(imgThresholded.rows * imgThresholded.cols);
    cv::Mat m(imgThresholded.rows, imgThresholded.cols, CV_8UC3, &pixels[0]);
    imgThresholded.copyTo(m);


    for(int i =0;i<1000;i++)
    cout<<pixels[0];
    if(pixels[0][0]==black)
    // do some calculations!
imgOriginal=来自摄影机的帧
Mat-imgHSV;
CVT颜色(原色、原色、原色)//将捕获的帧从BGR转换为HSV
垫高;
范围(imgHSV,标量(iLowH,iLowS,iLowV),标量(iHighH,iHighS,iHighV),IMGTHRESHOLED)//对图像设置阈值
//形态打开(从前景中移除小对象)
侵蚀(IMGTHRESHOLED、IMGTHRESHOLED、getStructuringElement(变形椭圆,大小(5,5));
扩张(imgThresholded,imgThresholded,getStructuringElement(变形椭圆,大小(5,5));
//形态闭合(填充前景中的小孔)
扩张(imgThresholded,imgThresholded,getStructuringElement(变形椭圆,大小(5,5));
侵蚀(IMGTHRESHOLED、IMGTHRESHOLED、getStructuringElement(变形椭圆,大小(5,5));
//************************************
std::矢量像素(imgThresholded.rows*imgThresholded.cols);
cv::Mat m(imgThresholded.rows、imgThresholded.cols、cv_8UC3和像素[0]);
imgThresholded.copyTo(m);

对于(int i=0;i
for(int i=0;它的工作方式是…)如何从Vec3b中提取RGB值…int R=int G=int B=如果我代码中的img是RGB,那么R=pixel[2],G=pixel[1],B=pixel[0]变量类型是什么?int?
uchar R=pixel[2];
等等
for(int i =0;i<1000;i++)
    cout<<pixels[0];
Vec3b black(0, 0, 0);

for(int i =0;i<1000;i++)
{
    cout << pixels[i];
    if pixels[i] == black)
    {
       /* ... */
    }
}
Vec3b black(0, 0, 0);

Mat img(imgThresholded); // just to make a short name

for(int y = 0; y < img.rows; ++y)
{
    Vec3b* row = img.ptr<Vec3b>(y);
    for(int x = 0; x < img.cols; ++x)
    {
        Vec3b& pixel = row[x];
        if(pixel == black)
        {
            /* ... */
        }
    }
 }