Ask Your Question
0

Converting python opencv code to c++ problem!

asked 2017-12-31 01:28:28 -0600

rezaee gravatar image

I am trying to convert this piece of python code to c++:

import cv2
import numpy as np

##(1) read into  bgr-space
img = cv2.imread("test2.jpg")

##(2) convert to hsv-space, then split the channels
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

##(3) threshold the S channel using adaptive method(`THRESH_OTSU`) or fixed thresh
th, threshed = cv2.threshold(s, 50, 255, cv2.THRESH_BINARY_INV)

##(4) find all the external contours on the threshed S
_, cnts, _ = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
canvas  = img.copy()
#cv2.drawContours(canvas, cnts, -1, (0,255,0), 1)

## sort and choose the largest contour
cnts = sorted(cnts, key = cv2.contourArea)
cnt = cnts[-1]

## approx the contour, so the get the corner points
arclen = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.02* arclen, True)
cv2.drawContours(canvas, [cnt], -1, (255,0,0), 1, cv2.LINE_AA)
cv2.drawContours(canvas, [approx], -1, (0, 0, 255), 1, cv2.LINE_AA)

## Ok, you can see the result as tag(6)
cv2.imwrite("detected.png", canvas)

But from threshold line, I don't know how to get the outputs? Actually I don't know what are the outputs? integer? Mat?

edit retag flag offensive close merge delete

Comments

take a look at this

sturkmen gravatar imagesturkmen ( 2017-12-31 04:49:15 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-12-31 03:41:59 -0600

LBerger gravatar image

updated 2017-12-31 03:57:52 -0600

use doc :

th = threshold(s,threshed,50, 255, THRESH_BINARY_INV);
edit flag offensive delete link more

Comments

Thanks alot! May you help to convert this line too: cnts = sorted(cnts, key = cv2.contourArea) Should I use for loop here?

rezaee gravatar imagerezaee ( 2017-12-31 04:04:12 -0600 )edit

@rezaee Did you look at @sturkmen 's answer. is it enough to your question?

LBerger gravatar imageLBerger ( 2017-12-31 08:04:56 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-31 01:28:28 -0600

Seen: 354 times

Last updated: Dec 31 '17