이미지의 ASCII 코드 표시 -------(아래)

3984 단어
이 글은 이전 그림의 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");
}


좋은 웹페이지 즐겨찾기