You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Ajo Fod <aj...@gmail.com> on 2014/11/11 19:47:04 UTC

[math]: Is something wrong with BicubicInterpolation?

I feel like bicubic interpolation should result in images like:
http://en.wikipedia.org/wiki/Bicubic_interpolation

But, here I'm getting something different:

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import
org.apache.commons.math3.analysis.interpolation.BicubicSplineInterpolatingFunction;
import
org.apache.commons.math3.analysis.interpolation.BicubicSplineInterpolator;

public class Draw {

    static final int STEP = 128;

    public static void main(String[] args) throws IOException {

        final int width = 1024;
        final int height = 512;
        final int totalHt = height + 64;

        // Constructs a BufferedImage of one of the predefined image types.
        final BufferedImage bufferedImage = new BufferedImage(width,
totalHt, BufferedImage.TYPE_INT_RGB);

        // Create a graphics which can be used to draw into the buffered
image
        final Graphics2D g2d = bufferedImage.createGraphics();

        final Random rand = new Random(123);
        final double vx[][] = new double[width / STEP][height / STEP];
        for (int i = 0; i < width / STEP; i++) {
            for (int j = 0; j < height / STEP; j++) {
                final double val = 2 * rand.nextDouble() - 1;
                vx[i][j] = val;
            }
        }
        final double x[] = new double[width / STEP];
        for (int i = 0; i < width / STEP; i++) {
            x[i] = i * STEP;
        }
        final double y[] = new double[height / STEP];
        for (int j = 0; j < height / STEP; j++) {
            y[j] = j * STEP;
        }

        //(1 - yfrac) * [(1 - xfrac)*s00 + xfrac*s01] +
        //yfrac * [(1 - xfrac)*s10 + xfrac*s11]
        final BicubicSplineInterpolatingFunction fun = new
BicubicSplineInterpolator().interpolate(x, y, vx);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (fun.isValidPoint(i, j)) {
                    final float val = (float) fun.value(i, j);
                    final float bx = (float) Math.abs(val);
                    final float hue = val > 0 ? 0.666f : 1;
                    final Color myRGBColor = Color.getHSBColor(hue, bx, 1f);
                    g2d.setColor(myRGBColor);
                    g2d.fillRect(i, j, 1, 1);
                }
            }
        }


        // Disposes of this graphics context and releases any system
resources that it is using.
        g2d.dispose();

        // Save as JPEG
        final File file = new File(System.getProperty("user.home")
+"/myimage.jpg");
        ImageIO.write(bufferedImage, "jpg", file);

    }

}

Re: [math]: Is something wrong with BicubicInterpolation?

Posted by Ajo Fod <aj...@gmail.com>.
I have a feeling that (x,y) being assigned values from (y,x) may be the
cause.

Ajo
On Nov 11, 2014 2:11 PM, "Gilles" <gi...@harfang.homelinux.org> wrote:

> On Tue, 11 Nov 2014 16:10:35 -0500, Hank Grabowski wrote:
>
>> There are known problems with the bicubic interpolation algorithm.  In the
>> current head revision there are now PiecewiseBicubicSpline interpolators
>> that may get you what you need.  Can you try that and see if you are
>> getting the results you expect?
>>
>
> See
>   https://issues.apache.org/jira/browse/MATH-1166
>
> I'm under the impression that this case could shed some light on
> where the bugs lie in the old code...
>
>
> Regards,
> Gilles
>
>  [...]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [math]: Is something wrong with BicubicInterpolation?

Posted by Gilles <gi...@harfang.homelinux.org>.
On Tue, 11 Nov 2014 16:10:35 -0500, Hank Grabowski wrote:
> There are known problems with the bicubic interpolation algorithm.  
> In the
> current head revision there are now PiecewiseBicubicSpline 
> interpolators
> that may get you what you need.  Can you try that and see if you are
> getting the results you expect?

See
   https://issues.apache.org/jira/browse/MATH-1166

I'm under the impression that this case could shed some light on
where the bugs lie in the old code...


Regards,
Gilles

> [...]


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math]: Is something wrong with BicubicInterpolation?

Posted by Hank Grabowski <ha...@applieddefense.com>.
There are known problems with the bicubic interpolation algorithm.  In the
current head revision there are now PiecewiseBicubicSpline interpolators
that may get you what you need.  Can you try that and see if you are
getting the results you expect?

On Tue, Nov 11, 2014 at 1:47 PM, Ajo Fod <aj...@gmail.com> wrote:

> I feel like bicubic interpolation should result in images like:
> http://en.wikipedia.org/wiki/Bicubic_interpolation
>
> But, here I'm getting something different:
>
> import java.awt.Color;
> import java.awt.Font;
> import java.awt.FontMetrics;
> import java.awt.Graphics2D;
> import java.awt.image.BufferedImage;
> import java.io.File;
> import java.io.IOException;
> import java.util.Random;
> import javax.imageio.ImageIO;
> import
>
> org.apache.commons.math3.analysis.interpolation.BicubicSplineInterpolatingFunction;
> import
> org.apache.commons.math3.analysis.interpolation.BicubicSplineInterpolator;
>
> public class Draw {
>
>     static final int STEP = 128;
>
>     public static void main(String[] args) throws IOException {
>
>         final int width = 1024;
>         final int height = 512;
>         final int totalHt = height + 64;
>
>         // Constructs a BufferedImage of one of the predefined image types.
>         final BufferedImage bufferedImage = new BufferedImage(width,
> totalHt, BufferedImage.TYPE_INT_RGB);
>
>         // Create a graphics which can be used to draw into the buffered
> image
>         final Graphics2D g2d = bufferedImage.createGraphics();
>
>         final Random rand = new Random(123);
>         final double vx[][] = new double[width / STEP][height / STEP];
>         for (int i = 0; i < width / STEP; i++) {
>             for (int j = 0; j < height / STEP; j++) {
>                 final double val = 2 * rand.nextDouble() - 1;
>                 vx[i][j] = val;
>             }
>         }
>         final double x[] = new double[width / STEP];
>         for (int i = 0; i < width / STEP; i++) {
>             x[i] = i * STEP;
>         }
>         final double y[] = new double[height / STEP];
>         for (int j = 0; j < height / STEP; j++) {
>             y[j] = j * STEP;
>         }
>
>         //(1 - yfrac) * [(1 - xfrac)*s00 + xfrac*s01] +
>         //yfrac * [(1 - xfrac)*s10 + xfrac*s11]
>         final BicubicSplineInterpolatingFunction fun = new
> BicubicSplineInterpolator().interpolate(x, y, vx);
>         for (int i = 0; i < width; i++) {
>             for (int j = 0; j < height; j++) {
>                 if (fun.isValidPoint(i, j)) {
>                     final float val = (float) fun.value(i, j);
>                     final float bx = (float) Math.abs(val);
>                     final float hue = val > 0 ? 0.666f : 1;
>                     final Color myRGBColor = Color.getHSBColor(hue, bx,
> 1f);
>                     g2d.setColor(myRGBColor);
>                     g2d.fillRect(i, j, 1, 1);
>                 }
>             }
>         }
>
>
>         // Disposes of this graphics context and releases any system
> resources that it is using.
>         g2d.dispose();
>
>         // Save as JPEG
>         final File file = new File(System.getProperty("user.home")
> +"/myimage.jpg");
>         ImageIO.write(bufferedImage, "jpg", file);
>
>     }
>
> }
>