0

I'm working on an image processing assignment, and we are asked to detect edges of a photo using integral images and specific methods and libraries. Below is the code I wrote: `

import matplotlib.pyplot as plt
from PIL import Image, ImageFilter
import matplotlib
import numpy as np

#Function 1: Given an image, returns the image and it's squared version in array format
def toArrayAndSquare(im):
  img = [[0 for x in range(im.size[0])] for y in range(im.size[1])]
  sqr = [[0 for x in range(im.size[0])] for y in range(im.size[1])]
  for i in range (0,im.size[0]):
    for j in range (0,im.size[1]):
      img[j][i] = im.getpixel((i,j))
      sqr[j][i] = img[j][i] ** 2
  return img,sqr

#Function 2: Given an image, applies a certain threshold
def applyThreshold (im, th):
  res = [[0 for x in range(len(im[0]))] for y in range(len(im))]
  for i in range (0,len(im)):
    for j in range (0,len(im[0])):
      if(im[i][j]<th):
        res[i][j] = 0
      else:
        res[i][j] = 255
  return res

imgArray = toArrayAndSquare(image1)

def integralArray (imagee, k):
  image = np.array(imagee)
  height = len(image)
  width = len(image[0])
  integral_image = [ [ 0 for y in range(width) ] for x in range(height) ]

  for i in range (0, height):
    sum = 0
    for j in range (0, width):
      sum += image[i][j]
      integral_image[i][j] = sum

      if j>0:
        integral_image[i][j] += integral_image[i-1][j]
  output_image = [ [ 0 for y in range(width) ] for x in range(height)]
  for i in range(height):
    for j in range(width):
        min_row, max_row = max( 0, i-k), min( height-1, i+k)
        min_col, max_col = max( 0, j-k), min( width-1, j+k) 
        output_image[i][j] = integral_image[max_row][max_col]

        if min_row > 0:
          output_image[i][j] -= integral_image[min_row-1][max_col]
        
        if min_col > 0:
          output_image[i][j] -= integral_image[max_row][min_col-1]

        if min_col > 0 and min_row > 0:
          output_image[i][j] += integral_image[min_row-1][min_col-1]
  return output_image

image3 = integralArray(imgArrayDouble, 1)


def localSum(imagee, x1, y1, x2, y2):
  image = np.array(imagee)
  topLeft = image[x1][y1]
  bottomRight = image[x2][y2]
  bottomLeft = image[x1][y2]
  topRight = image[x2][y1]
  Local_Sum = topLeft + bottomRight - bottomLeft - topRight
  print(topLeft , bottomRight , bottomLeft , topRight)
  return Local_Sum


def imgWithIntegral(imagee, x, y):
  # Variance = mean of square of image - square of mean of image
  array1 = imagee[0]
  array2 = imagee[1]
  OutputArray = np.array(imagee)
  OutputArray = (np.var(array1))**2 - np.var(array2)
  return OutputArray

`

Now I'm asked to implement another function that will calculate the variance of every pixel using sliding window which is the last method in the code and actually I don't know how it can be done. Any help?

I didn't expect much from it(The last method) as the aim is to create sliding window to calculate the variance of every pixel and I didn't know how to create it.

  • For computing local variance we use the sum and the sum of squares of the "window" around each pixel. Take a look at the [following post](https://stackoverflow.com/a/33497963/4926757). The uniform_filter is also named "boxfilter". Apply boxfilter(img) and boxfilter(img*img), and compute the local variance as `win_var = win_sqr_mean - win_mean*win_mean` – Rotem Nov 24 '22 at 22:12

0 Answers0