如何将拆分的图像显示为 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 x 8 像素,但结果在左上角仅显示 1 块 (8 x 8(。

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

你能帮我这个吗?

如果要

将源图像划分为多个 8x8 图块,则转换函数的循环不正确。如果要将源图像划分为 64 个图块,则它们是正确的,但 DCT 通常不适用于一组静态的 64 个图像。我不清楚你想要哪一个,但我在这里修复了多个 8x8 图像:

      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 的倍数。

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

然后块是(假设灰度(

  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

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

如果无法填充数据,最好的办法是将值复制到 64 个条目数组中。您将执行类似于上述的循环,但是,每当您需要访问超出图像宽度或高度的值时,请在临时数组中插入 0。

最新更新