测试硬件pcduino(arm),操作系统ubuntu 3.4.29+ armv7l GNU/Linux。测试文件,图片jpeg,大约1.3M。测试内容统计图片的RGB里的值,存在数据库里将来看看能不能做搜索用。
先是用python,开发速度非常快,但是效率有点问题,所以有用C做了一下。以下是代码和两次的测试结果。C使用的jpeglib库8.0版本。
python代码
#!/bin/python #-*- coding:utf-8 -*- # Filename: # Revision: 1.0 # Date: 2013-12-04 # Author: simonzhang # web: www.simonzhang.net # Email: simon-zzm@163.com ### END INIT INFO from PIL import Image # 对RGB简历字典 R = {} G = {} B = {} # 初始化值 for i in xrange(256): R[i] = 0 G[i] = 0 B[i] = 0 # 大图片需要处理很长时间,所以要用python可以按比例将图缩小 a = Image.open('123.jpg') #a = a.resize((int(a.size[0] * 0.1), int(a.size[1] * 0.1)), Image.ANTIALIAS) x = a.size[0] y = a.size[1] # 循环处理每个像素点的RGB值 for lx in xrange(x): for ly in xrange(y): rgb = a.getpixel((lx, ly)) R[rgb[0]] += 1 G[rgb[1]] += 1 B[rgb[2]] += 1 # 打印最终结果 print sorted(R.items(), key=lambda R:R[1],reverse=True)[0] print sorted(G.items(), key=lambda G:G[1],reverse=True)[0] print sorted(B.items(), key=lambda B:B[1],reverse=True)[0]
C代码
/* *Filename: * Revision: 1.0 * Date: 2013-12-04 * Author: simonzhang * web: www.simonzhang.net * Email: simon-zzm@163.com */ #include#include #include /* we will be using this uninitialized pointer later to store raw, uncompressd image */ //unsigned char *raw_image = NULL; /* */ int max_num; /* find max values*/ int sort_values(int arr[255]) { int largest1, largest2, temp; max_num = 0; largest1 = arr[0]; largest2 = arr[1]; if (largest1 < largest2) { temp = largest1; largest1 = largest2; largest2 = temp; } int i; for (i = 2; i < 255; i++) { if (arr[i] >= largest1) { largest2 = largest1; largest1 = arr[i]; max_num = i; } else if (arr[i] > largest2) { largest2 = arr[i]; } } return largest1; } read_jpeg_file(char *filename) { /* these are standard libjpeg structures for reading(decompression) */ struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; /* libjpeg data structure for storing one row, that is, scanline of an image */ JSAMPROW row_pointer[1]; FILE *infile = fopen( filename, "rb" ); unsigned long location = 0; int i = 0; if ( !infile ) { printf("Error opening jpeg file %s\n!", filename ); return -1; } /* here we set up the standard libjpeg error handler */ cinfo.err = jpeg_std_error( &jerr ); /* setup decompression process and source, then read JPEG header */ jpeg_create_decompress( &cinfo ); /* this makes the library read from infile */ jpeg_stdio_src( &cinfo, infile ); /* reading the image header which contains image information */ jpeg_read_header( &cinfo, TRUE ); /* Uncomment the following to output image information, if needed. */ /* Start decompression jpeg here */ jpeg_start_decompress( &cinfo ); /* allocate memory to hold the uncompressed image */ //raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components ); /* now actually read the jpeg into the raw buffer */ row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components ); /* read one scan line at a time */ int r[255], g[255], b[255]; for(i=0;i<255;i++) { r[i] = 0; g[i] = 0; b[i] = 0; } while( cinfo.output_scanline < cinfo.image_height ) { jpeg_read_scanlines( &cinfo, row_pointer, 1 ); for( i=0; i 先是测试python部分
#time python countrgb.py(12, 510858)
(17, 429677)
(9, 662996)real 11m4.009s
user 10m42.200s
sys 0m1.090s开始测试C的部分
首先优化编译一下
#gcc countrgb.c -l jpeg -O3 -o countrgb
#time ./countrgb 123.jpg
12,510858
17,429677
9,662996real 0m0.750s
user 0m0.730s
sys 0m0.010s两次统计结果相同,说明统计方法没有问题。python用了11分钟,C用了0.75秒,看来真的不是一般的快了。C代码编译后的文件8K,静态编译500多K。不过我是喜欢用python,开发速度快,结构清晰。