Converting Python code in to C#

Brandon Boone 31 Reputation points
2022-06-20T03:35:26.687+00:00

Hi!,
I am trying to convert this code :

import numpy as np  
  
def gabor(sigma, theta, Lambda, psi, gamma):  
    """Gabor feature extraction."""  
    sigma_x = sigma  
    sigma_y = float(sigma) / gamma  
  
    # Bounding box  
    nstds = 3  # Number of standard deviation sigma  
    xmax = max(abs(nstds * sigma_x * np.cos(theta)), abs(nstds * sigma_y * np.sin(theta)))  
    xmax = np.ceil(max(1, xmax))  
    ymax = max(abs(nstds * sigma_x * np.sin(theta)), abs(nstds * sigma_y * np.cos(theta)))  
    ymax = np.ceil(max(1, ymax))  
    xmin = -xmax  
    ymin = -ymax  
    (y, x) = np.meshgrid(np.arange(ymin, ymax + 1), np.arange(xmin, xmax + 1))  
  
    # Rotation  
    x_theta = x * np.cos(theta) + y * np.sin(theta)  
    y_theta = -x * np.sin(theta) + y * np.cos(theta)  
  
    gb = np.exp(-.5 * (x_theta ** 2 / sigma_x ** 2 + y_theta ** 2 / sigma_y ** 2)) * np.cos(2 * np.pi / Lambda * x_theta + psi)  
    return gb  

in to C#.

This is what I have :

        public double GetGabor(double sigma, double theta, double Lambda,double psi,double gamma)  
        {  
            double sigma_x = sigma;  
            double sigma_y = sigma / gamma;  
            // Bounding box  
            double nstds = 3;  
            double xmax = Math.Max(Math.Abs(nstds * sigma_x * Math.Cos(theta)), Math.Abs(nstds * sigma_y * Math.Sin(theta)));  
            xmax = Math.Ceiling(Math.Max(1, xmax));  
            double ymax = Math.Max(Math.Abs(nstds * sigma_x * Math.Sin(theta)), Math.Abs(nstds * sigma_y * Math.Cos(theta)));  
            ymax = Math.Ceiling(Math.Max(1, ymax));  
            double xmin = -xmax;  
            double ymin = -ymax;  
            (y, x) = Math.meshgrid(Math.arange(ymin, ymax + 1), Math.arange(xmin, xmax + 1));  
            // Rotation  
            double x_theta = x * Math.Cos(theta) + y * Math.Sin(theta);  
            double y_theta = -x * Math.Sin(theta) + y * Math.Cos(theta);  
            double gb = Math.Exp(-0.5 * (Math.Pow(x_theta, 2) / Math.Pow(sigma_x, 2) + Math.Pow(y_theta, 2) / Math.Pow(sigma_y, 2))) * Math.Cos(2 * Math.PI / Lambda * x_theta + psi);  
            return gb;  
        }  
  

but I do not know what :
(y, x) = np.meshgrid(np.arange(ymin, ymax + 1), np.arange(xmin, xmax + 1))
is doing nor how to convert it in to C#

Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-06-20T10:34:30.147+00:00

    Looks like MeshGrid is a named value tuple. So for .NET Framework 4.8, .NET Core x here is what you can try

    var (x, y) = Math.meshgrid(Math.arange(ymin, ymax + 1), Math.arange(xmin, xmax + 1));  
    

    At this point treat x and y as local variables.

    If we were to see what might be going on under the covers (and yes it doesn't really do anything, it's conceptual)

    public class SomeClass  
    {  
        public static (int value1, int value2) meshgrid(int pValue1, int pValue2)   
            => (pValue1 + 1, pValue2 + 1);  
    }  
    

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-06-20T15:08:30.63+00:00

    It is part of a python scientific library that supports multi-dimensional arrays and vectors,

    https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html

    You should be able to to replace NumPy with a MatLab library, which is callable from c#

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.