Python OpenCV Code to C++

asked 2017-09-10 03:21:27 -0500

nicksridhar gravatar image

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
edit retag flag offensive close merge delete

Comments

Please start to write in c++ and run your code. If you get a problem feel free to post a real question about your code in c++

LBerger gravatar imageLBerger ( 2017-09-10 07:36:04 -0500 )edit

I would start to write code but I'm not even sure what the rectify function does. An explanation would be helpful.

nicksridhar gravatar imagenicksridhar ( 2017-09-10 11:23:55 -0500 )edit

Can you add some information about this code, like where you found it, and what it's supposed to do? Also, can you change the title to be more specific (e.g. rectify in c++)

Lorenzo Riano gravatar imageLorenzo Riano ( 2017-09-14 12:09:09 -0500 )edit