前言
1.OpenCV3之后的dnn模型可以可以调用好多用深度学习框架的训练好的模型。
2.这里我演示是我自己训练的一个围棋棋盘识别模型,使用的神经网络是VGG16,OpenCV3.3,IDE是VS2015。
3.训练好模型之后把标签文件,模型文件,配置文件复制到当前工程目录方便调用。
代码
1.标签文件内容:
label: 0
display_name: "background"
label:1
display_name: "B"
label:2
display_name: "H"
2.代码
#pragma once
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
using namespace cv;
using namespace cv::dnn;
using namespace std;
const int meanValues[3] = { 104, 117, 123 };
const size_t width = 300;
const size_t height = 300;
//得到标签名
static Mat getMean(const size_t &w, const size_t &h);
static Mat preprocess(const Mat &frame);
vector<string> readLabels(string label_file);
//得到棋盘
void getBarod(Mat &src, Mat &go_barod, string model_file, string model_text_file, string label_file);
static Mat getMean(const size_t &w, const size_t &h)
{
Mat mean;
vector<Mat> channels;
for (int i = 0; i < 3; i++)
{
Mat channel(h, w, CV_32F, Scalar(meanValues[i]));
channels.push_back(channel);
}
merge(channels, mean);
return mean;
}
static Mat preprocess(const Mat &frame)
{
Mat preprocessed;
frame.convertTo(preprocessed, CV_32F);
resize(preprocessed, preprocessed, Size(width, height));
Mat mean = getMean(width, height);
subtract(preprocessed, mean, preprocessed);
return preprocessed;
}
void getBarod(Mat &src, Mat &go_barod, string model_file, string model_text_file, string label_file)
{
Rect rect;
vector<string> obj_names = readLabels(label_file);
Ptr<dnn::Importer> importer;
try
{
importer = createCaffeImporter(model_text_file, model_file);
}
catch (const cv::Exception &err)
{
cerr << err.msg << endl;
}
Net net;
importer->populateNet(net);
importer.release();
Mat input_image = preprocess(src);
Mat blobImage = blobFromImage(input_image);
net.setInput(blobImage, "data");
Mat detection = net.forward("detection_out");
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
float confidence_threshold = 0.2;
for (int i = 0; i < detectionMat.rows; i++)
{
float confidence = detectionMat.at<float>(i, 2);
if (confidence > confidence_threshold)
{
size_t objIndex = (size_t)(detectionMat.at<float>(i, 1));
float tl_x = detectionMat.at<float>(i, 3) * src.cols;
float tl_y = detectionMat.at<float>(i, 4) * src.rows;
float br_x = detectionMat.at<float>(i, 5) * src.cols;
float br_y = detectionMat.at<float>(i, 6) * src.rows;
rect = Rect((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
rectangle(src, rect, Scalar(i * 10, 0, 255), 2, 8, 0);
putText(src, format("%s", obj_names[objIndex].c_str()), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2);
}
}
namedWindow("src", 0);
imshow("src",src);
}
//得到标签名
void readLabels(vector<string> obj_names, string label_file)
{
ifstream fp(label_file);
if (!fp.is_open())
{
printf("could not open the file...\n");
exit(-1);
}
string name;
while (!fp.eof())
{
getline(fp, name);
if (name.length() && (name.find("display_name:") == 0))
{
string temp = name.substr(15);
temp.replace(temp.end() - 1, temp.end(), "");
obj_names.push_back(temp);
}
}
}
void adaptiveLogarithmicMapping(const Mat& img, Mat &dst)
{
Mat ldrDrago;
img.convertTo(ldrDrago, CV_32FC3, 1.0f / 255);
cvtColor(ldrDrago, ldrDrago, cv::COLOR_BGR2XYZ);
Ptr<TonemapDrago> tonemapDrago = createTonemapDrago(1.f, 1.f, 0.85f);
tonemapDrago->process(ldrDrago, dst);
cvtColor(dst, dst, cv::COLOR_XYZ2BGR);
dst.convertTo(dst, CV_8UC3, 255);
}
vector<string> readLabels(string label_file)
{
vector<string> objNames;
ifstream fp(label_file);
if (!fp.is_open()) {
printf("could not open the file...\n");
exit(-1);
}
string name;
while (!fp.eof())
{
getline(fp, name);
if (name.length() && (name.find("display_name:") == 0))
{
string temp = name.substr(15);
temp.replace(temp.end() - 1, temp.end(), "");
objNames.push_back(temp);
}
}
return objNames;
}
主函数:
#include "function.h"
string label_file = "weiqi.txt";
string model_file = "weiqi.caffemodel";
string model_text_file = "weiqi.prototxt";
int main(void)
{
Mat backbackground = imread("b1.jpg");
if (backbackground.empty())
{
cout<<"could not load image...\n")<<endl;
return -1;
}
Mat frame, go_barod,standard_barod;
getBarod(backbackground, go_barod,model_file,model_text_file, label_file);
waitKey(0);
return 0;
}
测试结果: