[FEA] Provide a utility function to convert OpenCV image to CuPy array
Open
@gigony is already working on this.
Since Jul 16, 2022.
feature request
- Dominant language
- Jupyter Notebook
- Stars
- 467
- Forks
- 92
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.