opencv - Convert images to grayscale using C#

Hi everybody I want to convert all images to grayscales in my directory but code that I know only do that task one by one!!But I have a lot of images and I want If there is a code that convert all images in directory to grayscale or

1 Answer

  1. Aaron- Reply

    2019-11-13

    Assuming you have a variable called path which is the path of the folder with the images to process, and assuming you can use WPF APIs to convert the image:

    DirectoryInfo dirInfo = new DirectoryInfo(path);
    
    foreach ( FileInfo fileInfo in dirInfo.EnumerateFile() ) {
        ProcessImage(FileInfo.FullName);
    }
    
    void ProcessImage( string image ) {
        byte[] imageData = null;
    
        try {
            // Load the data in the file into a  byte array for processing.
            imageData = File.ReadAllBytes( image );
    
        } catch ( Exception) {
            // Your error handling code here
        }
    
        // We're going to put the image into this object
        BitmapImage src = new BitmapImage();
    
        try {
            // Load the image into a memory stream, then into src.
            using( MemoryStream stream = new MemoryStream( imageData ) ) {
                src.BeginInit();
                src.CacheOption = BitmapCacheOption.OnLoad; // Causes the bitmap data to be loaded now, not at a later time.
                src.StreamSource = stream;
                src.EndInit();
            }
        } catch ( NotSupportedException ) {
            // The bitmap format is not supported.  Your error handler here.
        }
    
        try {
            // Create a FormatConvertedBitmap object & set it up.
            FormatConvertedBitmap dst = new FormatConvertedBitmap();
            dst.BeginInit();
            dst.DestinationFormat = PixelFormats.Gray8;  // Use whatever gray scale format you need here
            dst.Source            = src;
    
            // Now convert the image to 8 bits per pixel grey scale.
            dst.EndInit();
    
            // Compute the dst Bitmap's stride (the length of one row of pixels in bytes).
            int stride = ( ( dst.PixelWidth * dst.Format.BitsPerPixel + 31 ) / 32 ) * 4;
    
            // Allocate space for the imageBytes array.
            imageBytes = new byte[ stride * dst.PixelHeight ];
    
            // Get the pixels out of the dst image and put them into imageBytes.
            dst.CopyPixels( Int32Rect.Empty, imageBytes, stride, 0 );
    
        } catch ( Exception ex ) {
            // Your error handler here.
        }
    
        // Any other code to save or do something else with the image.
    }
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>