欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Javascript圖像處理—虛擬邊緣介紹及使用方法

 更新時(shí)間:2012年12月27日 14:30:45   作者:  
虛擬邊緣就是按照一定映射關(guān)系,給圖像添加邊緣那么虛擬邊緣有什么用呢?接下來詳細(xì)介紹,需要了解的朋友可以參考下
前言
上一篇文章,我們來給矩陣添加一些常用方法,這篇文章將講解圖像的虛擬邊緣。

虛擬邊緣
虛擬邊緣就是按照一定映射關(guān)系,給圖像添加邊緣。
那么虛擬邊緣有什么用呢?比如可以很容易做一個(gè)倒影的效果:
 
當(dāng)然這只是附帶效果了,虛擬邊緣主要用在圖像卷積運(yùn)算(例如平滑操作)時(shí)候,由于卷積運(yùn)算的特點(diǎn),需要將圖片擴(kuò)大才能對邊角進(jìn)行卷積運(yùn)算,這時(shí)候就需要對圖片進(jìn)行預(yù)處理,添加虛擬邊緣。
說白了,就是在一些圖片處理前進(jìn)行預(yù)處理。

邊緣類型
這里參考OpenCV相關(guān)文檔的邊緣描述:
復(fù)制代碼 代碼如下:

/*
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'
*/

舉個(gè)例子BODER_REFLECT就是對于某一行或某一列像素點(diǎn):
  abcdefgh
其左的虛擬邊緣對應(yīng)為fedcba,右邊對應(yīng)為hgfedcb,也就是反射映射。上圖就是通過對圖片底部進(jìn)行添加BORDER_REFLECT類型的虛擬邊緣得到的。
而BORDER_CONSTANT則是所有邊緣都是固定值i。
實(shí)現(xiàn)
因?yàn)锽ORDER_CONSTANT比較特殊,所以和其他類型分開處理。
復(fù)制代碼 代碼如下:

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);
}
};

這個(gè)函數(shù)接受一個(gè)輸入矩陣src,每個(gè)方向要添加的像素大小top,left,bottom,right,邊緣的類型borderType,還有一個(gè)數(shù)組value,即如果是常數(shù)邊緣時(shí)候添加的常數(shù)值。
然后我們引入一個(gè)邊緣的映射關(guān)系函數(shù)borderInterpolate。
復(fù)制代碼 代碼如下:

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/* {line} */);
}
}
return __p;
};

這個(gè)函數(shù)的意義是對于原長度為len的某一行或者某一列的虛擬像素點(diǎn)p(p一般是負(fù)數(shù)或者大于或等于該行原長度的數(shù),負(fù)數(shù)則表示該行左邊的像素點(diǎn),大于或等于原長度則表示是右邊的像素點(diǎn)),映射成這一行的哪一個(gè)像素點(diǎn)。我們拿CV_BORDER_REPLICATE分析一下,其表達(dá)式是:
  __p = __p < 0 ? 0 : __len - 1;
也就是說p為負(fù)數(shù)時(shí)(也就是左邊)的時(shí)候映射為0,否則映射成len - 1。
然后我們來實(shí)現(xiàn)copyMakeBorder_8U函數(shù):
復(fù)制代碼 代碼如下:

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));
}

這里需要解釋下,邊緣的復(fù)制順序是:先對每行的左右進(jìn)行擴(kuò)展,然后在此基礎(chǔ)上進(jìn)行上下擴(kuò)展,如圖所示。

