简体   繁体   English

如何将分割后的图像显示为8x8像素?

[英]How to show a split images into 8x8 pixels?

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];


        }
    }
}

I have made this code to divide an image into 8 x 8 pixels, but the result shows just 1 block (8 x 8) in the upper left corner. 我编写了此代码,将图像划分为8 x 8像素,但是结果在左上角仅显示了1个块(8 x 8)。

All I want is like this: |xxxx|xxxx|xxxx|xxxx|.......... until the same width to original image. 我想要的只是这样: |xxxx|xxxx|xxxx|xxxx|..........直到与原始图像相同的宽度。

Can you help me with this? 你能帮我吗?

If you want to divide your source image into multiple 8x8 tiles, then your transform function's loops are incorrect. 如果要将源图像划分为多个8x8的图块,则变换函数的循环是不正确的。 If you want to divide your source image into 64 tiles then they are correct, but DCT doesn't usually work with a static set of 64 images. 如果要将源图像划分为64个图块,那么它们是正确的,但是DCT通常不适用于64个图像的静态集合。 I am unclear on which one you want, but I fixed it up for multiple 8x8 images here: 我不清楚要选择哪一个,但在这里我将其修复为多个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;

The boundary condition on the right and bottom edge is also annoying to take care of (which I have not here), since the source image dimensions may not be multiples of 8. 由于源图像的尺寸可能不是8的倍数,因此右侧和底部边缘的边界条件也很烦人(我在这里没有)。

There are a couple of approach you can do. 您可以采取几种方法。 The easiest way for processing is to store your image values in rows with lengths that are multiples of 8. Pad the data out. 最简单的处理方法是将图像值存储在长度为8的倍数的行中。将数据填充出来。 Do the same for then do the same for the number of rows. 进行相同的操作,然后对行数进行相同的操作。

Then the blocks are (assuming grayscale) 然后是块(假设灰度)

  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

Keep in mind that your output elements need to be twice the size of the input. 请记住,您的输出元素需要是输入大小的两倍。 If your image data is 8 bits, your DCT output will need to be 16 bits for an integer representation (or a floating point value). 如果图像数据为8位,则DCT输出需要为整数表示(或浮点值)的16位。

If you cannot pad the data, your best bet is to copy the values to a 64 entry array. 如果无法填充数据,最好的选择是将这些值复制到64个条目数组中。 You'd do a loop similar to the above but, any time you would need to access a value beyond the width or height of the image, insert a 0 into the temporary array. 您将执行与上述类似的循环,但是任何时候您需要访问超出图像的宽度或高度的值时,请在临时数组中插入0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM