prerequisite:经过二值化,每个像素点只有0(黑色)和255(白色)两个值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| float rowTotal = 0; float rowMax = 0; float maxRowIndex = 0; float rowCount[height];
for(y = 0 ;y <height;y++){ rowCount[y] = 0; for( x = 0;x<width;x++){ rowCount[y] = rowCount[y] + (1.0-(buffer[y*width+x]/255.0f)); } rowTotal = rowTotal + rowCount[y]; if(rowCount[y]>rowMax){ rowMax = rowCount[y]; maxRowIndex = y; } } float rowAverage = rowTotal/height; int y_start = maxRowIndex; int y_end = maxRowIndex; for(;y_start>0;){ if(rowCount[y_start]> rowAverage/3){ y_start--; } else { break; } } for(;y_end<height;){ if(rowCount[y_end] > rowAverage/3){ y_end++; } else { break; } }
buffer_temp = (unsigned char*)malloc(height*height); memset(buffer_temp, 255, height*height); int i;
for (y = 0; y < height; y++) { int index = (1.0 - rowCount[y] / rowMax) * height; if (y == height - 1) { index = (1.0 - rowAverage / rowMax) * height; } if (y == y_start || y == y_end) { index = 0; } for (i = 0; i < height; i++) { if (i > index) { buffer_temp[y * height + i] = 0; } } }
image.buffer = buffer_temp; image.width = height; image.height = height; return image;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| float colTotal = 0; float colMax = 0; float colColIndex = 0; float colCount[width];
for ( x = 0; x < width; x++) { colCount[x] = 0; for ( y = y_start; y <= y_end; y++) { colCount[x] = colCount[x] + (1.0 - (buffer[y * width + x] / 255.0f)); } colTotal = colTotal + colCount[x]; if (colCount[x] > colMax) { colMax = colCount[x]; colColIndex = x; } }
float colAverage = colTotal / width;
buffer_temp = (unsigned char *) malloc(width * width); memset(buffer_temp, 255, width * width);
for ( x = 0; x < width; x++) { int index = (1.0 - colCount[x] / colMax) * width; if (x == width - 1) { index = (1.0 - colAverage / colMax) * width; }
for ( y = 0; y < width; y++) { if (y > index) { buffer_temp[y * width + x] = 0; } } } image.buffer = buffer_temp; image.width = width; image.height = width; return image;
|