我的第一个openCV程序,是改别人的成果,呵呵。

类别:编程语言 点击:0 评论:0 推荐:

我的第一个openCV程序,是改别人的成果,呵呵。

实现了简单的图像处理功能,更多的程序,会随着我的学习逐渐添加。

// cvmedianfilterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "cvmedianfilter.h"
#include "cvmedianfilterDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#include "cvaux.h"
#include "cv.h"
#include "highgui.h"
//#include "ijl.h"   ????
#include "cvcam.h"
#include "math.h"
//#include "ipl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <direct.h>
//#include "dibapi.h"  ????

#pragma comment(lib,"cvaux.lib")
#pragma comment(lib,"cvcam.lib")
//#pragma comment(lib,"ijl15.lib")  ????
#pragma comment(lib,"cv.lib")
#pragma comment(lib, "highgui.lib")

#define ORIG_WIN_SIZE  24
static CvMemStorage* storage1 = 0;
static CvHidHaarClassifierCascade* hid_cascade = 0;
//on_mouse( int event, int x, int y, int flags );
IplImage* detect_and_draw( IplImage* image );
 char filename[]="D:\\Sample256-1.bmp";


class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

// Dialog Data
 //{{AFX_DATA(CAboutDlg)
 enum { IDD = IDD_ABOUTBOX };
 //}}AFX_DATA

 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CAboutDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 //{{AFX_MSG(CAboutDlg)
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
 //{{AFX_DATA_INIT(CAboutDlg)
 //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 
 //{{AFX_DATA_MAP(CAboutDlg)
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 //{{AFX_MSG_MAP(CAboutDlg)
  // No message handlers
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCvmedianfilterDlg dialog

CCvmedianfilterDlg::CCvmedianfilterDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CCvmedianfilterDlg::IDD, pParent)
{
 //m_num = 0;
 //{{AFX_DATA_INIT(CCvmedianfilterDlg)
  // NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCvmedianfilterDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //DDX_Text(pDX, IDC_NUM, m_num);
 
 //{{AFX_DATA_MAP(CCvmedianfilterDlg)
  // NOTE: the ClassWizard will add DDX and DDV calls here
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCvmedianfilterDlg, CDialog)
 //{{AFX_MSG_MAP(CCvmedianfilterDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDC_gray, Ongray)
 ON_BN_CLICKED(IDC_medfilter, Onmedfilter)
 ON_BN_CLICKED(IDC_singreen, Onsingreen)
 ON_BN_CLICKED(IDC_noise, Onnoise)
 ON_BN_CLICKED(IDC_saltnoise, Onsaltnoise)
 ON_BN_CLICKED(IDC_BUTTON1, OnCanny)
 ON_BN_CLICKED(IDC_BUTTON2, OnThreshold)
 ON_BN_CLICKED(IDC_Laplace, OnLaplace)
 ON_BN_CLICKED(IDC_Log, OnLog)
 ON_BN_CLICKED(IDC_Tough, OnTough)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCvmedianfilterDlg message handlers

BOOL CCvmedianfilterDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 // Add "About..." menu item to system menu.

 // IDM_ABOUTBOX must be in the system command range.
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);

 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
  CString strAboutMenu;
  strAboutMenu.LoadString(IDS_ABOUTBOX);
  if (!strAboutMenu.IsEmpty())
  {
   pSysMenu->AppendMenu(MF_SEPARATOR);
   pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  }
 }

 // Set the icon for this dialog.  The framework does this automatically
 //  when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE);   // Set big icon
 SetIcon(m_hIcon, FALSE);  // Set small icon
 
 // TODO: Add extra initialization here
 
 return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCvmedianfilterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  CDialog::OnSysCommand(nID, lParam);
 }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCvmedianfilterDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  // Center icon in client rectangle
  int cxIcon = GetSystemMetrics(SM_CXICON);
  int cyIcon = GetSystemMetrics(SM_CYICON);
  CRect rect;
  GetClientRect(&rect);
  int x = (rect.Width() - cxIcon + 1) / 2;
  int y = (rect.Height() - cyIcon + 1) / 2;

  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  CDialog::OnPaint();
 }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCvmedianfilterDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}

void CCvmedianfilterDlg::Ongray()
{
 // TODO: Add your control notification handler code here
 UpdateData(true);
 char filename[]="D:\\Sample256-1.bmp";
 //char filename[]="F:\\switty\\opencv吕斌\\newimage008.dib";//"F:\\xhl\\搞笑图片\\ert.jpg";
 char *wndname;

 IplImage *gray = 0,*gray2=0;
 gray=cvLoadImage(filename,1);
// gray2=cvLoadImage(filename,1);

 gray2=cvCreateImage(cvSize(gray->width,gray->height),IPL_DEPTH_8U,1);
 
 wndname="GRAY2BGR";
 cvNamedWindow(wndname,1);
 cvCvtColor(gray, gray2, CV_BGR2GRAY); //rgb转换为灰度
 
 cvShowImage(wndname,gray2);
 
 cvWaitKey(0);
 cvReleaseImage(&gray);
 cvReleaseImage(&gray2);

 cvDestroyWindow( wndname );

}

void CCvmedianfilterDlg::Onmedfilter()
{
 // TODO: Add your control notification handler code here
 char filename2[]="D:\\Sample256-1.bmp";
 char filename1[]="D:\\Sample256-1.bmp";
 char filename[]="D:\\Sample256-1.bmp";

 char *wndname;
 IplImage *gray,*gray2,*dst,*dst3;
 gray=cvLoadImage(filename,1);
 gray2=cvCreateImage(cvSize(gray->width,gray->height),IPL_DEPTH_8U,1);
 dst=cvCreateImage(cvSize(gray->width,gray->height),IPL_DEPTH_8U,4);
 dst3=cvCreateImage(cvSize(gray->width,gray->height),IPL_DEPTH_8U,3);
  
// wndname="以位图图像格式保存";
// cvSaveImage(filename1,dst3);//cvSaveImage(filename1,dst3);
// cvNamedWindow(wndname,1);
// gray1=cvLoadImage(filename1,1);  
// cvShowImage(wndname,gray1);
 if(num==0)
 {
  wndname="原图";
  cvNamedWindow(wndname,1);
  cvShowImage(wndname,gray);

  wndname="灰度图像";
  cvNamedWindow(wndname,1);
  cvCvtColor(gray, gray2, CV_BGR2GRAY);
  cvCvtColor(gray2,dst3,8);
  cvShowImage(wndname,dst3);

  cvWaitKey(0);
 // cvReleaseImage(&gray);
 // cvReleaseImage(&gray2);
 // cvReleaseImage(&dst3);
 // cvReleaseImage(&dst);
 // cvDestroyWindow(wndname);
  num=1;
 }
 else
 { 

 CDC* pDC=GetDC();
 CDC dcMem;
 CBitmap myBitmap;
 myBitmap.CreateCompatibleBitmap(pDC,gray->height,gray->width);
 dcMem.CreateCompatibleDC(pDC);
 dcMem.SelectObject(&myBitmap);
 
 //char *rvalue=0,*bvalue=0,*gvalue=0;
    DWORD   iFilterH=3 ; // 滤波器的高度
    DWORD   iFilterW=3 ; // - 滤波器的宽度
    DWORD   iFilterMX=1 ; //- 滤波器的中心元素X坐标
    DWORD   iFilterMY=1 ; //- 滤波器的中心元素Y坐标
 DWORD   iFilterLen=iFilterH * iFilterW;//窗口大小
 DWORD i,j,k,l,m,n;
 DWORD temp,temp1;
 BYTE r,r1,g,g1,b,b1,r2,g2,b2;
 BYTE bTemp;
 BYTE rvalue[9],bvalue[9],gvalue[9];

 for(i=iFilterMY;i<(gray->height+iFilterMY+1);i++)
  for(j=iFilterMX;j<(gray->width-iFilterW+iFilterMX+1);j++)
  {
   temp=i*(gray->width)*3+j*3;
   r=(BYTE)(gray->imageData[temp]);
   g=(BYTE)(gray->imageData[temp+1]);
   b=(BYTE)(gray->imageData[temp+2]);
   for (k = 0; k < iFilterH; k++)
    for (l = 0; l < iFilterW; l++)
    {
     temp1=(i-iFilterMY+k)*(gray->width)*3+(j-iFilterMX+l)*3;
     r1=(BYTE)(gray->imageData[temp1]);
     g1=(BYTE)(gray->imageData[temp1+1]);
     b1=(BYTE)(gray->imageData[temp1+2]);
           rvalue[k * iFilterW + l]=r1;
     bvalue[k * iFilterW + l]=b1;
     gvalue[k * iFilterW + l]=g1;
    
    }

  for (m = 0; m < iFilterLen - 1; m ++)
  for (n = 0; n < iFilterLen - m - 1; n ++)
   if (rvalue[n] > rvalue[n + 1])
   {
    bTemp = rvalue[n];
    rvalue[n] = rvalue[n + 1];
    rvalue[n + 1] = bTemp;
   }

  r2=rvalue[(iFilterLen + 1) / 2];

  for (m = 0; m < iFilterLen - 1; m ++)
  for (n = 0; n < iFilterLen - m - 1; n ++)
   if (bvalue[n] > bvalue[n + 1])
   {
    bTemp = bvalue[n];
    bvalue[n] = bvalue[n + 1];
    bvalue[n + 1] = bTemp;
   }

  b2=bvalue[(iFilterLen + 1) / 2];

  for (m = 0; m < iFilterLen - 1; m ++)
  for (n = 0; n < iFilterLen - m - 1; n ++)
   if (gvalue[n] > gvalue[n + 1])
   {
    bTemp = gvalue[n];
    gvalue[n] = gvalue[n + 1];
    gvalue[n + 1] = bTemp;
   }

  g2=gvalue[(iFilterLen + 1) / 2];

   //b2=(BYTE)GetMedianNum(bvalue, iFilterH * iFilterW);
   //g2=(BYTE)GetMedianNum(gvalue, iFilterH * iFilterW);
   //r2=(BYTE)GetMedianNum(rvalue, iFilterH * iFilterW);*/
   dcMem.SetPixel(j,i,RGB(b2,g2,r2));
  }

  pDC->StretchBlt(0,0,gray->width,gray->height,&dcMem,0,0,gray->width,gray->height,SRCCOPY);
 

  
 ReleaseDC(pDC);
 ReleaseDC(&dcMem); 
 // cvReleaseImage(&gray1);
 //cvDestroyWindow( wndname );
 }
  cvReleaseImage(&gray);
  cvReleaseImage(&gray2);
  cvReleaseImage(&dst3);
  cvReleaseImage(&dst);
  cvDestroyWindow( wndname );
}


