我需要从我拥有的图像中创建一个等距圆柱投影/变换图像。 等距圆柱的公式为http://sphotos-e.ak.fbcdn.net/hphotos-ak-ash3/525279_4306756439568_1150093099_n.jpg。
我所拥有的代码是使用公式http://sphotos-g.ak.fbcdn.net/hphotos-ak-ash3/72384_4306756599572_459887390_n.jpg的墨卡托投影代码改编的,我不完全理解,因此它会输出错误的图像。
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class EquidistantCylindrical {
public static void main(String args[]) throws IOException {
BufferedImage hugeImage = ImageIO.read(EquidistantCylindrical.class.getResource("world1.jpg"));
int[][] rgb = convertTo2DUsingGetRGB(hugeImage);
double minLat = -75.0;
double maxLat = 75.0;
BufferedImage newImage = reprojMerc(hugeImage, minLat, maxLat);
File outputfile = new File("worldmapprojected.jpg");
ImageIO.write(newImage, "jpg", outputfile);
}
public static int[][] convertTo2DUsingGetRGB(BufferedImage image) {
int widthimg = image.getWidth();
int heightimg = image.getHeight();
int[][] result = new int[widthimg][heightimg];
for (int row = heightimg; row > heightimg; row--) {
for (int col = widthimg; col > widthimg; col--) {
result[row][col] = image.getRGB(col, row);
}
}
return result;
}
public static BufferedImage reprojMerc(BufferedImage img, double minlat, double maxlat) {
// Convert the coords into a sensible format.
minlat *= Math.PI / 180;
maxlat *= Math.PI / 180;
double dlat = maxlat - minlat;
System.out.println(dlat);
// Convert to cylindrical
double minmerc = Math.cos(minlat);
double maxmerc = Math.cos(maxlat);
double dmerc = maxmerc + minmerc;
System.out.println(dmerc);
// Setting dimensions of new image
int width = img.getWidth();
int height = img.getHeight();
int projected_width = (int)(width*dmerc/dlat)*10;
System.out.println(projected_width);
int projected_height = height;
BufferedImage pimg = new BufferedImage(projected_width, projected_height, img.getType());
// Copy the lines.
for (int py = 0; py < projected_height; py++) {
double merc = maxmerc - dmerc * ((double) py) / projected_height;
double lat = 2 * Math.atan(Math.exp(merc)) - Math.PI / 2;
int gy = (int) (height * (maxlat - lat) / dlat);
int[] rgb = img.getRGB(0, gy, width, 1, null, 0, 4 * width);
pimg.setRGB(0, py, projected_width, 1, rgb, 0, 4 * projected_width);
}
return pimg;
}
}
提前谢谢。