通过 D2XX 库或 OPENCV 捕获相机(USB)
Capturing camera(USB) via D2XX library or OPENCV
我想编写一个应用程序(用 C++ 编写),以便从采集系统中使用的相机捕获图像。相机连接到一个盒子(采集系统),我发现使用的芯片是 FTDI。该芯片位于相机和 PC 之间的盒子中。相机连接到这个盒子。 USB 电缆连接到 PC 和盒子。其他一些工具连接到盒子上并不重要。
另外,有一个简单的商业应用程序,是用MFC写的,我想做的一模一样。在应用程序的文件夹中有D2XX驱动文件(ftd2xx.h等)和相机的信息文件(*.inf)。
此外,摄像头不是在录制视频,而是在很短的时间间隔(<0.1s)拍照,并且间隔是由采集系统决定的,而不是商业应用(采集系统会检测相机何时需要拍照)
这是我的问题:
由于提供了USB设备的信息文件,我可以只使用Open-CV库来捕获相机还是只能使用D2XX库?
如果我必须使用 D2XX 库才能读取数据,我如何将原始数据转换为图像格式(在 Qt 中)?
我不能简单地在设备上一遍又一遍地编写应用程序和测试来找到解决方案,因为设备离我的位置很远,而且每次测试我都必须走这么远的路。所以,我想确保我的应用程序能够正常工作。
一家中国公司为我们制造了这个设备,他们不会再支持它了:(
相机使用自定义通信协议,未实现成像设备class。 OpenCV 不会看到它,任何其他多媒体库也不会。无论如何,您都需要实施该协议。然后,如果您愿意,可以将其公开给 OpenCV。
要转换成图片,试试这个:
Mat hwnd2mat(HWND hwnd){
HDC hwindowDC, hwindowCompatibleDC;
int height, width, srcheight, srcwidth;
HBITMAP hbwindow; // <-- The image represented by hBitmap
cv::Mat src; // <-- The image represented by mat
BITMAPINFOHEADER bi;
// Initialize DCs
hwindowDC = GetDC(hwnd); // Get DC of the target capture..
hwindowCompatibleDC = CreateCompatibleDC(hwindowDC); // Create compatible DC
SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);
RECT windowsize; // get the height and width of the screen
GetClientRect(hwnd, &windowsize);
srcheight = windowsize.bottom;
srcwidth = windowsize.right;
height = windowsize.bottom *2/ 2; //change this to whatever size you want to resize to
width = windowsize.right *2/ 2;
src.create(height, width, CV_8UC4);
// create a bitmap
hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = -height; //this is the line that makes it draw upside down or not
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// use the previously created device context with the bitmap
SelectObject(hwindowCompatibleDC, hbwindow);
// copy from the window device context to the bitmap device context
StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !
GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS); //copy from hwindowCompatibleDC to hbwindow
// avoid memory leak
DeleteObject(hbwindow); DeleteDC(hwindowCompatibleDC); ReleaseDC(hwnd, hwindowDC);
return src;
}
我想编写一个应用程序(用 C++ 编写),以便从采集系统中使用的相机捕获图像。相机连接到一个盒子(采集系统),我发现使用的芯片是 FTDI。该芯片位于相机和 PC 之间的盒子中。相机连接到这个盒子。 USB 电缆连接到 PC 和盒子。其他一些工具连接到盒子上并不重要。
另外,有一个简单的商业应用程序,是用MFC写的,我想做的一模一样。在应用程序的文件夹中有D2XX驱动文件(ftd2xx.h等)和相机的信息文件(*.inf)。
此外,摄像头不是在录制视频,而是在很短的时间间隔(<0.1s)拍照,并且间隔是由采集系统决定的,而不是商业应用(采集系统会检测相机何时需要拍照)
这是我的问题:
由于提供了USB设备的信息文件,我可以只使用Open-CV库来捕获相机还是只能使用D2XX库?
如果我必须使用 D2XX 库才能读取数据,我如何将原始数据转换为图像格式(在 Qt 中)?
我不能简单地在设备上一遍又一遍地编写应用程序和测试来找到解决方案,因为设备离我的位置很远,而且每次测试我都必须走这么远的路。所以,我想确保我的应用程序能够正常工作。
一家中国公司为我们制造了这个设备,他们不会再支持它了:(
相机使用自定义通信协议,未实现成像设备class。 OpenCV 不会看到它,任何其他多媒体库也不会。无论如何,您都需要实施该协议。然后,如果您愿意,可以将其公开给 OpenCV。
要转换成图片,试试这个:
Mat hwnd2mat(HWND hwnd){
HDC hwindowDC, hwindowCompatibleDC;
int height, width, srcheight, srcwidth;
HBITMAP hbwindow; // <-- The image represented by hBitmap
cv::Mat src; // <-- The image represented by mat
BITMAPINFOHEADER bi;
// Initialize DCs
hwindowDC = GetDC(hwnd); // Get DC of the target capture..
hwindowCompatibleDC = CreateCompatibleDC(hwindowDC); // Create compatible DC
SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);
RECT windowsize; // get the height and width of the screen
GetClientRect(hwnd, &windowsize);
srcheight = windowsize.bottom;
srcwidth = windowsize.right;
height = windowsize.bottom *2/ 2; //change this to whatever size you want to resize to
width = windowsize.right *2/ 2;
src.create(height, width, CV_8UC4);
// create a bitmap
hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = -height; //this is the line that makes it draw upside down or not
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// use the previously created device context with the bitmap
SelectObject(hwindowCompatibleDC, hbwindow);
// copy from the window device context to the bitmap device context
StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !
GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS); //copy from hwindowCompatibleDC to hbwindow
// avoid memory leak
DeleteObject(hbwindow); DeleteDC(hwindowCompatibleDC); ReleaseDC(hwnd, hwindowDC);
return src;
}