Reputation: 11
I'm trying to develop a javascript code using opencv.js, I have python code with the same requirement, I converted many lines but some are very hard to find, please guide me.
last 3 lines from python code unable to find for 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)
Upvotes: 1
Views: 1907
Reputation: 152
In the first line, the code is using the function in imutils python package. If you see the grab_contours method in imutils.convenience file located at https://github.com/jrosebr1/imutils/blob/master/imutils/convenience.py, you can see how it is implemented. This is very simple to implement as a one liner in js.
cnts = (cnts.length == 2) ? cnts[0] : (cnts.length == 3) ? cnts[1] : cnts
In second line, max is the inbuilt function of python for iterating through an iterable and to find the maximum based on the key. This same functionality can be achieved in js as follows
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
}
});
Now for the third line I assume cv2.minAreaRect function is present in the js version too. I'm not sure though. But hope the above code works for you. Thank you.
Upvotes: 2