尝试将python转换为javascript opencv.js

时间:2019-06-04 10:02:37

标签: javascript html opencv

我正在尝试使用opencv.js开发javascript代码,我具有相同要求的python代码,我转换了很多行,但是有些行很难找到,请指导我。

python代码的最后3行无法找到javascript opencv.js。

def find_marker(image):

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)
    cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    cnts = imutils.grab_contours(cnts) //these three line are unable to find for javascript.
    c = max(cnts, key = cv2.contourArea)
return cv2.minAreaRect(c)

1 个答案:

答案 0 :(得分:0)

在第一行中,代码正在使用imutils python包中的函数。如果您在https://github.com/jrosebr1/imutils/blob/master/imutils/convenience.py的imutils.convenience文件中看到了handle_contours方法,则可以看到它的实现方式。 在js中将它作为一个内衬实现非常简单。

cnts = (cnts.length == 2) ? cnts[0] : (cnts.length == 3) ? cnts[1] : cnts

在第二行中,max是python的内置函数,用于迭代可迭代对象并根据键查找最大值。 可以在js中实现以下相同功能

c = cnts.reduce(function(max, cur) {
    // here key is the cv2.contourArea function,
    // we apply that on the cnts[i] and finds the cnts[i]
    // such that cv2.contourArea(cnts[i]) is maximum
    if(cv2.contourArea(max) < cv2.countourArea(cur)) {
        return cur
    } else {
        return max
    }
});

现在第三行我假设js版本中也存在cv2.minAreaRect函数。我不确定。但是希望以上代码对您有用。谢谢。