image-processingcomputer-visionimage-recognitionobject-recognitionmathematical-morphology

Scripting objects in greyscale images as zero-one array[]


I am trying to script a greyscale object in a captured image as a matrix of 0 1 that represents a block of object pixels (or something like object style scaling), i can imagine the manual processing by looping the object, scaling and writing the matrix according to the grade of color, however i'm looking for intelligent or open source tools,

.NET are preferred,

[Update, to explain in more details]

The original images are colored, however, i'm converting it into 256 greyscale, then i want to scale it into black or white only, so at the end of the day it's just a black and white picture i want convert it to zero-one matrix,

the following url contains a discussion of how to convert black-white picture to zero-one matrix using a software called imagemagick:

http://studio.imagemagick.org/discourse-server/viewtopic.php?f=1&t=18433

notice the Zero one matrix which demonstrate a dragon face image!, is there any techniques or open source tools that helping me to achieve that?


Solution

  • Something like the following using Emgu OpenCV for .NET would work.

    using Emgu.CV;
    using Emgu.CV.CvEnum;
    using Emgu.CV.Structure;
    using System;
    using System.Drawing;
    using System.IO;
    
    using (Image<Bgr, Byte> img = new Image<Bgr, Byte>("MyImage.jpg")) 
    {
        Matrix<Int32> matrix = new Matrix<Int32>(img.Width, img.Height);
    
        for (int i = 0; i<img.Height;i++)
        {
           for (int j = 0; j<img.Width;j++)
           {
               if (img.Data[i,j,2] == 255 && 
                   img.Data[i,j,1] == 255 && 
                   img.Data[i,j,0] == 255)
               {
                   matrix.Data[i,j] = 0;
               }
               else 
               {
                   matrix.Data[i,j] = 1;
               }
           }
        }
    
        TextWriter tw = new StreamWriter("output.txt");
        for (int i = 0; i<img.Height;i++)
        {
           for (int j = 0; j<img.Width;j++)
           {
               tw.Write(matrix.Data[i,j]);
           }
           tw.Write(tw.NewLine); 
        }
    
    
    }
    

    Note that the snippet above loads colour images and creates a matrix with white as 0 and 1 otherwise.

    In order to load and work with grayscale images the Image<Bgr, Byte> becomes an Image<Gray, Byte> and the comparison simplifies to just if (img.Data[i,j,0] == 255).

    Also to do the thresholding (conversion from colour to grayscale to black and white), you can use Otsu's thresholding using the cvThreshold method, using something like :

    int threshold = 150;
    Image<Bgr, Byte> img = new Image<Bgr, Byte>("MyImage.jpg");
    Image<Gray, Single> img2 = img1.Convert<Gray, Single>();
    Image<Gray, Single> img3 = new Image<Gray, Single>(img2.Width, img2.Height);
    CvInvoke.cvThreshold(img2, img3, threshold, 255, THRESH.CV_THRESH_OTSU);    
    

    Other possible tools include