Winbuntu
Winbuntu

Reputation: 127

Depth map shows everything grey! OpenCV- Python

My code:

import cv2
import numpy as np

imgL = cv2.imread('Blender_Suzanne1.jpg')
img1 = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
imgR = cv2.imread('Blender_Suzanne2.jpg')
img2 = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)

stereo = cv2.StereoBM_create(numDisparities = 16, blockSize = 17)
disparity = stereo.compute(img2, img1)

cv2.imshow('DepthMap', disparity)
cv2.waitKey()
cv2.destroyAllWindows()

When I run it, I see a window which is all grey? I think it is wrong. I used this code from the OpenCV docs website. Can anyone help?

PS: First I had some error which did not allow the output window to pop up. So, I added the two lines namely img1 and img 2 in my code.

Upvotes: 1

Views: 8184

Answers (2)

Jeru Luke
Jeru Luke

Reputation: 21203

You can display the result disparity using cv2.imshow() as well after you normalize it.

norm_image = cv2.normalize(disparity, None, alpha = 0, beta = 1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)

cv2.imshow('norm_image', norm_image)

enter image description here

Notice the change of data type after normalizing the image. Prior to normalization disparity was of type int16. After normalization it is float32 (mentioned within the function cv2.normalize())

Upvotes: 3

user9753692
user9753692

Reputation:

Instead of using imshow use matplotlib to visualization as per the documentation. Also you can convert image into gray in the same line you read the image as follows.

import cv2
from matplotlib import pyplot as plt

imgL = cv2.imread('Blender_Suzanne1.jpg',0)
imgR = cv2.imread('Blender_Suzanne2.jpg',0)

stereo = cv2.StereoBM_create(numDisparities = 16, blockSize = 17)
disparity = stereo.compute(imgL, imgR)

plt.imshow(disparity,'gray')
plt.show()

Upvotes: 0

Related Questions