 # Is there an elegant way to calculate integral image using PyTorch api？

Hello. I want to calculate the integral image, also known as summed area table of a tensor, is there an efficient way to do it using PyTorch api ?
Thanks !

Hi,

I think you can implement this by doing two `torch.cumsum`, one in each dimension that correspond to the height and width of your image. No?

@albanD Thank you. I found that. It did works. However I realize that a integral image is not what I want…
given a edge image, for every pixel, I need to specify whether the neighbor pixel in a rectangular area is on the same side with that central pixel. Initialy I think integral image can do that, however I found the counter example that it fails… Any suggestion ?

I would say, if you image contains only 0s and 1s where the edges are.
You can put it in a convolution whole kernel size the the rectangular area you want. And all weights are 1 (no bias).
Every point of the output that has a non-zero value has it’s rectangle touching an edge.
Is that good for you?

@albanD Maybe I’ve expressed unclearly. The task is that: in every rectangular center on one pixel, I want to determine those pixel that stand on the same side with the central pixel, and those that don’t stand on the same side with the central pixel. For example, if for one central pixel, there are no edges go across the rectangular corresponding to the pixel, them we can conclude that all the pixel in that rentangular are standing on the same side with the central pixel. if there is a edge goes across the rectangular, them I need to seperate the pixel in the rectangular into two categories. One for those stand on same side with the central pixel, one for the others.

I’m not sure you can do that without having to loop through most of these rectangles to generate these two sets.

@albanD Doing that by customing cuda layer is possible. However it’s not device friendly… Therefore I am trying to figure out whether it can be done by Pytorch api. To generate all the rectangular, I can use `unfold`, but I am not sure whether the task can be accomplished by some calculation, without resorting to cuda layer…

Hello,
Just a small intrusion, If you compute the integral image and than use a binary kernel with ones in the edges and all zeros everywhere else to compute the local sum, it should be the same as directly doing your convolution with all ones.
If you use a Gpu I don’t think you get any advantage at doing convolution this way.