Camera測試 Auto Exposure 設定
曝光設定參考在OpenCV官網內的set有關主要參數有兩個
CAP_PROP_AUTO_EXPOSURE 開啟要不要打開
CAP_PROP_EXPOSURE 設定曝光值
CAP_PROP_AUTO_EXPOSURE
CAP_PROP_EXPOSURE
https://docs.opencv.org/4.x/d4/d15/group__videoio__flags__base.html
int auto_mode = 3;
int manual_mode = 1;
int exposure_number = 5;
cap.set(cv::CAP_PROP_AUTO_EXPOSURE, auto_mode); // 自動模式
cap.set(cv::CAP_PROP_AUTO_EXPOSURE, manual_mode); // 關閉自動模式
cap.set(cv::CAP_PROP_EXPOSURE, exposure_number); // 設定曝光值
void Auto_Exposure_OnOff(VideoCapture& cap, bool TurnOn = true, int exposure_number = 0)
{
int auto_mode = 3;
int manual_mode = 1;
cap.set(cv::CAP_PROP_AUTO_EXPOSURE, auto_mode);
if (!TurnOn)
{
cap.set(cv::CAP_PROP_AUTO_EXPOSURE, manual_mode);
cap.set(cv::CAP_PROP_EXPOSURE, exposure_number);
}
}
示範程序:
// g++ opencv-camera.cpp -o a.out `pkg-config --cflags --libs opencv`
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
bool Check_VideoCapture_isOpened(const VideoCapture& cap, bool falseMessage_Ouput = true, string fail_Message = "Cannot open camera \n")
{
bool check0 = cap.isOpened();
if (falseMessage_Ouput && !check0)
cout << fail_Message;
return check0;
}
bool Check_VideoCapture_read(VideoCapture& cap, Mat& frame, bool falseMessage_Ouput = true, string fail_Message = "Can't receive frame (stream end?). Exiting ...\n")
{
bool check0 = cap.read(frame);
if (falseMessage_Ouput && !check0)
cout << fail_Message;
return check0;
}
/////////////////////////////////////////////////////
void Color_Cout(const Mat& frame, int Select_color1)
{
for (int i = 0; i < frame.rows; i = i + 10)
{
for (int j = 0; j < frame.cols * 3; j = j + 3 * 10)
cout << (int)frame.at<unsigned char>(i, j + Select_color1) << " ";
cout << endl;
}
cout << endl << endl;
}
void Auto_Exposure_OnOff(VideoCapture& cap, bool TurnOn = true, int exposure_number = 0)
{
int auto_mode = 3;
int manual_mode = 1;
cap.set(cv::CAP_PROP_AUTO_EXPOSURE, auto_mode);
if (!TurnOn)
{
cap.set(cv::CAP_PROP_AUTO_EXPOSURE, manual_mode);
cap.set(cv::CAP_PROP_EXPOSURE, exposure_number);
}
}
int main() {
Mat frame0, frame1;
VideoCapture cap0(0), cap1(1);
Check_VideoCapture_isOpened(cap0);
Auto_Exposure_OnOff(cap0, true);
Check_VideoCapture_isOpened(cap1);
Auto_Exposure_OnOff(cap1, false,-5);
int Camera_Number = 0;
while (true)
{
if (Camera_Number == 0)
{
Check_VideoCapture_read(cap0, frame0);
imshow("live", frame0);
}
else
{
Check_VideoCapture_read(cap1, frame1);
imshow("live", frame1);
}
int WK = waitKey(1);
if (WK == 'q')
{
break;
}
else if (WK == 'a')
{
if (Camera_Number == 0)
Camera_Number = 1;
else
Camera_Number = 0;
}
else if (WK == 's')
{
cout << "Camera Size:" << frame0.total() << endl;
cout << "Camera rows:" << frame0.rows << endl;
cout << "Camera cols:" << frame0.cols << endl;
}
else if (WK == 'd')
{
int R_color1(2), G_color1(1), B_color1(0);
if (Camera_Number == 0)
{
Color_Cout(frame0, R_color1);
Color_Cout(frame0, G_color1);
Color_Cout(frame0, B_color1);
}
else if (Camera_Number == 1)
{
Color_Cout(frame1, R_color1);
Color_Cout(frame1, G_color1);
Color_Cout(frame1, B_color1);
}
}
}
return 0;
}