1

我有一个用 c++ 编写的 opencv 项目,我将使用 javacv 将其转换为 java

给定这个用 C++ 编写的方法:

void extractBOWDescriptor(const path& basepath, Mat& descriptors, Mat& labels) {
for (directory_iterator iter = directory_iterator(basepath); iter
        != directory_iterator(); iter++) {
    directory_entry entry = *iter;
    if (is_directory(entry.path())) {
        cout << "Processing directory " << entry.path().string() << endl;
        extractBOWDescriptor(entry.path(), descriptors, labels);
    } else {
        path entryPath = entry.path();
        if (entryPath.extension() == ".jpg") {
            cout << "Processing file " << entryPath.string() << endl;
            Mat img = imread(entryPath.string());
            if (!img.empty()) {
                vector<KeyPoint> keypoints;
                detector->detect(img, keypoints);
                if (keypoints.empty()) {
                    cerr << "Warning: Could not find key points in image: "
                            << entryPath.string() << endl;
                } else {
                    Mat bowDescriptor;
                    bowDE.compute(img, keypoints, bowDescriptor);
                    descriptors.push_back(bowDescriptor);
                    float label=atof(entryPath.filename().c_str());
                    labels.push_back(label);
                }
            } else {
                cerr << "Warning: Could not read image: "
                        << entryPath.string() << endl;
            }
        }
    }
}

}

没有 javacv 文档,我也不是 C++ 开发人员,所以我仔细尝试了一些 javacv 示例,这就是我得到的结果;

        public void extractBOWDescriptor (String baseurl , CvMat descriptors , CvMat labels) 

  {
    File dir = new File(baseurl);

    for (File child : dir.listFiles())
    {
        if (child.isDirectory())
        {
            System.out.println("Processing directory"+child.getAbsolutePath());

            extractBOWDescriptor(child.getName(), descriptors, labels);
        }


        else 
        {
            if (child.getName().contains(".jpg"))
            {
                 System.out.println("Processing file" + child.getName());

                 final CvMat image = cvLoadImageM(child.getName());

                 if (!image.empty())
                 {

                    KeyPoint keypoints = new KeyPoint(); // a verifier 
                    featureDetector.detect(image, keypoints, null);

                    if (keypoints.isNull())
                    {
                        System.out.println("Warning: Could not find key points in image: "+

                                child.getName()
                                );
                    }

                    else 
                    {
                        CvMat BoWdescriptors = new CvMat() ; // 
                        bDescriptorExtractor.compute(image, keypoints, BoWdescriptors, null, null);

                        descriptors.put(descriptors);
                        float label=Float.parseFloat(child.getName());
                        labels.put(label);

                         }
                 }

                 else 
                 {
                     System.out.println("Warning: Could not read image: "+ child.getName());
                 }

            }
        }
    }

  }

转换是否正确?我特别想知道这些线条:

        Mat bowDescriptor; --> CvMat BoWdescriptors = new CvMat() ; 

     float label=atof(entryPath.filename().c_str());--> float label=Float.parseFloat(child.getName());
4

0 回答 0