Matlab Gradient of depth image


I would like to know if it is possible to calculate the gradient of a depth image, given a depth array in Matlab, (i.e. calculate the distance between connected pixels, 2D array cells, in the depth array).

Since each pixel, contains the distance at witch that pixel is from the camera this should be possible to do with the Matlab [FX,FY]=gradient(F) function, I think.

The problem is that I’m getting this error:

>> [Fx,Fy]=gradient(depth_array)
Error using  ./ 
Integers can only be combined with integers of the same class, or scalar doubles.

Error in gradient (line 67)
   g(2:n-1,:) = (f(3:n,:)-f(1:n-2,:)) ./ (h(3:n) - h(1:n-2));

I would really appreciate if someone could give me a hand. Google doesn’t have much information on this topic, at leats that I could found.


My input has the following format:480×640 uint16
Sample of the input:

    1       2       3       4 < (column numbers) 
1 1077    1077    1080    1080
2 1073    1073    1080    1080
3 1073    1073    1073    1073
4 1073    1073    1073    1073
5 1073    1073    1073    1073
6 1073    1073    1073    1073
7 1073    1073    1073    1073
^(row numbers)


I would first check what format your input has, and then place a breakpoint in line 67 to check what happpens there?
I have had an input sample to the original question. The erro on line 67 is inside a Matlab function, how could I set a breakpoint there ?
– Sebastião
4 hours ago
Click with the mouse on the error message (line 67) matlab will open the function at this line and you place the breakpoint.



Taking into account the answer from Piglet and your comment to it I think you want to use function diff.

Fx = diff(depth_array'); Fx = Fx'
Fy = diff(depth_array)

Fx =

 0     3     0
 0     7     0
 0     0     0
 0     0     0
 0     0     0
 0     0     0
 0     0     0

Fy =

-4    -4     0     0
 0     0    -7    -7
 0     0     0     0
 0     0     0     0
 0     0     0     0
 0     0     0     0

notice that size of the output matrices is reduced by one in the corresponding direction.

The difference from gradient is that gradient provides mean value between differences in the up and down directions. As I understand it you don’t want that but rather want a simple difference.


Thank you very much. I think that this is what I was looking! I just need to figure out how to apply this to the rest of my algorithm, but that should not be a problem.
– Sebastião
3 hours ago


From the Matlab reference:

The input type to gradient must be either single or double. Not uint16.

Try converting your data to one of these types using double(X) or single(X)


Oh I did’t saw that part. This actually made ir run. But I not sure if the output is what I would like it to be. What I would like is something that can calculate the distance (actually just a subtractions) between one pixel and the ones connected to them. Maybe gradient is not the write function…
– Sebastião
4 hours ago
@Sebastião maybe give a result you expect for the sample input you provided?