Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';不要在Java中对我的图像使用鱼眼镜头效果_Java_Distortion_Fisheye - Fatal编程技术网

Can';不要在Java中对我的图像使用鱼眼镜头效果

Can';不要在Java中对我的图像使用鱼眼镜头效果,java,distortion,fisheye,Java,Distortion,Fisheye,我想在Java中使用,但无法实现main方法 在的底部,我找到了实现fisheye算法的java代码,但没有任何main()方法。我曾尝试自己实现main()方法,但到目前为止,我只能显示图像,但我不能在该图像上使用barrel()方法,这会在我的图像上产生鱼眼效果 请更正我的主要方法,或者实现一个新方法,加载一些图像(BuffereImage),然后在该图像上使用barrel()方法,好吗 到目前为止,我的代码是: import java.awt.FlowLayout; import java

我想在Java中使用,但无法实现main方法

在的底部,我找到了实现fisheye算法的java代码,但没有任何main()方法。我曾尝试自己实现main()方法,但到目前为止,我只能显示图像,但我不能在该图像上使用barrel()方法,这会在我的图像上产生鱼眼效果

请更正我的主要方法,或者实现一个新方法,加载一些图像(BuffereImage),然后在该图像上使用barrel()方法,好吗

到目前为止,我的代码是:

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class DisplayImage {
float xscale;
float yscale;
float xshift;
float yshift;
int [] s;                             


public BufferedImage barrel (BufferedImage input, float k){

    float centerX=input.getWidth()/2; //center of distortion
    float centerY=input.getHeight()/2;

    int width = input.getWidth(); //image bounds
    int height = input.getHeight();

    BufferedImage dst = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB); //output pic

      xshift = calc_shift(0,centerX-1,centerX,k);
      float newcenterX = width-centerX;
      float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,k);

      yshift = calc_shift(0,centerY-1,centerY,k);
      float newcenterY = height-centerY;
      float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,k);

      xscale = (width-xshift-xshift_2)/width;
      yscale = (height-yshift-yshift_2)/height;

      for(int j=0;j<dst.getHeight();j++){
          for(int i=0;i<dst.getWidth();i++){
            float x = getRadialX((float)i,(float)j,centerX,centerY,k);
            float y = getRadialY((float)i,(float)j,centerX,centerY,k);
            sampleImage(input,x,y);
            int color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
//            System.out.print(i+" "+j+" \\");

            dst.setRGB(i, j, color);

          }
        }
    return dst;
}

void sampleImage(BufferedImage arr, float idx0, float idx1)
{
    s = new int [4];
  if(idx0<0 || idx1<0 || idx0>(arr.getHeight()-1) || idx1>(arr.getWidth()-1)){
    s[0]=0;
    s[1]=0;
    s[2]=0;
    s[3]=0;
    return;
  }

  float idx0_fl=(float) Math.floor(idx0);
  float idx0_cl=(float) Math.ceil(idx0);
  float idx1_fl=(float) Math.floor(idx1);
  float idx1_cl=(float) Math.ceil(idx1);

  int [] s1 = getARGB(arr,(int)idx0_fl,(int)idx1_fl);
  int [] s2 = getARGB(arr,(int)idx0_fl,(int)idx1_cl);
  int [] s3 = getARGB(arr,(int)idx0_cl,(int)idx1_cl);
  int [] s4 = getARGB(arr,(int)idx0_cl,(int)idx1_fl);

  float x = idx0 - idx0_fl;
  float y = idx1 - idx1_fl;

  s[0]= (int) (s1[0]*(1-x)*(1-y) + s2[0]*(1-x)*y + s3[0]*x*y + s4[0]*x*(1-y));
  s[1]= (int) (s1[1]*(1-x)*(1-y) + s2[1]*(1-x)*y + s3[1]*x*y + s4[1]*x*(1-y));
  s[2]= (int) (s1[2]*(1-x)*(1-y) + s2[2]*(1-x)*y + s3[2]*x*y + s4[2]*x*(1-y));
  s[3]= (int) (s1[3]*(1-x)*(1-y) + s2[3]*(1-x)*y + s3[3]*x*y + s4[3]*x*(1-y));
}

int [] getARGB(BufferedImage buf,int x, int y){
    int rgb = buf.getRGB(x, y); // Returns by default ARGB.
    int [] scalar = new int[4];
    scalar[0] = (rgb >>> 24) & 0xFF;
    scalar[1] = (rgb >>> 16) & 0xFF;
    scalar[2] = (rgb >>> 8) & 0xFF;
    scalar[3] = (rgb >>> 0) & 0xFF;
    return scalar;
}

