c语言0xC0000005 - 爱问答

(爱问答)

c语言0xC0000005

// SupportVectormachineClassification.cpp : 定义控制台应用程序的入口点。

//

 

#include "stdafx.h"

 

struct PTS{

int x;// x coordinate

int y;// y coordinate

int l;// label

};

 

void SVMClassification()

{

// image files (directories should be marked by '')

const TCHAR * trianing_sample_pts =_T("C:UsersAdministratorDesktopBYSJyangbenjieguojieguo1_30jieguo1subset_1.csv");

const TCHAR * image_data =_T("C:UsersAdministratorDesktopBYSJBYLWSYSJITUXIANG");// image data should be BSQ format!

const TCHAR * classification_result =_T("C:UsersAdministratorDesktopBYSJ uxiangfenleijieguo esult_1_30");

 

int w = 1500;// image width

int h = 1000;// image height

int dim = 9;// number of image feature bands

 

// read training samples

_AutoFilePtr train_pts_fp(trianing_sample_pts,_T("r"));

if(0 == train_pts_fp.get()){

std::cerr<<"open training sample file error!"<<std::endl;

return;

}

 

int num_class;// number of classes

int num_pts;// number of training samples points

_ftscanf(train_pts_fp.get(),_T("%d,%d "),&num_class,&num_pts);

 

_AutoPtr<PTS> points(num_pts);

if(0 == points.get()){

std::cerr<<"memory application error for training points!"<<std::endl;

return;

}

 

for(int i=0;i<num_pts;++i){

_ftscanf(train_pts_fp.get(),_T("%d,%d,%d "),&points[i].l,&points[i].x,&points[i].y);

}

 

_AutoPtr<float> train_label(num_pts);

if(0 == train_label.get()){

std::cerr<<"memory application for training data label information"<<std::endl;

return;

}

 

for(int i=0;i<num_pts;++i){

train_label[i] = static_cast<float>(points[i].l);

}

 

// read sample feature data

 

_AutoFilePtr data_fp(image_data,_T("rb"));

if(0 == data_fp.get()){

std::cerr<<"file open error for image data!"<<std::endl;

return;

}

 

int all_size = w*h*dim;

_AutoPtr<float> image_data_all(all_size);

if(0 == image_data_all.get()){

std::cerr<<"memory application error for image data!"<<std::endl;

return;

}

 

fread(image_data_all.get(),sizeof(float),all_size,data_fp.get());

 

int data_size = num_pts*dim;

_AutoPtr<float> image_data_train_pts(data_size);

if(0 == image_data_train_pts.get()){

std::cerr<<"memory application error for training points feature data!"<<std::endl;

return;

}

 

int num_pixels = w*h;

for(int i=0;i<num_pts;++i){

 

int image_offset = points[i].y*w + points[i].x;

 

for(int j=0;j<dim;++j){

//rewind(data_fp.get());

//fseek(data_fp.get(),image_offset+num_pixels*j,SEEK_SET);

//float value;

//fread(&value,sizeof(float),1,data_fp.get());

float value = image_data_all[image_offset+j*num_pixels];

 

image_data_train_pts[i*dim+j] = value;

}

}

 

// construct data for random forest training

Mat train_label_mat(num_pts,1,CV_32FC1,train_label.get());

Mat train_data_mat(num_pts,dim,CV_32FC1,image_data_train_pts.get());

 

// train random forest

CvSVM svm_alg;

 

CvSVMParams params;

params.svm_type    = CvSVM::C_Svc;

params.kernel_type = CvSVM::RBF;// radial basis function

    params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER,100,1e-6);//termination: end when maximal iterative number is reached

int cv_fold = 10;// very important! 10 as a default setting, but smaller number is needed when the number of training samples are too low

 

svm_alg.train_auto(train_data_mat,train_label_mat,Mat(),Mat(),params,cv_fold);// this method allows grid search

 

Mat var_type = Mat(dim + 1, 1, CV_8U );

var_type.setTo(Scalar(CV_VAR_NUMERICAL) ); // all inputs are numerical

var_type.at<uchar>(dim, 0) = CV_VAR_CATEGORICAL;// this is a classification problem

 

svm_alg.clear();

svm_alg.train_auto(train_data_mat,train_label_mat,Mat(),Mat(),params,cv_fold);

 

// prediction (classification)

_AutoPtr<float> vec_data(dim);// to store the feature vector for each pixel

if(0 == vec_data.get()){

std::cerr<<"memory application error for pixel feature vector data!"<<std::endl;

return;

}

 

_AutoPtr<short int> result(num_pixels);

if(0 == result.get()){

std::cerr<<"memory application error for result!"<<std::endl;

return;

}

 

for(int i=0;i<h;++i){

for(int j=0;j<w;++j){

 

int image_offset = i*w+j;// current pixel position

for(int k=0;k<dim;++k){

//rewind(data_fp.get());

//fseek(data_fp.get(),image_offset+num_pixels*k,SEEK_SET);

//float value;

//fread(&value,sizeof(float),1,data_fp.get());

float value = image_data_all[image_offset + k*num_pixels];

vec_data[k] = value;

}

 

Mat feature_vec(1,dim,CV_32FC1,vec_data.get());

 

short int result_label = static_cast<short int>(svm_alg.predict(feature_vec));

 

result[image_offset] = result_label;

}

}

 

// output classification result

_AutoFilePtr result_fp(classification_result,_T("wb+"));

if(0 == result_fp.get()){

std::cerr<<"open file error for result!"<<std::endl;

return;

}

 

fwrite(result.get(),num_pixels,sizeof(short int),result_fp.get());

}

 

int _tmain(int argc, _TCHAR* argv[])

{

SVMClassification();

 

return 0;

}

怎么改啊   各位大神

首先,你的程序不是c语言的而是C++的(它们不是同一语言啊)

其次,你确定你的两个原始文件确实存在且图像一定是BSQ格式?

个人一向的建议,初学者不要尝试自己看不懂的程序,这样没有意义

(网上程序大多不能直接编译运行的)

相关标签:c语言

下一篇:c语言用for循环语句实现数组中每个元素都加5

上一篇:用c语言完成下列问题!!!

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?