我正在尝试将Python代码翻译成C++。这是代码:
kernelx = cv2.getStructuringElement(cv2.MORPH_RECT,(2,10))
dx = cv2.Sobel(res,cv2.CV_16S,1,0)
dx = cv2.convertScaleAbs(dx)
cv2.normalize(dx,dx,0,255,cv2.NORM_MINMAX)
ret,close = cv2.threshold(dx,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
close = cv2.morphologyEx(close,cv2.MORPH_DILATE,kernelx,iterations = 1)
contour, hier = cv2.findContours(close,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contour:
x,y,w,h = cv2.boundingRect(cnt)
if h/w > 5:
cv2.drawContours(close,[cnt],0,255,-1)
else:
cv2.drawContours(close,[cnt],0,0,-1)
close = cv2.morphologyEx(close,cv2.MORPH_CLOSE,None,iterations = 2)
closex = close.copy()
我唯一的问题是这条线路:
if h/w > 5:
我很难找到解决办法。
在C++API中,x
、y
、h
和w
是cv::boundingRect()
:返回的cv::Rect
对象的属性
for (size_t i = 0; i < contour.size(); i++)
{
cv::Mat cnt = contour[i];
cv::Rect rect = cv::boundingRect(cnt);
if (rect.height / rect.width > 5)
// ...
else
// ...
}