简体   繁体   English

用户在OpenCV中分配的数据的Mat构造函数

[英]Mat constructor from user allocated data in OpenCV

I am trying to create a Mat object from some data I have allocated in memory using this constructor. 我正在尝试使用此构造函数从内存中分配的一些数据创建Mat对象。

C++: Mat::Mat(Size size, int type, void* data, size_t step=AUTO_STEP)

I am fairly new to C++ so bear that in mind if I am making a rudimentary error somewhere. 我对C ++相当陌生,因此如果我在某个地方犯了基本错误,请记住这一点。 Basically what I am trying to do is display an image from a raw video file. 基本上我想做的是显示来自原始视频文件的图像。 To do this, I read the data from the file and allocated it in memory. 为此,我从文件中读取数据并将其分配到内存中。

struct videoFrame{
    float frameTime;
    unsigned short int year, day, msec;
    unsigned char hour, min, sec;
    unsigned short int mFrame[SIZE];
};

... ...

//seek to first frame
vfs.seekg(2560, ios_base::beg);
int pos;
for(pos=0; pos<SIZE;pos++){
    vfs.read( (char*) &mVideoFrame1->mFrame[pos], sizeof(short));
}
Mat mImage1( Size(WIDTH, HEIGHT), CV_16UC1, mVideoFrame1->mFrame, HEIGHT * 2);
namedWindow("Frame 1", CV_WINDOW_AUTOSIZE);
imshow("Frame 1", mImage1);
waitKey(0);

I have been able to read data from the file... such as frame and video headerers . 我已经能够从文件中读取数据...,例如视频标题 I think I may not be clear on how the constructor works. 我想我可能不清楚构造函数的工作方式。 If someone could offer some insight that would be great. 如果有人可以提供一些见解,那将是很好的。 When I show the frame in the namedWindow it is all black. 当我在namedWindow中显示框架时,它全是黑色的。

By the way... 顺便说说...

const int HEIGHT = 512;
const int WIDTH = 640;
const int SIZE = HEIGHT * WIDTH * 2;

the raw data in the first frame is 512x640 16bit unsigned little Endian byte order black and white. 第一帧中的原始数据是512x640 16位无符号小字节序字节顺序的黑白图像。

step is the size of the image stride which is equal to or larger than sizeof(_PIXELTYPE) * Width . step是图像步幅的大小,等于或大于sizeof(_PIXELTYPE) * Width You don't specify your problem, but I note you wrongly have put step equal to 2*HEIGHT . 您没有指定问题,但我注意到您错误地将步长设置为2*HEIGHT Otherwise your constructor usage seems ok. 否则,您的构造函数用法似乎还可以。

EDIT: of course just use auto step which calculates the default stride for you. 编辑:当然,只是使用自动步为您计算默认步幅。

Found what was wrong... The data in the file is 16 bit unsigned, but the range for each pixel is only 12 bit. 发现问题所在...文件中的数据为16位无符号,但是每个像素的范围仅为12位。 So that was the whole reason the window was showing up black. 因此,这就是窗口显示为黑色的全部原因。

OpenCV I suppose does not offer support for displaying 12bit unsigned data, so I had to convert it to 8bit unsigned for it to be displayed. 我想OpenCV不支持显示12位无符号数据,因此我不得不将其转换为8位无符号数据才能显示。 And yes its much more efficient to read all pixel data in block, rather than a loop. 是的,以块而不是循环读取所有像素数据要高效得多。

vfs.read( (char*) &mVideoFrame1->mFrame, sizeof(mVideoFrame1->mFrame));

Mat mImage1(HEIGHT, WIDTH, CV_16UC1, mVideoFrame1->mFrame, Mat::AUTO_STEP);
Mat mine(Size(640,512), CV_8UC1);
mImage1.convertTo(mine, CV_8UC1, 255.0/4095.0);
namedWindow("Frame 1", CV_WINDOW_AUTOSIZE);
imshow("Frame 1", mine);
waitKey(0);

So yeah, wasn't to hard lol 是的,不是很辛苦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM