Skip to content

Instantly share code, notes, and snippets.

@vumbumy
Created September 28, 2016 07:43
Show Gist options
  • Save vumbumy/20551a01531d2a95984ae76ccd239550 to your computer and use it in GitHub Desktop.
Save vumbumy/20551a01531d2a95984ae76ccd239550 to your computer and use it in GitHub Desktop.
private void crtLensDistor(double correctionFactor){
lookUpTable = new ArrayList<>();
double bOptimiz = 0.026731; // most cases only require b optimization
double linearScal = 1.0 - bOptimiz - correctionFactor; // describes the linear scaling of the image
int d = Math.min(width, height) / 2; // radius of the circle
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// center of dst image
double centerX = (width - 1) / 2.0;
double centerY = (height - 1) / 2.0;
// cartesian coordinates of the destination point (relative to the centre of the image)
double deltaX = (x - centerX) / d;
double deltaY = (y - centerY) / d;
// distance or radius of dst image
double dstR = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
// distance or radius of src image (with formula)
double srcR = (bOptimiz * dstR * dstR + correctionFactor * dstR + linearScal) * dstR;
// comparing old and new distance to get factor
double factor = Math.abs(dstR / srcR);
// coordinates in source image
double srcXd = centerX + (deltaX * factor * d);
double srcYd = centerY + (deltaY * factor * d);
// no interpolation yet (just nearest point)
short srcX = (short) 0;
short srcY = (short) 0;
if (srcX >= 0 && srcY >= 0 && srcX < width && srcY < height) {
srcX = (short) srcXd;
srcY = (short) srcYd;
}
lookUpTable.add(new Pair(srcX,srcY));
}
}
}
private void matchingTable(Bitmap src){
int[] srcPixels = new int[width*height];
int[] dstPixels = new int[width*height];
src.getPixels(srcPixels,0,width,0,0,width,height);
for(int i=0;i<lookUpTable.size();i++){
short srcX = lookUpTable.get(i).getFirst();
short srcY = lookUpTable.get(i).getSecond();
dstPixels[i] = srcPixels[srcY*width+srcX];
}
src.setPixels(dstPixels,0,width,0,0,width,height);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment