opencv输出文字 opencv简单的图像模板匹配代码
2012-09-10 23:20
615 查看
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pImg = NULL;
IplImage* ptempImg = NULL;
IplImage* presult=NULL;
if( argc == 3)
{
pImg=cvLoadImage(argv[1],0);
ptempImg=cvLoadImage(argv[2],0);
cout<<pImg->nChannels<<endl;
cout<<pImg->depth<<endl;
cout<<pImg->height<<endl;
cout<<pImg->width<<endl;
cout<<ptempImg->height<<endl;
cout<<ptempImg->width<<endl;
int h=pImg->height-ptempImg->height+1;
int w=pImg->width-ptempImg->width+1;
CvSize imgsize;
imgsize.height=h;
imgsize.width=w;
//*
presult=cvCreateImage(imgsize,IPL_DEPTH_32F,1);
cvMatchTemplate( pImg, ptempImg,presult,CV_TM_CCORR_NORMED);//CV_TM_SQDIFF);
float dMax=0;
CvPoint point=cvPoint(0,0);
for (int i=0;i<h;i++)
{
for (int j=0;j<w;j++)
{
float tem=CV_IMAGE_ELEM(presult,float,j,i);
if (dMax<tem)
{
dMax=tem;
point=cvPoint(i,j);
}
}
}
cout<<"pos is"<<point.x<<" "<<point.y<<endl;
CvPoint point1=cvPoint(point.x+ptempImg->width,point.y+ptempImg->height);
CvScalar colo=CV_RGB(255,0,0);
cvRectangle(pImg,point, point1, colo);
/* //输入文本
CvFont font;
double hscale = 1.0;
double vscale = 1.0;
int linewidth = 2;
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,hscale,vscale,0,linewidth);
CvPoint textPos =cvPoint((point.x+point1.x)/2, (point.y+point1.y)/2);
cvPutText(pImg, "*", textPos, &font,colo);
*/
cvCircle( pImg, textPos, 4,colo);// 用圆来标记模板匹配的中心点在图像中的位置
cvSaveImage("match.bmp",pImg);
cvNamedWindow("img");
cvShowImage("img",pImg);
cvNamedWindow("timg",2);
cvShowImage("timg",ptempImg);
cvNamedWindow("rimg",1);
cvShowImage("rimg",presult);
//*/
cvWaitKey(0);
cvDestroyWindow("img");
cvDestroyWindow("timg");
cvReleaseImage( &pImg );
cvReleaseImage( &ptempImg );
cvDestroyWindow("rimg");
cvReleaseImage(&presult);
}
return 0;
}
#include <cstdio>
#include "cv.h"
#include "highgui.h"
#pragma comment (lib, "cv.lib")
#pragma comment (lib, "cvaux.lib")
#pragma comment (lib, "cxcore.lib")
#pragma comment (lib, "highgui.lib")
void cvText(IplImage* img, const char* text, int x, int y)
{
CvFont font;
double hscale = 1.0;
double vscale = 1.0;
int linewidth = 2;
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,hscale,vscale,0,linewidth);
CvScalar textColor =cvScalar(0,255,255);
CvPoint textPos =cvPoint(x, y);
cvPutText(img, text, textPos, &font,textColor);
}
void main()
{
IplImage* image =cvCreateImage(cvSize(500, 600), 8,3);
for (int i =0; i <image->width; i++)
{
for (int j =0; j <image->height; j++)
{
((uchar*)(image->imageData +image->widthStep *j))[i *3] =0;
((uchar*)(image->imageData +image->widthStep *j))[i *3 +1] =0;
((uchar*)(image->imageData +image->widthStep *j))[i *3 +2] =0;
}
}
int x1 =35;
int y1 =40;
cvText(image, "\(35,40\)",35,40);
int x2 =122;
int y2 =230;
cvText(image, "\(122,230\)",122,230);
cvLine(image,cvPoint(35,40),cvPoint(122,230),cvScalar(255,255,0),2);
cvNamedWindow( "window", 1 );
cvShowImage( "window", image);
cvWaitKey( 0 );
cvReleaseImage(&image);
}
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pImg = NULL;
IplImage* ptempImg = NULL;
IplImage* presult=NULL;
if( argc == 3)
{
pImg=cvLoadImage(argv[1],0);
ptempImg=cvLoadImage(argv[2],0);
cout<<pImg->nChannels<<endl;
cout<<pImg->depth<<endl;
cout<<pImg->height<<endl;
cout<<pImg->width<<endl;
cout<<ptempImg->height<<endl;
cout<<ptempImg->width<<endl;
int h=pImg->height-ptempImg->height+1;
int w=pImg->width-ptempImg->width+1;
CvSize imgsize;
imgsize.height=h;
imgsize.width=w;
//*
presult=cvCreateImage(imgsize,IPL_DEPTH_32F,1);
cvMatchTemplate( pImg, ptempImg,presult,CV_TM_CCORR_NORMED);//CV_TM_SQDIFF);
float dMax=0;
CvPoint point=cvPoint(0,0);
for (int i=0;i<h;i++)
{
for (int j=0;j<w;j++)
{
float tem=CV_IMAGE_ELEM(presult,float,j,i);
if (dMax<tem)
{
dMax=tem;
point=cvPoint(i,j);
}
}
}
cout<<"pos is"<<point.x<<" "<<point.y<<endl;
CvPoint point1=cvPoint(point.x+ptempImg->width,point.y+ptempImg->height);
CvScalar colo=CV_RGB(255,0,0);
cvRectangle(pImg,point, point1, colo);
/* //输入文本
CvFont font;
double hscale = 1.0;
double vscale = 1.0;
int linewidth = 2;
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,hscale,vscale,0,linewidth);
CvPoint textPos =cvPoint((point.x+point1.x)/2, (point.y+point1.y)/2);
cvPutText(pImg, "*", textPos, &font,colo);
*/
cvCircle( pImg, textPos, 4,colo);// 用圆来标记模板匹配的中心点在图像中的位置
cvSaveImage("match.bmp",pImg);
cvNamedWindow("img");
cvShowImage("img",pImg);
cvNamedWindow("timg",2);
cvShowImage("timg",ptempImg);
cvNamedWindow("rimg",1);
cvShowImage("rimg",presult);
//*/
cvWaitKey(0);
cvDestroyWindow("img");
cvDestroyWindow("timg");
cvReleaseImage( &pImg );
cvReleaseImage( &ptempImg );
cvDestroyWindow("rimg");
cvReleaseImage(&presult);
}
return 0;
}
#include <cstdio>
#include "cv.h"
#include "highgui.h"
#pragma comment (lib, "cv.lib")
#pragma comment (lib, "cvaux.lib")
#pragma comment (lib, "cxcore.lib")
#pragma comment (lib, "highgui.lib")
void cvText(IplImage* img, const char* text, int x, int y)
{
CvFont font;
double hscale = 1.0;
double vscale = 1.0;
int linewidth = 2;
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,hscale,vscale,0,linewidth);
CvScalar textColor =cvScalar(0,255,255);
CvPoint textPos =cvPoint(x, y);
cvPutText(img, text, textPos, &font,textColor);
}
void main()
{
IplImage* image =cvCreateImage(cvSize(500, 600), 8,3);
for (int i =0; i <image->width; i++)
{
for (int j =0; j <image->height; j++)
{
((uchar*)(image->imageData +image->widthStep *j))[i *3] =0;
((uchar*)(image->imageData +image->widthStep *j))[i *3 +1] =0;
((uchar*)(image->imageData +image->widthStep *j))[i *3 +2] =0;
}
}
int x1 =35;
int y1 =40;
cvText(image, "\(35,40\)",35,40);
int x2 =122;
int y2 =230;
cvText(image, "\(122,230\)",122,230);
cvLine(image,cvPoint(35,40),cvPoint(122,230),cvScalar(255,255,0),2);
cvNamedWindow( "window", 1 );
cvShowImage( "window", image);
cvWaitKey( 0 );
cvReleaseImage(&image);
}
相关文章推荐
- Python OpenCV图像模板匹配
- Python OpenCV学习笔记之:图像模板匹配
- opencv学习(三十八)之图像模板匹配matchTemplate()
- OpenCV的模板匹配简单包装
- Opencv实现图像旋转,非常简单,几行代码
- ROS学习笔记(2):在ROS中使用OpenCV进行简单的图像处理---代码实现篇
- 用opencv简单显示图像和视频(代码有注释)
- Python3与OpenCV3.3 图像处理(十四)--模板匹配
- 【OpenCV】模板匹配和图像拼接
- opencv学习-模板匹配算法(单图像模板匹配和基于模板匹配的目标跟踪)
- matlab/opencv图像处理之模板匹配
- Opencv 输出文字,实现简单水印
- python for OpenCV图像处理之模板匹配以及分水岭算法
- 使用VS2010+OpenCV2.4.9简单图像水印代码
- 【opencv学习之三十】简单模板匹配matchTemplate
- opencv 图像模板匹配
- OpenCV之imgproc 模块. 图像处理(4)直方图均衡化 直方图计算 直方图对比 反向投影 模板匹配
- opencv简单的图像读取
- 一种基于特征点匹配的图像检索方法的opencv实现
- opencv学习_11 (模板匹配(包括单模板和多模板))