如何获得图像在彩色模式下使用opencv c + + raspberry相机模块 [英] How to get image in color mode using opencv c++ with raspberry camera module

查看:860
本文介绍了如何获得图像在彩色模式下使用opencv c + + raspberry相机模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用raspberry pi 2与opencv 3金和raspicam-0.1.3 libarrry的pi相机模块
我已经测试下面的代码,它的工作,但它提供了一个灰度模式的图像黑色和白色),但我想要它在彩色模式(RGB)

i'm using raspberry pi 2 with opencv 3 gold and raspicam-0.1.3 libarrry for the pi camera module I have test the code below and it worked but it provide for me an image in grayscale mode (black and white) but i want it in color mode (RGB)

这里是代码:

#include <ctime>
#include <iostream>
#include <raspicam/raspicam_cv.h>
using namespace std; 

int main ( int argc,char **argv ) {

    time_t timer_begin,timer_end;
    raspicam::RaspiCam_Cv Camera;
    cv::Mat image;
    int nCount=100;
    //set camera params
    Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );
    //Open camera
    cout<<"Opening Camera..."<<endl;
    if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
    //Start capture
    cout<<"Capturing "<<nCount<<" frames ...."<<endl;
    time ( &timer_begin );
    for ( int i=0; i<nCount; i++ ) {
        Camera.grab();
        Camera.retrieve ( image);
        if ( i%5==0 )  cout<<"\r captured "<<i<<" images"<<std::flush;
    }
    cout<<"Stop camera..."<<endl;
    Camera.release();
    //show time statistics
    time ( &timer_end ); /* get current time; same as: timer = time(NULL)  */
    double secondsElapsed = difftime ( timer_end,timer_begin );
    cout<< secondsElapsed<<" seconds for "<< nCount<<"  frames : FPS = "<<  ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
    //save image 
    cv::imwrite("raspicam_cv_image.jpg",image);
    cout<<"Image saved at raspicam_cv_image.jpg"<<endl;
}

只是为了注意我在尝试第一个示例时得到一个彩色图片: http://sourceforge.net/projects/raspicam/files/?source=navbar

just to notice that i get a colored image when trying the first example here : http://sourceforge.net/projects/raspicam/files/?source=navbar

我做的是在imwrite之前插入 cvtColor(image,cimg,CV_GRAY2RGB); ,但这没有解决我的问题

what i did is to insert cvtColor(image, cimg, CV_GRAY2RGB); before imwrite but this didn't solve my problem

任何帮助将感激...

any help will be appreciated ... thanks

推荐答案

将相机设置为灰度模式:

You are setting your camera to "grayscale mode" here:

Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );

如果你想要彩色模式,你可以这样做:

If you want "colored mode", you shold do this instead:

Camera.set( CV_CAP_PROP_FORMAT, CV_8UC3 );

CV_8UC1 表示每种颜色1个字节,即灰度,而 CV_8UC3 表示每种颜色3个字节,例如RGB

CV_8UC1 means 1 byte per color, i.e. grayscale, while CV_8UC3 means 3 bytes per color, e.g. RGB

这篇关于如何获得图像在彩色模式下使用opencv c + + raspberry相机模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