k510 摄像头采集图片是灰度3通道

用k510摄像头采集的图片是3channel,但是显示出来是灰度的。查看了pixel的R,G,B 值,几乎都是一样的,所以显示灰度图片,请问如何解决或者有什么建议?谢谢大神!
使用了opencv的包写的cpp程序如下,

采集的图片如图

mtx.lock();
cv::VideoCapture capture;
capture.open(5);
// video setting
capture.set(cv::CAP_PROP_CONVERT_RGB, 0);
capture.set(cv::CAP_PROP_FRAME_WIDTH, valid_width);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, valid_height);

capture.set(cv::CAP_PROP_FOURCC, V4L2_PIX_FMT_RGB24);
mtx.unlock();
1 个赞

万分感谢,我也参考了 Object detect 里面的这段程序, V4L2_PIX_FMT_RGB24 这个格式显示error,找不到定义或者 h头文件,请问在哪里定义这个‘V4L2_PIX_FMT_RGB24’?我程序如下,比较乱,抱歉。
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include
#include

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core/utils/logger.hpp>
#include
//#include
using namespace cv;
using namespace std;

int main() {
// Open the video capture object for the camera (0 is usually the default camera)
//cv::VideoCapture cap(3);
// cv::VideoCapture cap(“/dev/video0”);
cv::VideoCapture cap(“/dev/video3”);
// Check if the camera opened successfully
if (!cap.isOpened()) {
printf(“Error: Could not open camera.\n”);
return -1;
}
int i_test=0;
// Check if the camera opened successfully
printf(“start.\n”);

 // Get the width and height of the video stream

// int frame_width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
// int frame_height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
int frame_width = 1072; // corresponding to video 3 frame setting
int frame_height = 604;

cap.set(cv::CAP_PROP_CONVERT_RGB, 0);
cap.set(cv::CAP_PROP_FRAME_WIDTH, frame_width);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, frame_height);
// RRRRRR....GGGGGGG....BBBBBB, CHW
cap.set(cv::CAP_PROP_FOURCC, VideoWriter::fourcc('R', 'G', 'B', '3')); // Set the pixel format to RGB24
//cap.set(cv::CAP_PROP_FOURCC, V4L2_PIX_FMT_RGB24);

printf("Video frame dimensions: %dx%d\n", frame_width, frame_height);

if (!cap.isOpened()) {
    printf("Error: Could not open camera.\n");
    return -1;
} else {
    printf("Camera opened successfully.\n");
}

printf("Attempting to read a frame...\n");          


// Read a single frame from the camera
cv::Mat frame;

#include <linux/videodev2.h>在这里面,你可以就利用示例代码,在这个基础上进行修改调整你想实现的功能

1 个赞

非常感谢大神,根据您的提示 用了这个 V4L2_PIX_FMT_RGB24,但是采集的图片还是显示成灰度的 3channel图像,太奇怪了。我想就是摄像头采集图片,保存成和显示屏显示一致的彩色图片,为啥这么难T-T


程序如下,希望可以得到指点:
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include
#include

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core/utils/logger.hpp>
#include
#include <linux/videodev2.h>
//#include <v4l2.h>

using namespace cv;
using namespace std;

int main() {
// Open the video capture object for the camera (0 is usually the default camera)
// mtx.lock();
cv::VideoCapture cap(“/dev/video3”); // Check if the camera opened successfully
if (!cap.isOpened()) {
printf(“Error: Could not open camera.\n”);
return -1;
}
int i_test=0;
// Check if the camera opened successfully
printf(“start.\n”);

// Get the width and height of the video stream
int frame_width = 1088;
int frame_height = 1920;
cap.set(cv::CAP_PROP_CONVERT_RGB, 0);
cap.set(CAP_PROP_FRAME_WIDTH, frame_width); //设置摄像头获得图像的宽度
cap.set(CAP_PROP_FRAME_HEIGHT, frame_height); //设置摄像头获得图像的高度
cap.set(cv::CAP_PROP_FOURCC, V4L2_PIX_FMT_RGB24);


printf("Video frame dimensions: %dx%d\n", frame_width, frame_height);

if (!cap.isOpened()) {
    printf("Error: Could not open camera.\n");
    return -1;
} else {
    printf("Camera opened successfully.\n");
}

printf("Attempting to read a frame...\n");          


// Read a single frame from the camera
cv::Mat frame;
if (cap.read(frame)) {
    std::cout << "Number of channels: " << frame.channels() << std::endl;

    if (frame.channels() == 1) {
        std::cout << "Captured grayscale image." << std::endl;
    } else {
        std::cout << "Captured color image." << std::endl;
    }
    }
// Delay for a specified number of milliseconds

// Capture frames from camera
cv::Mat frame;
bool ret = cap.read(frame);
printf("frame channel: %d\n",frame.channels()); // Channels: Number of channels in the image (1 for grayscale, 3 for BGR, etc.)


if (ret) {
    // Save the captured images
    std::string image_path = "image_from_cam.jpg";
 
    cv::imwrite(image_path, frame);
 
} else {
    if (!ret) std::cerr << "Error: Could not read frame from /dev/video3." << std::endl;
}


i_test++;
printf("i_test: %d\n",i_test);
// Release the camera
cap.release();

// mtx.unlock();
return 0;
}

cv::Mat ori_img_R = cv::Mat(valid_height, valid_width, CV_8UC1, p_src);
cv::Mat ori_img_G = cv::Mat(valid_height, valid_width, CV_8UC1, p_src + offset_channel);
cv::Mat ori_img_B = cv::Mat(valid_height, valid_width, CV_8UC1, p_src + offset_channel * 2);

std::vectorcv::Mat ori_imgparts(3);
ori_imgparts.clear();
ori_imgparts.push_back(ori_img_B);
ori_imgparts.push_back(ori_img_G);
ori_imgparts.push_back(ori_img_R);
cv::Mat ori_img;
cv::merge(ori_imgparts, ori_img);
这样子试试

1 个赞