Calculate the standard deviation of a moving windows using 2d convolution
I am doing a image processing project.
I want to calculate the standard deviation of a silding window using 2d convolution. I can now calculate the mean, but I cannot find a way to calculate the standard deviation. Here is my code:

import torch
import numpy as np
import cv2
import matplotlib.pyplot as plt
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
torch.set_default_tensor_type('torch.cuda.FloatTensor')
testvideo=cv2.VideoCapture ("test.mp4")
if testvideo.isOpened():
    ret, frame = testvideo.read()
    if ret == True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        imgarr=np.asarray(gray)
        #plt.imshow(imgarr,cmap="gray")
        imgtensor=torch.from_numpy(imgarr).float().to(device).unsqueeze(0).unsqueeze(0)
        print(imgtensor.device)
        #print(intensity.device)
        MeanConv=torch.nn.Conv2d(1, 1, kernel_size=100)
        kernel=(torch.ones(100,100)/10000).unsqueeze(0).unsqueeze(0)
        MeanConv.weight = torch.nn.Parameter(kernel)
        print(kernel)
        intensity=MeanConv(imgtensor)
        plt.imshow(intensity.squeeze(0).squeeze(0).detach().cpu().numpy(),cmap="gray")
        #for i in range(714):
            #for j in range(714):
                #intensity[i,j]=torch.mean(imgtensor[i:i+7,j:j+7])
                #sd[i,j]=torch.std(imgtensor[i:i+7,j:j+7])
        #x=(sd/intensity).cpu()
        #plt.imshow(x.numpy(),cmap="gray")
testvideo.release()


I think it can be somehow like this:


mean (img) using convolution.
Subtract from the original image of p. 1
Calculate the square of each element.
By convolution we find the average, p. 3
Calculates the square root of the elements of p. 4

