1

Looking for some solution for following type of image , I want to make it flat and straighten for further comparison. I tried searching but some solutions here on stack overflow didn't work

import numpy as np
import cv2
from scipy import ndimage


image1 = cv2.imread('image.jpg')
gray=cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray,50,150,apertureSize = 3)

canimg = cv2.Canny(gray, 50, 200)
lines= cv2.HoughLines(canimg, 1, np.pi/180.0, 250, np.array([]))
#lines= cv2.HoughLines(edges, 1, np.pi/180, 80, np.array([]))
for line in lines:
    rho, theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(image1,(x1,y1),(x2,y2),(0,0,255),2)
print(theta)
print(rho)
img_rotated = ndimage.rotate(image1, 180*theta/3.1415926 + 180)
cv2.imshow("img",img_rotated)
cv2.waitKey(0) 
  
#closing all open windows 
cv2.destroyAllWindows() 

This did very slight straightening I am not expert at OpenCV but I it didn't work.

enter image description here

The above code does very slight adjustment. I know it doesnt do any flattening work. I am looking for something related to this.

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93
  • You may want to see [Detecting a sheet of paper / Square Detection](https://stackoverflow.com/questions/8667818/opencv-c-obj-c-detecting-a-sheet-of-paper-square-detection) and [OpenCV Transform Shape with Arbitrary Contour into Rectangle](https://stackoverflow.com/questions/31008791/opencv-transform-shape-with-arbitrary-contour-into-rectangle) based on what you want. Transformation with 4 points is an easy task after detecrion of the points. The example image requires a non-linear approach. You will need [`remap`](https://docs.opencv.org/master/d1/da0/tutorial_remap.html) in that case. – Burak Jun 29 '21 at 14:14
  • 1
    you will not be able to to make *that* flat. it's warped in non-trivial ways. put it on a flat surface, *then* you have a chance. – Christoph Rackwitz Jun 29 '21 at 16:02
  • `warpPerspective` looks like what you need – Yunus Temurlenk Jun 30 '21 at 05:42

0 Answers0