基于opencv的logo识别

 基于opencv的logo识别

首先环境为visual studio 2012+opencv3.2.0.  大家尽量采用此环境,opencv对于环境要求较高,环境不同会一堆报错很难处理。

 

1、opencv安装配置环境(此节略过后续会补充在博客)

2、开始做logo识别。一个logo大致步骤,先通过颜色区别将logo从整体环境中剥离出来。此处以美的logo做解释;

 

 

 

 

 

 

 

 

图一为原图,图二为经过opencv 里的hsv变换的图片,图三为针对蓝色做颜色过滤。经过此部分操作即可得到logo。

3、然后针对logo部分转为轮廓化,进行轮廓比对识别。

 

 图一为过滤出来的logo黑白二值图,图二为轮廓图,图三为logo中字母轮廓,图四为用来比对的轮廓。黑色xin文字为相似度,值越小代表相似度越高。

至此即实现logo的检测及比对,详细代码如下:

// opencv.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv2/opencv.hpp>


#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
 
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;  

/*int main()  
{  
    //the path of test image
    Mat img=imread("C:\\Users\\14675\\Desktop\\扫码枪教程\\无标题h.png"); 
    namedWindow("dog");  
    imshow("dog",img);  
    waitKey(6000); // window closed after 6000 ms
}
*/





int main()
{

	/*
	//VideoCapture cap(0);

	while (true)
	{ //Mat matSrc;
	 //cap >> matSrc;


    //Mat matSrc=imread("D://程序工程/opencv/x64/Debug/2.jpg",IMREAD_UNCHANGED);
	 
	resize(matSrc, matSrc, Size(), 0.5, 0.5);
	Mat matHsv;
	//waitKey(2000);
	cvtColor(matSrc,matHsv,COLOR_BGR2HSV);


	Mat a,b,c,d,e;
	inRange(matSrc,Scalar(0, 0, 0),Scalar(180, 255, 46),a);
	medianBlur(a,b,3);
	medianBlur(b,c,5);

	Mat element = getStructuringElement(MORPH_ELLIPSE, Size(2 * 1 + 1, 2 * 1 + 1), Point(1, 1));
	Mat element1 = getStructuringElement(MORPH_ELLIPSE, Size(2 * 3 + 1, 2 * 3 + 1), Point(3, 3));

		erode(c, d, element);//腐蚀  


 imshow("显示灰度图",matSrc);
 imshow("显示灰度图1",matHsv);
 
 int ca = waitKey(100);//按ESC则退出当前视频
   if ((char)ca == 27)
   {
    break;
   }

	}*/
	
	//图片初始化
	Mat matSrc=imread("D://程序工程/opencv/x64/Debug/3.jpg",IMREAD_UNCHANGED);
	Mat matSrc2=imread("D://程序工程/opencv/x64/Debug/4.jpg",IMREAD_UNCHANGED);

	//尺寸归一化 
	resize(matSrc, matSrc, Size(300,300), 1, 1);
	resize(matSrc2, matSrc2, Size(300,300), 1, 1);
	//imshow("显示灰度图1",matSrc);
	//hsv通道
	Mat matHsv,matHsv2;
	cvtColor(matSrc,matHsv,COLOR_BGR2HSV);
	cvtColor(matSrc2,matHsv2,COLOR_BGR2HSV);
	//imshow("显示灰度图2",matHsv);
	//过滤蓝色二值化
	Mat lan,lan2;
	inRange(matHsv,Scalar(35, 43, 46),Scalar(124, 255, 255),lan);
	inRange(matHsv2,Scalar(35, 43, 46),Scalar(124, 255, 255),lan2);
	imshow("显示灰度图3",lan);

	Mat fu,peng,fu2,peng2;
	Mat element = getStructuringElement(MORPH_ELLIPSE, Size(2 * 1 + 1, 2 * 1 + 1), Point(1, 1));
	erode(lan,fu, element);//腐蚀  
	dilate(lan, peng, element);//膨胀
	erode(lan2,fu2, element);//腐蚀  
	dilate(lan2, peng2, element);//膨胀
	peng-=fu;
	peng2-=fu2;
	
	lan=peng;
	lan2=peng2;
	imshow("形态学1",lan);
	//提取轮廓
	CvSeq *test_seqs;
	CvSeq *mode_seqs;
	vector<Vec4i> hierarchy,hierarchy2;
	vector<vector<Point>> contours,contours2;

    findContours(lan,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());//提取轮廓元素
	findContours(lan2,contours2,hierarchy2,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());//提取轮廓元素
	//绘制轮廓
	drawContours(matSrc,contours, 1, Scalar(115), 3, 8, hierarchy);
	drawContours(matSrc2,contours2,3, Scalar(115), 3, 8, hierarchy2);
	imshow("形态学2",matSrc);
	imshow("形态学对比模板",matSrc2);
	double zhixin=matchShapes(contours[1],contours2[3],CV_CONTOURS_MATCH_I3,1.0);

	

	printf("轮廓一数目:%d  轮廓二数目:%d   xin:%f",contours.size(),contours2.size(),zhixin);
	//cvMatchShapes(lan,lan2,1);

	//imshow("显示灰度图1",matSrc);
    //imshow("显示灰度图2",matSrc2);
	//Canny(lan, lan, 3, 9, 3);
	//Canny(lan2, lan2, 3, 9, 3);
	//imshow("显示灰度图3",lan);
   // imshow("显示灰度图4",lan2);
	waitKey();
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章