cucim icon indicating copy to clipboard operation
cucim copied to clipboard

[FEA] Provide a utility function to convert OpenCV image to CuPy array

Open gigony opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe. It would be nice to have a utility function in cuCIM Python module so that OpenCV object can be easily converted to CuPy array.

Describe the solution you'd like

Like this(https://github.com/rapidsai/cucim/issues/329#issuecomment-1186088451), provide a utility method/class to handle/exchange OpenCV image data.

class CudaArrayInterface:
    def __init__(self, gpu_mat):
        w, h = gpu_mat.size()
        type_map = {
            cv2.CV_8U: "u1",
            cv2.CV_8UC1: "u1",
            cv2.CV_8UC2: "u1",
            cv2.CV_8UC3: "u1",
            cv2.CV_8UC4: "u1",
            cv2.CV_8S: "i1",
            cv2.CV_16U: "u2", cv2.CV_16S: "i2",
            cv2.CV_32S: "i4",
            cv2.CV_32F: "f4", cv2.CV_64F: "f8",
        }
        self.__cuda_array_interface__ = {
            "version": 3,
            "shape": (h, w, gpu_mat.channels()),
            "typestr": type_map[gpu_mat.type()],
            "descr": [("", type_map[gpu_mat.type()])],
            "stream": 1,
            "strides": (gpu_mat.step, gpu_mat.elemSize(), gpu_mat.elemSize1()),
            "data": (gpu_mat.cudaPtr(), False),
        }

Describe alternatives you've considered

  • Can be implemented at C++ level but providing it at Python level is easy to implement.

Additional context

  • https://github.com/rapidsai/cucim/issues/329

gigony avatar Jul 16 '22 05:07 gigony