void draw_text(char *text, Uint16 x, Uint16 y) { SDL_Rect rect, rect2; Uint8 pos; pos = 0; while(text[pos] != 0) { cut_image(font, screen, text[pos], 16, x+pos*LETTER_WIDTH, y, LETTER_WIDTH, LETTER_HEIGHT); pos++; } }
void draw_text_br(char *text, Uint16 x, Uint16 y) { SDL_Rect rect, rect2; Uint8 pos, i, line; pos = 0; i = 0; line = 0; while(text[pos] != 0) { if (x+i*LETTER_WIDTH+10 > RES_X) { i = 0; line++; } cut_image(font, screen, text[pos], 16, x+i*LETTER_WIDTH, y+line*LETTER_HEIGHT, LETTER_WIDTH, LETTER_HEIGHT); i++; pos++; } }
// get image from buffer int CameraMJPG::get_image(char* buffer, int length) { if(!buffer || length<=0) return 1; if(buf_video.data){ if(buf_video.real_size - buf_video.size > length){ memcpy(buf_video.data+buf_video.size, buffer, length ); buf_video.size += length; } else{ printf("[!][CameraMJPG][get_image] Error: need more buffer!\n"); buf_video.zero(); return 2; } return cut_image(buf_video.data, buf_video.size); } return 0; }
int main_plate(const char * car_name) { /*********************************************准备工作*****************************************/ IplImage * img_car = NULL; IplImage * img_car_after_resize = NULL; IplImage * img_after_preprocess = NULL; IplImage * img_plate = NULL; IplImage * img_after_resize = NULL; IplImage * img_character = NULL; List rects; /*保存预选车牌位置矩形的列表*/ double scale = -1; /*在尺寸归一化时要用到*/ int width = 0, height = 0; /*最开始时候的尺寸归一化的长宽*/ int number = -1; /*最后一个字符的数字结果*/ int count_recog = 0; char filename[50]; #if 1 //cvNamedWindow("img_car", 1); // cvNamedWindow("img_car_after_resize", 1); //cvNamedWindow("img_after_preprocess", 1); //cvNamedWindow("img_plate", 1); #endif if ((img_car = cvLoadImage(car_name, -1)) == NULL) { fprintf(stderr, "Can not open car image file in main.c!\n"); exit(-1); } /*****************************************开始进行图像处理***************************************/ /*由于得到的车辆图像中车占的比例太小,所以需要考虑重新截取图像,保证得到的图像中车辆整体占整个图像的比例较大 要实现这个目的我们观察发现拍到的照片中车基本都是处于整个图像的中心,所以我们截取整个图像的中心作为新的图片 策略: 1.先将图片按宽度分成三份,取中间的一份,车牌肯定在这一份中 2.将图片上四分之一截取掉,下四分之一截取点,车牌肯定在剩下的二分之一份图片中 */ /*********现在开始进行截取车身操作****************/ #if 0 IplImage * tmp_img = cvCreateImage(cvSize(1.0 / 3 * img_car->width, 1.0 / 2 * img_car->height), img_car->depth, img_car->nChannels); cvSetImageROI(img_car, cvRect(1.0 / 3 * img_car->width, 1.0 / 4 * img_car->height, 1.0 / 3 * img_car->width, 1.0 / 2 * img_car->height)); cvCopy(img_car, tmp_img); cvSaveImage("tmp_img.bmp", tmp_img); cvResetImageROI(img_car); img_car = cvLoadImage("tmp_img.bmp", -1); /*img_car现在是新的截取后的图片了*/ assert(img_car != NULL); cvNamedWindow("haha", 1); cvShowImage("haha", tmp_img); cvWaitKey(0); #endif cut_image(img_car); img_car = cvLoadImage("image/tmp_img.bmp", -1); /*img_car现在是新的截取后的图片了*/ /********************************************************************************************************/ /*为了便于对图像进行统一处理,先对图像尺寸进行处理,让图像的尺寸大小合适, 一般大概大小为640*480规格的,所以只需要大概按照这个比例进行resize */ /*用cvResize函数进行处理即可*/ #if 1 scale = 1.0 * 640 / img_car->width; /*将长度规整为640即可,宽就按比例伸长就行了*/ width = scale * img_car->width; height = scale * img_car->height; img_car_after_resize = cvCreateImage(cvSize(width, height), img_car->depth, img_car->nChannels); cvResize(img_car, img_car_after_resize); /*对尺寸进行归一化,得到宽为640的图像*/ cvSaveImage("image/img_car_after_resize.bmp", img_car_after_resize); #endif /*图像预处理:输入为尺寸归一化后的车牌图像,输出为一张img_after_preprocess.bmp图像*/ preprocess_car_img(img_car_after_resize); /*读取img_after_preprocess.bmp图像*/ if ((img_after_preprocess = cvLoadImage("image/img_after_preprocess.bmp", -1)) == NULL) { fprintf(stderr, "Can not open file img_after_preprocess.bmp in main.c"); exit(-1); } #if 1 /*显示预处理完成后的图像*/ //cvShowImage("img_car", img_after_preprocess); //cvShowImage("img_after_preprocess", img_after_preprocess); #endif /***************************************预处理完成,开始找车牌位置*****************************************************************/ rects = get_location(img_after_preprocess, img_car_after_resize); /*得到车牌的位置,起初设计阶段是可以有多个预选位置,但是后来发现不用,所以rects其实只有一个位置,但是也是用一个链表装着的*/ /*由于在get_location中返回的是头结点的next节点,所以这里的参数不用rects->next*/ assert(count_node(rects) == 1); /*断言这个链表里只有一个待选车牌位置*/ /****************************************找到车牌位置,开始截取车牌******************************************************************/ get_plate_image(img_car_after_resize, rects); /*得到车牌的图像*/ img_plate = cvLoadImage("image/plate_img0.bmp", -1); /*上面那个函数中得到的plate_img.bmp图像*/ if (img_plate == NULL) { fprintf(stderr, "Can not open plate image file!\n"); exit(-1); } /*******************************************对车牌进行尺寸变化***************************************************************/ scale = plate_resize_scale(img_plate); resize_image(img_plate,img_after_resize, scale); /*最后一个参数为5表示将原车牌图像变长为原来的五倍*/ if ((img_after_resize = cvLoadImage("image/plate_img_after_resize.bmp", -1)) == NULL) { fprintf(stderr, "Can not open file plate_img_after_resize.bmp in main.c"); exit(-1); } /*******************************************对车牌进行预处理***************************************************************/ preprocess_plate_image(img_after_resize); /*对车牌图像进行预处理*/ /********************************************获得车牌上的字符信息**************************************************************/ get_character(img_after_resize); /*得到每一个字符的图像*/ //cvShowImage("image_car", img_after_resize); //printf("the plate is: \n"); count_recog = 0; FILE *fp = fopen("result.txt", "wb"); char buf[1] = {0}; int ct = 0; while(ct++ < 10000) { fwrite(buf, 1, sizeof(char), fp); } fclose(fp); while (count_recog < 7) { sprintf(filename, "image/character%d.png", count_recog); img_character = cvLoadImage(filename, -1); if (img_character == NULL) { break; } /*********************************************开始进行字符识别***********************************************************/ number = character_recognizing(img_character); count_recog++; } cvWaitKey(0); printf("Time used = %.2f\n", (double)clock() / CLOCKS_PER_SEC); return 0; }
void draw_tile(int x, int y, int tiletype, int tile, SDL_Surface *target) { SDL_Surface *image; SDL_Rect rect, rect2; if (tiletype != 3) { cut_image(tileset, target, tile, 8, x*BLOCK_WIDTH, y*BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT); } else { cut_image(tileset, target, 0, 8, x*BLOCK_WIDTH, y*BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT); image = events; rect.x = 7*BLOCK_WIDTH; rect.y = 7*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; switch (tile) { case 0: image = monster[0]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 1: image = events; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 2: image = events; rect.x = 10; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 3: image = events; rect.x = 30; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 4: image = events; rect.x = 0; rect.y = 10; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 5: image = events; rect.x = 10; rect.y = 10; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 6: image = events; rect.x = 20; rect.y = 10; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 7: image = monster[1]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 8: image = events; rect.x = 50; rect.y = 10; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 9: image = events; rect.x = 60; rect.y = 10; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 10: image = events; rect.x = 70; rect.y = 10; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 11: image = events; rect.x = 30; rect.y = 10; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 12: image = events; rect.x = 0; rect.y = 20; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 13: image = events; rect.x = 10; rect.y = 20; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 14: image = events; rect.x = 20; rect.y = 20; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 15: image = events; rect.x = 30; rect.y = 20; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 16: image = monster[2]; rect.x = 0; rect.y = 0; rect.w = 2*BLOCK_WIDTH; rect.h = 2*BLOCK_HEIGHT; break; case 17: image = events; rect.x = 4*BLOCK_WIDTH; rect.y = 2*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 18: image = monster[3]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 19: image = monster[4]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 20: image = tileset; rect.x = 4*BLOCK_WIDTH; rect.y = 2*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 21: image = events; rect.x = 6*BLOCK_WIDTH; rect.y = 2*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 22: image = events; rect.x = 7*BLOCK_WIDTH; rect.y = 2*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 23: image = monster[5]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 24: image = events; rect.x = BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 25: image = man; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 26: image = oke_img; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 27: image = monster[6]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 28: image = events; rect.x = 2*BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 29: image = monster[7]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 30: image = events; rect.x = 3*BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 31: image = events; rect.x = 4*BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 32: image = monster[8]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 33: image = monster[9]; rect.x = 0; rect.y = 0; rect.w = 4*BLOCK_WIDTH; rect.h = 4*BLOCK_HEIGHT; break; case 34: image = torch; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 35: image = monster[10]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 36: image = monster[10]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 37: image = events; rect.x = 5*BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 38: image = events; rect.x = 6*BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 39: image = rock; rect.x = 0; rect.y = 0; rect.w = 3*BLOCK_WIDTH; rect.h = 3*BLOCK_HEIGHT; break; case 40: image = events; rect.x = 7*BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 41: image = events; rect.x = 0; rect.y = 4*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = 2*BLOCK_HEIGHT; break; case 42: image = events; rect.x = BLOCK_WIDTH; rect.y = 4*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 43: image = events; rect.x = 2*BLOCK_WIDTH; rect.y = 4*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 44: case 45: case 46: case 47: image = monster[12]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 48: image = monster[5]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 49: image = glass; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH*2; rect.h = BLOCK_HEIGHT*4; break; case 50: image = computer; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 51: image = monster[13]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 52: image = monster[13]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 53: image = monster[14]; rect.x = 0; rect.y = 0; rect.w = 2*BLOCK_WIDTH; rect.h = 2*BLOCK_HEIGHT; break; case 54: image = monster[14]; rect.x = 0; rect.y = 0; rect.w = 2*BLOCK_WIDTH; rect.h = 2*BLOCK_HEIGHT; break; case 55: image = events; rect.x = 7*BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 56: image = events; rect.x = 7*BLOCK_WIDTH; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 57: image = events; rect.x = 3*BLOCK_WIDTH; rect.y = 4*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 58: image = events; rect.x = 4*BLOCK_WIDTH; rect.y = 4*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 59: image = events; rect.x = 5*BLOCK_WIDTH; rect.y = 4*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 60: image = monster[11]; rect.x = 0; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT+5; break; case 61: image = events; rect.x = 0; rect.y = 3*BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 62: image = events; rect.x = 4*BLOCK_WIDTH; rect.y = BLOCK_HEIGHT; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; case 63: image = events; rect.x = 20; rect.y = 0; rect.w = BLOCK_WIDTH; rect.h = BLOCK_HEIGHT; break; } rect2.x = x*BLOCK_WIDTH; rect2.y = y*BLOCK_HEIGHT; rect2.w = BLOCK_WIDTH; rect2.h = BLOCK_HEIGHT; SDL_BlitSurface(image, &rect, target, &rect2); } }