C# 如何将分割图像显示为8x8像素?

C# 如何将分割图像显示为8x8像素?,c#,.net,image,dct,C#,.net,Image,Dct,我编写了这段代码,将图像分割为8 x 8像素,但结果仅显示左上角的1个块8 x 8 我想要的就是这样: |xxxx | xxxx | xxxx | xxxx |。。。。。。。。。。直到与原始图像的宽度相同 你能帮我一下吗?如果你想将源图像分割成多个8x8分片,那么变换函数的循环是不正确的。如果您想将源图像划分为64个分幅,那么它们是正确的,但是DCT通常不适用于静态的64个图像集。我不清楚您想要哪一个,但我在这里为多个8x8图像设置了它: using System; using System.C

我编写了这段代码,将图像分割为8 x 8像素,但结果仅显示左上角的1个块8 x 8

我想要的就是这样: |xxxx | xxxx | xxxx | xxxx |。。。。。。。。。。直到与原始图像的宽度相同


你能帮我一下吗?

如果你想将源图像分割成多个8x8分片,那么变换函数的循环是不正确的。如果您想将源图像划分为64个分幅,那么它们是正确的,但是DCT通常不适用于静态的64个图像集。我不清楚您想要哪一个,但我在这里为多个8x8图像设置了它:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;

namespace DCTTransform
{
    public partial class Form1 : Form
    {
        Image<Bgr, Byte> img;
        Image<Ycc, Byte> imgYcc;
        Bitmap bmp;

        public Form1()
        {
            InitializeComponent();
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog od = new OpenFileDialog();
            if (od.ShowDialog() == DialogResult.OK)
            {
                img = new Image<Bgr, byte>(od.FileName);
                pcCitra.Image = img.ToBitmap();

                label1.Text = img.Width.ToString();
            }
        }

        //Split citra
        public List<Image> subBlok(Image img, int blokX, int blokY)
        {
            List<Image> res = new List<Image>();
            int pembagiLebar = img.Width / blokX;
            int pembagiTinggi = img.Height / blokY;


            for (int i = 0; i < blokX; i++)//baris
            {
                for (int j = 0; j < blokY; j++)//kolom
                {
                    Bitmap bmp = new Bitmap(img.Width / pembagiLebar, img.Height / pembagiTinggi);
                    //Bitmap bmp = new Bitmap(img.Width, img.Height);

                    Graphics grp = Graphics.FromImage(bmp);
                    grp.DrawImage(img, new Rectangle(0,0 , bmp.Width, bmp.Height), new Rectangle(j * bmp.Width, i * bmp.Height, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                    res.Add(bmp);
                }
            }


            return res;

        }


        private void btTransform_Click(object sender, EventArgs e)
        {
            //SplitImage
            List<Image> imgs = subBlok(pcCitra.Image, 8, 8);

            pbDCT.Image = imgs[0];


        }
    }
}

右边缘和下边缘的边界条件也很烦人,我这里没有提到,因为源图像的尺寸可能不是8的倍数。

有几种方法可以做到。最简单的处理方法是将图像值存储在长度为8的倍数的行中。把数据填出来。对行数执行相同的操作,然后对行数执行相同的操作

然后这些块采用灰度

      List<Image> res = new List<Image>();
        int pembagiLebar = (int)Math.Ceil((float)img.Width / (float)blokX);
        int pembagiTinggi = (int)Math.Ceil((float)img.Height / (float)blokY);


        for (int i = 0; i < pembagiLebar ; i++)//baris
        {
            for (int j = 0; j < pembagiTinggi ; j++)//kolom
            {
                Bitmap bmp = new Bitmap(blokX, blokY);

                using (Graphics grp = Graphics.FromImage(bmp)) {
                     grp.DrawImage(img, 0, 0, new Rectangle(i * blokX, j * blokY, blokX, blokY), GraphicsUnit.Pixel);
                }

                res.Add(bmp);
            }
        }


        return res;
请记住,输出元素的大小必须是输入元素的两倍。如果图像数据为8位,则对于整数表示或浮点值,DCT输出需要为16位

如果无法填充数据,最好将值复制到64个条目的数组中。您可以执行与上面类似的循环,但任何时候需要访问图像宽度或高度以外的值时,都可以在临时数组中插入0

  X = starting point

  v0 = X
  v1 = X + 1 
  v2 = X + 2 
  . . . 
  v8 = X + row length
  v9 = X + row length + 1
  v10 = X + row length + 2
  . . . 
  v63 = x = 7 * row length + 7

  To move to the next block in the row X = X + 8. When you reach the end of the row
  X = X + 8 * row length