In computer graphics and image processing, image skew is an angle deviation of rectangular images. It is also known as image tilt or distortion. The term “skew” refers to the angle of an image. Image skew occurs when the visual position of elements in an image is not uniform. Skewing can result in any corners being at an angle, or even shifted so that one side is longer than the other. Such photos or scanned images can look distorted and unappealing. In this article, we will learn how to detect image skew programmatically. We will also learn how to perform image skew correction in Java.
The following topics shall be covered in this article:
- Java API for Image Skew Correction
- Detect Image Skew Angle
- Detect Image Skew Angle of Live Image
- Automatic Image Skew Correction
- Recognize Image Text with Automatic Skew Correction
- Manual Image Skew Correction
- Recognize Image Text with Manual Image Skew Correction
Java API for Image Skew Correction
For image skew detection and corrections, we will be using the Aspose.OCR for Java API. It allows performing OCR on scanned images, smartphone photos, screenshots, areas of an image, and scanned PDFs. The API also allows saving the recognized text results in popular document formats.
The AsposeOCR class of the API is the main class for recognizes the text from images. It provides the CalcSkewImage(string) method that calculates the skew angle of the provided source image. It also provides another CalcSkewImageFromUri(String uri) method that calculates the skew angle of an image provided by URI link. The PreprocessingFilter class is the base class for image processing commands. The RecognitionSettings class of the API provides settings for the image recognition contains elements that allow customizing the recognition process.
Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-ocr</artifactId>
<version>22.9</version>
</dependency>
Detect Image Skew Angle in Java
We can easily detect the skew angle of a text image by following the steps given below:
- Firstly, create an instance of the AsposeOCR class.
- Call the CalcSkewImage() method. It takes the image path as an argument.
- Finally, show the calculated skew angle.
The following sample code shows how to calculate the skew angle of an image in Java.
Detect Image Skew Angle from Live Image URL
We can also detect the skew angle of an image from a live URL by following the steps given below:
- Firstly, create an instance of the AsposeOCR class.
- Call the CalcSkewImageFromUri() method. It takes the image path as an argument.
- Finally, show the calculated skew angle.
The following sample code shows how to calculate the skew angle of an image in Java.
Automatic Image Skew Correction in Java
We can automatically correct image skew correction by following the steps given below:
- Firstly, create an instance of the AsposeOCR class.
- Next, initialize an object of the PreprocessingFilter class.
- Then, add the AutoSkew() filter.
- After that, call the PreprocessImage() method to apply the filter on the source image.
- Finally, save the corrected image.
The following sample code shows how to apply auto skew correction preprocessing filter in Java.
Recognize Image Text with Automatic Skew Correction in Java
We can apply automatic skew correction and recognize the text by following the steps given below:
- Firstly, create an instance of the AsposeOCR class.
- Next, initialize an object of the RecognitionSettings class.
- Then, call the setAutoSkew with true as an argument.
- After that, call the RecognizePage() method with the source image and RecognitionSettings object.
- Finally, show the recognized text.
The following sample code shows how to apply auto skew correction and recognize the text in Java.
Manual Image Skew Correction in Java
We can define the skew angle manually by rotating the image to the specified degree by following the steps given below:
- Firstly, create an instance of the AsposeOCR class.
- Next, initialize an object of the PreprocessingFilter class.
- Then, add rotate angle using the Rotate() method filter.
- Next, create an instance of the RecognitionSettings class.
- After that, call the PreprocessImage() method to apply the filter on the source image.
- Finally, show the recognized text.
The following sample code shows how to apply manual skew correction with preprocessing filter in Java.
Recognize Image Text with Manual Image Skew Correction in Java
We can define the skew angle manually and recognize the text by following the steps given below:
- Firstly, create an instance of the AsposeOCR class.
- Next, initialize an object of the RecognitionSettings class.
- Then, call the setSkew() method.
- After that, call the RecognizePage() method with the source image and RecognitionSettings object.
- Finally, show the recognized text.
The following sample code shows how to apply manual skew correction and recognize the image text in Java.
Get a Free Evaluation License
You can get a free temporary license to try Aspose.OCR for Java without evaluation limitations.
Conclusion
In this article, we have learned how to:
- perform OCR and recognize text on images;
- apply filters and process images;
- use recognition settings;
- save the corrected images using Java.
Besides image skew correction in Java, you can learn more about Aspose.OCR for Java API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.