From aed1e7e55ac5a77feb2daa1ab82dfbd0b8ad76b9 Mon Sep 17 00:00:00 2001 From: bmy <2583236812@qq.com> Date: Thu, 8 Aug 2024 23:28:58 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E5=A2=9E=E5=8A=A0=E5=BC=80=E5=90=AF?= =?UTF-8?q?=E6=91=84=E5=83=8F=E5=A4=B4=E5=A4=B1=E8=B4=A5=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- capture.cc | 57 +++++++++++++++++++++++++++++++++++++++++++++++++----- capture.h | 1 + main.cc | 2 +- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/capture.cc b/capture.cc index 359328b..09161fc 100644 --- a/capture.cc +++ b/capture.cc @@ -9,6 +9,21 @@ #include "logc/log.h" #include +void adjustImageTemperature(cv::Mat &img, float scale) +{ + // 分离通道 + std::vector channels; + split(img, channels); + + // 增加蓝色通道和绿色通道的值,减少红色通道的值 + channels[0] = channels[0] * scale; // 蓝色通道 + channels[1] = channels[1] * scale; // 绿色通道 + channels[2] = channels[2] / scale; // 红色通道 + + // 合并通道 + merge(channels, img); +} + capture::capture(int camera_index, int zmq_port, int width_set, int height_set, int fps_set, bool _flip) { index = camera_index; @@ -18,22 +33,48 @@ capture::capture(int camera_index, int zmq_port, int width_set, int height_set, fps = fps_set; flip = _flip; - log_info("trying open camera %d", index); + log_info("尝试开启摄像头 %d", index); cap = new cv::VideoCapture(index, cv::CAP_V4L2); + + sleep(2); // 等待两秒进行构造 + cap->set(cv::CAP_PROP_FRAME_WIDTH, width); cap->set(cv::CAP_PROP_FRAME_HEIGHT, height); cap->set(cv::CAP_PROP_FPS, fps); - if (!cap->isOpened()) + // if (10 == camera_index) + // { + // int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G'); // 例如,使用 MJPG 编码 + // // 设置输出视频的分辨率和帧率,与输入视频一致或根据需要调整 + // cv::Size frameSize = cv::Size((int)cap->get(cv::CAP_PROP_FRAME_WIDTH), + // (int)cap->get(cv::CAP_PROP_FRAME_HEIGHT)); + // int fps = (int)cap->get(cv::CAP_PROP_FPS); + // writer = new cv::VideoWriter("/home/evan/Workplace/project_capture/capture.avi", fourcc, fps, frameSize, true); + // } + + int cnt = 1; + for (int i = 20; i > 0; i--) { - log_error("开启摄像头 %d 失败", index); + if (cap->isOpened()) + { + status = true; + break; + } + + log_error("开启摄像头 %d 失败,重试中", index); + cap->open(index, cv::CAP_V4L2); + usleep(100000); status = false; } + + if (status) + { + log_info("开启摄像头 %d 成功", index); + } else { - log_info("开启摄像头 %d 成功", index); - status = true; + log_fatal("开启摄像头 %d 十次重试后失败", index); } context = new zmq::context_t(1); @@ -85,6 +126,10 @@ void capture::get(void) *cap >> dst; } + // if(nullptr != writer){ + // writer->write(dst); + // } + // 确保图像是连续的 if (!dst.isContinuous()) { @@ -97,6 +142,8 @@ void capture::get(void) cv::flip(dst, dst, -1); } + // adjustImageTemperature(dst, 1.2); + // 将图像转换为 rgb cv::cvtColor(dst, frame, cv::COLOR_BGR2RGB); diff --git a/capture.h b/capture.h index 1ecbe7e..037dede 100644 --- a/capture.h +++ b/capture.h @@ -24,6 +24,7 @@ public: cv::Mat frame; zmq::context_t *context; zmq::socket_t *socket; + cv::VideoWriter *writer = nullptr; capture(int camera_index, int zmq_port, int width_set = 320, int height_set = 240, int fps_set = 20, bool flip = false); void start(void); diff --git a/main.cc b/main.cc index 66f0842..245d15e 100644 --- a/main.cc +++ b/main.cc @@ -17,7 +17,7 @@ int main(int argc, char **argv) char config_path[200]; sprintf(config_path, "%s/%s", getcwd(NULL, 0), config_file_path); - log_info("load config from %s", config_path); + log_info("[capture] 从以下路径加载配置:%s", config_path); fp = fopen(config_path, "r"); if (!fp) {