本文主要是介绍windows10平台下使用opencv识别舌苔,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
因为学业上的需求,我需要用到机器学习来训练分类器来识别物体,又因为正好看到一篇博客有详细的讲解训练分类器的过程,这里给出链接: https://blog.csdn.net/Shea1992/article/details/83592899
总之,非常的感谢这篇博客给我的帮助,然后,因为我比较常用c++来写代码,而这篇博客里面最后检测分类器效果时用到的却是python语句,所以,我将识别的代码改写成c++并且结合了我自己的需求更新了些功能。
这是通过训练生成出来的文件,总花时14小时多
这是训练器的下载链接:https://download.csdn.net/download/qq_38344751/11949649
代码:
//识别舌苔、舌头
#include<stdio.h>
#include<iostream>#include<math.h>
#include<time.h>
#include<opencv2\opencv.hpp>
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<dlib\opencv.h>
#include<dlib\image_processing\frontal_face_detector.h>
#include<dlib\image_processing\render_face_detections.h>
#include<dlib\image_processing.h>
#include<dlib\gui_widgets.h>
#include "src\facedetectcnn.h"
#include<opencv2\objdetect\objdetect.hpp>
#include<opencv2\video\tracking.hpp>#include<vector>using namespace cv;
using namespace std;
using namespace dlib;#define DETECT_BUFFER_SIZE 0x20000int main(int argc, char*argv[])
{//Mat user_image=imread("C:\\Users\\Keyone\\Desktop\\42.jpg");Mat user_image = imread("E:\\舌头数据集3-300\\TongeImageDataset\\dataset\\7.bmp");CascadeClassifier tongue_cascade;tongue_cascade.load("E:\\Visual_code\\tongue_trainer2\\xml\\cascade.xml");//训练器位置if (tongue_cascade.empty()){printf("--(!)Error loading\n");return -1;}Mat gray;std::vector<Rect> faces;resize(user_image, user_image, Size(240, 160));cvtColor(user_image, gray, CV_BGR2GRAY);equalizeHist(gray, gray);tongue_cascade.detectMultiScale(gray, faces, 1.0243111, 3, IMREAD_GRAYSCALE, Size(20, 20));Mat roi_image;Mat copy_image = user_image.clone();Mat canny_image;for (size_t i = 0; i < faces.size(); i++){if (i != 0)break;Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);//ellipse(user_image, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0); cv::rectangle(user_image, Rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height), Scalar(0, 255, 0), 2);}roi_image = copy_image(Rect(faces[0].x-10, faces[0].y, faces[0].width+25, faces[0].height));//imshow("4", user_image);namedWindow("5", 1);imshow("5", roi_image);Canny(roi_image, canny_image, 45, 90);namedWindow("6", 1);imshow("6", canny_image);//threshold(roi_image, canny_image, 100, 255, THRESH_BINARY);canny算子后面找阈值 方法//std::vector<std::vector<Point>> contours;vector<Mat> contours;//std::vector<Vec4i> hierarchy;//findContours(canny_image, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));//Mat drawing = Mat::zeros(canny_image.size(), CV_8UC3);//for (size_t i = 0; i < contours.size(); i++)//{// Scalar color = Scalar(0,0,255);// drawContours(drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point());//}//Mat mask;//Mat mask2;//mask.create(canny_image.size(), CV_8UC3);//Rect rect;//Mat bgdModel;//Mat fgdModel;//grabCut(canny_image,mask,rect,bgdModel,fgdModel,5,GC_INIT_WITH_RECT);//imshow("7", canny_image);//namedWindow("Contours", WINDOW_AUTOSIZE);//imshow("Contours", drawing);waitKey(0);return 0;
}
这篇关于windows10平台下使用opencv识别舌苔的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!