代碼如下:
- /******************************************************************************
- * 作用: 灰度閾值變換函數
- * 參數: pDst 輸出圖像的像素數組
- * 參數: pSrc 原始圖像的像素數組
- * 參數: nWidth 原始圖像寬度
- * 參數: nHeight 原始圖像高度
- * 參數: thr 閾值
- ******************************************************************************/
- int ThresholdTrans(BYTE* pDst, BYTE* pSrc, int nWidth, int nHeight, int thr)
- {
- if (!pSrc || !pDst)
- {
- return EXIT_FAILURE;
- }
- // 灰度映射表
- BYTE map[256];
- int i;
- for (i = 0; i < 256; i++)
- {
- if (i >= thr)
- map[i] = 255;
- else
- map[i] = 0;
- }
- // 變換後的值直接在映射表中查找
- for (i = 0; i < nWidth * nHeight; i++)
- {
- int x = pSrc[i * 4];
- pDst[i*4] = pDst[i*4+1] = pDst[i*4+2] = map[x];
- pDst[i*4+3] = 255;
- }
- return EXIT_SUCCESS;
- }