I have python code, I want to chang it to C++. How to di it?

img = cv2.imread(image_path)
h, w, _ = img.shape

mser = cv2.MSER_create(_min_area=100, _max_area=400)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
regions, boxes = mser.detectRegions(gray)

boxes = np.array(boxes)
boxes = boxes[boxes[:, 2] > 10]
boxes = boxes[boxes[:, 3] > 10]

boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
boxes[:, 3] = boxes[:, 1] + boxes[:, 3]

xmin = max(np.min(boxes[:, 0]) - 10, 0)
ymin = max(np.min(boxes[:, 1]) - 10, 0)
xmax = min(np.max(boxes[:, 2]) + 10, w)
ymax = min(np.max(boxes[:, 3]) + 10, h)

img = gray[ymin:ymax, xmin:xmax]
cv2.imread(image_path)

You need a way to read an image file. One simple way is to use the stb_image library, see: https://github.com/nothings/stb

boxes = np.array(boxes)

Use an std::vector
https://en.cppreference.com/w/cpp/container/vector

np.min(boxes[:, 0]

The minimum element of a collection can be obtained with std::min_element
See:
https://en.cppreference.com/w/cpp/algorithm/min_element
https://en.cppreference.com/w/cpp/algorithm/max_element

boxes[:, 0]

I'm less familiar with how slicing or this type of array access works. If it's like Matlab, then the alternative in C++ is to just make your own for loops that iterate over and copy elements.

img = gray

Implement a suitable grayscale algorithm by using the formula in https://en.wikipedia.org/wiki/Grayscale (converting color to grayscale section)
I'm sure there's also an image processing library that could do this as well, but I don't know of one off the top of my head, try a search.

I don't know what the rest of the stuff is. My advice is to not try to translate the whole thing at once. Just getting an image to load and printing out the RGB value of the first pixel will be enough of a challenge at first. Start small.
Last edited on
Um... MSER and OpenCV are already C/C++ projects.

Why not spend some time on their sites to see how to use them?
https://docs.opencv.org/trunk/d3/d28/classcv_1_1MSER.html
1
2
3
4
5
boxes = boxes[boxes[:, 2] > 10]
boxes = boxes[boxes[:, 3] > 10]

boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
boxes[:, 3] = boxes[:, 1] + boxes[:, 3]
¿what are you doing there?
Topic archived. No new replies allowed.