我下面的代码行在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 ++中进行转换。请帮忙。谢谢
答案 0 :(得分:1)
python-> 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,宽度,高度,行,校准值,以确保我没有混淆轴