Конвертировать форму cv2 из python в c ++ - PullRequest
2 голосов
/ 01 июля 2019

У меня ниже строки кода, который находится в 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 ++.Пожалуйста помоги.Спасибо

1 Ответ

1 голос
/ 01 июля 2019

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, ширину, высоту,строки, cals значения, чтобы увидеть, что я не перепутал ось

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...