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/imgcodecs.hpp"
include "opencv2/highgui/highgui.hpp"
include "opencv2/imgproc/imgproc.hpp"
//#include "opencv2/opencv.hpp" //editted
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;<="" p="">
} //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]="" );="" }<="" p="">
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 ) );
// Camera.setSharpness ( getParamVal ( "-sh",argc,argv,0 ) ); // if ( findParam ( "-vs",argc,argv ) !=-1 ) // Camera.setVideoStabilization ( true ); // Camera.setExposureCompensation ( getParamVal ( "-ev",argc,argv ,0 ) );
}
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";<="" p="">
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;</p> <pre><code>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;="" mat="" src_test1,="" hsv_test1;="" mat="" src_test2,="" hsv_test2;="" mat="" hsv_half_down;="" load="" three="" images="" with="" different="" environment="" settings="" if="" (argc="" <="" 4)="" {="" printf("**="" error.="" usage:="" .="" comparehist_demo="" <image_settings0>="" <image_setting1>="" <image_settings2>\n");="" return="" -1;="" }="" src_base="imread("image0"," 1);="" src_test1="imread("image1"," 1);="" src_test2="imread("image2"," 1);="" convert="" to="" hsv="" cvtcolor(src_base,="" hsv_base,="" color_bgr2hsv);="" cvtcolor(src_test1,="" hsv_test1,="" color_bgr2hsv);="" cvtcolor(src_test2,="" hsv_test2,="" color_bgr2hsv);="" hsv_half_down="hsv_base(Range(hsv_base.rows" 2,="" hsv_base.rows="" -="" 1),="" range(0,="" hsv_base.cols="" -="" 1));="" using="" 50="" bins="" for="" hue="" and="" 60="" for="" saturation="" int="" h_bins="50;" int="" s_bins="60;" int="" histsize[]="{" h_bins,="" s_bins="" };="" hue="" varies="" from="" 0="" to="" 179,="" saturation="" from="" 0="" to="" 255="" float="" h_ranges[]="{" 0,="" 180="" };="" float="" s_ranges[]="{" 0,="" 256="" };="" const="" float*="" ranges[]="{" h_ranges,="" s_ranges="" };="" use="" the="" o-th="" and="" 1-st="" channels="" int="" channels[]="{" 0,="" 1="" };="" histograms="" matnd="" hist_base;="" matnd="" hist_half_down;="" matnd="" hist_test1;="" matnd="" hist_test2;="" calculate="" the="" histograms="" for="" the="" hsv="" images="" calchist(&hsv_base,="" 1,="" channels,="" mat(),="" hist_base,="" 2,="" histsize,="" ranges,="" true,="" false);="" normalize(hist_base,="" hist_base,="" 0,="" 1,="" norm_minmax,="" -1,="" mat());="" calchist(&hsv_half_down,="" 1,="" channels,="" mat(),="" hist_half_down,="" 2,="" histsize,="" ranges,="" true,="" false);="" normalize(hist_half_down,="" hist_half_down,="" 0,="" 1,="" norm_minmax,="" -1,="" mat());="" calchist(&hsv_test1,="" 1,="" channels,="" mat(),="" hist_test1,="" 2,="" histsize,="" ranges,="" true,="" false);="" normalize(hist_test1,="" hist_test1,="" 0,="" 1,="" norm_minmax,="" -1,="" mat());="" calchist(&hsv_test2,="" 1,="" channels,="" mat(),="" hist_test2,="" 2,="" histsize,="" ranges,="" true,="" false);="" normalize(hist_test2,="" hist_test2,="" 0,="" 1,="" norm_minmax,="" -1,="" mat());="" apply="" the="" histogram="" comparison="" methods="" for="" (int="" i="0;" i="" <="" 4;="" i++)="" {="" int="" compare_method="i;" double="" base_base="compareHist(hist_base," hist_base,="" compare_method);="" double="" base_half="compareHist(hist_base," hist_half_down,="" compare_method);="" double="" base_test1="compareHist(hist_base," hist_test1,="" compare_method);="" double="" base_test2="compareHist(hist_base," hist_test2,="" compare_method);="" printf("="" method="" [%d]="" perfect,="" base-half,="" base-test(1),="" base-test(2)="" :="" %f,="" %f,="" %f,="" %f="" \n",="" i,="" base_base,="" base_half,="" base_test1,="" base_test2);="" }="" printf("done="" \n");="" camera.release();="" <="" code="">
}`
I use the raspberry pi to take images and then compare them. But when I run this code, the camera take pictures, but it gave this error information `OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cvtColor, file /usr/src/packages/BUILD/opencv-2.4.1+dfsg/modules/imgproc/src/color.cpp, line 3344 terminate called after throwing an instance of 'cv::Exception' what(): /usr/src/packages/BUILD/opencv-2.4.1+dfsg/modules/imgproc/src/color.cpp:3344: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cvtColor
Aborted` Could anyone help me with this? In addition, my opencv version is 2.4.1