鱼眼失真 [英] fisheye distortion

查看:140
本文介绍了鱼眼失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了code以下用Java编写的这个以缓冲图像鱼眼镜头效果。是否有可能使用这个code对不同的图像格式,如JPEG或位图?我试着用替换位图的BufferedImage和,而不是使用SET / getARGB我已经替换位图的get / setPixel。我只是得到一个黑色的位图....我知道是有点长镜头:)的。

 导入java.awt.image.BufferedImage中;
进口的java.io.File;
进口java.io.IOException异常;
进口javax.imageio.ImageIO中;
进口com.jhlabs.image.InterpolateFilter;

一流的过滤器{
    浮动的XScale;
    浮动yscale;
    浮动XSHIFT;
    浮动YSHIFT;
    INT [] S;
    公共过滤器(){

    }

    公共BufferedImage的桶(BufferedImage的输入,浮动K){

        浮动的centerX = input.getWidth()/ 2; //失真中心
        浮centerY = input.getHeight()/ 2;

        INT宽度= input.getWidth(); //图像边界
        INT高= input.getHeight();

        BufferedImage的DST =新的BufferedImage(宽度,高度,BufferedImage.TYPE_INT_RGB); //输出峰

          XSHIFT = calc_shift(0,的centerX-1,的centerX,k)的;
          浮newcenterX =宽度的centerX;
          浮xshift_2 = calc_shift(0,newcenterX-1,newcenterX,k)的;

          YSHIFT = calc_shift(0,centerY-1,centerY,k)的;
          浮newcenterY =身高centerY;
          浮yshift_2 = calc_shift(0,newcenterY-1,newcenterY,k)的;

          的XScale =(宽度XSHIFT-xshift_2)/宽度;
          yscale =(身高YSHIFT-yshift_2)/身高;

          对于(INT J = 0; J< dst.getHeight(); J ++){
              的for(int i = 0; I< dst.getWidth();我++){
                浮动X = getRadialX((浮点)我,(浮动)Ĵ,的centerX,centerY,K);
                浮动Y = getRadialY((浮点)我,(浮动)Ĵ,的centerX,centerY,K);
                sampleImage(输入,X,Y);
                INT颜色=((S [1]&安培; 0x0ff)&其中;&所述; 16)|((S [2]&安培; 0x0ff)&其中;&所述; 8)|(S [3]&安培; 0x0ff);
    // System.out.print第(i +,+ J +\\);

                dst.setRGB(I,J,颜色);

              }
            }
        返回DST;
    }

    无效sampleImage(BufferedImage的改编,浮IDX0,浮动IDX1)
    {
        S =新INT [4];
      如果(IDX0℃,|| IDX1℃,|| IDX0>(arr.getHeight() -  1)|| IDX1>(arr.getWidth() -  1)){
        S [0] = 0;
        S [1] = 0;
        S [2] = 0;
        S [3] = 0;
        返回;
      }

      浮动idx0_fl =(浮点)Math.floor(IDX0);
      浮动idx0_cl =(浮点)Math.ceil(IDX0);
      浮动idx1_fl =(浮点)Math.floor(IDX1);
      浮动idx1_cl =(浮点)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);

      浮动X = IDX0  -  idx0_fl;
      浮动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-γ));
      S [1] =(int)的(S1 [1] *(1-X)*(1-y)的+ S2 [1] *(1-X)* Y + S3 [1] * X * Y + S4 [ 1] * X *(1-γ));
      S [2] =(int)的(S1 [2] *(1-X)*(1-y)的+ S2 [2] *(1-X)* Y + S3 [2] * X * Y + S4 [ 2] * X *(1-γ));
      S [3] =(int)的(S1 [3] *(1-X)*(1-y)的+ S2 [3] *(1-X)* Y + S3 [3] * X * Y + S4 [ 3] * X *(1-γ));
    }

    INT [] getARGB(BufferedImage的BUF,诠释的x,int y)对{
        INT RGB = buf.getRGB(X,Y); //返回默认ARGB。
        INT []标量=新INT [4];
        标量[0] =(RGB>>> 24)及0xFF的;
        标量[1] =(RGB>>> 16)及0xFF的;
        标[2] =(RGB>>→8)及0xFF的;
        标[3] =(RGB>>大于0)及0xFF的;
        返回标;
    }

    浮动getRadialX(浮X,浮动Y,浮CX,CY浮动,浮动K){
      X =(X *的XScale + XSHIFT);
      Y =(Y * yscale + YSHIFT);
      浮解析度= X +((X-CX)* K *((X-CX)*(X-CX)+(γ-CY)*(γ-CY)));
      返回水库;
    }

    浮动getRadialY(浮X,浮动Y,浮CX,CY浮动,浮动K){
      X =(X *的XScale + XSHIFT);
      Y =(Y * yscale + YSHIFT);
      浮解析度= Y +((γ-CY)* K *((X-CX)*(X-CX)+(γ-CY)*(γ-CY)));
      返回水库;
    }

    浮脱粒= 1;

    浮动calc_shift(浮动X1,X2浮球,浮球CX,浮K){
      浮X3 =(浮点)(X1 +(X2-X1)* 0.5);
      浮RES1 = X1 +((X1-CX)* K *((X1-CX)*(X1-CX)));
      浮RES3 = X3 +((×3-CX)* K *((×3-CX)*(×3-CX)));

      如果(RES1> -thresh和放大器;&安培; RES1<脱粒)
        返回X1;
      如果(RES3℃,){
        返回calc_shift(×,×2,CX,k)的;
      }
      其他{
        返回calc_shift(X1,X3,CX,k)的;
      }
    }
}
 

解决方案

简短的回答是是的,这是可能的。这将需要一些时间让你翻译的BufferedImage方法的位图的方法,但也有很多很好的参考网络,将能够帮助你。

I've found the code below written in java that places a fisheye effect on a buffered image. Is it possible to use this code on a different image format eg jpeg or bitmap? I've tried replacing the bufferedImage with bitmap and instead of using set/getARGB i've replaced with bitmap's get/setPixel. i just get a black bitmap.... i know was a bit of a long-shot:).

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.jhlabs.image.InterpolateFilter;

class Filters{
    float xscale;
    float yscale;
    float xshift;
    float yshift;
    int [] s;
    public Filters(){

    }

    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_RGB); //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);
      }
    }
}

解决方案

The short answer is "yes, it is possible". It will take some time for you to translate the BufferedImage methods to the Bitmap methods but there are plenty of good references online that will be able to help you out.

这篇关于鱼眼失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