I found some code in python to prepare an image for scanning using opencv. This code works, but now I want to covert it to objective c++ (or c++) to implement its use on the IOS devices. I converted most of it manually, but am now stuck. I can't figure out what the method rectify does and how it would look like in c++. Somebody please help.
Code:
# get approximate contour
for c in contours:
p = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * p, True)
if len(approx) == 4:
target = approx
break
# mapping target points to 800x800 quadrilateral
approx = rect.rectify(target)
pts2 = np.float32([[0,0],[800,0],[800,800],[0,800]])
M = cv2.getPerspectiveTransform(approx,pts2)
dst = cv2.warpPerspective(orig,M,(800,800))
Rectify Function:
import numpy as np
def rectify(h):
h = h.reshape((4,2))
print h
hnew = np.zeros((4,2),dtype = np.float32)
add = h.sum(1)
print add
hnew[0] = h[np.argmin(add)]
hnew[2] = h[np.argmax(add)]
diff = np.diff(h,axis = 1)
hnew[1] = h[np.argmin(diff)]
hnew[3] = h[np.argmax(diff)]
return hnew
↧