gpt4 book ai didi

python - Opencv Python 代码到 C++

转载 作者:太空宇宙 更新时间:2023-11-03 22:55:37 24 4
gpt4 key购买 nike

我正在尝试将 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 中,xyhwcv 的属性cv::boundingRect() 返回的::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
// ...
}

关于python - Opencv Python 代码到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31144378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com