Please read the
before reading, thank you! <br><br> Regarding convolution, we have already mentioned it in the previous article. If you don’t understand, you can read the previous article. <br><br>After seeing the title, smart children should understand their direct relationship. For the convolution operation, different results can be obtained by using different templates (Mask). The convolution operation is implemented first. <br><br>The code is as follows: </preface></p><pre class="brush:java;toolbar:false">public void filter(double[][] mask) {
toGray();//灰度化
int mh = mask.length;
int mw = mask[1].length;
int sh = (mh+1)/2;
int sw = (mw+1)/2;
double maskSum = math.sum(mask);
int[] d= new int[w*h];
for(int i=(mh-1)/2+1;i<h-(mh-1)/2;i++){
for(int j=(mw-1)/2+1;j<w-(mw-1)/2;j++){
int s = 0;
for(int m=0; m<mh ; m++){
for(int n=0;n<mw;n++){
s = s + (int)(mask[m]
*this.data[j+n-sw +(i+m-sh)*w]);
}
}
if(maskSum != 0)
s /= maskSum;
if(s < 0)
s =0;
if(s > 255)
s = 255;
d[j + i * w] = s;
}
}
this.data = d;
}Copy after login
For the Gaussian template generated by the Gaussian kernel as follows:
The running results are as follows. The right side is the result generated by the Gaussian 7*7 template:
Sharpening template:
Run result:
Laplacian operator:
Run Result:
The above is the content of java image convolution operation, Gaussian blur and Laplacian operator. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!