2

The following picture will illustrate what i want.

Image contain 4 reference rectangle which is base for image deskewing. Assume, Image contain some other information like text, circle and rectangle. Now, I want to write a script to straighten the image based on four rectangle.my resultant image should be straighten. So i can extract some information after deskewing it. I am using OpenCV python, please tell me a way to accomplish this. Kindly show some code as examples of OpenCV Python.enter image description here after that it must be like enter image description here

Pandey Rashmi
  • 147
  • 1
  • 4
  • 14
  • Find Homography matrix using opencv's , `findHomography` function with matching points obtained from reference rectangles then use `warpPerspective`to deskew it. – Optimus 1072 Dec 27 '16 at 08:00
  • @Optimus 1072 it's really gud solution but homography is for fixed point like source to destination. In my case four reference rectangle is not fixed as the image changes. Because image template has similar rectangle but not a fixed point. So i need some code/approach to deskew based that only. – Pandey Rashmi Dec 27 '16 at 10:34
  • I think I haven't understand your question properly yet. Do you need algorithm to find the target area, if not then Homography will work because you can find the target four rectangles in four corners(destination), otherwise the question is not about deskewing. – Optimus 1072 Dec 27 '16 at 12:26
  • Actual scenario is: 1] one evaluation sheet contain 4 filled black square.Inside that some information is like student's roll number,subject is there. 2]this sheet is first scanned by scanner with 200 dpi and store in folder. 3]now this sheet is evaluated by online by fetching roll no. and subject 4] before fetching such information scanned image must be proper aligned else information won't be fetched by OCR properly. analyzing the image i found that image has 4 rectangle which can help to deskew image. As i am new in opencv with python i am not able to get solution. – Pandey Rashmi Dec 28 '16 at 05:32
  • So those 4 rectangles are at same position or different position in different images. If they are at same Homography is the solution if they are at different then you can pick one corner of the skewed image and define the destination(corrected) corners by giving width and height (which I guess would be same every time). – Optimus 1072 Dec 28 '16 at 06:11
  • For same template image, squares will be at fixed position. As template changes according to university location of square also change based on landscape or A4 template. how can i initially detect square or find position? – Pandey Rashmi Dec 28 '16 at 06:43
  • finding square is a different question ill suggest you to start another thread for this. – Optimus 1072 Dec 28 '16 at 06:59
  • sorry. It's rectangle. – Pandey Rashmi Dec 28 '16 at 10:18
  • google search gave me following result http://stackoverflow.com/questions/1817442/how-to-recognize-rectangles-in-this-image & http://stackoverflow.com/questions/40714646/how-to-find-rectangle-shape-in-this-image-using-python. Try these to detect rectangles than apply Homography for deskewing – Optimus 1072 Dec 28 '16 at 10:36

1 Answers1

-1

I finally solved it.

contours, hierarchy = cv2.findContours(img_dilate.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

points=[]
i=0
for contour in contours:
    area = cv2.contourArea(contour)
    if area > 10 and area < 800:
        x,y,w,h = cv2.boundingRect(contour)
        cv2.rectangle(img_dilate,(x,y),(x+w,y+h),(0,255,0),2)
        center = (int(x),int(y))
        position = (center[0], center[1])
        points.append(position)
        print position,i
        text_color = (255,0,255)
        cv2.putText(resized,str(i+1), position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 2)
        i=i+1
pnts = np.array(points)
rect = cv2.minAreaRect(pnts)
box = cv2.cv.BoxPoints(rect)
box = np.int0(box)
cv2.drawContours(resized,[box],0,(0,255,0),2)

root_mat = cv2.getRotationMatrix2D(rect[0], angle , 1)
rotated = cv2.warpAffine(resized, root_mat, dim, flags=cv2.INTER_CUBIC)
cv2.getRectSubPix(rotated, dim, center) 
Pandey Rashmi
  • 147
  • 1
  • 4
  • 14