Home >Java >javaTutorial >Corrosion and expansion of java images
Corrosion: Ba is obtained after translating structural element B by a. If Ba is included in X, we record this point a. The set of all a points that meet the above conditions is called the result of Erosion of X by B. The formula is expressed as: E(X)={a|
Ba
X}=X
B, as shown in the figure
Expansion: It can be seen as the dual operation of corrosion. Its definition is: after translating structural element B by a, Ba is obtained. If Ba hits X, we record this point a. The set of all points a that satisfy the above conditions is called the result of X being expanded by B. The formula is expressed as: D(X)={a
| Ba↑X}=X
B, as shown in the figure.
For more details, please see Image Expansion, Erosion, and Refinement
Now that we understand the principle, here is the code to implement it:
Expansion:
public void Expand(int[][] mask){ IterBinary();//二值化 int mh = mask.length; int mw = mask[1].length; int sh = (mh+1)/2; int sw = (mw+1)/2; 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++){ if(mask[m] *this.data[j+n-sw +(i+m-sh)*w] == 255) s = 255; } } d[j + i * w] = s; } } this.data = d; }
Corrosion:
public void Erosion(int[][] mask){ IterBinary();//二值化 int mh = mask.length; int mw = mask[1].length; int sh = (mh+1)/2; int sw = (mw+1)/2; 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++){ if(mask[m] *255 == this.data[j+n-sw +(i+m-sh)*w]) s++; } } d[j + i * w] = (s==mh*mw)?255:0; } } this.data = d; }
Among them, mask is the template, and we all take a 3*3 all-one matrix. Note that these two operations are performed on white text on a black background.
Original image and the result of expansion on the original image:
Then corrode based on the expansion (in the original image There will be no corrosion. .):
## If the dialogue is black on a white background, the results are just the opposite, and the result is as follows:The above is the content of corrosion and expansion of java images. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!