Giter VIP home page Giter VIP logo

Comments (9)

henon avatar henon commented on August 11, 2024

Can you make a little snippet that doesn't depend on anything else? Show that it works in Python and that it fails in Numpy.NET. Then I can fix the problem quickly.

from numpy.net.

henon avatar henon commented on August 11, 2024

And no huge data like images please.

from numpy.net.

timiil avatar timiil commented on August 11, 2024

test.py:

import cv2
import numpy as np

img = cv2.imread("green.tif", cv2.IMREAD_GRAYSCALE)

fft_img = np.fft.fft2(img)
fft_img = np.fft.fftshift(fft_img)

print(fft_img.size)
python3 ./test.py

the above code is very work. any kind of image like tif or png is OK .

from numpy.net.

henon avatar henon commented on August 11, 2024

I have here a snippet without cv2 that works in python:

>>> img = np.arange(27).reshape(3,3,3)
>>> img
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> fft_img = np.fft.fft2(img)
>>> fft_img = np.fft.fftshift(fft_img)
>>> fft_img
array([[[  0. +0.j        , -13.5-7.79422863j,   0. +0.j        ],
        [ -4.5-2.59807621j, 198. +0.j        ,  -4.5+2.59807621j],
        [  0. +0.j        , -13.5+7.79422863j,   0. +0.j        ]],

       [[  0. +0.j        , -13.5-7.79422863j,   0. +0.j        ],
        [ -4.5-2.59807621j,  36. +0.j        ,  -4.5+2.59807621j],
        [  0. +0.j        , -13.5+7.79422863j,   0. +0.j        ]],

       [[  0. +0.j        , -13.5-7.79422863j,   0. +0.j        ],
        [ -4.5-2.59807621j, 117. +0.j        ,  -4.5+2.59807621j],
        [  0. +0.j        , -13.5+7.79422863j,   0. +0.j        ]]])
>>>

It also works in C# with Numpy.NET

        [TestMethod]
        public async Task IssueByTimiil()
        {            
            var img = np.arange(27).reshape(3, 3, 3);
            var fft_img = np.fft.fft2(img);
            fft_img = np.fft.fftshift(fft_img);
            Console.WriteLine(fft_img.repr);
        }

So my conclusion is that the data you input is wrong. I guess the shape is wrong because I get the same error in python when I apply fft2 to a one-dimensional array.

from numpy.net.

timiil avatar timiil commented on August 11, 2024

opencv load an image into an struct Image<Gray, byte> , which should be a 'two dimensional matrix', do i need to convert 'Image<Gray, byte>' or 'Mat' into a one-dimensional array to join fftshift method ??

from numpy.net.

henon avatar henon commented on August 11, 2024

Image<Gray, byte> img = new Image<Gray, byte>(args[0]);

var data = img.Mat.GetRawData();
var ndasrc = np.asfarray(data);
var ndaFFT = np.fft.fft2(ndasrc); //failed here ->

By doing a GetRawData you get a 1D array and you create a 1D arra ndasrc from it. I guess you need to reshape it so that it has the dimensions of the image.

from numpy.net.

henon avatar henon commented on August 11, 2024

To be clear you need to do this:

var data = img.Mat.GetRawData();
var ndasrc = np.asfarray(data).reshape(imagew, imageh); 

before doing the fft2

from numpy.net.

timiil avatar timiil commented on August 11, 2024

To be clear you need to do this:

var data = img.Mat.GetRawData();
var ndasrc = np.asfarray(data).reshape(imagew, imageh); 

before doing the fft2

thanks very much, it is all WORK for now:

var file = args.Length > 0 ? args[0] : @"..\..\..\..\green.tif";
Image<Gray, byte> img = new Image<Gray, byte>(file);

var data = img.Mat.GetRawData();
var ndasrc = np.asfarray(data);
var reshape = ndasrc.reshape(img.Height, img.Width);

var ndaFFT = np.fft.fft2(reshape);

var fftShift = np.fft.fftshift(ndaFFT);

...

Thanks you very much.

BTW, hou can i convert the NDArray back to CLR types ?

var file = args.Length > 0 ? args[0] : @"..\..\..\..\green.tif";
Image<Gray, byte> img = new Image<Gray, byte>(file);

var data = img.Mat.GetRawData();
var ndasrc = np.asfarray(data);
var reshape = ndasrc.reshape(img.Height, img.Width);

byte[,] backraw = null;  //which API can make reshape convert (boxing) back to backraw ??

from numpy.net.

henon avatar henon commented on August 11, 2024

Getting the data of an NDarray as a C# array is documented here: https://github.com/SciSharp/Numpy.NET#create-a-numpy-array-from-a-c-array-and-vice-versa

from numpy.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.