JavaScript image Processing-image morphology (expansion and corrosion) _javascript techniques

Source: Internet
Author: User
Preface

In the previous article, we explained the threshold function in image processing, and this article is about the expansion and corrosion function.

Expansion and Corrosion

To say that the concept may be difficult to explain, we look at the map, first of all:

It will become like this after the expansion:

Corrosion will become like this:

It may seem a little inexplicable, is obviously swelling, why the word instead of thin, and obviously corrosion, why the word instead of thick.

in fact, the so-called expansion should mean :

Lighter than the bright block expansion.

and the so-called corrosion should mean :

Lighter than bright block corrosion.

In the above picture, because the background white is lighter than the color block, so when the expansion of black darker pieces of the word squashed ... When the opposite is corrosive, the word absorbs water and expands ...

in a mathematical formula , it means:

The white is to find the darkest or brightest pixel in the kernel of the specified size and replace the pixel on the kernel anchor point with that point.

Implement

First we implement the expansion dilate function.

Copy Code code as follows:

var dilate = function (__src, __size, __bordertype, __DST) {
__SRC | | Error (Arguments.callee, is_undefined_or_null/* {line} */);
if (__src.type && __src.type = "Cv_rgba") {
var width = __src.col,
Height = __src.row,
Size = __size | | 3,
DST = __DST | | New Mat (height, width, cv_rgba),
Dstdata = Dst.data;

var start = size >> 1;
var Withbordermat = Copymakeborder (__src, start, start, 0, 0, __bordertype),
Mdata = Withbordermat.data,
Mwidth = Withbordermat.col;

var newoffset, Total, nowx, OffsetY, Offseti, Nowoffset, I, J;

if (size & 1 = 0) {
Error (Arguments.callee, unspport_size/* {line} */);
return __SRC;
}

for (i = height; i--;) {
Offseti = i * width;
for (j = width; j--;) {
Newoffset = 0;
Total = 0;
for (y = size; y--;) {
OffsetY = (y + i) * mwidth * 4;
for (x = size; x--;) {
NOWX = (x + j) * 4;
Nowoffset = OffsetY + nowx;
(Mdata[nowoffset] + mdata[nowoffset + 1] + Mdata[nowoffset + 2] > Total) && (total = Mdata[nowoffset] + mdata[n Owoffset + 1] + Mdata[nowoffset + 2]) && (newoffset = Nowoffset);
}
}
dstdata[(j + Offseti) * 4] = Mdata[newoffset];
dstdata[(j + Offseti) * 4 + 1] = Mdata[newoffset + 1];
dstdata[(j + Offseti) * 4 + 2] = Mdata[newoffset + 2];
dstdata[(j + Offseti) * 4 + 3] = Mdata[newoffset + 3];
}
}

}else{
Error (Arguments.callee, unspport_data_type/* {line} */);
}
return DST;
};

In this line of code, we first compare the value of RGB with the total of the previous maximum, and then, if the new value is large, assign the new value to total, and assign the new offset newoffset the current offset nowoffset.

Then the entire kernel size is recycled to find a maximum total and the corresponding offset newoffset. You can assign a value:

dstdata[(j + Offseti) * 4] = Mdata[newoffset];
dstdata[(j + Offseti) * 4 + 1] = Mdata[newoffset + 1];
dstdata[(j + Offseti) * 4 + 2] = Mdata[newoffset + 2];
dstdata[(j + Offseti) * 4 + 3] = Mdata[newoffset + 3];

So the corrosion erode function is instead, find a minimum value on the line.

Copy Code code as follows:

var erode = function (__src, __size, __bordertype, __DST) {
__SRC | | Error (Arguments.callee, is_undefined_or_null/* {line} */);
if (__src.type && __src.type = "Cv_rgba") {
var width = __src.col,
Height = __src.row,
Size = __size | | 3,
DST = __DST | | New Mat (height, width, cv_rgba),
Dstdata = Dst.data;

var start = size >> 1;
var Withbordermat = Copymakeborder (__src, start, start, 0, 0, __bordertype),
Mdata = Withbordermat.data,
Mwidth = Withbordermat.col;

var newoffset, Total, nowx, OffsetY, Offseti, Nowoffset, I, J;

if (size & 1 = 0) {
Error (Arguments.callee, unspport_size/* {line} */);
return __SRC;
}

for (i = height; i--;) {
Offseti = i * width;
for (j = width; j--;) {
Newoffset = 0;
Total = 765;
for (y = size; y--;) {
OffsetY = (y + i) * mwidth * 4;
for (x = size; x--;) {
NOWX = (x + j) * 4;
Nowoffset = OffsetY + nowx;
(Mdata[nowoffset] + mdata[nowoffset + 1] + Mdata[nowoffset + 2] < total) && (total = Mdata[nowoffset] + mdata[n Owoffset + 1] + Mdata[nowoffset + 2]) && (newoffset = Nowoffset);
}
}
dstdata[(j + Offseti) * 4] = Mdata[newoffset];
dstdata[(j + Offseti) * 4 + 1] = Mdata[newoffset + 1];
dstdata[(j + Offseti) * 4 + 2] = Mdata[newoffset + 2];
dstdata[(j + Offseti) * 4 + 3] = Mdata[newoffset + 3];
}
}

}else{
Error (Arguments.callee, unspport_data_type/* {line} */);
}
return DST;
};

effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.