How to get image in color mode using opencv c++ wi

2019-09-10 03:10发布

问题:

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)

here is the code :

#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;
}

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

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

回答1:

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 means 1 byte per color, i.e. grayscale, while CV_8UC3 means 3 bytes per color, e.g. RGB