/*
这个是完整版本,添加标签,分类
*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include<iostream>
#include<opencv2\opencv.hpp>
#include <windows.h>
using namespace std;
using namespace cv;
using namespace cv::ml;
void on_mouse(int event, int x, int y, int flags, void* ustc); //鼠标取样本点
void scale_Sampling(const Mat &src, Mat &dst, double Ratio);
int main(int argc, char *argv[])
{
long red_numb = 0, yellow_numb = 0, green_numb = 0, blue_numb = 0, back_numb = 0;
//------------------------坐标信息----------------------//
//红色:x245,y211
//绿色:x500,y710
//黄色:x770,y60
//蓝色:x1040,y490
//背景:x13,y246
int redx = 245, redy = 211;
int greenx = 500, greeny = 710;
int yellowx = 770, yellowy = 60;
int bluex = 1040, bluey = 490;
int backx = 13, backy = 150;
int radi = 11;
Mat arry[10];
String a = "C:/Users/ncutl/Desktop/siasuna.png";
Mat ori_image = imread(a);
Point c(0, 0), b(128, 100);
Rect rect1(c, b);
Mat roi1;
ori_image(rect1).copyTo(roi1); // copy the region rect1 from the image to roi1
imshow("1", roi1);
//namedWindow("ori", CV_WINDOW_NORMAL);
//imshow("ori", ori_image);
//setMouseCallback("ori", on_mouse, 0);
/*Mat Red_Mat(50, 50, CV_8UC3, Scalar(0, 0, 255));
cout << endl << Red_Mat << endl << endl;
uchar *p = Red_Mat.data;
//waitKey(0);
Mat T3_M(Red_Mat.rows*Red_Mat.cols, 3, CV_8UC1, p);//转化成一维
cout << "T3_M: " <<endl<< T3_M << endl;
*/
//--------------------红色训练集---------------------//
//rectangle(red, cv::Rect(0, 0, 200, 150), cv::Scalar(255, 0, 0), 10);
Mat red_roi_uf(ori_image, Rect(redx, redy, 100, 100));
Mat red_roi;
DWORD tim1 = GetTickCount();
medianBlur(red_roi_uf, red_roi, radi);
DWORD tim2 = GetTickCount();
cout << tim2 - tim1 << endl;
//cout << red_roi << endl << endl;
Mat red_roi_convert;
red_roi.convertTo(red_roi_convert, CV_32FC1);
Mat red_roi_data(red_roi_convert.rows*red_roi_convert.cols, 3, CV_32FC1, red_roi_convert.data);
//cout << red_roi_data << endl << endl;
Mat red_label = Mat(red_roi_convert.rows*red_roi_convert.cols, 1, CV_32SC1, Scalar::all(1));
//cout << red_label << endl << endl;
imshow("红色", red_roi);
//-------------------黄色训练集-----------------//
Mat yellow_roi_uf(ori_image, Rect(yellowx, yellowy, 100, 100));
Mat yellow_roi;
tim1 = GetTickCount();
medianBlur(yellow_roi_uf, yellow_roi, radi);
tim2 = GetTickCount();
cout << tim2 - tim1 << endl;
//cout << red_roi << endl << endl;
Mat yellow_roi_convert;
yellow_roi.convertTo(yellow_roi_convert, CV_32FC1);
Mat yellow_roi_data(yellow_roi_convert.rows*yellow_roi_convert.cols, 3, CV_32FC1, yellow_roi_convert.data);
//cout << yellow_roi_data << endl << endl;
Mat yellow_label = Mat(yellow_roi_convert.rows*yellow_roi_convert.cols, 1, CV_32SC1, Scalar::all(2));
imshow("黄色", yellow_roi);
//------------------------绿色训练集---------------//
Mat green_roi_uf(ori_image, Rect(greenx, greeny, 100, 100));
Mat green_roi;
tim1 = GetTickCount();
medianBlur(green_roi_uf, green_roi, radi);
tim2 = GetTickCount();
cout << tim2 - tim1 << endl;
//cout << green_roi << endl << endl;
Mat green_roi_convert;
green_roi.convertTo(green_roi_convert, CV_32FC1);
Mat green_roi_data(green_roi_convert.rows*green_roi_convert.cols, 3, CV_32FC1, green_roi_convert.data);
//cout << green_roi_data << endl << endl;
Mat green_label = Mat(green_roi_convert.rows*green_roi_convert.cols, 1, CV_32SC1, Scalar::all(3));
imshow("绿色", green_roi);
//cout << green_label << endl;
//-----------------------蓝色训练集---------------//
Mat blue_roi_uf(ori_image, Rect(bluex, bluey, 100, 100));
Mat blue_roi;
tim1 = GetTickCount();
medianBlur(blue_roi_uf, blue_roi, radi);
tim2 = GetTickCount();
cout << tim2 - tim1 << endl;
//cout << blue_roi << endl << endl;
Mat blue_roi_convert;
blue_roi.convertTo(blue_roi_convert, CV_32FC1);
Mat blue_roi_data(blue_roi_convert.rows*blue_roi_convert.cols, 3, CV_32FC1, blue_roi_convert.data);
//cout << blue_roi_data << endl << endl;
Mat blue_label = Mat(blue_roi_convert.rows*blue_roi_convert.cols, 1, CV_32SC1, Scalar::all(4));
imshow("蓝色", blue_roi);
//---------------------背景训练集-----------------//
Mat back_roi_uf(ori_image, Rect(backx, backy, 80, 180));
Mat back_roi;
tim1 = GetTickCount();
medianBlur(back_roi_uf, back_roi, radi);
tim2 = GetTickCount();
cout << tim2 - tim1 << endl;
//cout << back_roi << endl << endl;
Mat back_roi_convert;
back_roi.convertTo(back_roi_convert, CV_32FC1);
Mat back_roi_data(back_roi_convert.rows*back_roi_convert.cols, 3, CV_32FC1, back_roi_convert.data);
//cout << back_roi_data << endl << endl;
Mat back_label = Mat(back_roi_convert.rows*back_roi_convert.cols, 1, CV_32SC1, Scalar::all(5));
imshow("背景", back_roi);
//setMouseCallback("红色", on_mouse, 0);
//-----------------合并所有的样本点,作为训练数据----------------------//
Mat train_data, train_label;
vconcat(red_roi_data, yellow_roi_data, train_data);
vconcat(train_data, green_roi_data, train_data);
vconcat(train_data, blue_roi_data, train_data);
vconcat(train_data, back_roi_data, train_data);
vconcat(red_label, yellow_label, train_label);
vconcat(train_label, green_label, train_label);
vconcat(train_label, blue_label, train_label);
vconcat(train_label, back_label, train_label);
//cout << train_data << endl;
//cout << train_label << endl;
// 设置参数
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::POLY);
//svm->setNu(0.5);
svm->setGamma(100);
//svm->setC(100);
svm->setDegree(0.1);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
// 训练分类器
Ptr<TrainData> tData = TrainData::create(train_data, ROW_SAMPLE, train_label);
svm->train(tData);
cout << "训练完成" << endl << endl;
Vec3b green(0, 255, 0), blue(255, 0, 0), red(0, 0, 255), yellow(0, 255, 255), black(0, 0, 0);
Mat test_img_uf = imread("C:\\Users\\ncutl\\Desktop\\22.png");
Mat test_img;
Mat samp_ed;
scale_Sampling(test_img_uf, test_img, 0.3);
//imwrite("C:\\Users\\ncutl\\Desktop\\22_samp.png", test_img);
DWORD star_time = GetTickCount();
tim1 = GetTickCount();
//transpose(test_img_uf, test_img);
medianBlur(test_img, test_img, 3);
//boxFilter(test_img_uf, test_img, -1, Size(5, 5));
tim2 = GetTickCount();
cout << tim2 - tim1 << endl;
/*Mat copyim(ori_image_uf, Rect(244, 208, 140, 140));
Mat test_img = copyim.clone();*/
//pixel.val[0];
//cout << (int)green.val[0] << endl;
//CvScalar s = cvGet2D(&red, 200, 201);
//cout << '\t' << s.val[1] << '\t' << s.val[2] << endl;//输出B值,G值,R值
//Mat sampleMat = (Mat_<float>(1, 3) << temp.at<Vec3b>(j, i)[0], temp.at<Vec3b>(j, i)[1], temp.at<Vec3b>(j, i)[2]);
//----------------单个像素测试----------------//
for (int i = 0; i < test_img.rows; i++)
for (int j = 0; j < test_img.cols; j++)
{
Vec3b pixel = test_img.at<Vec3b>(i, j);
float a_t = pixel[0];
float b_t = pixel[1];
float c_t = pixel[2];
//cout << a_t << endl << b_t << endl << c_t << endl;
Mat sampleMat = (Mat_<float>(1, 3) << a_t, b_t, c_t);
int response = svm->predict(sampleMat);
if (response == 1)
{
test_img.at<Vec3b>(i, j) = red; red_numb++;
}
if (response == 2)
{
test_img.at<Vec3b>(i, j) = yellow;
yellow_numb++;
}
if (response == 3)
{
test_img.at<Vec3b>(i, j) = green;
green_numb++;
}
if (response == 4)
{
test_img.at<Vec3b>(i, j) = blue;
blue_numb++;
}
if (response == 5)
//cout << "beijing " << endl;
{
test_img.at<Vec3b>(i, j) = black;
back_numb++;
}