opencv svm训练问题
编写了个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;
}
#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;
}
...全文
相关推荐
opencv SVM实现二分类的训练与测试
在本教程中,我们将聚焦于如何使用OpenCV中的支持向量机(SVM,Support Vector Machine)来实现二分类问题的训练与测试。
OpenCV支持向量机SVM用于非线性可分离数据
OpenCV支持向量机SVM用于非线性可分离数据支持向量机SVM用于非线性可分离数据目标动机Motivation优化问题的扩展源代码解释设置训练数据设置SVM的参数训练SVM显示决策区域显示训练数据支持向量结果 支持向量机SVM用于非线性可分离数据 目标 在本教程中,您将学习如何: 在无法线性分离训练数据时,为SVM定义优化问题。 如何配置参数以使SVM适应此类问题。 动机Motivation 为什么扩展SVM优化问题以处理非线性可分离的训练数据有趣吗?与简单的线性分类器相比,在计算机视觉中使用SVM的大多
SVM(Opencv程序)
本程序示例展示了如何使用OpenCV来构建一个简单的SVM,解决2维空间中的3级分类问题。
Opencv_SVM训练_识别几何体
SVM功能在OpenCV中被封装在`cv::SVM`类中,可以方便地进行训练和预测。**SVM(支持向量机)**: SVM是一种监督学习模型,主要用于二分类和多分类问题,也可以用于回归任务。
opencv使用SVM实现图像分类识别
**OpenCV 使用 SVM 实现图像分类识别**支持向量机(Support Vector Machine,简称 SVM)是一种广泛应用的机器学习算法,特别是在计算机视觉领域中,它在图像分类和识别方面表现出色


模式及实现
