简体   繁体   English

OpenCV C ++ CalcHist到Java

[英]OpenCV C++ calcHist to Java

I'm trying to get some c++ code to run on my Android device; 我正在尝试让一些c ++代码在我的Android设备上运行; however, I'm running into a small little problem with the type of Mat I'm using. 但是,我在使用的Mat类型上遇到了一个小问题。 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. 现在我的java代码将这2个函数合并为1,因为我的类型始终是相同的: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 . 我收到错误OpenCV Error: Unsupported format or combination of formats () in void cv::calcHist我从另一个问题中发现这是因为src矩阵的类型为CV_32SC1 So my problem is that I don't know how to convert these lines from the second c++ function into Java properly: 所以我的问题是我不知道如何将这些行从第二个c ++函数正确地转换为Java:

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. 我试图弄清楚如何在Java中专门执行类似于Mat_<float>(src)

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 . 只是创建类型CV_32F与SRC内容的新的垫目标所要求的calcHist

So it should be sufficient to do a 因此,做一个

src.convertTo(src, CvType.CV_32F);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM