opencv svm训练问题

qq_24490801 2017-12-16 02:00:47
编写了个svm代码,但是运行到train那步就出错,求大神告知错误原因,非常感谢!
#include<opencv2/opencv.hpp>
#include<iostream>
#include<string>
using namespace std;
using namespace cv;
using namespace cv::ml;

int r=10,last_x=0,last_y=0,drawing=0;
Mat asd(512, 512, CV_8UC1), clean_asd;
void draw(int x, int y)
{
circle(asd, Point(x, y), r, Scalar(0, 0, 0), -1, 8);
asd.copyTo(clean_asd);
imshow("手写板", clean_asd);
}
void drawCursor(int x, int y)
{
asd.copyTo(clean_asd);
circle(clean_asd, Point(x, y), r, Scalar(0, 0, 0), -1, 8);
}
void on_mouse(int event, int x, int y, int flags, void* param)
{
last_x = x;
last_y = y;
drawCursor(x, y);
//Select mouse Event
if (event == CV_EVENT_LBUTTONDOWN)
{
drawing = 1;
draw(x, y);
}
else if (event == CV_EVENT_LBUTTONUP)
{
drawing = 0;
}
else if (event == CV_EVENT_MOUSEMOVE && flags & CV_EVENT_FLAG_LBUTTON)
{
if (drawing)
draw(x, y);
}
}
int xmax = 0, xmin = 0;
void findx(Mat src)
{
int i, j,k,a=0,minfind = 0;
Mat dst = src.clone();
threshold(dst, dst, 128, 255, THRESH_BINARY_INV);
for (i = 0; i < dst.cols; i++)
{
for (j = 0; j < dst.rows; j++)
{
uchar* data = dst.ptr(j);
a += data[i];
}
if (a>1)
{
xmax = i;
if (!minfind)
{
xmin = i;
minfind = 1;
}
}
a = 0;
}
}
int ymax = 0, ymin = 0;
void findy(Mat src)
{
int i, j, a = 0, minfind = 0;
Mat dst = src.clone();
threshold(dst, dst, 128, 255, THRESH_BINARY_INV);
for (i = 0; i < dst.rows; i++)
{
uchar* data = dst.ptr(i);
for (j = 0; j < dst.cols; j++)
{
a += data[j];
}
if (a>1)
{
ymax = i;
if (!minfind)
{
ymin = i;
minfind = 1;
}
}
a = 0;
}
}
int xrange, yrange;
void findrect(Mat src)
{

findx(src);
findy(src);
xrange = xmax - xmin;
yrange = ymax - ymin;
}
Mat data[10];
string fname[] = { "C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\0\\*.pbm", "C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\1\\*.pbm",
"C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\2\\*.pbm" ,"C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\3\\*.pbm",
"C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\4\\*.pbm" ,"C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\5\\*.pbm",
"C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\6\\*.pbm" ,"C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\7\\*.pbm",
"C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\8\\*.pbm" ,"C:\\Users\\shinelon\\Desktop\\数字\\数字\\OCR\\9\\*.pbm" };
void read(void)
{
vector<String> files;
int i,j;
for (j = 0; j < 10; j++)
{
glob(fname[j], files, false);
for (i = 0; i < files.size(); i++)
{
Mat test = imread(files[i]);
data[j].push_back(test);
}
}
}
void getTrainData(Mat *_train_data, Mat *_train_label)
{
int i, j, k;
vector<String> files;
_train_data->release();//清零
_train_label->release();//清零
for (j = 0; j < 10; j++)
{
glob(fname[j], files, false);
for (i = 0; i < files.size(); i++)
{
Mat test = imread(files[i],0);//128*128
_train_data->push_back(test.reshape(0, 1));//reshape第一个为通道数,0表示不变,第二个为行数,0表示不变
}
}
int buf[11] = {0,1,2,3,4,5,6,7,8,9};
for (j = 0; j < 10; j++)
{
for (i = 0; i < 100; i++)
{
_train_label->push_back(buf[j]);
}
}
}
Mat dst(128, 128, CV_32SC1);
void svm()
{
Mat train_data, train_label;
getTrainData(&train_data, &train_label); //获取样本训练数据
train_data.convertTo(train_data, CV_32SC1);
train_label.convertTo(train_label, CV_32SC1);
Mat train_data2 = Mat::ones(1000, 3, CV_32SC1);
// 设置训练数据
Ptr<TrainData> tData = TrainData::create(train_data, ROW_SAMPLE, train_label);

// 设置参数
Ptr<SVM> model = SVM::create();
model->setType(SVM::C_SVC);
model->setKernel(SVM::LINEAR);

model->setC(1);

model->train(tData);
}

int main()
{
Mat image(512, 512, CV_8UC1);
namedWindow("手写板", WINDOW_AUTOSIZE);
asd = Scalar::all(255);
asd.copyTo(clean_asd);
setMouseCallback("手写板",on_mouse,0);
while (1)
{
char c;
imshow("手写板", clean_asd);
c=waitKey(10);
if(c==27)
break;
else if (c=='r')
{
asd = Scalar::all(255);
drawCursor(last_x, last_y);
}
else if (c=='='||c=='+')
{
r++;
drawCursor(last_x, last_y);
}
else if ((c=='-')&&(r>1))
{
r--;
drawCursor(last_x, last_y);
}
else if (c == 'a')
{
findrect(clean_asd);
image = clean_asd( Range(ymin, ymin + yrange),Range(xmin, xmin + xrange));
resize(image, dst, dst.size());
dst = dst.reshape(0, 1);
svm();
namedWindow("asd", WINDOW_AUTOSIZE);
imshow("asd", dst);
}
}
return 0;
}
...全文
731 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_24490801 2017-12-16
  • 打赏
  • 举报
回复
出错位置在较下面的void svm里的model->train(tData);

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