float getRadialX(float x,float y,float cx,float cy,float k){
  x = (x*xscale+xshift);
  y = (y*yscale+yshift);
  float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
  return res;
}

float getRadialY(float x,float y,float cx,float cy,float k){
  x = (x*xscale+xshift);
  y = (y*yscale+yshift);
  float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
  return res;
}

float thresh = 1;

float calc_shift(float x1,float x2,float cx,float k){
  float x3 = (float)(x1+(x2-x1)*0.5);
  float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
  float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));

  if(res1>-thresh && res1 < thresh)
    return x1;
  if(res3<0){
    return calc_shift(x3,x2,cx,k);
  }
  else{
    return calc_shift(x1,x3,cx,k);
  }
}



public static void main(String avg[]) throws IOException
{
    BufferedImage img=ImageIO.read(new File("image.jpg"));       
    ImageIcon icon=new ImageIcon(img);   
    JFrame frame=new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setSize(625,648);
    JLabel lbl=new JLabel();
    lbl.setIcon(icon);
    frame.add(lbl);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}
导入java.awt.FlowLayout;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
公共类显示图像{
浮动xscale;
浮标;
浮动X档;
浮动换档;
int[]s;
公共BuffereImage存储桶(BuffereImage输入,浮点k){
float centerX=input.getWidth()/2;//变形中心
float centerY=input.getHeight()/2;
int width=input.getWidth();//图像边界
int height=input.getHeight();
BuffereImage dst=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_ARGB);//输出图片
xshift=计算移位(0,centerX-1,centerX,k);
浮动新中心x=宽度中心x;
float xshift_2=计算移位(0,newcenterX-1,newcenterX,k);
yshift=计算移位(0,centerY-1,centerY,k);
浮动新中心Y=高度中心Y;
float yshift_2=计算移位(0,newcenterY-1,newcenterY,k);
xscale=(宽度-xshift-xshift_2)/width;
yscale=(高度-YSShift-YSShift_2)/高度;
对于(int j=0;j 16)&0xFF;
标量[2]=(rgb>>>8)&0xFF;
标量[3]=(rgb>>>0)&0xFF;
返回标量;
}
浮点getRadialX(浮点x、浮点y、浮点cx、浮点cy、浮点k){
x=(x*xscale+xshift);
y=(y*yscale+yshift);
浮点数res=x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy));
返回res;
}
浮点getRadialY(浮点x、浮点y、浮点cx、浮点cy、浮点k){
x=(x*xscale+xshift);
y=(y*yscale+yshift);
浮点数res=y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy));
返回res;
}
浮动脱粒=1;
浮动计算移位(浮动x1、浮动x2、浮动cx、浮动k){
浮动x3=(浮动)(x1+(x2-x1)*0.5);
浮点数res1=x1+((x1-cx)*k*((x1-cx)*(x1-cx));
浮点数res3=x3+((x3-cx)*k*((x3-cx)*(x3-cx));
如果(res1>-thresh&&res1


它包含了本文结尾的概念和工作源。

使您的函数保持静态

public static BufferedImage barrel (BufferedImage input, float k)
改变

public static void main(String avg[]) throws IOException
{ 
    BufferedImage img=ImageIO.read(new File("image.jpg"));

    //now pass this image to your barrel method and return in new image
    //note that, method can be called now without instatntiating any object, as it is static now
    BufferImage barrelImage = barrel(img,2.0);       
    ImageIcon icon=new ImageIcon(barrelImage);   
    ...
    ...
}

因此,我最终实现了所需的fisheye!只是因为和站点。我在这里为任何有类似问题的人提供了非常混乱但有效的代码:

package fisheye;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class DitherMain {

   static String imageString = "image.bmp";

   BufferedImage startImage, endImage;
   int[] startPixels, endPixels, imR, imG, imB;
   int width, height;

   public static void main(String[] args){
      new DitherMain(loadImage(imageString));
   }

   //this object transforms the old image and writes the new image
   DitherMain(BufferedImage img){
      //filing the image into startpixels
      startImage = img;
      width = img.getWidth();
      height = img.getHeight();
      startPixels = new int[width*height];
      img.getRGB(0, 0, width, height, startPixels, 0, width);

      endPixels = fisheye(startPixels, width, height);

      transformPixels();

      WritableRaster wRaster = endImage.getData().createCompatibleWritableRaster();

      wRaster.setSamples(0, 0, width, height, 0, imR);
      wRaster.setSamples(0, 0, width, height, 1, imG);
      wRaster.setSamples(0, 0, width, height, 2, imB);

      endImage.setData(wRaster);
      System.out.println(endImage);

      //writing the file for endImage into the harddrive
      writeFile();
   }

   void transformPixels(){
      //endPixels = startPixels;
      endImage = startImage;

      imR = new int[endPixels.length];
      imG = new int[endPixels.length];
      imB = new int[endPixels.length];

      for(int i = 0; i < endPixels.length; i++){
         imR[i] = (endPixels[i] >> 16) & 0x000000FF;
         imG[i] = (endPixels[i] >> 8) & 0x000000FF;
         imB[i] = endPixels[i] & 0x000000FF;
      }
   }

   void writeFile(){
      try {
         ImageIO.write(endImage,"BMP",new File("RESULT.bmp"));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   //this method just loads a specific buffered image
   public static BufferedImage loadImage(String fileName){   
      BufferedImage img;

      try{
         img=ImageIO.read(new File("image.bmp"));
         //img = ImageIO.read(DitherMain.class.getResource(fileName));
      } catch (Exception e) {
         e.printStackTrace();
         throw new RuntimeException(e);
      }
      return img;
   }

public static int[] fisheye(int[] srcpixels, double w, double h) {

    /*
     *    Fish eye effect
     *    tejopa, 2012-04-29
     *    http://popscan.blogspot.com
     *    http://www.eemeli.de
     */

    // 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++) {                            
            // 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];    
                    }
                }
            }
        }
    }
    //return result pixels
    return dstpixels;
}    

}
包装鱼眼;
导入java.awt.image.buffereImage;
导入java.awt.image.WritableRaster;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
公共类双色胺{
静态字符串imageString=“image.bmp”;
BuffereImage开始图像、结束图像;
int[]起始像素、结束像素、imR、imG、imB;
int宽度、高度;
公共静态void main(字符串[]args){
新的双色胺(loadImage(imageString));
}
//此对象变换旧图像并写入新图像
双热蛋白(BuffereImage img){
//将图像归档到startpixels中
startImage=img;
宽度=img.getWidth();
高度=img.getHeight();
startPixels=新整数[宽度*高度];
img.getRGB(0,0,宽度,高度,起始像素,0,宽度);
endPixels=鱼眼(起始像素、宽度、高度);
转换像素();
WritableRaster wRaster=endImage.getData().createCompatibleWritableRaster();
wRaster.设置样本(0,0,宽度,高度,0,imR);
wRaster.设置样本(0,0,宽度,高度,1,imG);
包装器设置样本(0,0,宽度,高度,2,imB);
endImage.setData(wRaster);
System.out.println(endImage);
//将endImage的文件写入硬盘驱动器
writeFile();
}
无效转换像素(){
//endPixels=起始像素;
endImage=起始图像;
imR=新整数[endPixels.length];
imG=新整数[endPixels.length];
imB=新整数[endPixels.length];
对于(int i=0;i>16)&0x000000FF;
imG[i]=(endPixels[i]>>8)&0x000000FF;
imB[i]=endPixels[i]&0x000000FF;
}
}
void writeFile(){
试一试{
write(endImage,“BMP”,新文件(“RESULT.BMP”);
}捕获(IOE异常){
e、 printStackTrace();
}
}
//此方法仅加载特定的缓冲图像
公共静态缓冲区映像加载映像(字符串文件名){
缓冲图像img;
试一试{
img=ImageIO.read(新文件(“image.bmp”);
//img=ImageIO.read(DitherMain.class.getResource(文件名));
}捕获(例外e){
e、 printStackTrace();
抛出新的运行时异常(e);
}
返回img;
}
公共静态int[]鱼眼(int[]像素,双w,双h){
/*
*鱼眼效应
*特约帕,2012-04-29
*    http://popscan.blogspot.com
*    http://www.eemeli.de
*/
//创建结果数据
int[]像素=新的int[(int)(w*h)];
//每行

对于(int y=0;y感谢您回答我的问题,但这不起作用。它只显示没有图像的应用程序窗口。您是否尝试更改浮点值?我只是输入了一个随机值。是的,我将其更改为1,也更改为5,但没有任何帮助。我担心该算法存在问题。是的,似乎如此。您可能应该查看一些文档n该函数可用。顺便问一下,您的问题得到回答了吗?谢谢!它看起来更简单,但也没有main()方法:DAdd这是您的答案。