OpenCV 预期的构造函数、析构函数或类型转换

Posted

技术标签:

【中文标题】OpenCV 预期的构造函数、析构函数或类型转换【英文标题】:OpenCV expected constructor, desctructor or type conversion 【发布时间】:2010-08-01 17:46:08 【问题描述】:

当我编译下面的代码时,我得到以下错误:

./main.cpp:150: error: expected constructor, destructor, or type conversion before ‘(’ token <br>
../main.cpp:154: error: expected constructor, destructor, or type conversion before ‘(’ token
../main.cpp:156: error: expected constructor, destructor, or type conversion before ‘(’ token
../main.cpp:158: error: expected constructor, destructor, or type conversion before ‘(’ token <br>

但代码看起来还不错。 他有问题

cvReleaseVideoWriter(&writer);
cvDestroyWindow("monoimage");
cvReleaseCapture(&capture);

程序:

#include "iostream"
#include "stdlib.h"
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
#include "cstring"/*the includes*/

/*You may change the values of the sthreshold and hlower and hupper to get different results....*/
const int sthreshold=210;
const double hlower=178;
const double hupper=3;

/*Note:The detection algorithm is the same as the one you can find in one of my previous posts

The same algorithm is copied from that post and pasted in this
post only the values of the "sthreshold" and the "hupper" and the "hlower" are different....*/

int main()


int i,j,k;//for iterations
int height,width,step,channels;/*HSV means the frame after color conversion*/
int heightmono,widthmono,stepmono,channelsmono;/*mono means the frame which has the monochrome image*/
const char string1[]="monoimg.avi";/*This is the name of the video which would be the outcome of the blob detection program..*/

uchar *data,*datamono;

i=j=k=0;

IplImage *frame = 0;

int key = 0;/*Initializing the capture from the video...*/

CvCapture* capture = cvCreateFileCapture( "partofvideo3.avi" );

double fps = cvGetCaptureProperty (/*getting the capture properties......the frame rate..*/
capture,CV_CAP_PROP_FPS);

CvSize size = cvSize(

(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),

(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)

);

CvVideoWriter* writer=cvCreateVideoWriter(string1, CV_FOURCC( 'D','I','V','X'),fps,size) ;

/* always check */

if (!capture) 

fprintf (stderr, "Cannot open video file!\n");

return(1);



height = frame->height;

width = frame->width;

step = frame->widthStep;

channels = frame->nChannels;

data = (uchar *)frame->imageData;

/* create a window for the video,one for the monochrome image and the other for the original frame */

cvNamedWindow("monoimage", CV_WINDOW_AUTOSIZE);

cvNamedWindow("original frame", CV_WINDOW_AUTOSIZE);

for (;;) /*keep looping till we are out of frames...*/

if (!cvGrabFrame(capture)) 

break;



/*colimgbot---means color image of the bot*/

frame = cvRetrieveFrame(capture);

IplImage *colimgbot = cvCreateImage( cvGetSize(frame), 8, 3 );

IplImage *monoimgbot = cvCreateImage( cvGetSize(frame), 8, 1 );

//------------------------------------------------------------------

cvCvtColor(frame,frame,CV_RGB2HSV);/*Converting the color space of the Video....*/

/*I have explained this loop in the previous program....*/

for(i=0;i< (height);i++)

for(j=0;j<(width);j++)

if((data[(height-i)*step+j*channels]<=hlower) && (data[(height-i)*step+j*channels]>=hupper))

if((data[(height-i)*step+j*(channels)+1])>sthreshold)
/*"height-i" because if we use only "i" we were getting vertically inverted result...hence reinverting the same
would do the required....*/

datamono[i*stepmono+j*channelsmono]=255;
else
datamono[i*stepmono+j*channelsmono]=0;
else datamono[i*stepmono+j*channelsmono]=0;


/*Please check these links for the explanation of the erosion and dilation functions

http://www.dca.fee.unicamp.br/dipcourse/html-dip/c9/s4/front-page.html*/

/*so now the last parameter in the function indicates how many times you want to apply dilation
or erosion*/

cvErode(monoimgbot,monoimgbot,0,14);
cvDilate( monoimgbot,monoimgbot,0,15);

/*here i have experimented with the values by changing them...and i have found

that i come to a good result by applying erosion 14 times and dilation 15 times
you can comment/uncomment play with the values and see what is going on
Sometimes you will find the areas which are shining in the image also get detected...

Please think why and then try to post a comment the best commment would get visible on this page*/

cvWriteFrame(writer, monoimgbot);/*Write the monochrome video to the writer..

to save the results of the video extract*/

cvShowImage("original frame", frame);

cvShowImage("monoimage", monoimgbot);

if( (cvWaitKey(10) & 255) == 27 ) break;





/* free memory */

cvReleaseVideoWriter(&writer);

//cvDestroyWindow("original frame");

cvDestroyWindow("monoimage");

cvReleaseCapture(&capture);

exit(0);

【问题讨论】:

【参考方案1】:

如果你缩进你的代码,它可能会帮助你发现这样的问题。将main(或任何其他单个函数)保持在合理长度的一半可能也会有所帮助。从外观上看,这段代码:

cvReleaseVideoWriter(&writer);

//cvDestroyWindow("original frame");

cvDestroyWindow("monoimage");

cvReleaseCapture(&capture);

exit(0);

...(在您发布的内容的末尾)完全不在任何功能范围内。像这样的代码必须在函数内部。

【讨论】:

【参考方案2】:

最后应该有一个额外的

if( (cvWaitKey(10) & 255) == 27 ) break;



  <--- This should be in the end of the program.

/* free memory */

这样编译:

        if ((cvWaitKey(10) & 255) == 27) 
            break;

       

    /* free memory */
    cvReleaseVideoWriter(&writer);
    cvDestroyWindow("original frame");
    cvDestroyWindow("monoimage");
    cvReleaseCapture(&capture);

    exit(0);

【讨论】:

以上是关于OpenCV 预期的构造函数、析构函数或类型转换的主要内容,如果未能解决你的问题,请参考以下文章

c++ 错误:预期构造函数、析构函数或在“(”标记之前的类型转换

错误:“;”之前的预期构造函数、析构函数或类型转换令牌

错误:“(”标记之前的预期构造函数、析构函数或类型转换?

错误:“。”之前的预期构造函数、析构函数或类型转换令牌 - 了解 fstream

错误:预期的构造函数、析构函数、“<”标记之前的类型转换

构造函数constructor 与析构函数destructor