Я пытаюсь реализовать 3D-приложение для Android, которое также должно поддерживать картонные средства просмотра. Я видел некоторые из этих изображений, и они, кажется, имеют какое-то бочкообразное искажение, чтобы быть ортогональными через картонные линзы.
Поэтому я искал алгоритмы или библиотеки специально для Java / Android, которые помогли бы мне добиться этого.
Я нашел эту реализацию: http://www.helviojunior.com.br/fotografia/barrel-and-pincushion-distortion/
Было бы здорово иметь что-то подобное, потому что в нем есть все, что мне нужно. К сожалению, это для C #, и в нем есть определенный код, который я просто не мог легко перевести в более общий код.
Тогда есть более простая реализация Java здесь: http://popscan.blogspot.de/2012/04/fisheye-lens-equation-simple-fisheye.html
Я изменил его на:
public static Bitmap fisheye(Bitmap srcimage) {
/*
* Fish eye effect
* tejopa, 2012-04-29
* http://popscan.blogspot.com
* http://www.eemeli.de
*/
// get image pixels
double w = srcimage.getWidth();
double h = srcimage.getHeight();
int[] srcpixels = new int[(int)(w*h)];
srcimage.getPixels(srcpixels, 0, (int)w, 0, 0, (int)w, (int)h);
Bitmap resultimage = srcimage.copy(srcimage.getConfig(), true);
// create the result data
int[] dstpixels = new int[(int)(w*h)];
// for each row
for (int y=0;y<h;y++) {
// normalize y coordinate to -1 ... 1
double ny = ((2*y)/h)-1;
// pre calculate ny*ny
double ny2 = ny*ny;
// for each column
for (int x=0;x<w;x++) {
// preset to black
dstpixels[(int)(y*w+x)] = 0;
// normalize x coordinate to -1 ... 1
double nx = ((2*x)/w)-1;
// pre calculate nx*nx
double nx2 = nx*nx;
// calculate distance from center (0,0)
// this will include circle or ellipse shape portion
// of the image, depending on image dimensions
// you can experiment with images with different dimensions
double r = Math.sqrt(nx2+ny2);
// discard pixels outside from circle!
if (0.0<=r&&r<=1.0) {
double nr = Math.sqrt(1.0-r*r);
// new distance is between 0 ... 1
nr = (r + (1.0-nr)) / 2.0;
// discard radius greater than 1.0
if (nr<=1.0) {
// calculate the angle for polar coordinates
double theta = Math.atan2(ny,nx);
// calculate new x position with new distance in same angle
double nxn = nr*Math.cos(theta);
// calculate new y position with new distance in same angle
double nyn = nr*Math.sin(theta);
// map from -1 ... 1 to image coordinates
int x2 = (int)(((nxn+1)*w)/2.0);
// map from -1 ... 1 to image coordinates
int y2 = (int)(((nyn+1)*h)/2.0);
// find (x2,y2) position from source pixels
int srcpos = (int)(y2*w+x2);
// make sure that position stays within arrays
if (srcpos>=0 & srcpos < w*h) {
// get new pixel (x2,y2) and put it to target array at (x,y)
dstpixels[(int)(y*w+x)] = srcpixels[srcpos];
}
}
}
}
}
resultimage.setPixels(dstpixels, 0, (int)w, 0, 0, (int)w, (int)h);
//return result pixels
return resultimage;
}
Но у него нет этой линзы factor
, поэтому результирующее изображение всегда представляет собой полный круг / эллипс.
Есть ли шанс, что вы могли бы указать мне на какой-нибудь рабочий код Java или библиотеку или (может быть, даже лучше) помочь мне изменить этот код для учета фактора линзы (0,0 ‹= коэффициент‹ = 1,0)?