基于C++的攝像頭圖像采集及拼接程序的簡單實現(xiàn)
程序的說明
實現(xiàn)從攝像頭實時采集單幀圖像,之后完成圖像的拼接,本程序?qū)崿F(xiàn)了兩張圖片的拼接和三張圖片的拼接。
在此之前你需要在 linux 下安裝 opencv Package 這個包,因為本程序主要使用 opencv 這個包中提供的 api 函數(shù)。
實現(xiàn)從攝像頭實時不同視角采集視頻的單幀圖像并保存實時采集的視頻文件之后,完成圖像的拼接,由于實驗室設備有限,手頭只有兩個攝像頭一次只能抓取。
兩張不同視角的單幀圖像,我們抓取的單幀圖像保存在當前項目目錄下的 frame1 和 frame2 文件夾中,因此我同時制作了兩個完成程序。
拼接的程序,一個實現(xiàn)完成兩個不同視角的圖像拼接,另一個實現(xiàn)三張不同視角的單幀圖像的拼接。其中的 testusb.cpp 文件是測試攝像頭的程序。在執(zhí)行本程序前,你應該保證有兩個是攝像頭插在主機端口上,用于實時采集單幀圖像。
代碼介紹
在進行程序的編譯前,請確定你已經(jīng)安裝了 opencv2.4.9 和 pkg-config 包,本程序是在 ubuntu14.04 平臺下實現(xiàn)的,在本項目目錄下,已經(jīng)有編譯生成的可執(zhí)行程序,其中 Camera_to_Frmae.cpp 是我們從雙攝像頭實時抓取單幀圖像的源碼。
- ImageJoint.cpp 和 ImageJoint2.cpp、ImageJoint3.cpp 分別是完成兩張不同視角的圖像拼接和三張不同視角的圖像拼接程序,其中三張圖像拼接的圖像是我從網(wǎng)上找的現(xiàn)成的圖像庫
- testusb.cpp 是我測試攝像頭的程序
程序編譯
g++ -o dst src.cpp \`pkg-config opencv --cflags --libs\`
程序的執(zhí)行和退出
- ./dst
- 程序需要退出時,按 Ctrl + C 快捷鍵
效果
從攝像頭設備采集兩張單幀圖像
圖像拼接效果圖
補充:c++利用opencv打開攝像頭并且保存圖片
項目背景
利用一個usb雙目攝像機進行雙目測距的項目,這個項目代碼有助于使用usb雙目攝像機打開攝像機并且保存圖片
打開雙目相機的函數(shù)
void SetCam(int weigth, int height, int num) { string a = "0"; string Error; VideoCapture Cam(0); /*設定緩沖區(qū)大小*/ Cam.set(CV_CAP_PROP_FRAME_WIDTH, weigth); Cam.set(CV_CAP_PROP_FRAME_HEIGHT, height); while (!Cam.isOpened()) { a = to_string(num); Error = "cannot open the camera1!"; Error = Error.replace(22, 1, a); //Error.copy(error, 24, 0);//這里5代表復制幾個字符,0代表復制的位置, } //namedWindow("攝像頭");//關(guān)鍵一句代碼 while (true) { Cam >> input_image;//將影像傳入圖片 leftImage = input_image(Rect(0, 0, input_image.size().width / 2, input_image.size().height));//split left image rightImage = input_image(Rect(input_image.size().width / 2, 0, input_image.size().width / 2, input_image.size().height)); imshow("leftImage", leftImage);//left image imshow("rightImage", rightImage);//right image Save(i, 20); if (27 == waitKey(30)) break; return ; }
保存圖片函數(shù)Save
void Save(int &imgnum, int amount) { if (imgnum < amount) { a = to_string(imgnum); seat = floor((imgnum - 1) / 10); Left = Left.replace(4 + seat, 1, a); Right = Right.replace(5 + seat, 1, a); imwrite(Left, leftImage); imwrite(Right, rightImage); imgnum += 1; } }
全部代碼
#include <opencv2/opencv.hpp> #include<iostream> using namespace cv; using namespace std; VideoCapture Cam1, Cam2; const int weigth = 1280; const int height = 480; static string Left = "Left0.jpg", Right = "Right0.jpg", a = "0"; static int seat = 0; static Mat input_image, leftImage, rightImage; static int i = 0; void Save(int &imgnum, int amount) { if (imgnum < amount) { a = to_string(imgnum); seat = floor((imgnum - 1) / 10); Left = Left.replace(4 + seat, 1, a); Right = Right.replace(5 + seat, 1, a); imwrite(Left, leftImage); imwrite(Right, rightImage); imgnum += 1; } } void SetCam(int weigth, int height, int num) string a = "0"; string Error; VideoCapture Cam(0); /*設定緩沖區(qū)大小*/ Cam.set(CV_CAP_PROP_FRAME_WIDTH, weigth); Cam.set(CV_CAP_PROP_FRAME_HEIGHT, height); while (!Cam.isOpened()) a = to_string(num); Error = "cannot open the camera1!"; Error = Error.replace(22, 1, a); //Error.copy(error, 24, 0);//這里5代表復制幾個字符,0代表復制的位置, //namedWindow("攝像頭");//關(guān)鍵一句代碼 while (true) { Cam >> input_image;//將影像傳入圖片 leftImage = input_image(Rect(0, 0, input_image.size().width / 2, input_image.size().height));//split left image rightImage = input_image(Rect(input_image.size().width / 2, 0, input_image.size().width / 2, input_image.size().height)); imshow("leftImage", leftImage);//left image imshow("rightImage", rightImage);//right image Save(i, 20); if (27 == waitKey(30)) break; return ; void main() //char* error = "error"; SetCam(weigth, height, 10);
到此這篇關(guān)于基于C++的攝像頭圖像采集及拼接程序的實現(xiàn)的文章就介紹到這了,更多相關(guān)C++攝像頭圖像采集內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你Visual?Studio?2022如何新建一個C語言工程(圖文詳解)
這篇文章主要介紹了Visual?Studio?2022如何新建一個C語言工程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09基于Qt播放器的實現(xiàn)詳解(支持Rgb,YUV格式)
這篇文章主要為大家詳細介紹了如何利用Qt實現(xiàn)簡易的播放器,可以支持支持Rgb,YUV格式。文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下2022-12-12C++中與輸入相關(guān)的istream類成員函數(shù)簡介
這篇文章主要介紹了C++中與輸入相關(guān)的istream類成員函數(shù)簡介,包括eof函數(shù)和peek函數(shù)以及putback函數(shù)還有ignore函數(shù),需要的朋友可以參考下2015-09-09