上一篇我們替矩陣加了些常用方法,這篇則來談談圖像的虛擬邊緣。
虛擬邊緣,就是依循某種映射關係,為圖像「補上」邊緣。它能做什麼呢?舉例來說,做倒影效果輕而易舉:

當然這只是附帶功能。虛擬邊緣真正的舞台是圖像卷積運算(例如平滑處理):由於卷積的特性,需要把圖片擴大才能對邊角進行運算,這時就得先為圖片「加邊」做預處理。說白了,就是在某些圖像處理之前先做好前置準備。
以下參考 OpenCV 的邊緣描述:
/*
Various border types, image boundaries are denoted with '|'
* BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh
* BORDER_REFLECT: fedcba|abcdefgh|hgfedcb
* BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba
* BORDER_WRAP: cdefgh|abcdefgh|abcdefg
* BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified 'i'
*/
舉例來說,BORDER_REFLECT 對某一列像素 abcdefgh 而言,左側虛擬邊緣對應為 fedcba、右側為 hgfedcb,也就是鏡像反射。上面那張圖正是對底部加上 BORDER_REFLECT 型別虛擬邊緣後的效果;而 BORDER_CONSTANT 則是所有邊緣都填成固定值 i。
由於 BORDER_CONSTANT 較為特殊,因此與其他類型分開處理:
function copyMakeBorder(__src, __top, __left, __bottom, __right, __borderType, __value){
if(__src.type != "CV_RGBA"){ console.error("不支持類型!"); }
if(__borderType === CV_BORDER_CONSTANT){ return copyMakeConstBorder_8U(__src, __top, __left, __bottom, __right, __value); }
else{ return copyMakeBorder_8U(__src, __top, __left, __bottom, __right, __borderType); }
};
此函式接受輸入矩陣 src、四個方向要添加的像素數(top、left、bottom、right)、邊緣型別 borderType,以及常數邊緣時所用的值 value。
接著引入一個邊緣映射關係函式 borderInterpolate:
function borderInterpolate(__p, __len, __borderType){
if(__p < 0 || __p >= __len){
switch(__borderType){
case CV_BORDER_REPLICATE: __p = __p < 0 ? 0 : __len - 1; break;
case CV_BORDER_REFLECT:
case CV_BORDER_REFLECT_101:
var delta = __borderType == CV_BORDER_REFLECT_101;
if(__len == 1) return 0;
do{ if(__p < 0) __p = -__p - 1 + delta; else __p = __len - 1 - (__p - __len) - delta; }while(__p < 0 || __p >= __len)
break;
case CV_BORDER_WRAP: if(__p < 0) __p -= ((__p - __len + 1) / __len) * __len; if(__p >= __len) __p %= __len; break;
case CV_BORDER_CONSTANT: __p = -1;
default: error(arguments.callee, UNSPPORT_BORDER_TYPE);
}
}
return __p;
};
它的意義是:對原長度為 len 的某列或某行而言,把虛擬像素點 p(通常是負數或大於等於原長度的值,負數代表左側、大於等於原長度代表右側)映射成該行哪一個真實像素點。以 CV_BORDER_REPLICATE 為例,__p = __p < 0 ? 0 : __len - 1; 代表 p 為負時映射為 0,否則映射為 len - 1。
接著實作 copyMakeBorder_8U:
function copyMakeBorder_8U(__src, __top, __left, __bottom, __right, __borderType){
var i, j;
var width = __src.col, height = __src.row;
var top = __top, left = __left || __top, right = __right || left, bottom = __bottom || top, dstWidth = width + left + right, dstHeight = height + top + bottom, borderType = borderType || CV_BORDER_REFLECT;
var buffer = new ArrayBuffer(dstHeight * dstWidth * 4), tab = new Uint32Array(left + right);
for(i = 0; i < left; i++){ tab[i] = borderInterpolate(i - left, width, __borderType); }
for(i = 0; i < right; i++){ tab[i + left] = borderInterpolate(width + i, width, __borderType); }
var tempArray, data;
for(i = 0; i < height; i++){
tempArray = new Uint32Array(buffer, (i + top) * dstWidth * 4, dstWidth);
data = new Uint32Array(__src.buffer, i * width * 4, width);
for(j = 0; j < left; j++) tempArray[j] = data[tab[j]];
for(j = 0; j < right; j++) tempArray[j + width + left] = data[tab[j + left]];
tempArray.set(data, left);
}
var allArray = new Uint32Array(buffer);
for(i = 0; i < top; i++){ j = borderInterpolate(i - top, height, __borderType); tempArray = new Uint32Array(buffer, i * dstWidth * 4, dstWidth); tempArray.set(allArray.subarray((j + top) * dstWidth, (j + top + 1) * dstWidth)); }
for(i = 0; i < bottom; i++){ j = borderInterpolate(i + height, height, __borderType); tempArray = new Uint32Array(buffer, (i + top + height) * dstWidth * 4, dstWidth); tempArray.set(allArray.subarray((j + top) * dstWidth, (j + top + 1) * dstWidth)); }
return new Mat(dstHeight, dstWidth, new Uint8ClampedArray(buffer));
}
這裡要說明的是擴邊順序:先對每一列的左右進行擴展,再在此基礎上進行上下擴展:

由於每個像素的 RGBA 各通道以 8 位無符號整數儲存,一個像素剛好佔 32 位元。利用 ArrayBuffer 可轉型的特性,把資料轉成 Uint32Array,就能讓每個操作單位正好對應一個像素點,大幅簡化運算。
如此一來,copyMakeConstBorder_8U 就好寫多了:
function copyMakeConstBorder_8U(__src, __top, __left, __bottom, __right, __value){
var i, j;
var width = __src.col, height = __src.row;
var top = __top, left = __left || __top, right = __right || left, bottom = __bottom || top, dstWidth = width + left + right, dstHeight = height + top + bottom, value = __value || [0, 0, 0, 255];
var constBuf = new ArrayBuffer(dstWidth * 4), constArray = new Uint8ClampedArray(constBuf);
buffer = new ArrayBuffer(dstHeight * dstWidth * 4);
for(i = 0; i < dstWidth; i++){ for( j = 0; j < 4; j++){ constArray[i * 4 + j] = value[j]; } }
constArray = new Uint32Array(constBuf);
var tempArray;
for(i = 0; i < height; i++){
tempArray = new Uint32Array(buffer, (i + top) * dstWidth * 4, left); tempArray.set(constArray.subarray(0, left));
tempArray = new Uint32Array(buffer, ((i + top + 1) * dstWidth - right) * 4, right); tempArray.set(constArray.subarray(0, right));
tempArray = new Uint32Array(buffer, ((i + top) * dstWidth + left) * 4, width); tempArray.set(new Uint32Array(__src.buffer, i * width * 4, width));
}
for(i = 0; i < top; i++){ tempArray = new Uint32Array(buffer, i * dstWidth * 4, dstWidth); tempArray.set(constArray); }
for(i = 0; i < bottom; i++){ tempArray = new Uint32Array(buffer, (i + top + height) * dstWidth * 4, dstWidth); tempArray.set(constArray); }
return new Mat(dstHeight, dstWidth, new Uint8ClampedArray(buffer));
}
CV_BORDER_REPLICATE

CV_BORDER_REFLECT

CV_BORDER_WRAP

CV_BORDER_CONSTANT
