Search code examples
pythonmatplotlibprojection

matplotlib plot_surface 3D depth values


I used the following code to get the 3D depth projection of the shown 2 images. I need the max and minimum depth values, and the x and y coordinates of these max and min depth values. Is there a function/method from which I can get this information? Even if it will be using a library other than matplotlib.

import cv2
import numpy as np
import  math
import scipy.ndimage as ndimage

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


image2=cv2.imread('D:/Post_Grad/STDF/iPython_notebooks/2228.jpg')
image2 = image2[:,:,1] # get the first channel
rows, cols = image2.shape
x, y= np.meshgrid(range(cols), range(rows)[::-1])

blurred = ndimage.gaussian_filter(image2,(5, 5))
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(221)
ax.imshow(image2, cmap='gray')
ax = fig.add_subplot(222, projection='3d')
ax.elev= 5
f1=ax.plot_surface(x,y,image2, cmap=cm.jet)
ax = fig.add_subplot(223)
ax.imshow(blurred, cmap='gray')
ax = fig.add_subplot(224, projection='3d')
ax.elev= 5
f2=ax.plot_surface(x,y,blurred, cmap=cm.jet)
plt.show()

enter image description here


Solution

  • max depth and min depth are just maximum and minimum pixel values of image. And you can easily find the values via np.max(image2),np.min(image2) etc.. Also coordinates can be found via a simple function

    def getCoord(image,val):
        coords = []
        for i in range(image.shape[0]):
            for j in range(image.shape[1]):
                if image[i][j] == val:
                    coords.append([i,j])
        return coords
    

    so getCoord(image2,np.max(image2)) will return all highest pixel coordinates in image2 (it can be more than 1) , getCoord(blurred,np.min(blurred)) will return all lowest pixel coordinates in blurred etc..