Fastest way to do image analysis

I need to calculate a few image statistics from a source colored jpeg loaded. The statistics are:

  1. Calculate color entropy
 histR = new int[256];
 histG = new int[256];
 histB = new int[256];

for each pixel do

 histR[r]++;
 histG[g]++;
 histB[b]++;

then calculate entopy from color histograms

I also need to convert the image to grayscale to do the following on it (8 bits per pixel)
2. I need to calculate minimal gray and maximal gray

  1. I then need to apply Gaussian blur and then Sobel filter to then do some calculations on the resulting pixels

I have implemented all of this on my own based on an initial call to

image.LoadArgb32Pixels

which returns the color pixels which I then turn to grayscale and do the calculations on the grayscale array

I thought about maybe calling the Imaging library APIs would be faster (I need this to be as fast as possible because I need to process many images like that a day). Would it be faster? Can you please share an example of how to implement this using the APIs?

@DanAvni

To perform image analysis efficiently using the Aspose.Imaging library, you can leverage its built-in functions for tasks such as calculating color entropy, converting images to grayscale, applying Gaussian blur, and using the Sobel filter. Below is a step-by-step guide along with code examples to help you implement these functionalities.

1. Calculate Color Entropy

You can calculate the color entropy by first creating histograms for the red, green, and blue channels. Here’s how you can do it:

using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Filters;

// Load the image
using (var image = Image.Load("path/to/your/image.jpg"))
{
    int[] histR = new int[256];
    int[] histG = new int[256];
    int[] histB = new int[256];

    // Iterate through each pixel
    for (int y = 0; y < image.Height; y++)
    {
        for (int x = 0; x < image.Width; x++)
        {
            var pixel = image.GetPixel(x, y);
            histR[pixel.R]++;
            histG[pixel.G]++;
            histB[pixel.B]++;
        }
    }

    // Calculate entropy from histograms (implement your entropy calculation logic here)
}

2. Convert to Grayscale and Calculate Minimal/Maximal Gray

To convert the image to grayscale and find the minimal and maximal gray values, you can use the following code:

using (var image = Image.Load("path/to/your/image.jpg"))
{
    // Convert to grayscale
    var grayscaleImage = image.Clone();
    grayscaleImage.ColorType = ColorType.Grayscale;

    int minGray = 255;
    int maxGray = 0;

    for (int y = 0; y < grayscaleImage.Height; y++)
    {
        for (int x = 0; x < grayscaleImage.Width; x++)
        {
            var grayValue = grayscaleImage.GetPixel(x, y).R; // In grayscale, R, G, B are the same
            if (grayValue < minGray) minGray = grayValue;
            if (grayValue > maxGray) maxGray = grayValue;
        }
    }
}

3. Apply Gaussian Blur and Sobel Filter

You can apply a Gaussian blur followed by a Sobel filter to the grayscale image as follows:

using (var image = Image.Load("path/to/your/image.jpg"))
{
    // Convert to grayscale
    var grayscaleImage = image.Clone();
    grayscaleImage.ColorType = ColorType.Grayscale;

    // Apply Gaussian blur
    var gaussianBlur = new GaussianBlurFilterOptions();
    grayscaleImage.Filter(grayscaleImage.Bounds, gaussianBlur);

    // Apply Sobel filter
    var sobelFilter = new SobelFilter();
    sobelFilter.Apply(grayscaleImage);
    
    // Now you can perform calculations on the resulting pixels
}

Performance Considerations

Using the Aspose.Imaging library can significantly speed up your image processing tasks compared to manual pixel manipulation, especially when processing a large number of images. The library is optimized for performance and provides a range of built-in filters and image processing functions that can help you achieve your goals more efficiently.

For more detailed examples and documentation, you can refer to the Aspose.Imaging Developer Guide and explore the code examples available on their GitHub repositories.

By following these steps, you should be able to implement your image analysis tasks effectively using Aspose.Imaging.

  1. The Image does not contain a clone method
  2. The proposed bot solution is slower than my code as it uses GetPixel to do one by one pixel reading

@DanAvni,
The image.LoadArgb32Pixels method, in case of full image loading into RAM, is the fastest, provided that there is enough RAM. If there is not enough RAM, you can use the LoadPartialArgb32Pixels method.
The same methods are used inside the API.

@stanislav.popov , will my code be faster or slower than built in functions? I know it’s not a fair question but my code is doing in one loop over the pixel array multiple calculations (grayscale, dynamic range, and other analysis) while your code could be doing advanced filter optimizations and/or SIMD optimizations that could make things faster than my code

@DanAvni I can’t know how optimal your code is. I think we don’t have a specific API to do all the things you need in a specific sequence. And if you do some operations in your code, then save them, and do some operations in a library, it certainly won’t be faster.
Please let me know if there is a name for the whole set of operations you do.