将我的阵列移动到 Mat 并使用打开的 CV 显示图像

Moving my array to Mat and showing image with open CV

本文关键字:CV 显示 图像 显示图 阵列 我的 移动 Mat      更新时间:2023-10-16

我在使用 opencv 显示图像时遇到问题。由于我的代码当前正在工作,我有一个函数,可以将 78 张大小为 710X710 的无符号短裤图像加载到单个数组中。我已经通过将数据写入文件并使用 imageJ 读取它来验证这是有效的。我现在正在尝试从数组中提取单个图像帧并将其加载到 Mat 中,以便对其执行一些处理。现在我已经尝试了两种方法来做到这一点。如果我不尝试读取输出,但如果我 cout,代码将编译并运行<<p>我的问题是,如何将尺寸为 710*710 的 78 张图像的大型一维数组中的数据提取到单个垫图像中。或者有没有更有效的方法,我可以将图像加载到尺寸为 710X710X78 的 3-D 垫中,并根据需要对每个 710X710 切片进行操作?

int main(int argc, char *argv[])
{
    Mat OriginalMat, TestImage;
    long int VImageSize = 710*710;
    int NumberofPlanes = 78;
    int FrameNum = 150;
    unsigned short int *PlaneStack = new unsigned short int[NumberofPlanes*VImageSize];
    unsigned short int *testplane = new unsigned short int[VImageSize];
    /////Load PlaneStack/////
    Load_Vimage(PlaneStack, Path, NumberofPlanes); 
    //Here I try to extract a single plane image to the mat testplane, I try it two    different ways with the same results
    memcpy(testplane, &PlaneStack[710*710*40], VImageSize*sizeof(unsigned short int));
    //copy(&PlaneStack[VImageSize*40],&PlaneStack[VImageSize*41], testplane);
    // move single plane to a mat file
    OriginalMat = Mat(710,710,CV_8U, &testplane) ;
    //cout<<OriginalMat;
    namedWindow("Original");
    imshow("Original", OriginalMat);
}

问题是您正在使用带有指向 16 位数据(无符号短整型)的指针的构造函数Mat::Mat(int rows, int cols, int type, void* data),但您正在指定类型 CV_8U(8 位)。因此,16位像素的第一个字节成为OriginalMat中的第一个像素,第一个像素的第二个字节成为OriginalMat中的第二个像素,依此类推。

您需要创建一个 16 位 Mat,如果要显示它,请将其转换为 8 位,例如:

int main(int argc, char *argv[])
{
    long int VImageSize = 710*710;    
    int NumberofPlanes = 78;
    int FrameNum = 150;
    /////Load PlaneStack/////
    unsigned short int *PlaneStack = new unsigned short int[NumberofPlanes*VImageSize];      
    Load_Vimage(PlaneStack, Path, NumberofPlanes); 
    // Get a pointer to the plane we want to view
    unsigned short int *testplane = &PlaneStack[710*710*40];
    // "move" single plane to a mat file
    //  actually nothing gets moved, OriginalMat will just contain a pointer to your data.
    Mat OriginalMat(710,710,CV_16UC1, &testplane) ;
    double scale_factor = 1.0 / 256.0;
    Mat DisplayMat;
    OriginalMat.convertTo(DisplayMat, CV_8UC1, scale_factor);
    namedWindow("Original");
    imshow("Original", DisplayMat);
}