Ask Your Question
0

error about cvtColor

asked 2015-12-02 21:32:57 -0600

jason gravatar image

updated 2015-12-02 21:38:08 -0600

Hello, I am using the opencv to compare images with the code compareHist_Demo.cpp, here is my code

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include "raspicam_cv.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h> 
#include <wiringPi.h>
 using namespace cv;
using namespace std;
bool doTestSpeedOnly=false;
//parse command line
//returns the index of a command line param in argv. If not found, return -1
 int findParam ( string param,int argc,char **argv ) {
int idx=-1;
for ( int i=0; i<argc && idx==-1; i++ )
    if ( string ( argv[i] ) ==param ) idx=i;
return idx;

}
 //parse command line
 //returns the value of a command line param. If not found, defvalue is returned
 float getParamVal ( string param,int argc,char **argv,float defvalue=-1 ) {
int idx=-1;
for ( int i=0; i<argc && idx==-1; i++ )
    if ( string ( argv[i] ) ==param ) idx=i;
if ( idx==-1 ) return defvalue;
else return atof ( argv[  idx+1] );
}

void processCommandLine ( int argc,char **argv,raspicam::RaspiCam_Cv &Camera ) {
Camera.set ( CV_CAP_PROP_FRAME_WIDTH,  getParamVal ( "-w",argc,argv,192 ) );
Camera.set ( CV_CAP_PROP_FRAME_HEIGHT, getParamVal ( "-h",argc,argv,144 ) );
Camera.set ( CV_CAP_PROP_BRIGHTNESS,getParamVal ( "-br",argc,argv,50 ) );
Camera.set ( CV_CAP_PROP_CONTRAST ,getParamVal ( "-co",argc,argv,50 ) );
Camera.set ( CV_CAP_PROP_SATURATION, getParamVal ( "-sa",argc,argv,50 ) );
Camera.set ( CV_CAP_PROP_GAIN, getParamVal ( "-g",argc,argv ,50 ) );
if ( findParam ( "-gr",argc,argv ) !=-1 )
    Camera.set ( CV_CAP_PROP_FORMAT, CV_8UC1 );
if ( findParam ( "-test_speed",argc,argv ) !=-1 )
    doTestSpeedOnly=true;
if ( findParam ( "-ss",argc,argv ) !=-1 )
    Camera.set ( CV_CAP_PROP_EXPOSURE, getParamVal ( "-ss",argc,argv )  );
if ( findParam ( "-wb_r",argc,argv ) !=-1 )
    Camera.set ( CV_CAP_PROP_WHITE_BALANCE_RED_V,getParamVal ( "-wb_r",argc,argv )     );
if ( findParam ( "-wb_b",argc,argv ) !=-1 )
    Camera.set ( CV_CAP_PROP_WHITE_BALANCE_BLUE_U,getParamVal ( "-wb_b",argc,argv )     );





  }

 void showUsage() {
cout<<"Usage: "<<endl;
cout<<"[-gr set gray color capture]\n";
cout<<"[-test_speed use for test speed and no images will be saved]\n";
cout<<"[-w width] [-h height] \n[-br brightness_val(0,100)]\n";
cout<<"[-co contrast_val (0 to 100)]\n[-sa saturation_val (0 to 100)]";
cout<<"[-g gain_val  (0 to 100)]\n";
cout<<"[-ss shutter_speed (0 to 100) 0 auto]\n";
cout<<"[-wb_r val  (0 to 100),0 auto: white balance red component]\n";
cout<<"[-wb_b val  (0 to 100),0 auto: white balance blue component]\n";

cout<<endl;
   }



    int main ( int argc,char **argv ) {
int fd;
int a;
int b;
if ((fd = serialOpen("/dev/ttyAMA0", 9600)) < 0)
{
    fprintf(stderr, "unable to open serial device; %s\n", strerror(errno));
    return 1;
}
if ( argc==1 ) {
    cerr<<"Usage (-help for help)"<<endl;
}
if ( findParam ( "-help",argc,argv ) !=-1 ) {
    showUsage();
    return -1;
}
raspicam::RaspiCam_Cv Camera;
Camera.set(CV_CAP_PROP_FPS, 30);
processCommandLine(argc, argv, Camera);
cout << "Connecting to camera" << endl;
if (!Camera.open()) {
    cerr << "Error opening camera" << endl;
    return -1;
}
cout << "Connected to camera =" << Camera.getId() << endl;

cv::Mat image;
vector<Mat> Concat;
//int nCount = 70;
cout << "Capturing" << endl;
for (int i = 0; i <= 3; i++){
    Camera.grab();
    Camera.retrieve(image);
    Concat.push_back(image.clone());
    cv::imwrite("image"+std::to_string(i)+".jpg",image);
    //delay(150);
}
Mat src_base, hsv_base ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-12-03 01:01:55 -0600

LorenaGdL gravatar image

The problem is here:

src_base = imread("image0", 1);
src_test1 = imread("image1", 1);
src_test2 = imread("image2", 1);

imread() is not reading your saved images because you're not using the full filename, with extension. It should be replaced with this:

src_base = imread("image0.jpg", 1);
src_test1 = imread("image1.jpg", 1);
src_test2 = imread("image2.jpg", 1);

In fact, you should add proper checks every time you use imread(), to make sure you're properly reading images. Example:

src_base = imread("image0.jpg", 1);
if (src_base.empty()){
    cout << "Error reading image from file" << endl;
    return -1;
}
edit flag offensive delete link more

Comments

Yes, it worker. Thank you very much, now I can finish my final project. Thank you!

jason gravatar imagejason ( 2015-12-04 19:02:34 -0600 )edit

Please, mark the answer as solved. And you're welcome

LorenaGdL gravatar imageLorenaGdL ( 2015-12-05 01:59:26 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-02 21:32:57 -0600

Seen: 579 times

Last updated: Dec 03 '15