Skip to content

Instantly share code, notes, and snippets.

@hageldave
Created April 24, 2017 12:49
Show Gist options
  • Save hageldave/0449183447dfd4961609fc81ead11018 to your computer and use it in GitHub Desktop.
Save hageldave/0449183447dfd4961609fc81ead11018 to your computer and use it in GitHub Desktop.
using the deviation from a direction vector the weight for pixels weight is determined
import hageldave.imagingkit.core.ImageLoader;
import hageldave.imagingkit.core.Img;
import hageldave.imagingkit.core.util.ImageFrame;
import hageldave.imagingkit.filter.implementations.ConvolutionFilter;
public class testmain {
public static void main(String[] args) throws InterruptedException {
Img img = ImageLoader.loadImgFromURL("http://www.wellcomeimageawards.org/WI-6561.24_WIA_2017_PP_holding_screen.jpg");
ConvolutionFilter c = new ConvolutionFilter();
float[] kernel = new float[17*17];
float xdir = 1;
float ydir = 2;
for(int y = 0; y < 17; y++){
float yf = (y-(17/2))*1.0f/(17/2);
for(int x = 0; x < 17; x++){
float xf = (x-(17/2))*1.0f/(17/2);
if(x == 8 && y == 8){
kernel[y*17+x] = 1;
} else {
float parallelity = parallelity(xf, yf, xdir, ydir);
kernel[y*17+x] = (float) (Math.pow(parallelity,3)*(1-Math.sqrt(xf*xf+yf*yf)/1.42));
}
}
}
c.setConvolutionKernel(17, 17, kernel);
Img kernelImg = new Img(17, 17);
kernelImg.forEach(px->px.setRGB_fromNormalized(kernel[px.getIndex()], kernel[px.getIndex()], kernel[px.getIndex()]));
c.normalizeConvolutionKernel();
// c.applyTo(img, true);
ImageFrame.display(kernelImg);
}
static float parallelity(float x1, float y1, float x2, float y2){
float l1 = (float) Math.sqrt(x1*x1+y1*y1);
float l2 = (float) Math.sqrt(x2*x2+y2*y2);
x1 /= l1; y1 /= l1;
x2 /= l2; y2 /= l2;
// scalar product returns cosine of angle between vectors
return Math.abs(x1*x2+y1*y2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment