opencv输出文字 opencv简单的图像模板匹配代码

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值