[java] How to do perspective fixing?

I'm searching for a fast way to fix perspective of a picture given in java or any language.And currently i really don't have any idea how to do it, nor find anything useful in Google.

Input: Point[4] , Color[][]

Output: Perspective-Fixed Color[][]

By Perspective Fixing, i meant the one in Photoshop. Just Like:

I^d appreciate it if you tell me how the code piece works since i want to understand the logic.

This question is related to java math logic perspective

The answer is


The simple solution is to just remap coordinates from the original to the final image, copying pixels from one coordinate space to the other, rounding off as necessary -- which may result in some pixels being copied several times adjacent to each other, and other pixels being skipped, depending on whether you're stretching or shrinking (or both) in either dimension. Make sure your copying iterates through the destination space, so all pixels are covered there even if they're painted more than once, rather than thru the source which may skip pixels in the output.

The better solution involves calculating the corresponding source coordinate without rounding, and then using its fractional position between pixels to compute an appropriate average of the (typically) four pixels surrounding that location. This is essentially a filtering operation, so you lose some resolution -- but the result looks a LOT better to the human eye; it does a much better job of retaining small details and avoids creating straight-line artifacts which humans find objectionable.

Note that the same basic approach can be used to remap flat images onto any other shape, including 3D surface mapping.


Questions with java tag:

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error Instantiating a generic type When to create variables (memory management) java doesn't run if structure inside of onclick listener String method cannot be found in a main class method Are all Spring Framework Java Configuration injection examples buggy? Calling another method java GUI I need to know how to get my program to output the word i typed in and also the new rearranged word using a 2D array Java and unlimited decimal places? Read input from a JOptionPane.showInputDialog box Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable Two Page Login with Spring Security 3.2.x Hadoop MapReduce: Strange Result when Storing Previous Value in Memory in a Reduce Class (Java) Got a NumberFormatException while trying to parse a text file for objects Best way for storing Java application name and version properties Call japplet from jframe FragmentActivity to Fragment Comparing two joda DateTime instances Maven dependencies are failing with a 501 error IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Why am I getting Unknown error in line 1 of pom.xml? Gradle: Could not determine java version from '11.0.2' Error: Java: invalid target release: 11 - IntelliJ IDEA Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Why is 2 * (i * i) faster than 2 * i * i in Java? must declare a named package eclipse because this compilation unit is associated to the named module How do I install Java on Mac OSX allowing version switching? How to install JDK 11 under Ubuntu? Java 11 package javax.xml.bind does not exist IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Difference between OpenJDK and Adoptium/AdoptOpenJDK OpenJDK8 for windows How to allow all Network connection types HTTP and HTTPS in Android (9) Pie? Find the smallest positive integer that does not occur in a given sequence Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 How to uninstall Eclipse? Failed to resolve: com.google.firebase:firebase-core:16.0.1 How to resolve Unable to load authentication plugin 'caching_sha2_password' issue

Questions with math tag:

How to do perspective fixing? How to pad a string with leading zeros in Python 3 How can I use "e" (Euler's number) and power operation in python 2.7 numpy max vs amax vs maximum Efficiently getting all divisors of a given number Using atan2 to find angle between two vectors How to calculate percentage when old value is ZERO Finding square root without using sqrt function? Exponentiation in Python - should I prefer ** operator instead of math.pow and math.sqrt? How do I get the total number of unique pairs of a set in the database? To the power of in C? JavaScript math, round to two decimal places Rounding BigDecimal to *always* have two decimal places What do these operators mean (** , ^ , %, //)? Rotating a Vector in 3D Space finding multiples of a number in Python What does the ^ (XOR) operator do? I want to calculate the distance between two points in Java How do I determine whether my calculation of pi is accurate? What's the difference between “mod” and “remainder”? How to make rounded percentages add up to 100% Round float to x decimals? Calculating distance between two points (Latitude, Longitude) Creating all possible k combinations of n items in C++ Float sum with javascript How to find prime numbers between 0 - 100? Divide a number by 3 without using *, /, +, -, % operators How to test if a double is an integer Java, How to specify absolute value and square roots How do I compute derivative using Numpy? How can I convert radians to degrees with Python? How to find the Center Coordinate of Rectangle? How can I get sin, cos, and tan to use degrees instead of radians? Math operations from string Evenly distributing n points on a sphere How do I calculate square root in Python? Why is the Fibonacci series used in agile planning poker? Python Math - TypeError: 'NoneType' object is not subscriptable C: how to break apart a multi digit number into separate variables? What's the best practice to round a float to 2 decimals? Python math module Why do Python's math.ceil() and math.floor() operations return floats instead of integers? Make a number a percentage less than 10 add 0 to number Show a leading zero if a number is less than 10 Calculating powers of integers How do you calculate the variance, median, and standard deviation in C++ or Java? Math.random() explanation Java math function to convert positive int to negative and negative to positive? milliseconds to days

Questions with logic tag:

How to do perspective fixing? What is the optimal algorithm for the game 2048? ReferenceError: Invalid left-hand side in assignment Prolog "or" operator, query Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four) JQuery .hasClass for multiple values in an if statement AND/OR in Python? Simple 'if' or logic statement in Python 1 = false and 0 = true? Reference — What does this symbol mean in PHP? How to do while loops with multiple conditions

Questions with perspective tag:

How to do perspective fixing?