PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 30, 2022

[FIXED] How to straight an image by selecting four points in an Image?

 July 30, 2022     image, image-processing, java, opencv     No comments   

Issue

I want to straight an image by selecting four points in the image using getPerspectiveTransform() method of opencv in java. I know it can be done using with opencv in python: getPerspectiveTransform. If anyone used this to achieve image straightening ..please help.

enter image description here


Solution

There is perspective transformation which can be used to achieve quad to quad conversion. In OpenCV, there are mainly two methods to achieve getPerspectiveTransformation() and warpPerspective() method.

Here is a sample code to achieve Image Straightening:

First get four quadrilinear points in source image

Mat srcImage = Imgcodecs.imread("input.png");
Mat destImage = new Mat(500, 700, srcImage.type());
Mat src = new MatOfPoint2f(new Point(x1, y1), new Point(x2, y2), new Point(x3, y3), new Point(x4, y4));
Mat dst = new MatOfPoint2f(new Point(0, 0), new Point(destImage.width() - 1, 0), new Point(destImage.width() - 1, destImage.height() - 1), new Point(0, destImage.height() - 1));

Getting transformation metrix

Mat transform = Imgproc.getPerspectiveTransform(src, dst);
Imgproc.warpPerspective(srcImage, destImage, transform, destImage.size());

you will get transformed straighten image.

I have tested this code on this image. input.png

and The resulted image is output.png



Answered By - user5954693
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing