I am working on a project where I need to implement Image Straightening. I have got an idea to do this. I am rotating the Image on SeekBar as -10 to +10 degrees. It is working by the white background is visible when I rotate. So, We need to implement zoom functionality also so that it looks like image straighten as shown in below. Please advice with your suggestions.
Sample code
float a = (float) Math.atan(bmpHeight/bmpWidth);
// the length from the center to the corner of the green
float len1 = (float) ((bmpWidth/2)/Math.cos(a-Math.abs(curRotate)));
// the length from the center to the corner of the black (^ = power)
float len2 = (float) Math.sqrt((bmpWidth/2)^2 + (bmpHeight/2)^2);
// compute the scaling factor
curScale = len2 / len1;
Matrix matrix = new Matrix();
matrix.postScale(curScale, curScale);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmaprotate, 0, 0, bmpWidth, bmpHeight, matrix, true);
mainImage.setImageBitmap(resizedBitmap);
While in editing mode, tap the Crop button—as indicated by the two interlocking L shapes—and select Straighten from the toolbar that appears. To adjust the photo, swipe your finger up and down until the photo is aligned the way you want it. Once the photo is to your liking, tap Apply Straighten.
How do you straighten a picture? Open Fotor and click "Edit a Photo", then upload your image. Click "Rotate" and drag the Straighten button to adjust your image. Share and Save it as you like.
Go to File > Choose Automate > Crop and Straighten Photos. Photoshop handles this as a batch process. You don't have to select anything manually. It recognizes the scanned image and automatically crops, straightens, and separates each photo into its individual image.
To unskew your own painting, select the perspective tool and make sure to highlight “Crop to Result” in the options window below the toolbox. Then simply grab the corners of your image, line up the edges of your artwork with the dashed lines, and click the “Transform” button to apply.
In the diagram below the green rectangle is the valid part of the rotated image. What we need to determine is the scaling factor which will make the green region the same size as the original image. We can see from the figure that this scaling factor is the ratio of len2
to len1
.
Using the diagram and some basic trigonometry we can find len1
and len2
. The following c-like pseudo code describes the solution.
// theta : the angle of rotation of the image
// width : the width (number of columns) of the image
// height : the height (number of rows) of the image
a = atan(height/width);
// the length from the center to the corner of green region
len1 = (width/2)/cos(a-abs(theta));
// the length from the center to the corner of original image
len2 = sqrt(pow(width/2,2) + pow(height/2,2));
// compute the scaling factor
scale = len2 / len1;
That's it. Assuming all the transformations are done with regard to the center of the image then simply scale the image by the value of scale
after performing the rotation.
Note: the equations provided assume height > width
. Otherwise replace width
with height
in the len1
equation.
Update: Amulya Khare has posted an example implementation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With