void CCvmedianfilterDlg::OnOK()
{
 // TODO: Add extra validation here
 
 CDialog::OnOK();
}

void CCvmedianfilterDlg::Onsingreen()   //打开源图像
{
 // TODO: Add your control notification handler code here
  char filename[]="D:\\Sample256-1.bmp";
  char *wndname;
  wndname="原图";

  IplImage *image=0;
  image=cvLoadImage(filename,1);
  

  cvNamedWindow(wndname,1);
  
  cvShowImage(wndname,image);
  cvWaitKey(0);
  cvReleaseImage(&image);
 
}

void CCvmedianfilterDlg::Onnoise()
{
 // TODO: Add your control notification handler code here
 char filename[]="D:\\Sample256-1.bmp";
 char *wndname;
 wndname="噪声图像";

 IplImage *gray;
 gray=cvLoadImage(filename,1);
 
 cvNamedWindow(wndname,1);
 cvShowImage(wndname,gray);

 
}
/*
void CCvmedianfilterDlg::Onsaltnoise()
{
 // TODO: Add your control notification handler code here
 char filename[]="D:\\Sample256-1.bmp";
 char *wndname;
 IplImage *gray = 0,*gray2=0,*gray3=0,*dst=0,*dst3=0;
 gray=cvLoadImage(filename,1);
 
 gray2=cvCreateImage(cvSize(gray->width,gray->height),IPL_DEPTH_8U,1);
 gray3=cvCreateImage(cvSize(gray->width,gray->height),8,3);
 dst3=cvCreateImage(cvSize(gray->width,gray->height),IPL_DEPTH_8U,3);
 dst=cvCreateImage(cvSize(gray->width,gray->height),IPL_DEPTH_8U,4);
 wndname="GRAY2BGR";
 
 cvCvtColor(gray, gray2,CV_BGR2GRAY);
 
 cvCvtColor(gray2,dst3,8);
 
 gray3=cvCloneImage(dst3);


 //以下加入椒盐噪声
 CDC* pDC=GetDC();
 CDC dcMem;
 CBitmap myBitmap;
 myBitmap.CreateCompatibleBitmap(pDC,gray3->height,gray3->width);
 dcMem.CreateCompatibleDC(pDC);
 dcMem.SelectObject(&myBitmap);

 DWORD i,j;//,k,l,m,n;
 DWORD temp;//,temp1;
 BYTE r,g,b;

 srand((unsigned)time(NULL));//生成伪随机种子
 for (j = 0;j < gray3->height;j++)
 {
  for(i = 0;i < gray3->width ;i++)
  {
   
    // 指向源图像第j行,第i个象素的指针   
    temp =j* (gray3->width)*3+ i*3;
    r=(BYTE)(dst3->imageData[temp]);
    g=(BYTE)(dst3->imageData[temp+1]);
    b=(BYTE)(dst3->imageData[temp+2]);
    
   if(rand()>30000)
   {
    //图像中当前点置为黑
    r=b=g=0;
    dcMem.SetPixel(i,j,RGB(b,g,r));
   }
   else
   {
    dcMem.SetPixel(i,j,RGB(b,g,r));
     
   }
  }
 }
 pDC->StretchBlt(
  0,
  0,
  gray3->width,
  gray3->height,
  &dcMem,
  0,
  0,
  gray3->width*3,
  gray3->height*3,
  SRCCOPY);

 
// wndname="noisegray3";
// cvNamedWindow(wndname,1);
// cvShowImage(wndname,gray3);
//cvWaitKey(0);
//  pDC->ReleaseDC(pDC);
 cvReleaseImage(&gray3);

}
*/
void CCvmedianfilterDlg::Onsaltnoise()
{
 // TODO: Add your control notification handler code here
 char filename[]="D:\\Sample256-1.bmp";
 char *wndname;
 IplImage *gray = 0,*gray2=0,*gray3=0;
 gray=cvLoadImage(filename,1);
 
 gray2=cvCreateImage(cvSize(gray->width,gray->height),IPL_DEPTH_8U,1);
 gray3=cvCreateImage(cvSize(gray->width,gray->height),8,3);

 //cvCopy(gray,gray3);  //在彩色图像加噪声

// cvCvtColor(gray,gray3,CV_BGR2GRAY);  //在灰度图像加噪声,为什么这么写不行

 cvCvtColor(gray, gray2,CV_BGR2GRAY); //在灰度图像加噪声
 cvCvtColor(gray2,gray3,CV_GRAY2BGR);

 DWORD i,j;
 srand((unsigned)time(NULL));//生成伪随机种子
 for (j = 0;j < gray3->height;j++)
 {
  for(i = 0;i < gray3->width ;i++)
  {
   if(rand()>30000)
   {
    //图像中当前点置为黑
    ((uchar*)(gray3->imageData + gray3->widthStep*j))[i*3] = 0;
    ((uchar*)(gray3->imageData + gray3->widthStep*j))[i*3+1] = 0;
    ((uchar*)(gray3->imageData + gray3->widthStep*j))[i*3+2] = 0;
   }
  }
 }
 wndname="noisegray3";
 cvNamedWindow(wndname,1);
 cvShowImage(wndname,gray3);
 cvWaitKey(0);
 cvReleaseImage(&gray3);
 cvReleaseImage(&gray2);
 cvReleaseImage(&gray);

}

