Last active
August 16, 2021 13:55
-
-
Save TzuHuanTai/7790f31981f0c07f1393a62383d1b7c3 to your computer and use it in GitHub Desktop.
Test OpenCV on multiple async camera, stitching in c++.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <iostream> | |
#include <thread> | |
#include <chrono> | |
#include <future> | |
#include <opencv2/core.hpp> | |
#include <opencv2/videoio.hpp> | |
#include <opencv2/highgui.hpp> | |
#include <opencv2/imgproc.hpp> | |
using namespace cv; | |
using namespace std; | |
int show_camera(int deviceID, Mat &frame) | |
{ | |
try | |
{ | |
//--- INITIALIZE VIDEOCAPTURE | |
int apiID = cv::CAP_ANY; // 0 = autodetect default API | |
VideoCapture cap(deviceID, apiID); | |
cap.set(CAP_PROP_FRAME_WIDTH, 640); | |
cap.set(CAP_PROP_FRAME_HEIGHT, 480); | |
if (!cap.isOpened()) | |
{ | |
cerr << "ERROR! Unable to open camera\n"; | |
return -1; | |
} | |
// cout << cv::getBuildInformation() << endl; | |
// cout << "opened video capure device at idx " << 0 + cv::CAP_DSHOW << endl; | |
for (;;) | |
{ | |
cap.read(frame); | |
if (frame.empty()) | |
{ | |
cerr << "ERROR! blank frame grabbed\n"; | |
break; | |
} | |
// cvtColor(frame, frame, COLOR_BGR2GRAY); | |
imshow("Live " + to_string(deviceID), frame); | |
if (waitKey(5) >= 0) | |
break; | |
} | |
// the camera will be deinitialized automatically in VideoCapture destructor | |
} | |
catch (exception &exp) | |
{ | |
cerr << exp.what() << endl; | |
} | |
return 0; | |
} | |
int main() | |
{ | |
Mat frame0, frame1; | |
Mat pano; | |
Mat left_1d_frame; | |
Mat right_1d_frame; | |
cout << "start main" << endl; | |
future<int> camera0 = async(launch::async, show_camera, 0, ref(frame0)); | |
future<int> camera1 = async(launch::async, show_camera, 1, ref(frame1)); | |
for (;;) | |
{ | |
try | |
{ | |
if (!frame0.empty() && !frame1.empty()) | |
{ | |
cvtColor(frame1, right_1d_frame, COLOR_BGR2GRAY); | |
cvtColor(frame0, left_1d_frame, COLOR_BGR2GRAY); | |
cv::hconcat(left_1d_frame, right_1d_frame, pano); | |
imshow("Stitching", pano); | |
if (waitKey(5) >= 0) | |
{ | |
break; | |
} | |
} | |
} | |
catch (exception &exp) | |
{ | |
cerr << exp.what() << endl; | |
} | |
} | |
cout << "end main" << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <iostream> | |
#include <thread> | |
#include <chrono> | |
#include <opencv2/core.hpp> | |
#include <opencv2/videoio.hpp> | |
#include <opencv2/highgui.hpp> | |
using namespace cv; | |
using namespace std; | |
int show_camera(std::string url) | |
{ | |
Mat frame; | |
cout << "open: " << url << endl; | |
VideoCapture cap(url, CAP_FFMPEG); | |
if (!cap.isOpened()) | |
{ | |
cerr << "ERROR! Unable to open camera\n"; | |
return -1; | |
} | |
//--- GRAB AND WRITE LOOP | |
cout << cv::getBuildInformation() << endl; | |
cout << "opened video capure device at idx " << 0 + cv::CAP_DSHOW << endl; | |
cout << "start reading" << endl; | |
for (;;) | |
{ | |
cap.read(frame); | |
if (frame.empty()) | |
{ | |
cerr << "ERROR! blank frame grabbed\n"; | |
break; | |
} | |
imshow("Live", frame); | |
if (waitKey(5) >= 0) | |
break; | |
} | |
} | |
int main(int argc, char* argv[]) | |
{ | |
if(argc<2){ | |
cerr << "no input url, or set 0 open camera!!" << endl; | |
return 1; | |
} | |
thread t1(show_camera, argv[1]); | |
t1.join(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <iostream> | |
#include <opencv2/core.hpp> | |
#include <opencv2/videoio.hpp> | |
#include <opencv2/highgui.hpp> | |
#include <opencv2/imgcodecs.hpp> | |
#include <opencv2/stitching.hpp> | |
#include <opencv2/calib3d.hpp> | |
#include <chrono> | |
using namespace cv; | |
using namespace std; | |
Stitcher::Mode mode = Stitcher::PANORAMA; | |
vector<Mat> imgs; | |
int main() | |
{ | |
Mat pano; | |
Mat frame0; | |
Mat frame1; | |
vector<Mat> imgs; | |
//--- INITIALIZE VIDEOCAPTURE | |
int apiID = cv::CAP_ANY; // 0 = autodetect default API | |
VideoCapture cap0(0, apiID); | |
VideoCapture cap1(1, apiID); | |
Ptr<Stitcher> stitcher = Stitcher::create(mode); | |
// check if we succeeded | |
if (!cap0.isOpened() || !cap1.isOpened()) | |
{ | |
cerr << "ERROR! Unable to open camera\n"; | |
return -1; | |
} | |
cout << "Start grabbing" << endl; | |
cap0.read(frame0); | |
cap1.read(frame1); | |
imgs.push_back(frame0); | |
imgs.push_back(frame1); | |
for (;;) | |
{ | |
cap0.read(frame0); | |
cap1.read(frame1); | |
if (frame0.empty() || frame1.empty()) | |
{ | |
cerr << "ERROR! blank frame grabbed\n"; | |
break; | |
} | |
Stitcher::Status status = stitcher->stitch(imgs, pano); | |
imshow("Live0", frame0); | |
imshow("Live1", frame1); | |
if (status == cv::Stitcher::Status::OK) | |
{ | |
imshow("stitching", pano); | |
} | |
if (waitKey(5) >= 0) | |
break; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC = g++ | |
CFLAGS = -g -Wall | |
#FFMPEG_INCS = -I "D:\projects\test\include" | |
#FFMPEG_LIBS_PATH = -L "D:\projects\test\bin" | |
OPENCV_INC = -I "D:\lib\opencv-build\install\include" | |
OPENCV_LIB_PATH = -L "D:\lib\opencv-build\install\x64\mingw\bin" | |
#FFMPEG_LIBS = -lavdevice-58 \ | |
# -lavcodec-58 \ | |
# -lavfilter-7 \ | |
# -lavformat-58 \ | |
# -lavutil-56 \ | |
# -lswresample-3 \ | |
# -lswscale-5 | |
OPENCV_LINK_LIBS = -llibopencv_calib3d451 \ | |
-llibopencv_core451 \ | |
-llibopencv_dnn451 \ | |
-llibopencv_features2d451 \ | |
-llibopencv_flann451 \ | |
-llibopencv_highgui451 \ | |
-llibopencv_imgcodecs451 \ | |
-llibopencv_imgproc451 \ | |
-llibopencv_imgproc451 \ | |
-llibopencv_ml451 \ | |
-llibopencv_objdetect451 \ | |
-llibopencv_photo451 \ | |
-llibopencv_stitching451 \ | |
-llibopencv_video451 \ | |
-llibopencv_videoio451 \ | |
-llibopencv_xfeatures2d451 | |
all: camera_ffmpeg camera_async camera_stitching | |
camera_ffmpeg: camera_ffmpeg.cpp | |
$(CC) $(CFLAGS) "D:\projects\test\camera_ffmpeg.cpp" -o "D:\projects\test\camera_ffmpeg.exe" \ | |
$(ALL_INCS) $(LIB_PATH) $(OPENCV_LINK_LIBS) | |
camera_async: camera_async.cpp | |
$(CC) $(CFLAGS) "D:\projects\test\camera_async.cpp" -o "D:\projects\test\camera_async.exe" \ | |
$(OPENCV_INC) $(OPENCV_LIB_PATH) $(OPENCV_LINK_LIBS) | |
camera_stitching: camera_stitching.cpp | |
$(CC) $(CFLAGS) "D:\projects\test\camera_stitching.cpp" -o "D:\projects\test\camera_stitching.exe" \ | |
$(OPENCV_INC) $(OPENCV_LIB_PATH) $(OPENCV_LINK_LIBS) | |
clean: | |
del camera_ffmpeg.exe \ | |
camera_async.exe \ | |
camera_stitching.exe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment