简体   繁体   English

如何在OpenCV 3.0 C ++中获取链码xml文件

[英]How to get the Chain Code xml file in OpenCV 3.0 c++

I'm using Freeman Chain Code as Feature Extraction for an image. 我正在使用Freeman Chain Code作为图像的特征提取。 I'm not able to read the image and I need to obtain a chain code xml file. 我无法读取图像,我需要获取一个链码xml文件。 How can i retrieve the chain code xml file and save it? 如何检索链码xml文件并保存? Below is my c++ code in OpenCV 3.0 Can someone help.. 下面是我在OpenCV 3.0中的c ++代码有人可以帮忙..

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <fstream>
#include<string.h>

using namespace std;
using namespace cv;

int main() {

    Mat img = imread("test.jpg")
    imshow("Test", img);


    vector<vector<Point>> contours; // Vector for storing contour
    vector<Vec4i> hierarchy;


    cv::findContours(img, contours, RETR_EXTERNAL,CV_CHAIN_CODE);

    cout << Mat(contours[0]) << endl;

    findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    cout << "CHAIN_APPROX_SIMPLE" << endl;
    cout << Mat(contours[0]) << endl;

    CvChain* chain = 0;
    CvMemStorage* storage = 0;
    storage = cvCreateMemStorage();
    cvFindContours(&IplImage(img), storage, (CvSeq**)(&chain), sizeof(*chain), CV_RETR_TREE, CV_CHAIN_CODE);
    int total = chain->total;

    cv::Mat hist(1, 8, CV_32F, Scalar(0));

    int totalCount = 0;

    for (; chain != NULL; chain = (CvChain*)chain->h_next)
    {
        int numChain = 0;

        CvSeqReader reader;
        int i, total = chain->total;
        cvStartReadSeq((CvSeq*)chain, &reader, 0);
        cout<<"--------------------chain\n";

        for (i = 0; i<total; i++)
        {
            char code;
            CV_READ_SEQ_ELEM(code, reader);
            int Fchain = (int)code;

            hist.at<float>(0, Fchain)++;

            totalCount++;

            cout<<"%d"<<code;
        }
    }
    Mat prob = hist / totalCount;
    cout << prob << endl;

    waitKey(0);

    return 0;
}

Whenever the code is being run,I'm having this error.Have I used a wrong format?? 每当运行代码时,我都会遇到此错误。是否使用了错误的格式? Can anyone please help? 谁能帮忙吗?

OpenCV Error: Unsupported format or combination of formats ([Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only) in cvStartFindContours, file C:\buildslave64\win64_amdocl\master_PackSlave-win64-vc14-shared\opencv\modules\imgproc\src\contours.cpp, line 198

I have updated my code.I'm able to save the xml file but but I'm getting the data in only 1 row. 我已经更新了代码。我可以保存xml文件,但是我只能在1行中获取数据。

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <fstream>
#include<string.h>

using namespace std;
using namespace cv;

vector<String> files;
int main() {


    double totalCount = 0;
    cv::glob("C:/Users//Videos/Database/Frames/*.jpg", files);
    for (size_t i = 0; i < files.size(); i++) {
        Mat image = imread(files[i]);

        //Mat image = imread("Outline.jpg");

        Canny(image, image, 100, 100 * 2, 3, false);

        CvChain* chain;
        CvMemStorage* storage = 0;
        storage = cvCreateMemStorage();

        cvFindContours(&IplImage(image), storage, (CvSeq**)(&chain), sizeof(*chain), CV_RETR_EXTERNAL, CV_CHAIN_CODE);

        int total = chain->total;


        // 1 row, 8 cols, filled with zeros, (float type, because we want to normalize later):
        cv::Mat hist(1, 8, CV_32F, Scalar(0));

        for (; chain != NULL; chain = (CvChain*)chain->h_next)
        {
            CvSeqReader reader;
            int i, total = chain->total;

            cvStartReadSeq((CvSeq*)chain, &reader, 0);

            for (i = 0; i < total; i++)
            {
                char code;
                CV_READ_SEQ_ELEM(code, reader);
                int Fchain = (int)code;

                // increase the counter for the respective bin:
                hist.at<float>(0, Fchain)++;

                totalCount++;
            }
        }

        // print the raw histogram:
        cout << "Histo: " << hist << endl;
        cout << "Total: " << totalCount << endl;

        // normalize it:
        Mat prob = hist / totalCount;
        cout << "Proba: " << prob << endl;

        FileStorage fs("freeman.xml", FileStorage::WRITE);
        fs << "chain" << prob;
        waitKey(0);
        return 0;
    }
}

As shown below i'm having my chain code xml like this.Why am i getting this? 如下所示,我的链代码xml是这样的。为什么我会得到这个? Can anyone help me please? 谁能帮我吗?

<?xml version="1.0"?>
<opencv_storage>
<chain type_id="opencv-matrix">
  <rows>1</rows>
  <cols>8</cols>
  <dt>f</dt>
  <data>
    5.00000000e-01 0. 0. 0. 5.00000000e-01 0. 0. 0.</data></chain>
</opencv_storage>

The error message says exactly and unambiguously what is wrong - it's enough to read it. 错误消息准确无误地指出了什么地方出了错-足够阅读。 cv::findContours() accepts only images of CV_8UC1 pixel type or only CV_32SC1 if you use CV_RETR_FLOODFILL mode. cv :: findContours()仅接受CV_8UC1像素类型的图像,如果使用CV_RETR_FLOODFILL模式则仅接受 CV_32SC1 In your particular case, you need to convert your img object to CV_8UC1 after loading - you are probably loading an RGB image. 在特定情况下,您需要在加载后将img对象转换为CV_8UC1-您可能正在加载RGB图像。

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

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