void CCvmedianfilterDlg::OnCanny() //边缘检测
{
 // TODO: Add your control notification handler code here

 
 char filename[]="D:\\Sample256-1.bmp";
 IplImage *ww=0,*ww1=0,*dst=0;
  
 ww=cvLoadImage(filename,1);
 ww1=cvCreateImage(cvSize(ww->width,ww->height),IPL_DEPTH_8U,1);
 dst=cvCreateImage(cvSize(ww->width,ww->height),IPL_DEPTH_8U,1);

 cvCvtColor(ww, ww1, CV_BGR2GRAY);  
 cvCanny(ww1,dst, 100, 200, 3 );  //cvCanny的源图像必须是灰度图像
 // cvCanny(ww1,dst, 100, 200, 5 );
 // cvCanny(ww1,dst, 100, 200, 7 );


 cvNamedWindow("canny算子(核=3)",1);
 cvShowImage("canny算子(核=3)",dst);

 cvReleaseImage(&ww);
 cvReleaseImage(&dst);
 cvReleaseImage(&ww); 
}

void CCvmedianfilterDlg::OnThreshold()
{
 // TODO: Add your control notification handler code here

     IplImage *log=0,*pyr=0,*log1=0,*log2=0;
 IplImage *laplace=0,*laplace1=0,*dst=0,  *thr=0;

  
  log=cvLoadImage(filename,1);
  pyr=cvCreateImage(cvSize(log->width/2,log->height/2),8,3);

  laplace=cvCreateImage(cvSize(log->width,log->height),8,3);

 
  cvPyrDown(log,pyr,CV_GAUSSIAN_5x5 );
  cvPyrUp(pyr,laplace,CV_GAUSSIAN_5x5 );
  

  laplace1=cvCreateImage(cvSize(log->width,log->height),IPL_DEPTH_8U,1);
  dst=cvCreateImage(cvSize(log->width,log->height),IPL_DEPTH_16S,1);
  cvCvtColor(laplace,laplace1, CV_BGR2GRAY);
  cvLaplace(laplace1,dst,3);

  thr=cvCreateImage(cvSize(log->width,log->height),IPL_DEPTH_8U,1);//wty


    cvThreshold(dst,thr,25,255,CV_THRESH_BINARY);//wty二值化阈值增强


    cvMean( thr, laplace1);

  cvNamedWindow("LOG-二值化",1);
  cvShowImage("LOG-二值化",thr);


  cvReleaseImage(&thr);//wty

  cvReleaseImage(&log1);
  cvReleaseImage(&log2);

  cvReleaseImage(&log);
  cvReleaseImage(&pyr);

  cvReleaseImage(&laplace1);
  cvReleaseImage(&dst);
  cvReleaseImage(&laplace);
}

