这篇文章是接着上一篇图像的ASCII码显示,自己改了一下那个代码,可以运行,但是不知道怎么回事,得不到正确的结果,求有兴趣的大侠帮助
// ASCII-art.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "cv.h" #include "highgui.h" #include "cxcore.h" char ascii_code_symbol[10] = {'#','&','$','*','+',';','.',' ',0}; char ascii_code_letter[10] = {'m','n','e','f','t','l','i',' ',0}; char ascii_code_number[10] = {'8','9','5','3','2','7','1',' ',0}; char *ascii_code_8[3] = { ascii_code_symbol, ascii_code_letter, ascii_code_number}; CvSize board_size; //check the input image size and return a defined size, // that's the max one of width and height is not bigger than 100; void get_board_size(IplImage *image) { int f = 0; float big=(float)image->height , smal=(float)image->width ; if(image->width>image->height ) { f = 1; big = float(image->width); smal = float(image->height); } if(big <= 100.f) { board_size = cvSize(image->width,image->height); } else { board_size = cvSize(int(f==1?100:(100*image->width/big)), int(f==1?(100*image->height/big):100)); } } void main(int argc,char **argv) { IplImage* image=cvLoadImage("c:\\meinv.jpg",0); int code_type=2; int color_type=3; //if input is a gray image, set color_type to gray; if(image->nChannels==1&&code_type==2) color_type = 1; //create the output image int char_size = 4; get_board_size(image); IplImage *out_image=cvCreateImage(cvGetSize(image),8,1); cvSet(out_image,cvScalarAll(255)); //resize the input image to defined size; IplImage* resized_image=cvCreateImage(cvGetSize(image),image->depth ,image->nChannels ); cvCopy(image, resized_image); IplImage* gray_image=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1 ); if(resized_image->nChannels== 3) cvCvtColor(resized_image, gray_image, CV_BGR2GRAY); else gray_image = resized_image; //font init CvFont fontFace; cvInitFont(&fontFace,CV_FONT_HERSHEY_PLAIN,0.5,0.5,0,1,8); double fontScale = 0.5; int thickness = 1; //print char code to the output image for (int i=0; i<resized_image->height ; ++i) { uchar *ptr_bgr = (uchar *)(resized_image->imageData+i); uchar *ptr_gray = (uchar *)(gray_image->imageData+i); for (int j=0; j<resized_image->width; ++j) { //get pixel uchar pix_gray = ptr_gray[j]; //prepare the char code and coordinate; CvPoint textOrg=cvPoint(j*char_size, (i+1)*char_size); char text=ascii_code_8[code_type][8-(pix_gray>>5)]; //colored or not if (color_type==0) { cvPutText(out_image, &text, textOrg, &fontFace, cvScalar(0,255,0)); } else if(color_type==1) { cvPutText(out_image, &text, textOrg, &fontFace,cvScalar(0,255,0)); } else { cvPutText(out_image, &text, textOrg, &fontFace, cvScalar(0,255,0)); } } } cvNamedWindow("img",1); cvShowImage("img",out_image); cvWaitKey(0); cvReleaseImage(&out_image); cvDestroyWindow("img"); }