Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV - Adjusting photo with skew angle (tilt)

I have a camera pointing at a Zen Garden from above. However, the camera is fixed on the side rather than directly above the plate. As a result, the image looks like this (note the skewed shape of the rectangle): enter image description here

Is there a way to process the image so that the sand area can look more or less like a perfect square?

cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
    img = cv2.flip(img,0)
    cv2.imshow('Cropping', img)
    if cv2.waitKey(500) & 0xff == 27:
        cv2.destroyAllWindows()
        break

Many thanks.

like image 235
J_yang Avatar asked Nov 03 '15 11:11

J_yang


People also ask

How do I correct image skew With OpenCV?

Here we can see that that input image has a counter-clockwise skew of 4 degrees. Applying our skew correction with OpenCV detects this 4 degree skew and corrects for it. Here is another example, this time with a counter-clockwise skew of 28 degrees: $ python correct_skew.py --image images/neg_28.png [INFO] angle: -28.009

How do I tile a gray scale image in OpenCV?

Below you can see the full code to do the tiling procedure to a gray scale image: img = cv2.imread ('C:/Users/N/Desktop/Test.jpg') If you run the previous code, you should get an output similar to figure 2. Figure 2 – Gray scale images tiled.

How to determine the skew angle of the image?

To determine the skew angle, we compare the maximum difference between peaks and using this skew angle, rotate the image to correct the skew Left (original), Right (corrected)

How to change the orientation of an image in OpenCV?

Rotation of an image for an angle is achieved by the transformation matrix of the form But OpenCV provides scaled rotation with adjustable center of rotation so that you can rotate at any location you prefer. The modified transformation matrix is given by To find this transformation matrix, OpenCV provides a function, cv.getRotationMatrix2D.


1 Answers

You can do perspective transformation on your image. Here's a first shot that gets you in the ballpark:

import cv2
import numpy as np

img = cv2.imread('zen.jpg')
rows, cols, ch = img.shape    
pts1 = np.float32(
    [[cols*.25, rows*.95],
     [cols*.90, rows*.95],
     [cols*.10, 0],
     [cols,     0]]
)
pts2 = np.float32(
    [[cols*0.1, rows],
     [cols,     rows],
     [0,        0],
     [cols,     0]]
)    
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img, M, (cols, rows))
cv2.imshow('My Zen Garden', dst)
cv2.imwrite('zen.jpg', dst)
cv2.waitKey()

enter image description here

You can fiddle more with the numbers, but you get the idea.

Here's some online examples. The first link has a useful plot of peg points that correlate to the transformation matrix:

  • Geometric Transformations of Images
  • 4 Point OpenCV getPerspective Transform Example
  • Learn OpenCV by Examples: Perspective Transform
like image 172
Velimir Mlaker Avatar answered Oct 29 '22 00:10

Velimir Mlaker