CalcHist on java [closed]

asked 2017-10-02 03:06:46 -0500

aruno14 gravatar image

updated 2017-10-03 00:15:49 -0500

Hello,

I use calcHist function to learn colors (HSV) of sky images using svm. The learning process is coded in c++, I tested result and the precision is about 80%. Then I try to use the svm file in an android application. But all prediction are -1. I check my program but all seems ok. I think maybe the result of the calcHist function is different in c++ and java. Can someone check if I use same function in c++ and java ?

C++ code

int hbins = 30, sbins = 32; 
int histSize[] = {hbins, sbins};
float hranges[] = { 0, 180 };
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
MatND hist; int channels[] = {0, 1};
calcHist( &imageHSV, 1, channels, Mat(),  hist, 2, histSize, ranges, true, false);

Java code

int hbins = 30, sbins = 32;
MatOfInt histSize = new MatOfInt(hbins, sbins);
MatOfFloat histRange = new MatOfFloat(0f, 180f, 0f, 256f);
List<Mat> imageList = new ArrayList<>();
imageList.add(smallMat);
Mat hsvHist = new Mat();
Imgproc.calcHist(imageList, new MatOfInt(0, 1), new Mat(), hsvHist, histSize, histRange);

Resolved I find the problem, in C++ the image space is BGR by default but in java it is RGBA. All the data order was inversed that is why the svm prediction was completly wrong.

Thank you!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by aruno14
close date 2017-10-03 00:16:07.879660

Comments

results are identical, so it's not that.

how do you set up the SVM ?

berak gravatar imageberak ( 2017-10-02 03:23:02 -0500 )edit

In c++ I can set uniform=true, but in java, I cannot, the result is uniform by default ? I use below code to set up the SVM.

Ptr<SVM> svmHist = SVM::create();
svmHist->setType(SVM::C_SVC);
svmHist->setKernel(SVM::LINEAR);
svmHist->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
Ptr<TrainData> tdHist = TrainData::create(images_hist, ROW_SAMPLE, labelsMat);
svmHist->train(tdHist);
aruno14 gravatar imagearuno14 ( 2017-10-02 03:54:49 -0500 )edit