我有一个具有BGR 8位原始图像数据的读取缓冲区,我想使用opencv Mat函数读取它,我尝试了下面的代码,但没有得到正确的图像帧,看来我的程序未读取读取缓冲区正确
我尝试了这段代码
#include <time.h>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#include <stack>
#include <limits.h>
#include <time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
using namespace std;
using namespace cv;
//using namespace std::chrono;
int main(int argc , char *argv[]) {
if (argc < 3) {
printf("Usage: %s <phys_addr> <offset>\n", argv[0]);
return 0;
}
off_t offset = strtoul(argv[1], NULL, 0);
size_t len = strtoul(argv[2], NULL, 0);
/* Truncate offset to a multiple of the page size, or mmap will fail. */
size_t pagesize = sysconf(_SC_PAGE_SIZE);
off_t page_base = (offset / pagesize) * pagesize;
off_t page_offset = offset - page_base;
int fd = open("/dev/mem", O_SYNC);
unsigned char *mem = (unsigned char*)mmap(NULL, page_offset + len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, page_base);
if (mem == MAP_FAILED) {
perror("Can't map memory");
return -1;
}
cout << &mem << endl; // print the virtual address of the memory buffer
cv::Mat dst;
while(1)
{
cv::Mat imgbuf(Size(640, 480), CV_8UC3, mem, 640*3);
imshow("image2",imgbuf);
waitKey(0);
destroyAllWindows();
}
return 0;
}
〜 〜 output Image