您的位置:首页 > 编程语言

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: