000
02.06.2008, 12:37 Uhr
KOR
|
Hi ich habe ein riesengroßes Problem bis Freitag hat mein Program wunderbar Funktioniert(es macht bilder und binarisiert sie. nur seit heute stürzt es immer ab. Bei Bild 173. Der Quelltext(ein bisschen länger):
C++: |
#include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h> #include <iostream> #include <string> #include <time.h> #include <boost/numeric/ublas/matrix.hpp> using namespace std;
string Filename(int i); // creates a string in the form pic%i.bmp where %i is the integer given bool testthreshold(uchar* data, int step, int r, int c, const unsigned short threshold, unsigned short range, int height, int width);
int _tmain(int argc, _TCHAR* argv[]) { long start_time_init = clock(); const unsigned short threshold=100; const unsigned short range=10; const short delay_time=35; // (50=20fps 100=10fps) const int n_pics=500; // number of pictures taken cvNamedWindow( "Display", 1 ); // creation of a visualisation window cout<<"Initializing Camera please wait"; Sleep(250); cout<<"."; CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0 cout<<"."; //cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,640); // doesn't work //cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,480); // doesn't work cvQueryFrame(capture); // this call is necessary to get correct // capture properties int height = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); int width = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); if(!cvGrabFrame(capture)){ // capture a frame printf("Could not grab a frame\n\7"); // if it fails print errormsg exit(0); // and exit } boost::numeric::ublas::matrix<unsigned int> greyscale(height,width); // create matrix boost::numeric::ublas::matrix<bool> binary(height,width); // create matrix cout<<".\n"; cout<<height<<"x"<<width<<endl; long stop_time_init = clock();
cout<<"Camera is initialized. It took "<<stop_time_init-start_time_init<<" ms. Program is starting."<<endl; long t_start = clock(); for (int i=0;i<=n_pics;i++) { long start_time = clock(),now_time = clock(); IplImage* img = 0; // creating an empty image var if(!cvGrabFrame(capture)){ // capture a frame printf("Could not grab a frame\n\7"); // if it fails print errormsg exit(0); // and exit } img=cvRetrieveFrame(capture); // retrieve the captured frame string outFileName=Filename(i); // call the Filename Function cout<<outFileName<<endl; // print Filename //if(!cvSaveImage(outFileName.c_str(),img)) // save image //{ // printf("Could not save: %s\n",outFileName.c_str()); // if it fails print errormsg //}
// the dimension of the incoming image CvSize imgSize = cvGetSize( img ); //cout<<endl<<" the input image "<< endl; // for the used examples //cout<<" - height x width : "; //cout<<img->height<<" X "<<img->width<< endl; // 480 x 640 //cout<<" - depth : "<<img->depth <<endl; // 8 //cout<<" - step : "<<img->widthStep <<endl; // 1920 //cout<<" - color model : "<<img->colorModel<<endl; // RGB //cout<<" - number of channels: "<<img->nChannels <<endl; // 3 //cout<<" - channelseqquence : "<<img->channelSeq<<endl; // BGR // convert the input image to an one channel image IplImage *gray_img; // creating empty grey image pointer gray_img = cvCreateImage(imgSize, IPL_DEPTH_8U, 1); // allocate Memory cvCvtColor(img, gray_img, CV_BGR2GRAY); // converting function //cout<<cvGetReal2D(img,0,0)<<endl; //int height = gray_img->height; // height of image //int width = gray_img->width; // width of image int step = gray_img->widthStep; // no idea int channels = gray_img->nChannels; // channels RGB(3) grey(1) etc uchar *data = (uchar *)gray_img->imageData; // pixeldata of image int area=0; int maxarea=height*width;
for(int r=0;r<height;r++) // fill it { for(int c=0;c<width;c++) { greyscale(r,c)=int(data[r*step+c]); // write greyvalues of each pixel in array if(testthreshold(data,step,r,c,threshold,range,height,width)) /*##############hier ist der aufruf der funktion die den Fehler verursacht!#########*/ { binary(r,c)=1; area++; } else { binary(r,c)=0; } //cout<<int(data[r*step+c])<<" "; // never do such things except with one or two images } //cout<<r<<endl; } cout<<"The Area is "<<area<<" of "<<maxarea<<" pixels"<<endl; now_time=clock(); cvShowImage("Display",img); // display image in the Window named "Display" if((now_time-start_time)<delay_time) { cvWaitKey(delay_time-(now_time-start_time)); // wait delay_time ms } else { cout<<"It took too much time taking pictures as fast as possible"<<endl; cvWaitKey(1); }
} cvReleaseCapture(&capture); // Releasing the capture source long t_stop=clock(); cout<<"it took "<<t_stop-t_start<<" ms which is an average of "<<(t_stop-t_start)/n_pics<<" ms per picture."<<endl; string end; cin>>end; return 0; }
bool testthreshold(uchar* data, int step, int r, int c, const unsigned short threshold, unsigned short range, int height, int width) { if(int(data[r*step+c])>threshold+range) { return true; } else if(int(data[r*step+c])>=threshold && r>0 && r<height && c>0 && c<width) { if(int(data[(r-1)*step+c])>threshold) { return true; }else if(int(data[(r-1)*step+(c-1)])>threshold+range) { return true; }else if(int(data[(r-1)*step+(c+1)])>threshold+range) { return true; }else if(int(data[(r+1)*step+c])>threshold+range) { return true; }else if(int(data[(r+1)*step+(c-1)])>threshold+range) { return true; }else if(int(data[(r+1)*step+(c+1)])>threshold+range) { return true; }else if(int(data[r*step+(c+1)])>threshold+range) { return true; }else if(int(data[r*step+(c-1)])>threshold+range) { return true; }else { return false; } } else if(int(data[r*step+c])>=threshold-range && r>0 && r<height && c>0 && c<width) { if(int(data[(r-1)*step+c])>threshold+range*2) { return true; }else if(int(data[(r-1)*step+(c-1)])>threshold+range*2) { return true; }else if(int(data[(r-1)*step+(c+1)])>threshold+range*2) { return true; }else if(int(data[(r+1)*step+c])>threshold+range*2) /*##################### an der stelle bleibt er stehen ################*/ { return true; }else if(int(data[(r+1)*step+(c-1)])>threshold+range*2) { return true; }else if(int(data[(r+1)*step+(c+1)])>threshold+range*2) { return true; }else if(int(data[r*step+(c+1)])>threshold+range*2) { return true; }else if(int(data[r*step+(c-1)])>threshold+range*2) { return true; }else { return false; } } else { return false; } }
string Filename(int i) { char buffer [100]; sprintf(buffer,"pic%i",i); string outFN=buffer; return outFN; }
|
Am Freitag lief es naoch dann habe ich meinen rechner abgebaut und heute läuft es nicht mehr die auflösung der Camera hat sich von 640x480 auf 320x240 verstellt bekomme ich auch nicht wieder rückgängig. Und das Programm stürzt immer bei bild 173 ab. der Compiler sagt was von access violation in der funktion testthreshhold. MSVS2003 markiert mir data r und c rot. und r hat einen wert den es nicht annehmen dürfte: 34619086 aus der schleife kann r eigentlich nur so groß wie die höhe des bildes werden (239)
So langsam verzweifel ich echt an diesem problem. THX4UrF1 Dieser Post wurde am 02.06.2008 um 12:39 Uhr von KOR editiert. |