New filter experience, skillful use of convolution --java

Perhaps your filter has reached a bottleneck, and there are always some effects that cannot be completed. Remember one thing, when in doubt, turn to mathematics for help! If you use the concept of convolution in mathematics, you can succeed.

For the specific concept of convolution, you can ask Du Niang for help. Here is a brief explanation of how to use it.

Create a convolution kernel calculated by predecessors, use its center to overlap with the pixel you want, then multiply the corresponding positions, and then add them all to get the convolution.

Maybe this sounds more abstract, and it is impossible to imagine its effect, but there are pictures and truths for you.

And the relief effect after convolution processing:

 

 We can start doing:

public int juanji(int[][] karr/*Convolution kernel, easy to switch*/,int[][] tem){ int kk=0; 
    for 
    (int i=0;i< karr.length;i++){ 
        for(int j=0;j<karr[i].length;j++){ 
            kk+=karr[i][j]*tem[i][j]/*calculate a convolution, the required pixels* /; 
        } 
    } 
    return kk; 
}//convolution calculation method

The calculation of a pixel has been written! Start using the whole image!

public void bejuan(Graphics g,int[][] karr){     int[][] imgpix=getpix("C:\\Users\\27259\\Desktop\\aniya.png");//get picture pixel     int [][] temr=new int[karr.length][karr[0].length];     int[][] temg=new int[karr.length][karr[0].length];     int[][] temb=new int[karr.length][karr[0].length];/*Pixels required for convolution of a single pixel*/ /*It is     worth noting that R, G, B Split to find convolution, have better results*/     int[] ll=new int[3];//Separately store R, G, B values     ​​for(int i=0;i< imgpix.length- karr.length +1/*Maximum range of pixels to be sought*/;i++){         for(int j=0;j< imgpix[0].length-karr[0].length+1/*Maximum range of pixels to be sought*/ ;j++){            for(int a=0;a<karr.length;a++){                 for(int b=0;b<karr.length;b++){                     divide(imgpix[i+a][j+b]) ;











                    temr[a][b]=red;
                    temg[a][b]=green;
                    temb[a][b]=blue;
                }
            }
            ll[0]=juanji(karr,temr);
            ll[1]=juanji (karr,temg);
            ll[2]=juanji(karr,temb);//Get the R, G, and B values ​​of the convolution completed
            for(int n =0;n<3;n++){                 if(ll[n ]>255){                     ll[n]=255;                 }                 if(ll[n]<0){                     ll[n]=0;                 }             }//Remember to judge R, G, B values ​​to prevent cross-border             Color cl=new Color (ll[0],ll[1],ll[2]);             g.setColor(cl);//Set the convoluted color









            g.fillRect(500+i,200+j,1,1);
        }
    }
}

Well, there are other effects:

Sharpen:

Convolution kernel:

int[][] karr1={
   
  {-1,-1,-1},{-1,9,-1},{-1,-1,-1}};

 Horizontal profile:

Convolution kernel:

int[][] karr={
  
  {-1,-1,-1},{-1,8,-1},{-1,-1,-1}};

Let's try!!!

Guess you like

Origin blog.csdn.net/AkinanCZ/article/details/125451375