I need to save the area of connected components as a text file and i have written a code as below, but i dont know where i am going wrong. Can anyone help me to find out the problem?
imagefet=cvCreateImage(cvGetSize(lab),IPL_DEPTH_8U,3);
CvMemStorage* contour_storage = cvCreateMemStorage(0);
CvSeq* contours;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.6f, 0.6f, 0, 2);
cvFindContours(lab, contour_storage, &contours, sizeof (CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
cvZero(imagefet);
FILE *file=fopen("mytxt.txt","W");
for( int ncount=1; contours != NULL; contours = contours->h_next, ncount++)
{
CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
cvDrawContours( imagefet, contours, color, CV_RGB(255,255,255), -1, CV_FILLED, 8 ,cvPoint(0,0));
int area = abs(cvContourArea(contours, CV_WHOLE_SEQ));
fprintf(file,"%d",area);
}
fclose(file);
thnx
You haven’t been very specific about the error.
If your file is not opening, I’d suggest you use a lower-case
was the mode flag. I’ve never seen an upper-case one used, and I don’t know if that’s allowed.If your file is full of numbers with no spaces, it’s because you do not write out any space. Try adding a newline
\nto thefprintfcall:In that respect, you should really use text mode to create the file
"wt"instead of"w".The other thing you should do is test whether the file opened successfully by checking whether
fileis NULL after thefopencall.