将我的阵列移动到 Mat 并使用打开的 CV 显示图像
Moving my array to Mat and showing image with open CV
我在使用 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);
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 程序崩溃并显示"std::out_of_range"错误
- 在 QML VideoOutput 中将 cv::mat 显示为 QVideoFrame
- C++/openCV - 为什么显示图像会产生 Linux 分割错误?(打开CV错误?
- 在qpixMap中显示一个CV :: MAT(键入CV_32F)
- 在图片框中显示cv::Mat格式的网络摄像头提要
- 如何显示窗口大小不同的输出图像?通过使用 cv::Mat
- OpenCV 2.4:在MFC中显示cv::Mat
- 将我的阵列移动到 Mat 并使用打开的 CV 显示图像
- 使用直接显示控制相机,并使用打开的CV捕获图像
- 为什么我无法在 Open CV 中正确显示矩形?
- cv::imshow 在不同的线程上不显示 cv::mat 颜色
- 如何在Windows窗体应用程序中显示cv::Mat
- 显示带有cv::viz模块的WCloud
- 调试断言失败!在人脸识别打开Cv的检测和显示功能中