OPENCV 回调函数的使用

belarl 2009-06-16 04:00:14
小弟在研究opencv用于图像识别的时候,在一个例子里面看到有关于回调函数使用的问题!
查了很多网上的资料,所采用的方法基本都是设置static静态函数的进行函数地址传递,但是这里有个问题就是我这个回调函数中另外还需要调用本身类的成员函数及较多的成员变量,这些成员变量也不太好照网上的办法全部设置为static变量,所以导致成员总是无法通过。还请那位朋友帮忙看看解决一下啊!!多谢了!
源程序如下:
#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#endif

int thresh = 50;
IplImage* img = 0;
IplImage* img0 = 0;
CvMemStorage* storage = 0;
CvPoint pt[4];
const char* wndname = "Square Detection Demo";

double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )
{
return angle;
}
CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
{
return squares;
}
void drawSquares( IplImage* img, CvSeq* squares )
{
}
void on_trackbar( int a )
{
if( img )
drawSquares( img, findSquares4( img, storage ) );
}
char* names[] = { "0.JPG","pic2.png", "pic3.png",
"pic4.png", "pic5.png", "pic6.png", 0 };

int main(int argc, char** argv)
{
//cvCreateTrackbar函数为库函数,回调on_trackbar函数
cvCreateTrackbar( "canny thresh", wndname, &thresh, 1000, on_trackbar );
return 0;
}

#ifdef _EiC
main(1,"squares.c");
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////
///////以上为能够通过的标准C程序//////////以下为我修改后的程序,能通过编译,但是运行会程序崩溃////
class CImageIdenDoc : public CDocument
{
public:
void drawSquares( IplImage* img, CvSeq* squares );
CvSeq* findSquares( IplImage* img, CvMemStorage* storage );
double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 );
}
class CImageIdenView : public CFormView
{
static void on_trackbar(int a);
}
void CImageIdenView::OnButton2()
{
CImageIdenDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

int i=0, c=0;
// create memory storage that will contain all the dynamic data
pDoc->storage = cvCreateMemStorage(0);

// for( i = 0; i<pDoc->JPGNum; i++ )
for( i = 0; picnames[i]!="\0"; i++ )
{
// load i-th image
pDoc->m_cvImage = cvLoadImage( picnames[i], 1 );
if( c!= 1000 )
{
if( pDoc->m_cvImage == NULL)
{

CString s="Couldn't load "+picnames[i]+"!";
MessageBox(s);
}
else
{
IplImage* imgforect;
imgforect= cvCloneImage( pDoc->m_cvImage );
// create window and a trackbar (slider) with parent "image" and set callback
// (the slider regulates upper threshold, passed to Canny edge detector)
cvNamedWindow( pDoc->wndnamerect, CV_WINDOW_AUTOSIZE );
//自定义阈值
cvCreateTrackbar(pDoc->trackname, pDoc->wndnamerect, &pDoc->thresh, 256, on_trackbar);
//on_trackbar函数的形式必须为void Foo(int)
// force the image processing
on_trackbar(0);
// wait for key.

// Also the function cvWaitKey takes care of event processing
c = cvWaitKey(0);
release both images
cvReleaseImage( &imgforect );
// clear memory storage - reset free space position
cvClearMemStorage( pDoc->storage );
cvDestroyWindow(pDoc->wndnamerect);
}
}
}
void CImageIdenView::on_trackbar(int a)
{
CImageIdenDoc* pDoc;// = ::GetDocument();
ASSERT_VALID(pDoc);
AfxMessageBox("到这里了");//可以运行到这里
if(pDoc->m_cvImage != NULL)//这句话不能运行
{
AfxMessageBox("到这里了");//不能运行到这里
pDoc->drawSquares(pDoc->m_cvImage, pDoc->findSquares(pDoc->m_cvImage, pDoc->storage ) );
}
}

void CImageIdenDoc::drawSquares(IplImage *img, CvSeq *squares)
{
}
double CImageIdenDoc::angle(CvPoint *pt1, CvPoint *pt2, CvPoint *pt0)//四边形的角度计算
{
return angle;
}
CvSeq* CImageIdenDoc::findSquares(IplImage *img, CvMemStorage *storage)
{
}
...全文
391 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
belarl 2009-07-16
  • 打赏
  • 举报
回复
看了好一会你的回复,终于看明白为什么了!!!
FlySkyFree 2009-06-18
  • 打赏
  • 举报
回复
我来学习学习
hzcenter 2009-06-16
  • 打赏
  • 举报
回复
小心驴霸告你侵权哦
Conry 2009-06-16
  • 打赏
  • 举报
回复
void CImageIdenView::on_trackbar(int a)
{
CImageIdenDoc* pDoc;// = ::GetDocument();
ASSERT_VALID(pDoc);
AfxMessageBox("到这里了");//可以运行到这里
if(pDoc->m_cvImage != NULL)//这句话不能运行
{
AfxMessageBox("到这里了");//不能运行到这里
pDoc->drawSquares(pDoc->m_cvImage, pDoc->findSquares(pDoc->m_cvImage, pDoc->storage ) );
}
}

你的pDoc是个非法的指针,当然不能运行了

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