如何有效地扩展图像数据 [英] How to effectively scale image data

查看:117
本文介绍了如何有效地扩展图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在图像上的像素值进行操作。因此,我写了这样一个类:

I need to operate on pixel values in images. Therefore I wrote such a class:

public class image_class
{
    #region property
    public int width { get; private set; }
    public int height { get; private set; }

    byte[] buffer;
    #endregion

    #region constructors
    public image_class()
    {
        ;
    }
    public image_class(int Width, int Height, byte[] data)
    {
        if (data.Count() == (Width * Height * 3))
        {
            this.width = Width;
            this.height = Height;

            this.buffer = data;
        }
        else
            throw new Exception();
    }
    #endregion

    #region get bitmap
    public ImageSource bmp
    {
        get
        {
            try
            {
                var pixelFormat = PixelFormats.Rgb24;
                var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;
                var stride = bytesPerPixel * width;

                var bitmap = BitmapSource.Create(width, height, 96d, 96d, pixelFormat, null, buffer, stride);

                return bitmap;
            }

            catch { return null; }
        }
    }
    #endregion

    #region methods
    public void load_from_file(string path)
    {
            var bmp = new BitmapImage();
            bmp.BeginInit();
            bmp.UriSource = new Uri(ofd.FileName, UriKind.Absolute);
            bmp.EndInit();

            height = (int)bmp.Height;
            width = (int)bmp.Width;

            int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);

            int size = height * stride;

            var buffer2 = new byte[size];
            bmp.CopyPixels(buffer2, stride, 0);

            buffer = new byte[size * 3 / 4];
            for (int i = 0, j = 0; i < size; i += 4, j += 3)
            {
                buffer[j] = buffer2[i + 2];
                buffer[j + 1] = buffer2[i + 1];
                buffer[j + 2] = buffer2[i];
            }

    }

    public void load_from_file(string path, int Width, int Height)
    {
        this.width = Width;
        this.height = Height;

        var bmp = new BitmapImage();

        bmp.BeginInit();
        bmp.DecodePixelHeight = height;
        bmp.DecodePixelWidth = width;
        bmp.UriSource = new Uri(path, UriKind.Absolute);
        bmp.EndInit();

        int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);

        int size = height * stride;

        var buffer2 = new byte[size];
        bmp.CopyPixels(buffer2, stride, 0);

        buffer = new byte[size * 3 / 4];
        for (int i = 0, j = 0; i < size; i += 4, j += 3)
        {
            buffer[j] = buffer2[i + 2];
            buffer[j + 1] = buffer2[i + 1];
            buffer[j + 2] = buffer2[i];
        }

    }


    public void save_to_file(string path)
    {
        var pixelFormat = PixelFormats.Rgb24;
        var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;
        var stride = bytesPerPixel * width;

        var encoder = new JpegBitmapEncoder();

        encoder.Frames.Add(BitmapFrame.Create(BitmapSource.Create(width, height, 96d, 96d, pixelFormat, null, buffer, stride)));

        FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);



        encoder.Save(fs);

        fs.Close();
    }

    public image_class cut(int x0, int y0, int dx, int dy)
    {
        if (x0 >= 0 & y0 >= 0 & x0 + dx <= width & y0 + dy <= height)
        {
            byte[] buffer3 = new byte[dx * dy * 3];

            for (int y = 0, i = 0, j = -1; y < height; y++) for (int x = 0; x < width; x++, i += 3)
                    if (x >= x0 & x < x0 + dx & y >= y0 & y < y0 + dy)
                    {
                        buffer3[++j] = buffer[i];
                        buffer3[++j] = buffer[i + 1];
                        buffer3[++j] = buffer[i + 2];
                    }

            return new image_class(dx, dy, buffer3);

        }
        else
            throw new Exception();
    }
    #endregion
}

该缓冲区数组是我使用的一个。

The buffer array is the one I use.

本类工作完全正常。
当我需要从图像的片段的数据进行操作,我用的方法切,例如:

This class works perfectly fine. When I need to operate on data from fragment of an image, I use method cut, for example:

image_class img1=new image_class();
image_class img2=img1.cut(5,5,20,20);

我需要做的是削减图像扩展到其他价值和维护的像素数据的格式。比如我想从一个图片,扩展到20×20 50×50平方。这是我做的:

What I need is to scale cut image to other values and maintain format of pixel data. For example I want 50x50 square from a picture, scaled to 20x20. This is what I do:

image_class img1=new image_class();
image_class img2=img1.cut(5,5,50,50);
img2.save_to_file(path);
image_class img3=new image_class();
img3.load_from_file(path, 20,20);

它的工作原理,但当然是非常uneffective。谁能告诉我更好的解决方案?

It works, but of course is very uneffective. Can anyone show me better solution?

推荐答案

您并不需要这一切。为了一个矩形部分从一个源位图复制,并随后扩展它,你可能只需使用 CroppedBitmap TransformedBitmap

You don't need all this. In order to copy a rectangular part from a source bitmap, and subsequently scale it, you might simply use a CroppedBitmap and a TransformedBitmap:

BitmapSource original = ...
var crop = new CroppedBitmap(original, new Int32Rect(5, 5, 50, 50));
var result = new TransformedBitmap(crop, new ScaleTransform(0.4, 0.4));

这篇关于如何有效地扩展图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