I'm trying to get some c++ code to run on my Android device; however, I'm running into a small little problem with the type of Mat
I'm using. The code I'm trying to convert is as follow (the second function calls the first):
static Mat
histc_(const Mat& src, int minVal=0, int maxVal=255, bool normed=false)
{
Mat result;
// Establish the number of bins.
int histSize = maxVal-minVal+1;
// Set the ranges.
float range[] = { static_cast<float>(minVal), static_cast<float>(maxVal+1) };
const float* histRange = { range };
// calc histogram
calcHist(&src, 1, 0, Mat(), result, 1, &histSize, &histRange, true, false);
// normalize
if(normed) {
result /= (int)src.total();
}
return result.reshape(1,1);
}
static Mat histc(InputArray _src, int minVal, int maxVal, bool normed)
{
Mat src = _src.getMat();
switch (src.type()) {
case CV_8SC1:
return histc_(Mat_<float>(src), minVal, maxVal, normed);
break;
case CV_8UC1:
return histc_(src, minVal, maxVal, normed);
break;
case CV_16SC1:
return histc_(Mat_<float>(src), minVal, maxVal, normed);
break;
case CV_16UC1:
return histc_(src, minVal, maxVal, normed);
break;
case CV_32SC1:
return histc_(Mat_<float>(src), minVal, maxVal, normed);
break;
case CV_32FC1:
return histc_(src, minVal, maxVal, normed);
break;
default:
CV_Error(Error::StsUnmatchedFormats, "This type is not implemented yet."); break;
}
return Mat();
}
Now my java code combined these 2 functions into 1 since my type is always the same: CV_32SC1.
private Mat histc(Mat src, int minVal, int maxVal)
{
Mat result = new Mat();
MatOfInt histSize = new MatOfInt(maxVal - minVal + 1);
MatOfFloat histRange = new MatOfFloat(minVal, maxVal + 1);
MatOfInt channels = new MatOfInt(0);
Log.d(TAG, "Type: " + CvType.typeToString(src.type()));
src.convertTo(src, CvType.CV_32S);
Imgproc.calcHist(Arrays.asList(src), channels, new Mat(), result, histSize, histRange);
return result.reshape(1,1);
}
I'm getting an error OpenCV Error: Unsupported format or combination of formats () in void cv::calcHist
and I found from another question that this is because the type of my src
matrix is CV_32SC1
. So my problem is that I don't know how to convert these lines from the second c++ function into Java properly:
case CV_32SC1:
return histc_(Mat_<float>(src), minVal, maxVal, normed);
break;
I'm trying to figure out how to do something similar to Mat_<float>(src)
in Java specifically.
For reference: here is the link to the entire code for what I'm trying to do right now
Mat_<float>(src)
just creates a new Mat object of type CV_32F with src's content, as required by calcHist.
So it should be sufficient to do a
src.convertTo(src, CvType.CV_32F);
来源:https://stackoverflow.com/questions/34232349/opencv-c-calchist-to-java