void CCvmedianfilterDlg::OnLaplace()
{
 // TODO: Add your control notification handler code here

  IplImage *laplace=0,*laplace1=0,*dstlaplace=0;
  
  laplace=cvLoadImage(filename,1);
  laplace1=cvCreateImage(cvSize(laplace->width,laplace->height),IPL_DEPTH_8U,1);
  dstlaplace=cvCreateImage(cvSize(laplace->width,laplace->height),IPL_DEPTH_16S,1);

  cvCvtColor(laplace,laplace1, CV_BGR2GRAY);
  cvLaplace(laplace1,dstlaplace,3); 
  //cvLaplace(laplace1,dstlaplace,5); //或
 // cvLaplace(laplace1,dstlaplace,7); //或 
  cvNamedWindow("Laplace",1);
  cvShowImage("Laplace",dstlaplace);
 
  cvReleaseImage(&laplace1);
  cvReleaseImage(&dstlaplace);
  cvReleaseImage(&laplace); 
}

void CCvmedianfilterDlg::OnLog()
{
 // TODO: Add your control notification handler code here
 IplImage *log=0,*pyr=0,*log1=0,*log2=0;
 IplImage *laplace=0,*laplace1=0,*dst=0;
 
  log=cvLoadImage(filename,1);
  pyr=cvCreateImage(cvSize(log->width/2,log->height/2),8,3);

  laplace=cvCreateImage(cvSize(log->width,log->height),8,3);
 
  cvPyrDown(log,pyr,CV_GAUSSIAN_5x5 );
  cvPyrUp(pyr,laplace,CV_GAUSSIAN_5x5 );// 高斯
  
  laplace1=cvCreateImage(cvSize(log->width,log->height),IPL_DEPTH_8U,1);//laplace
  dst=cvCreateImage(cvSize(log->width,log->height),IPL_DEPTH_16S,1);
  cvCvtColor(laplace,laplace1, CV_BGR2GRAY);
  cvLaplace(laplace1,dst,3);
//cvLaplace(laplace1,dstlaplace,5);
//cvLaplace(laplace1,dstlaplace,7);

  cvNamedWindow("LOG算子(核=3)",1);
  cvShowImage("LOG算子(核=3)",dst);

  cvReleaseImage(&log1);
  cvReleaseImage(&log2);

  cvReleaseImage(&log);
  cvReleaseImage(&pyr);

  cvReleaseImage(&laplace1);
  cvReleaseImage(&dst);
  cvReleaseImage(&laplace);
}

