Hello guys,
how do I represent Mat (Size size, int type, void *data, size_t step=AUTO_STEP) from docs in python?
Im trying to change C++ code of someone to python, and Im stuck.
Here is the code he wrote:
rs2::frameset frameset = pipe.wait_for_frames();
auto disparity_frame = frameset.get_depth_frame();
Mat mDisparity = Mat(Size(imageW, imageH), CV_16UC1, (void*)disparity_frame.get_data());
please show how you intend to translate the first two lines of your code into python. then we can figure out how the OpenCV/numpy part (line 3) should work.
...
auto disparity_frame = frameset.get_depth_frame();
Mat mDisparity = Mat(Size(imageW, imageH), CV_16UC1,(void*)disparity_frame.get_data());
ofstream outFile("Disparity.raw", ofstream::binary);
outFile.write((char*)mDisparity.data, mDisparity.elemSize() * mDisparity.total());
outFile.close();
I discussed with my senior, he suggested me to create array of zeros and assign row by row:
mDisparity = np.zeros((imageH,imageW),dtype=np.uint16)
data = np.asanyarray(disparity_frame.get_data())
for i in range(imageH):
for j in range(imageW):
mDisparity[i][j]=data[i][j]
But he said it will be slow.
Also, what does mDisparity.data gets? And why is he multiplying elemSize with total number of elements in array? Im trying to figure out it also.
Hello crackwitz, sorry for late reply!
Thank you for your suggestion!
Although, I couldn’t get the desired image/data from the guy’s script, I could manage to get disparity map using StereoBM_create of opencv.
If you are one of OpenCV developers, I wanted to ask something. Currently, I am studying the book “Learning Opencv with python3” using D435i realsense camera.
There, the author gets disparity map using cv2.CAP_OPENNI_DISPARITY_MAP which is in uint8 data type (chapter 4). I could compute disparity map using Stereo_BM from stereo cameras. But the data type of computation is in int16. When I use cv2.imshow, the image looks like this:
However, when I use pyplot.imshow(disparity,'gray'), the image it shows is what I desire to get.
But still, I cannot use the data I get in the exercise in book because it is in int16. I tried to change the dtype to uint8, but it will look different.
Any suggestions would appreciate! If you need any additional information,please tell me.
P.s. I cannot put more than one image, I wanted to show the pyplot img, and data from compute.