c++ - 确定 OpenCV 中矩阵的类型

标签 c++ image-processing opencv matrix

我正在尝试在 opencv 中索引 3 channel 图像。

当我读入图像文件时,这段代码有效

int Blue  = LeftCol.at<cv::Vec3b>(v,u)[0];
int Green = LeftCol.at<cv::Vec3b>(v,u)[1]; 
int Red   = LeftCol.at<cv::Vec3b>(v,u)[2]; 

但是当我使用网络摄像头输入时它崩溃了。网络摄像头有 3 个 channel ,u,v0,0 开始。 我不知道为什么它不起作用。 我已经尝试了 Vec3bVec3iVec3sVec3fVec3d 的所有变体>

我迷路了....为什么我不能索引这个网络摄像头图像?

编辑

好几个小时后,这就是我必须要做的...这是程序的大纲。我在一个函数中遇到了上面提到的问题。所以我回到了基础,试图在函数之前查看矩阵...

void main (int argc, char** argv) {
Mat LeftCol;
while (1==1) {
    if (ProgramMode == "Files") {
        //read in the colour images
        LeftCol  = imread(ColImLeft.c_str(),1);
        RightCol = imread(ColImRight.c_str(),1);

    } else if (ProgramMode == "Camera") {
        VideoCapture CapLeft, CapRight;
        CapLeft.open(1);
        CapRight.open(2);

        CapLeft  >> LeftCol;
        CapRight >> RightCol;

                    //THIS WORKS, THIS PIXEL VALUES ARE DISPLAYED
        cout << "uchar" << endl;
        for (int x=0;x<10;x++) {
            for (int y=0;y<10;y++) {
                int pixel = LeftCol.at<cv::Vec3b>(x,y)[0];
                cout << pixel;
            }
            cout << endl;
        }
    } //end if

            ///////ADDED THIS BIT ////////
    cout << "channels = " << LeftCol.channels() << endl;
            //^^This bit works, output shows "channels = 3"

            //vv This bit doesn't work.... so there's a problem with LeftCol.
            //I wonder if reading the data like CapLeft  >> LeftCol; is changing something
    imshow("Test",LeftCol);
            ///////ADDED THIS BIT ////////

           //THIS DOES NOT WORK WHEN USING THE CAMERA INPUT, PROGRAM CRASHES
    cout << "uchar" << endl;
    for (int x=0;x<10;x++) {
        for (int y=0;y<10;y++) {
            int pixel = LeftCol.at<cv::Vec3b>(x,y)[0];
            cout << pixel;
        } //end for
        cout << endl;
    } //end for

   } //end while
} //end main

是的,我已经让它工作了,但它并不理想。我正在创建一个临时 Mat 以将文件读入其中然后克隆它们。

        Mat TempLeft;
        Mat TempRight;

        VideoCapture CapLeft, CapRight;
        CapLeft.open(1);
        CapRight.open(2);

        CapLeft  >> TempLeft;
        CapRight >> TempRight;

        LeftCol = TempLeft.clone();
        RightCol = TempRight.clone();

最佳答案

OpenCV 尽可能制作图像的软拷贝。 From the documentation :

the array assignment is an O(1) operation because it only copies the header and increases the reference counter. The Mat::clone() method can be used to get a full (deep) copy of the array when you need it.

我怀疑正在发生的事情是 LeftCol 使用了仍然属于 VideoCapture 对象的数据。如果是这种情况,那么当 CapLeftCapRightif 结束时超出范围时,它们将被析构函数和图像关闭LeftCol 仍指向的数据已被销毁。

可能的解决方案是像您正在做的那样克隆图像,或者在 if block 外部声明 VideoCapture CapLeft, CapRight;(如果需要,您仍然可以在内部打开它们)。

关于c++ - 确定 OpenCV 中矩阵的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12461300/

相关文章:

c++ - Windows CMD 无法正确输出 UTF-16

c++ - != 和 =! 之间的区别举个例子(C++)

image-processing - 从图像中去除背景噪音

java - 如何创建新的透明位图?

c++ - 如何输出浮点 vector 的 vector ?

C++ 图像库 - 找到一种颜色的像素并使它们变白,其他一切都变黑

c++ - 使用包含OpenCV的cmake构建MacOS可执行文件进行分发

OpenCV Python calcOpticalFlowPyrLK 从相机帧中返回点

c++ - Xcode 7 不使用 C++ 框架构建项目

c++ - 将字符串插入二维 vector 的指定位置