在使用opencv做svm时,遇到内存异常错误?

在使用opencv做svm时,遇到内存异常错误
报错为0x00007FFC029ECD29 处(位于 SVM.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x0000000B4CD2E770 处。
认为是输入数据的问题,将数据转为mat并设置为 CV_32FC1型,但是仍未解决
输出图像的分类结果图
#include 
#include 
#include "gdal_priv.h"
#include "gdalwarper.h"
#include "ogr_spatialref.h"
#include "string"
#include 
#include 
#include 
#include 
#include
#include "core/core.hpp"
#include "highgui/highgui.hpp"
#include "imgproc/imgproc_c.h"
#include
#include
#include
#include
using namespace std;
using namespace cv;

void split(const char* samplefile, vector<int>&lable_M, vectorint>>&sample_M)
{
    vectorint> >v;
    vector<int>tempdata;
    ifstream fs;
    string s;
    fs.open(samplefile);
    if (fs)
    {
        cout << "文件打开成功" << endl;
    }
    while (getline(fs, s))
    {
        istringstream is(s);
        int data;
        while (!is.eof())//判断文件是否到了末尾
        {
            is >> data;
            tempdata.push_back(data);
        }
        v.push_back(tempdata);
        tempdata.clear();
        s.clear();
    }
    fs.close();
    int rows = v.size();
    int cols = v[0].size();
    vectorint>>sample(cols);
    for (int i = 0; i < cols; i++)
    {
        sample[i].resize(rows);
        for (int j = 0; j < rows; j++)
        {
            sample[i][j] = v[j][i];
        }
    }
    lable_M = sample[0];
    sample_M.assign(sample.begin() + 1, sample.end());
}

int main()
{
    GDALAllRegister();
    CPLSetConfigOption("GDAL_FLLENAME_IS_UTF8", "NO");
    const char* infile = "D:\\c++\\bishe\\DATA\\JJJ_LCO8_123034_20200920.tif";
    const char* outfile = "D:\\c++\\bishe\\DATA\\JJJ_LCO8_123034_20200920_out.tif";
    const char* sample = "xixiaoshuiti.txt";
    GDALDataset* poin = (GDALDataset*)GDALOpen(infile, GA_ReadOnly);
    int src_width = poin->GetRasterXSize();
    int src_height = poin->GetRasterYSize();
    int bands = poin->GetRasterCount();

    double geo[6] = { 0 };
    const char* porj = NULL;
    poin->GetGeoTransform(geo);
    porj = poin->GetProjectionRef();
    GDALDataType datatype = poin->GetRasterBand(1)->GetRasterDataType();
    //cout << datatype << endl;
    unsigned short** image = (unsigned short**)malloc(sizeof(unsigned short*) * bands);
    for (int i = 0; i < bands; i++)
    {
        image[i] = (unsigned short*)malloc(sizeof(unsigned short) *src_width * src_height);
        poin->GetRasterBand(i + 1)->RasterIO(GF_Read, 0, 0, src_width, src_height, image[i], src_width, src_height, GDT_UInt16, 0, 0);
    }
    Mat mat_data = Mat(bands, src_width * src_height, CV_32FC1);//把整张影像转为mat格式
    for (int i = 0; i < bands; i++)
    {
        for (int j = 0; j < src_height * src_width; j++)
        {
            mat_data.at<float>(i, j) = image[i][j];
        }
    }
    Mat data;
    cv::normalize(mat_data, data, 1, 0, cv::NORM_MINMAX);


    vector<int>lable_M;
    vectorint>>sample_M;
    split(sample, lable_M, sample_M);//拆分txt文件

    /*cout << sample_M.size() << endl;
    cout << sample_M[0].size() << endl;
    cout << lable_M.size() << endl;*/
    /*for (int i = 0; i < lable_M.size(); i++)
    {
        cout << lable_M[i] << endl;
    }*/

    Mat train_labels = Mat(1, lable_M.size(), CV_32FC1);
    for (int i = 0; i < lable_M.size(); i++)
    {
        train_labels.at<float>(i) = lable_M[i];
    }
    //int* lables = new int[lable_M.size()];
    //memset(lables, 0, sizeof(int)*lable_M.size());
    //copy(lable_M.begin(), lable_M.end(), lables);
    //Mat lablemat(lable_M.size(), 1, CV_32SC1, lables);
    //cout << train_labels << endl;

    Mat train_images1 = Mat(sample_M.size(), sample_M[0].size(), CV_32FC1);
    for (int i = 0; i < sample_M.size(); i++)
    {
        for (int j = 0; j < sample_M[0].size(); j++)
        {
            train_images1.at<float>(i, j) = sample_M[i][j];//访问冲突
        }
    }
    Mat train_images = Mat(sample_M.size(), sample_M[0].size(), CV_32FC1);
    cv::normalize(train_images1, train_images, 1, 0, cv::NORM_MINMAX);//归一化
    //cout<< train_images <

    cv::Ptr < cv::ml::SVM > svm = cv::ml::SVM::create();//创建svm
    //设置类型为C_SVC代表分类
    svm->setType(cv::ml::SVM::C_SVC);
    //设置核函数
    svm->setKernel(cv::ml::SVM::POLY);
    ////设置其它属性
    svm->setGamma(3.0);
    svm->setDegree(3.0);
    ////设置迭代终止条件 
    svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER | cv::TermCriteria::EPS, 100, 0.0001));
    //开始训练
    cv::Ptr train_data = cv::ml::TrainData::create(train_images, cv::ml::ROW_SAMPLE, train_labels);
    cout << "开始进行训练..." << endl;
    svm->train(train_data);
    cout << "训练完成" << endl;

    Mat pixel = Mat_<float>(1, bands);//初始化一个长为波段数的mat,用于存放每一个像元
    char* fenlei = (char*)malloc(sizeof(char) * src_width * src_height);//接收分类结果
    for (int i = 0; i < src_height * src_width; i++)//循环遍历每一个像元
    {
        for (int j = 0; j < bands; j++)
        {
            pixel.at<float>(j) = data.at<float>(j, i);
        }//把每一个像元的的值赋值给pixel,从而获得当前像元
        //cout << pixel << endl;
        fenlei[i] = svm->predict(pixel);
    }
    

    unsigned char** class_rt = new unsigned char*[3];
    for (int i = 0; i < 3; i++)
    {
        class_rt[i] = new unsigned char[src_height * src_width];
    }
    for (int i = 0; i < src_height * src_width; i++)
    {
        if (fenlei[i] == 0)
        {
            class_rt[0][i] = 0;
            class_rt[1][i] = 0;
            class_rt[2][i] = 255;
        }
        else if (fenlei[i] == 1)
        {
            class_rt[0][i] = 255;
            class_rt[1][i] = 0;
            class_rt[2][i] = 255;
        }

    }
    GDALDriver* podriver = (GDALDriver*)GDALGetDriverByName("GTiff");
    GDALDataset* p_out = podriver->Create(outfile, src_width, src_height, 3, GDT_Byte, NULL);
    for (int i = 0; i < 3; i++)
    {
        p_out->GetRasterBand(i + 1)->RasterIO(GF_Write, 0, 0, src_width, src_height, class_rt[i], src_width, src_height, GDT_Byte, 0, 0);

    }
    GDALClose(p_out);

}



你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。