Home >Java >javaTutorial >Corrosion and expansion of java images

Corrosion and expansion of java images

黄舟
黄舟Original
2016-12-30 11:42:151549browse

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

Corrosion and expansion of java images

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.

Corrosion and expansion of java images

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:

Corrosion and expansion of java images

Corrosion and expansion of java images

Then corrode based on the expansion (in the original image There will be no corrosion. .):

Corrosion and expansion of java images

## If the dialogue is black on a white background, the results are just the opposite, and the result is as follows:

Corrosion and expansion of java images

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)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn