OPENCV에서 LBP,HOG특징을 SVM 벡터기 훈련과 결합하여 테스트
#include
#include
using namespace std;
using namespace cv;
int main()
{
char ad[128] = { 0 };
int filename = 0, filenum = 0;
Mat img = imread("digits.png");
Mat gray;
cvtColor(img, gray, CV_BGR2GRAY);
int b = 20;
int m = gray.rows / b; // 1000*2000
int n = gray.cols / b; // 5000 20*20
imshow("", img);
waitKey(0);
for (int i = 0; i < m; i++)
{
int offsetRow = i*b; //
if (i % 5 == 0 && i != 0)
{
filename++;
filenum = 0;
}
for (int j = 0; j < n; j++)
{
int offsetCol = j*b; //
sprintf_s(ad, "F:\\Picture1\\%d\\%d.bmp", filename, filenum++);
// 0,1 ,
//sprintf_s(ad, "D:\\data\\%d\\%d.jpg", filename, filenum++);
// 20*20
Mat tmp;
gray(Range(offsetRow, offsetRow + b), Range(offsetCol, offsetCol + b)).copyTo(tmp);
imwrite(ad, tmp);
}
}
return 0;
}
2단계: 훈련 및 식별
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
using namespace cv::ml;
void getFiles(string path, vector& files);
void get_trainingBLP(Mat& trainingImages, vector& trainingLabels, char *path, int typeName);
int get_testingBLP(Mat inMat, string modelpath);
void get_trainingHOG(Mat& trainingImages, vector& trainingLabels, char *path, int typeName);
Mat dealBLP(Mat inMat);
Mat dealHOG(Mat inMat);
void TrainSVM(Mat traingImage, vector trainLabel,string filename);
int get_testingHOG(Mat inMat, string modelpath);
int main()
{
//Mat classes;
vector trainingLabels;
Mat trainingImages;
Mat testingImages = Mat(Size(28, 28), CV_8UC3);
//get_trainingBLP(trainingImages, trainingLabels, "D:\\data\\0", 0);
//get_trainingBLP(trainingImages, trainingLabels, "D:\\data\\1", 1);
//"D:\\data\\0" 0
get_trainingHOG(trainingImages, trainingLabels, "D:\\data\\0", 0);
get_trainingHOG(trainingImages, trainingLabels, "D:\\data\\1", 1);
TrainSVM(trainingImages, trainingLabels,"svm1.xml");
int result = 0;
char* filePath = "F:\\Picture\\0";
vector files;
getFiles(filePath, files);
int number = files.size();
cout << "
" << number << " ";
for (int i = 0; i < number; i++)
{
Mat inMat = imread(files[i].c_str());
//int r = get_testingBLP(inMat, "svm.xml");
resize(inMat, testingImages, testingImages.size());
int r = get_testingHOG(testingImages, "svm1.xml");
if (r == 0)
{
result++;
}
}
cout << "
" << result << " 0" << endl;
Mat src = imread("E:/1.jpg");
imshow("ok", src);
waitKey(0);
}
void TrainSVM(Mat traingImage,vector trainLabel,string filename)
{
Mat trainingData;
Mat classes;
Mat(traingImage).copyTo(trainingData);
trainingData.convertTo(trainingData, CV_32FC1);
Mat(trainLabel).copyTo(classes);
Ptr model = SVM::create();
model->setType(SVM::C_SVC);
model->setKernel(SVM::LINEAR);
model->setDegree(0);
model->setGamma(1);
model->setCoef0(0);
model->setC(1);
model->setNu(0);
model->setP(0);
model->setTermCriteria(CvTermCriteria(CV_TERMCRIT_ITER, 1000, 0.01));
Ptr tData = TrainData::create(trainingData, ROW_SAMPLE, classes);
model->train(tData);
model->save(filename);
cout << " !!!!!" << endl;
}
void getFiles(string path, vector& files)
{
long hFile = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib&_A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void get_trainingBLP(Mat& trainingImages, vector& trainingLabels, char *path, int typeName)
{
char *filePath = path;
vector files;
getFiles(filePath, files);
int number = files.size();
for (int i = 0; i < number; i++)
{
cout << "
" << typeName << " " << i << "
" << endl;
Mat SrcImage = imread(files[i].c_str());
SrcImage = dealBLP(SrcImage);
trainingImages.push_back(SrcImage);
trainingLabels.push_back(typeName);
cout << " : " << files[i].c_str() << endl;
}
}
void get_trainingHOG(Mat& trainingImages, vector& trainingLabels, char *path, int typeName)
{
char *filePath = path;
vector files;
getFiles(filePath, files);
int number = files.size();
cout << " :" << number << endl;
Mat data_mat, labels_mat;
data_mat = Mat::zeros(number, 324, CV_32FC1);
labels_mat = Mat::zeros(number, 1, CV_32SC1);
Mat src;
Mat trainImg = Mat(Size(28, 28), CV_8UC3);
for (int i = 0; i < number; i++)
{
cout << "
" << typeName << " " << i << "
" << endl;
Mat SrcImage = imread(files[i].c_str());
resize(SrcImage, trainImg, trainImg.size());
Mat hogMat = dealHOG(trainImg);
//for (int j = 0; j < hogMat.cols; j++)
//{
// data_mat.at(i, j) = hogMat.at(0,j);
//}
trainingImages.push_back(hogMat);
trainingLabels.push_back(typeName);
cout << " : " << files[i].c_str() << endl;
}
}
Mat dealHOG(Mat inMat)
{
Mat result = Mat::zeros(1, 324, CV_32FC1);
HOGDescriptor *hog = new HOGDescriptor(Size(28, 28), Size(14, 14), Size(7, 7), Size(7, 7), 9);
vectordescriptors;// HOG
hog->compute(inMat, descriptors, Size(1, 1), Size(0, 0)); //Hog , (1,1)
//cout << "HOG : " << descriptors.size() << endl;
int number = descriptors.size();
for (int n = 0; n < number; n++)
{
result.at(0,n) = descriptors[n];// 1 n
}
return result;
}
Mat dealBLP(Mat inMat)
{
Mat gray_src;
cvtColor(inMat, gray_src, CV_BGR2GRAY);
Mat lbpImage = Mat::zeros(gray_src.rows - 2, gray_src.cols - 2, CV_8UC1);
for (int row = 1; row < gray_src.rows - 1; row++) {
for (int col = 1; col < gray_src.cols - 1; col++) {
uchar c = gray_src.at(row, col);
uchar code = 0;
code |= (gray_src.at(row - 1, col - 1) > c) << 7;
code |= (gray_src.at(row - 1, col) > c) << 6;
code |= (gray_src.at(row - 1, col + 1) > c) << 5;
code |= (gray_src.at(row, col + 1) > c) << 4;
code |= (gray_src.at(row + 1, col + 1) > c) << 3;
code |= (gray_src.at(row + 1, col) > c) << 2;
code |= (gray_src.at(row + 1, col - 1) > c) << 1;
code |= (gray_src.at(row, col - 1) > c) << 0;
lbpImage.at(row - 1, col - 1) = code;
}
}
Mat outMat = lbpImage.reshape(1, 1);
outMat.convertTo(outMat, CV_32FC1);
return outMat;
}
int get_testingBLP(Mat inMat, string modelpath)
{
int result = 0;
Ptr model;
FileStorage svm_fs(modelpath, FileStorage::READ);
if (svm_fs.isOpened())
{
model = StatModel::load(modelpath);
}
Mat SrcImage = dealBLP(inMat);
int response = (int)model->predict(SrcImage);
return response;
}
int get_testingHOG(Mat inMat, string modelpath)
{
int result = 0;
Ptr model;
FileStorage svm_fs(modelpath, FileStorage::READ);
if (svm_fs.isOpened())
{
model = StatModel::load(modelpath);
}
Mat SrcImage=dealHOG(inMat);
int response = (int)model->predict(SrcImage);
return response;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ip camera android에 액세스하고 java를 사용하여 모니터에 표시그런 다음 PC에서 다운로드 폴더를 추출해야 합니다 그런 다음 프로젝트 폴더에 다운로드한 javacv 라이브러리를 추가해야 합니다. 먼저 라이브러리 폴더를 마우스 오른쪽 버튼으로 클릭한 다음 jar/폴더 추가를 선택...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.