void CCvmedianfilterDlg::OnTough()
{
 // TODO: Add your control notification handler code here
  char *wndname;


  wndname="原图";
  cvNamedWindow(wndname,1);

    IplImage *ww = 0,*ww1=0,*dst=0,*dst1=0;
 ww=cvLoadImage(filename,1);

  cvShowImage(wndname,ww);

 ww1=cvCreateImage(cvSize(ww->width,ww->height),IPL_DEPTH_8U,1);

 dst=cvCreateImage(cvSize(ww->width,ww->height),IPL_DEPTH_8U,1);
    dst1=cvCreateImage(cvSize(ww->width,ww->height),IPL_DEPTH_8U,3);
 cvCvtColor(ww, dst, CV_BGR2GRAY);

 CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* lines = 0;
        int i;
        cvCanny(dst,ww1, 50, 200, 3 );
        cvCvtColor(ww1,dst1, CV_GRAY2BGR );


 #if 1
        lines = cvHoughLines2(ww1, storage,CV_HOUGH_STANDARD, 1, CV_PI/180,150,3,0 );

        for( i = 0; i < lines->total; i++ )
        {
            float* line = (float*)cvGetSeqElem(lines,i);
            float rho = line[0];
            float theta = line[1];
            CvPoint pt1, pt2;
            double a = cos(theta), b = sin(theta);
            if( fabs(a) < 0.001 )
            {
                pt1.x = pt2.x = cvRound(rho);
                pt1.y = 0;
                pt2.y = dst3->height;
            }
            else if( fabs(b) < 0.001 )
            {
                pt1.y = pt2.y = cvRound(rho);
                pt1.x = 0;
                pt2.x = dst3->width;
            }
            else
            {
                pt1.x = 0;
                pt1.y = cvRound(rho/b);
                pt2.x = cvRound(rho/a);
                pt2.y = 0;
            }
            cvLine( dst1, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
        }
#else
        lines = cvHoughLines2(ww1, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 20, 40, 20);
        for( i = 0; i < lines->total; i++ )
        {
            CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
            cvLine( dst1, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
        }
#endif
        cvNamedWindow( "原图", 1 );
        cvShowImage( "原图", ww );

        cvNamedWindow( "Hough检测", 1 );
        cvShowImage( "Hough检测", dst1 );


   cvWaitKey(0);
 cvReleaseImage(&dst);
 

 cvReleaseImage(&ww); 
 cvReleaseImage(&ww1);

 cvReleaseImage(&dst1);
}

本文地址:http://com.8s8s.com/it/it22661.htm