然后我們根據(jù)ArrayBuffer的性質(zhì),將數(shù)據(jù)轉(zhuǎn)成無符號32位整數(shù)來操作,這樣每個(gè)操作單位就對應(yīng)了每個(gè)像素點(diǎn)了。什么意思?
比如對于某個(gè)像素點(diǎn):RGBA,由于某個(gè)通道是用無符號8為整數(shù)來存儲(chǔ)的,所以實(shí)際上一個(gè)像素點(diǎn)則對應(yīng)了32位的存儲(chǔ)大小,由于ArrayBuffer的性質(zhì),可以將數(shù)據(jù)轉(zhuǎn)成任意類型來處理,這樣我們就可以通過轉(zhuǎn)成Uint32Array類型,將數(shù)據(jù)變成每個(gè)像素點(diǎn)的數(shù)據(jù)數(shù)組。
那么copyMakeConstBorder_8U就比較容易實(shí)現(xiàn)了:
復(fù)制代碼 代碼如下:

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

  • 關(guān)于javaScript注冊click事件傳遞參數(shù)的不成功問題

    關(guān)于javaScript注冊click事件傳遞參數(shù)的不成功問題

    在javaScript中給一個(gè)html元素注冊click事件處理函數(shù)時(shí),比如給該處理函數(shù)傳3個(gè)參數(shù)??墒遣还苁鞘褂孟旅婺欠N方式都不能給事件處理函數(shù)傳遞參數(shù)
    2014-07-07
  • js變形金剛文字特效代碼分享

    js變形金剛文字特效代碼分享

    這篇文章主要介紹了js變形金剛文字特效,實(shí)現(xiàn)效果超酷,很立體,感興趣的小伙伴們可以參考一下
    2015-08-08
  • JavaScript實(shí)現(xiàn)淘寶購物件數(shù)選擇

    JavaScript實(shí)現(xiàn)淘寶購物件數(shù)選擇

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)淘寶購物件數(shù)的選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • layui點(diǎn)擊左側(cè)導(dǎo)航欄,實(shí)現(xiàn)不刷新整個(gè)頁面,只刷新局部的方法

    layui點(diǎn)擊左側(cè)導(dǎo)航欄,實(shí)現(xiàn)不刷新整個(gè)頁面,只刷新局部的方法

    今天小編就為大家分享一篇layui點(diǎn)擊左側(cè)導(dǎo)航欄,實(shí)現(xiàn)不刷新整個(gè)頁面,只刷新局部的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-09-09
  • JS無限極樹形菜單,json格式、數(shù)組格式通用示例

    JS無限極樹形菜單,json格式、數(shù)組格式通用示例

    本文為大家介紹下JS無級樹形菜單的實(shí)現(xiàn),修改了一下數(shù)據(jù)格式,是json和數(shù)組或者混合型的數(shù)據(jù)都通用,不用特定key等,想學(xué)習(xí)的朋友可以參考下
    2013-07-07
  • 基于JavaScript實(shí)現(xiàn)通用tab選項(xiàng)卡(通用性強(qiáng))

    基于JavaScript實(shí)現(xiàn)通用tab選項(xiàng)卡(通用性強(qiáng))

    選項(xiàng)卡在大量的網(wǎng)站都有應(yīng)用,雖然形式各有不同,但是索要達(dá)成的目的都是一樣的,一般都是為了進(jìn)行分類或者節(jié)省網(wǎng)頁空間只用,算是一件利器,下面就是一個(gè)選項(xiàng)卡的代碼實(shí)例,通用性很強(qiáng),下面就和大家分享一下
    2016-01-01
  • JavaScript中in和hasOwnProperty區(qū)別詳解

    JavaScript中in和hasOwnProperty區(qū)別詳解

    in操作符只要通過對象能訪問到屬性就返回true。hasOwnProperty()只在屬性存在于實(shí)例中時(shí)才返回true。下面通過本文給大家分享JavaScript中in和hasOwnProperty區(qū)別詳解,感興趣的朋友一起看看吧
    2017-08-08
  • JavaScript判斷數(shù)據(jù)類型有幾種方法及區(qū)別介紹

    JavaScript判斷數(shù)據(jù)類型有幾種方法及區(qū)別介紹

    這篇文章主要介紹了JavaScript判斷數(shù)據(jù)類型有幾種方法及區(qū)別介紹,本文給大家分享多種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • JS中的算法與數(shù)據(jù)結(jié)構(gòu)之字典(Dictionary)實(shí)例詳解

    JS中的算法與數(shù)據(jù)結(jié)構(gòu)之字典(Dictionary)實(shí)例詳解

    這篇文章主要介紹了JS中的算法與數(shù)據(jù)結(jié)構(gòu)之字典(Dictionary),結(jié)合實(shí)例形式詳細(xì)分析了javascript數(shù)據(jù)結(jié)構(gòu)中字典的概念、原理、定義與常見使用方法,需要的朋友可以參考下
    2019-08-08
  • 最新評論