Exemple #1
0
        /// <summary>
        /// Return the segmentation-derived mask for the character.
        /// This may optionally be grown by some pixels.
        /// </summary>
        public override void GetMask(out Rect r, ref Bytearray outmask, int index, int grow)
        {
            r = boxes.At1d(index).Grow(grow);
            r.Intersect(new Rect(0, 0, labels.Dim(0), labels.Dim(1)));
            if (fullheight)
            {
                r.y0 = 0;
                r.y1 = labels.Dim(1);
            }
            int      x = r.x0, y = r.y0, w = r.Width(), h = r.Height();
            Intarray segs = segments.At1d(index);

            outmask.Resize(w, h);
            outmask.Fill(0);
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    int label = labels[x + i, y + j];
                    if (NarrayUtil.first_index_of(segs, label) >= 0)
                    {
                        outmask[i, j] = (byte)255;
                    }
                }
            }
            if (grow > 0)
            {
                Morph.binary_dilate_circle(outmask, grow);
            }
        }
Exemple #2
0
 public static Bitmap read_image_gray(Bytearray image, string path)
 {
     Bitmap bitmap = LoadBitmapFromFile(path);
     image.Resize(bitmap.Width, bitmap.Height);
     ImgRoutine.NarrayFromBitmap(image, bitmap);
     return bitmap;
 }
Exemple #3
0
        public static Bitmap read_image_gray(Bytearray image, string path)
        {
            Bitmap bitmap = LoadBitmapFromFile(path);

            image.Resize(bitmap.Width, bitmap.Height);
            ImgRoutine.NarrayFromBitmap(image, bitmap);
            return(bitmap);
        }
Exemple #4
0
 public static Bitmap read_image_binary(Bytearray image, string path)
 {
     Bitmap bitmap = LoadBitmapFromFile(path);
     image.Resize(bitmap.Width, bitmap.Height);
     ImgRoutine.NarrayFromBitmap(image, bitmap);
     double threshold = (NarrayUtil.Min(image) + NarrayUtil.Max(image)) / 2.0;
     for (int i = 0; i < image.Length1d(); i++)
         image.Put1d(i, (byte)((image.At1d(i) < threshold) ? 0 : 255));
     return bitmap;
 }
Exemple #5
0
        public static Bitmap read_image_binary(Bytearray image, string path)
        {
            Bitmap bitmap = LoadBitmapFromFile(path);

            image.Resize(bitmap.Width, bitmap.Height);
            ImgRoutine.NarrayFromBitmap(image, bitmap);
            double threshold = (NarrayUtil.Min(image) + NarrayUtil.Max(image)) / 2.0;

            for (int i = 0; i < image.Length1d(); i++)
            {
                image.Put1d(i, (byte)((image.At1d(i) < threshold) ? 0 : 255));
            }
            return(bitmap);
        }
