繁体   English   中英

将 cv2 形状从 python 转换为 C++

[英]Convert cv2 shape from python to c++

我在python有以下代码行。 我需要将其转换为等效的c++ ..

lowH = 0
lowS = 150
lowV = 42

highH = 11
highS = 255
highV = 255

crop = 15
height = 40
perc = 23

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

threshold_img = cv2.inRange(hsv, (lowH, lowS, lowV), (highH, highS, highV))

x = 0
y = int(threshold_img.shape[0] * crop / 100)
w = int(threshold_img.shape[1])
h = int(threshold_img.shape[0] * height / 100)
img_cropped = threshold_img[y: y + h, x: x + w]

if cv2.countNonZero(threshold_img) < img_cropped.size * perc / 100:
    print("False")
else:
    print("True")

对于上面的代码,我已经在c++转换了它,如下所示:

int lowH = 0;
int lowS = 150;
int lowV = 42;

int highH = 11;
int highS = 255;
int highV = 255;

int crop = 15;
int height = 40;
int perc = 23;

cv::Mat hsv, threshold_img, img_cropped;

cv::cvtColor(img, hsv, cv::COLOR_BGR2HSV);

cv::inRange(hsv, cv::Scalar(lowH, lowS, lowV), cv::Scalar(highH, highS, highV), threshold_img);

int x = 0;
int y = int(threshold_img.reshape[0] * crop / 100);   <- error
int w = int(threshold_img.reshape[1]);                <- error
int h = int(threshold_img.reshape[0] * height / 100); <- error
img_cropped = threshold_img.resize[y + h, x + w];     <- error

if (cv::countNonZero(threshold_img) < img_cropped.size * perc / 100)
{
    cout << "False" << endl;
}

else
    cout << "TRUE" << endl;

但是上面的代码给出了错误

错误 C2109 下标需要数组或指针类型

还有这个错误

错误(活动)E0349 没有运算符“*”与这些操作数匹配

在线

if (cv::countNonZero(threshold_img) < img_cropped.size * perc / 100)

排队

y = int(threshold_img.shape[0] * crop / 100)

没有可用的shape ,所以我使用了reshape

任何人都可以指导我如何解决这些错误以及python代码中的作者试图实现的目标,以便我可以在c++中轻松转换。 请帮忙。 谢谢

蟒蛇-> C++

threshold_img.shape[0] -> threshold_img.rows 
threshold_img.shape[1] -> threshold_img.cols 

img_cropped.size -> Size() 这个有高度和宽度成员的对象看到

"cv2.countNonZero(threshold_img) < img_cropped.size * perc / 100" 
 countNonZero(threshold_img) < img_cropped.rows * img_cropped.cols * perc /100

img_cropped = threshold_img[y: y + h, x: x + w] 是得到threshold_img从y到y+h和x到x+的子图

img_cropped = threshold_img(Rect(x,y,w,h))

注意地方检查 x、y、宽度、高度、行、cals 值,看看我没有混淆轴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM