0

I need to create a gaussian kernel, this is the formula:

enter image description here

This is what I've tried, but it returns a black picture.

import matplotlib
import numpy as np
from matplotlib import pyplot as plt, colors


def gaussian_kernel(row, col, mean_v=0, std_v=None, mean_h=0, std_h=None):
    if std_v is None:
        std_v = (row) / 2
    if std_h is None:
        std_h = (col) / 2
    if std_v == 0 or std_h == 0:
        raise Exception("std cannot be 0")
    M = np.zeros((row, col))
    xx = np.array(row)
    yy = np.array(col)
    x, y = np.meshgrid(xx, yy)
    g = (1 / (std_h * std_v * 2 * np.pi)) \
                     * np.exp(-1 * ((((x - mean_h) ** 2) / (2 * (std_h) ** 2))
                                    + (((y - mean_v) ** 2) / (2 * (std_v) ** 2))))
    M[i, j] = g
    return M


g_ker = gaussian_kernel(50, 100, mean_v=-10, std_v=10, mean_h=0, std_h=10)
plt.imshow(g_ker, cmap='gray')
print(g_ker)

plt.show()

joanis
  • 10,635
  • 14
  • 30
  • 40
Nitai
  • 1
  • 2
    Run your statements one by one (using the debugger is a great way to accomplish this, but you can also do it directly in the Python interpreter by manually copy-pasting each line in turn. Then look at the value of each variable. You'll notice for example that `xx` is not an array with coordinates, as required by `meshgrid`, but an array with one single element of value 50. – Cris Luengo Jun 11 '23 at 07:52

0 Answers0