簡體   English   中英

通過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庫才能讀取數據,如何將原始數據轉換為Image格式(以Qt格式)?

我不能簡單地一遍又一遍地編寫應用程序並在設備上進行測試以找到解決方案,因為設備的位置離我的位置很遠,並且每次測試都必須經過此距離。 因此,我想確保我的應用程序可以運行。

來自中國的一家公司為我們制造了該設備,他們將不再支持它:(

相機使用自定義通信協議,但未實現成像設備類。 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM