abhishek satmar
abhishek satmar

Reputation: 13

I am getting black Laplacian of Guassian image

import cv2
import math

gaussian function

def gaussianblur(img,sigma):
      if(sigma<0):
        print("SIGMA SHOULD BE POSITIVE")
        return;

calculating 1 dimensional kernal with g(x)=(1/squareroot(2*sigma*sigma*3.142)) * e^(-(x*x)/(2*sigma*sigma))

      deno=(((math.sqrt(2*3.142*sigma*sigma))))
      k=[0,0,0,0,0]
      sum=0




      for x in range(-2,3):
       numo=(math.exp(-((x*x)/(2*(sigma*sigma)))))
       k[x+2]=(numo/deno)
       sum=sum+k[x+2]




      for x in range(0,5):
        k[x]=(k[x]/sum)

applying convolution row by row

      for i in range(0,img.shape[0]):
       for j in range(2,img.shape[1]-2):
            img[i,j]=abs((img[i,j-2]*k[0])+(img[i,j-1]*k[1])+(img[i,j]*k[2])+(img[i,j+1]*k[3])+(img[i,j+2]*k[4]))



      return img;  `#end of gaussian blur function`

main function start

read image

dog=img = cv2.imread('art.jpg',cv2.IMREAD_GRAYSCALE)

apply 1 st blur

temp=img=gaussianblur(img,1)

#display image
cv2.imshow('blur1',img)

apply 2nd blur

temp=gaussianblur(temp,1)

cv2.imshow('blur2',temp)

the difference of Gaussian

for i in range(0,img.shape[0]):
    for j in range(0,img.shape[1]):
       dog[i,j]=abs((img[i,j])-(temp[i,j]))

cv2.imshow('DoG',dog)

output

blur1

blur2

DoG

Upvotes: 1

Views: 627

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60444

You are overwriting your input here:

for i in range(0,img.shape[0]):
    for j in range(2,img.shape[1]-2):
        img[i,j]=abs((img[i,j-2]*k[0])+(img[i,j-1]*k[1])+(img[i,j]*k[2])+(img[i,j+1]*k[3])+(img[i,j+2]*k[4]))

Try writing the result to a new image.

I do not know how the OpenCV python interface works, does temp=img cause temp to share the data with img (as in, when you change the one you also change the other)? Make sure you have two different data blocks!

Upvotes: 1

Related Questions