?? img_thin.c
字號:
/*############################################################################# * 文件名:img_thin.c * 功能: 實現了圖像細化操作 * modified by PRTsinghua@hotmail.com#############################################################################*/#include <math.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>#include "imagemanip.h"#ifndef min#define min(a,b) (((a)<(b))?(a):(b))#endif#define NOT_BK(pos) (image[pos]!=0)#define IS_BK(pos) (image[pos]==0)bool_t MatchPattern(uint8_t image[], int x, int y, int w, int h){ bool_t nRet = false; /* 驗證有無出界 */ int lhe = y * w; /* 本行 */ int lup = lhe - w; /* 上一行 */ int ldo = lhe + w; /* 下一行 */ int tl = lup + x - 1; /* 左上 */ int tc = lup + x; /* 中上 */ int tr = lup + x + 1; /* 右上 */ int hl = lhe + x - 1; /* 左 */ int hr = lhe + x + 1; /* 右 */ int bl = ldo + x - 1; /* 左下 */ int bc = ldo + x; /* 中下 */ int br = ldo + x + 1; /* 右下 */ /* 第一模式 ? ? ? one not 0 0 1 0 ? ? ? one not 0 */ if ( image[hr]==0 && image[hl]==0 && ((image[tl]!=0) || (image[tc]!=0) || (image[tr]!=0))&& ((image[bl]!=0) || (image[bc]!=0) || (image[br]!=0)) ) { nRet = true; } /* 同樣的旋轉90度 ? 0 ? ? 1 ? ? 0 ? */ else if ( image[tc]==0 && image[bc]==0 && ((image[bl]!=0) || (image[hl]!=0) || (image[tl]!=0))&& ((image[br]!=0) || (image[hr]!=0) || (image[tr]!=0)) ) { nRet = true; } /* ? ? ? ? 1 0 ? 0 1 */ else if (image[br]==0xFF && image[hr]==0 && image[bc]==0 && (image[tr]!=0 || image[tc]!=0 || image[tl]!=0 || image[hl]!=0 || image[bl]!=0) ) { nRet = true; } /* ? ? ? 0 1 ? */ else if (image[bl]==0xFF && image[hl]==0 && image[bc]==0 && (image[br]!=0 || image[hr]!=0 || image[tr]!=0 || image[tc]!=0 || image[tl]!=0)) { nRet = true; } /* 1 0 ? 0 1 ? ? ? ? */ else if (image[tl]==0xFF && image[tc]==0 && image[hl]==0 && (image[bl]!=0 || image[bc]!=0 || image[br]!=0 || image[hr]!=0 || image[tr]!=0)) { nRet = true; } /* ? 0 1 ? 1 0 ? ? ? */ else if (image[tr]==0xFF && image[hr]==0 && image[tc]==0 && (image[tl]!=0 || image[hl]!=0 || image[bl]!=0 || image[bc]!=0 || image[br]!=0)) { nRet = true; } image[y*w + x] = (nRet==true)?0xFF:0x00; return nRet;}/* 細化圖像 */FvsError_t ImageThin3(Image_t imgf){ bool_t Remain; int temp; uint8_t* image = ImageGetBuffer(imgf); register int x, y; int w = ImageGetWidth(imgf); /* 圖像寬度 */ int h = ImageGetHeight(imgf); /* 圖像高度 */ int tmp; int row; /* 提高細化速度 */ int _lastY; int _newY; /* 初始化 */ _lastY = _newY = 1; /* 標記:全部完成后再處理 */ Remain = true; while (Remain) { _lastY = 1; _newY = h; Remain = false; fprintf(stderr, "."); temp = false; for (y = _lastY; y < h-1; y++) for (x = 1; x < w-1; x++) { row = y*w; tmp = image[row +(x + 1)]; if (image[row + x] == 0xFF && tmp == 0 && MatchPattern(image, x, y, w, h) == false) if (temp==false) { _newY = min(_newY, y); Remain = true; temp = true; } } for (x = w*_lastY; x < w*h; x++) if (image[x] == 0x00) image[x] = 0; temp = false; for (y = _lastY; y < h-1; y++) for (x = 1; x < w-1; x++) { row = y*w; tmp = image[(y - 1) * w + x]; if (image[row + x] == 0xFF && tmp == 0 && MatchPattern(image, x, y, w, h)==false) if (temp==false) { _newY = min(_newY, y); Remain = true; temp = true; } } /* end for y */ for (x = w*_lastY; x < w*h; x++) if (image[x] == 0x00) image[x] = 0; temp = false; for (y = _lastY; y < h-1; y++) for (x = 1; x < w-1; x++) { row = y*w; tmp = image[row +(x - 1)]; /* -> */ if (image[row + x] == 0xFF && tmp == 0 && MatchPattern(image, x, y, w, h)==false) if (temp==false) { _newY = min(_newY, y); Remain = true; temp = true; } } /* end for y */ for (x = w*_lastY; x < w*h; x++) if (image[x] == 0x00) image[x] = 0; temp = false; for (y = _lastY; y < h-1; y++) for (x = 1; x < w-1; x++) { row = y*w; tmp = image[(y + 1) * w + x]; if (image[row + x] == 0xFF && tmp == 0 && MatchPattern(image, x, y, w, h)==false) if (temp==false) { _newY = min(_newY, y); Remain = true; temp = true; } } /* end for y */ for (x = w*_lastY; x < w*h; x++) if (image[x] == 0x00) image[x] = 0; } /* end while */ return FvsOK;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -