Java中的图片转换,矩阵乘法不起作用
我正在用Java实现图片转换。到目前为止,我已经实现了以下类:Java中的图片转换,矩阵乘法不起作用,java,image,swing,transformation,matrix-multiplication,Java,Image,Swing,Transformation,Matrix Multiplication,我正在用Java实现图片转换。到目前为止,我已经实现了以下类: 矩阵(包含一个3x3矩阵,用于与向量相乘) 矢量(用于与变换矩阵的多重化,以生成原始图像像素的新位置) PictureTransformer(转换图像并将新值存储到临时数组) Picture(保存图像mImage和像素数组mPixels的成员变量,并提供getter和setter方法) 窗口(用于测试功能的临时类,因为这些类将是更大程序的一部分) 在测试程序时,我注意到一些奇怪的行为: 无论我使用什么变换,图片一直在缩放 无论
- 矩阵(包含一个3x3矩阵,用于与向量相乘)
- 矢量(用于与变换矩阵的多重化,以生成原始图像像素的新位置)
- PictureTransformer(转换图像并将新值存储到临时数组)
- Picture(保存图像mImage和像素数组mPixels的成员变量,并提供getter和setter方法)
- 窗口(用于测试功能的临时类,因为这些类将是更大程序的一部分)
- 无论我使用什么变换,图片一直在缩放
- 无论触发MouseEvent的频率如何,转换只执行一次
公共静态向量乘法(矩阵a,向量v)中算法中的一些错误代码>因此乘法应按预期工作
在研究这些问题几个小时后,我不知道如何解决这个问题。任何帮助都将不胜感激
我希望可以发布整个代码,因为我不确定哪些部分有助于解决错误
这些是课程:
窗口:
图片转换器:
母体
我发现两件事,在矩阵课上,我改变了旋转矩阵:
public static Matrix rotate(final double a) {
final double rad = Math.PI * a / 180;
final double dM[][] = { { Math.cos(rad), Math.sin(rad), 0 }, { -Math.sin(rad), Math.cos(rad), 0 }, { 0, 0, 1 } };
return new Matrix(dM);
}
在PictureTransformer类转换方法中,我以不同的方式执行新图像创建:
// p.mImage = createImage(p.mImgSrc);
final MemoryImageSource mis = new MemoryImageSource(p.W, p.H, mPixelsResult, 0, p.W);
final Toolkit tk = Toolkit.getDefaultToolkit();
p.mImage = tk.createImage(mis);
p.mImage.flush();
结果是图像在窗口中旋转了-45º。
这不是解决办法,而是一个起点
希望对您有所帮助。或者,尝试前面提到的ConvolveOp
。至少现在发生了一些事情。我认为这接近于解决问题。我想,在实际转换或公共静态向量乘法(矩阵a,向量v)
中肯定还有另一个错误。非常感谢你的帮助!
import java.awt.*;
import java.awt.image.*;
public class Picture {
int[] mPixels;
MemoryImageSource mImgSrc;
Image mImage;
final int W = 640; final int H = 480;
public Picture(Image img)
{
mImage = img;
mPixels = new int[W*H];
PixelGrabber pg = new PixelGrabber(mImage ,0,0,W,H,mPixels,0,W);
try {
pg.grabPixels();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(mPixels);
mImgSrc = new MemoryImageSource(W,H,mPixels,0,W);
}
public int[] getPixels()
{
return this.mPixels;
}
public Image getImage()
{
return this.mImage;
}
public void setPixels(int[] newPix)
{
this.mPixels = newPix;
}
public void setImage(Image newImg)
{
this.mImage = newImg;
}
}
public class Matrix {
double[][] v;
Matrix(double[][] v){
this.v = v;
}
/** Creates an Matrix that will used to translate the picture to the coordinates
* clicked on the screen.
**/
public static Matrix translate(int dx, int dy){
double dM[][] = {{1, 0, 0}, {0, 1, 0}, {Math.round(-dx), Math.round(-dy), 1}};
return new Matrix(dM);
}
public static Matrix rotate(double a){
double rad = -(Math.PI * a / 180);
double dM[][] = {{Math.cos(rad), Math.sin(rad), 0},{Math.sin(rad), Math.cos(rad), 0}, {0, 0, 1}};
return new Matrix(dM);
}
/** Creates an Matrix that will used to scale the picture by the given factor. **/
public static Matrix scale(double f){
double dM[][] = {{1/f, 0, 0}, {0, 1/f, 0}, {0, 0, 1}};
return new Matrix(dM);
}
public static Matrix shearX(double sX){
double dM[][] = {{1, 0, 0}, {-sX, 1, 0}, {0, 0, 1}};
return new Matrix(dM);
}
public static Matrix shearY(double sY){
double dM[][] = {{1, -sY, 0}, {0, 1, 0}, {0, 0, 1}};
return new Matrix(dM);
}
public static Matrix multiply(Matrix x, Matrix y){
double[][] p = new double[3][3];
for(int i = 0; i < x.v.length; ++i){
for(int j = 0; j < x.v[i].length; j++){
for(int k = 0; k < 3; k++){
p[i][j] += + x.v[k][j] * y.v[i][k];
}
}
}
return new Matrix(p);
}
public static Vector multiply(Matrix a, Vector v){
int[] res = new int[a.v[0].length];
for(int i = 0; i < a.v[0].length; i++){
for(int j = 0; j < a.v.length; j++){
/* Multiplying the Vector with the Matrix.
* (x) [a d g] (a) (d) (g)
* (y) * [b e h] = x * (b) + y * (e) + z * (h)
* (z) [c f i] (c) (f) (i)
* (x*a + y*d + z*g)
* = (x*b + y*e + z*h)
* (x*c + y*f + z*i)
*/
res[i] += a.v[i][j] * v.getVector(j);
}
}
Vector r = new Vector(res[0], res[1]); //Copying the result which represents the new pixel location into an Vector
return r;
}
}
public class Vector {
private int[] v;
Vector(int x, int y){
v = new int[3]; //We'll always have a 3 Vector...
v[0] = x;
v[1] = y;
v[2] = 1;
// System.out.println("NEW VECTOR " + v[0] + " "+ v[1]);
}
Vector(){
v = new int[3];
v[0] = 0;
v[1] = 0;
v[2] = 1;
}
public int getVectorX(){
return v[0];
}
public int getVectorY(){
return v[1];
}
public int getVectorZ(){
return v[2];
}
public void setVector(int i, double d){
v[i] = (int)d;
}
public int getVector(int i){
return v[i];
}
public void setVectorX(int i){
v[0] = i;
}
public void setVectorY(int i){
v[1] = i;
}
}
public static Matrix rotate(final double a) {
final double rad = Math.PI * a / 180;
final double dM[][] = { { Math.cos(rad), Math.sin(rad), 0 }, { -Math.sin(rad), Math.cos(rad), 0 }, { 0, 0, 1 } };
return new Matrix(dM);
}
// p.mImage = createImage(p.mImgSrc);
final MemoryImageSource mis = new MemoryImageSource(p.W, p.H, mPixelsResult, 0, p.W);
final Toolkit tk = Toolkit.getDefaultToolkit();
p.mImage = tk.createImage(mis);
p.mImage.flush();