Exemple #6
0
        /// <summary>
        /// Get a mask at a given location
        /// </summary>
        public override void GetMaskAt(ref Bytearray mask, int index, Rect b)
        {
            if (!(b.x0 > -1000 && b.x1 < 10000 && b.y0 > -1000 && b.y1 < 10000))
            {
                throw new Exception("CHECK: (b.Left > -1000 && b.Right < 10000 && b.Top > -1000 && b.Bottom < 10000)");
            }
            mask.Resize(b.Width(), b.Height());
            mask.Fill(0);
            Intarray segs = segments.At1d(index);
            int      w = b.Width(), h = b.Height();

            for (int i = 0; i < w; i++)
            {
                int x = b.x0 + i;
                unchecked
                {
                    if ((uint)x >= labels.Dim(0))
                    {
                        continue;
                    }
                }
                for (int j = 0; j < h; j++)
                {
                    int y = b.y0 + j;
                    unchecked
                    {
                        if (((uint)y) >= labels.Dim(1))
                        {
                            continue;
                        }
                    }
                    int label = labels[b.x0 + i, b.y0 + j];
                    if (NarrayUtil.first_index_of(segs, label) >= 0)
                    {
                        mask[i, j] = (byte)255;
                    }
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Convert Bitmap image to any Narray
        /// </summary>
        public static unsafe void NarrayFromBitmap <T>(Narray <T> outarray, Bitmap bitmap, bool maxrgb = false, bool yflip = true)
        {
            if (bitmap.PixelFormat == PixelFormat.Format1bppIndexed)
            {
                bitmap = new Bitmap(bitmap);
            }
            int       w      = bitmap.Width;
            int       h      = bitmap.Height;
            Bytearray imgRgb = new Bytearray();

            imgRgb.Resize(w, h, 3);

            // lock source image
            BitmapData imageData = bitmap.LockBits(
                new Rectangle(0, 0, w, h),
                ImageLockMode.ReadOnly, bitmap.PixelFormat);
            int   srcOffset = imageData.Stride - w;
            int   pixelOffset = 1, r = 0, g = 0, b = 0;
            float cr = 0.5f, cg = 0.419f, cb = 0.081f;    // RMY convert method

            Color[] palette = null;
            // check image format
            if (bitmap.PixelFormat == PixelFormat.Format32bppArgb || bitmap.PixelFormat == PixelFormat.Format32bppRgb)
            {
                pixelOffset = 4;
                srcOffset   = imageData.Stride - w * pixelOffset;
                r           = 2; g = 1; b = 0;
                // get palette
                palette = new Color[256];
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    palette[i] = Color.FromArgb(i, i, i);
                }
            }
            else if (bitmap.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                pixelOffset = 3;
                srcOffset   = imageData.Stride - w * pixelOffset;
                r           = 2; g = 1; b = 0;
                // get palette
                palette = new Color[256];
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    palette[i] = Color.FromArgb(i, i, i);
                }
            }
            else
            {
                palette = bitmap.Palette.Entries;
            }

            // process convert the image to RGB Bytearray
            byte *src = (byte *)imageData.Scan0.ToPointer( );
            int   yres;

            // for each line
            //for (int y = h - 1; y >= 0; y--)
            for (int y = 0; y < h; y++)
            {
                // for each pixel
                for (int x = 0; x < w; x++, src += pixelOffset)
                {
                    yres = y;
                    if (yflip)
                    {
                        yres = h - y - 1;
                    }
                    imgRgb[x, yres, 0] = palette[src[r]].R;
                    imgRgb[x, yres, 1] = palette[src[g]].G;
                    imgRgb[x, yres, 2] = palette[src[b]].B;
                }
                src += srcOffset;
            }
            // unlock image
            bitmap.UnlockBits(imageData);

            // copy to output array
            if (outarray.Rank() == 3)
            {
                outarray.Copy(imgRgb);
            }
            else if (outarray.Rank() == 2)
            {
                // resize output array
                outarray.Resize(w, h);

                // packed image
                if (typeof(T).Name == "Int32")
                {
                    for (int y = 0; y < h; y++)
                    {
                        // for each RGB pixel convert to Gray
                        for (int x = 0; x < w; x++)
                        {
                            outarray[x, y] = (T)Convert.ChangeType(
                                ((imgRgb[x, y, 0] << 16) | (imgRgb[x, y, 1] << 8) | (imgRgb[x, y, 2])),
                                typeof(T));
                        }
                    }
                }
                else // gray image
                {
                    for (int x = 0; x < w; x++)
                    {
                        // for each RGB pixel convert to Gray
                        for (int y = 0; y < h; y++)
                        {
                            if (maxrgb)
                            {
                                outarray[x, y] = (T)Convert.ChangeType(
                                    Convert.ToByte(Math.Max(Math.Max(imgRgb[x, y, 0], imgRgb[x, y, 1]), imgRgb[x, y, 2])),
                                    typeof(T));
                            }
                            else
                            {
                                outarray[x, y] = (T)Convert.ChangeType(
                                    Convert.ToByte(cr * imgRgb[x, y, 0] + cg * imgRgb[x, y, 1] + cb * imgRgb[x, y, 2]),
                                    typeof(T));
                            }
                        }
                    }
                }
            }
            else
            {
                throw new ArgumentException("NarrayFromBitmap: array rank must be 2 or 3");
            }
        }
Exemple #8
0
 public Bytearray ToBytearray()
 {
     Bytearray ba = new Bytearray();
     ba.Resize(Width, Height);
     int yput;
     for (int y = 0; y < Height; y++)
     {
         yput = Height - y - 1;
         for (int x = 0; x < Width; x++)
             ba.Put(x, yput, Get(y, x));
     }
     return ba;
 }