OpenCV C ++ calcHist到Java [英] OpenCV C++ calcHist to Java

查看:1420
本文介绍了OpenCV C ++ calcHist到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Android设备上运行一些c ++代码;但是,我遇到了一个小的问题,类型 Mat 我正在使用。我试图转换的代码如下(第二个函数调用第一个):

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();
}



现在我的java代码将这两个函数合并为1,因为我的类型总是same:CV_32SC1。

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);
}

我收到错误 OpenCV错误:不支持格式或格式的组合()在void cv :: calcHist ,我发现从另一个问题,这是因为我的 src 矩阵的类型 CV_32SC1 。所以我的问题是,我不知道如何将这些线从第二个c ++函数转换为Java正确:

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;



我试图找出如何做类似 Mat_<

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)

只是根据 calcHist

>

So it should be sufficient to do a

src.convertTo(src, CvType.CV_32F);

这篇关于OpenCV C ++ calcHist到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