How can I observe the intermediate process of cv2.erode()?

Advertisements

I’ve been observing the results when I apply cv2.erode() with different kernel values. In the code below, it is (3, 3), but it is changed to various ways such as (1, 3) or (5, 1). The reason for this observation is to understand kernel.

I understand it in theory. And through practice, I can see what kind of results I get. But I want to go a little deeper.

I would like to see what happens every time the pixel targeted by kernel changes.

It’s okay if you have thousands of images stored.

How can I observe the intermediate process of cv2.erode()? Am I asking too much?

image = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
_, thresholded_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
inverted_image = cv2.bitwise_not(thresholded_image)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
eroded_image = cv2.erode(inverted_image, kernel, iterations=5)

cv2.imshow('image', eroded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

>Solution :

When you call cv2.erode from Python it eventually gets down to one C++ API call of cv::erode.

As you can see in the documentation, this API does not support inspecting intermediate result from the process. This means it is also not available from the Python wrapper.

The only way you can achieve what you want is to download the C++ source code for opencv (as it is open-source), change it to support inspecting intermediate result (e.g. by adding callbacks, or additional output images), compile it to a library and wrap for Python.
Keep in mind however that doing so is far from being trivial.

Leave a ReplyCancel reply