Java Software trapezoidal distortion correction algorithm

As part of my software, I am looking for a trapezoidal distortion correction filter to avoid the tombstone / trapezoidal distortion effect that can be obtained when the projector is displayed at an angle not perpendicular to the screen

At present, I have some work, but the speed is very slow (the whole image is about 100 milliseconds). Ideally, I want faster speed (preferably in the range of 10 milliseconds) Basically, I just cycle through the whole image pixels, copy the pixels I want into a new array by pixels, and then set the RGB content of the new image to this new array:

public BufferedImage getCorrectedImage() {
    double width = originalImage.getWidth(null) * 0.5;
    double increment = (originalImage.getWidth(null) - width)/originalImage.getHeight();

    BufferedImage ret = new BufferedImage(originalImage.getWidth(null),originalImage.getHeight(null),BufferedImage.TYPE_INT_ARGB);

    for (int h = 0; h < originalImage.getHeight(); h++) {
        int[] arr = new int[originalImage.getWidth()];
        for (int w = 0; w < originalImage.getWidth(); w++) {
            arr[w] = originalImage.getRGB(w,h);
        }
        int[] newPixels = getShortLine(arr,(int) (width + 0.5));

        for (int w = 0; w < originalImage.getWidth(); w++) {
            ret.setRGB(w,h,newPixels[w]);
        }
        width += increment;
    }

    return ret;
}

private int[] getShortLine(int[] original,int newSize) {
    int[] newArr = new int[original.length];
    double scale = original.length / newSize;
    int start = (original.length - newSize) / 2;
    int end = original.length - ((original.length - newSize) / 2);
    for (int i = start; i < end-1; i++) {
        newArr[i] = original[(int) ((i - start) * scale)];
    }
    return newArr;
}

What is the best way to do this? A custom affine transformation was originally what I wanted to see, but I couldn't find any code / examples to point me in the right direction Is there a better way to achieve the results I want?

Solution

You can take some steps to speed up your existing code Please note that this will make your code more cluttered, harder to read... And debug However, if the algorithm is running, it may not be too difficult to refactor for better performance:

>Try using the profiler to see if there are any unobvious bottlenecks in the algorithm. > You reposition a new array each time through the loop in the getcorrectedimage function This is a cumulative memory allocation over time To speed up, simply create an array (maximum pixel width / height) and reuse it throughout the life of the function You may need to add some additional variables to track what you actually use. > Try inlining your getshortline method I'm not sure if the JVM will inline it at run time, or if there is a way to check if it exists In any case, if array reuse does not improve your performance, then "manual" inlining may be worth it. > Just note that you can also make this function static Simply pass the originalimage member as a variable This may also have a slight impact on performance, especially for class loading It also makes sense to define it as static, because the only variable seems to be an image and can be easily passed in 1. E. no other class members are dependent It seems that it should be a utility method in a utility class

View ImageJ, written entirely in Java

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>