学习笔记:C#调用matlab函数(DCT嵌入水印图像)

C#与matlab混合编程

在matlab利用deploytool工具生成.NET的.dll动态链接库,在vs工程里添加引用(必须添加另一MWArray.dll)即可调用。调用时传参统一用MWArray类型,故须进行数据类型的转换。

    两个数组加、减的例子 MWNumericArray 为中间类型.ToArray()方法返回的类型与matlab函数返回类型相关

    int[] a = { 1,2,3,4,5,6};
            int[] b = { 3,5,7,5,4,3};
            double[,] c = new double[3, 2];
            double[,] d = new double[3, 2];
            MWNumericArray ma = new MWNumericArray(3,2,a);
            MWNumericArray mb = new MWNumericArray(3, 2, b);
            MWArray [] agrsout = new MWArray[2];
            MWArray[] agrsin = new MWArray[] {ma,mb};
            myMathClass mat = new myMathClass();
            mat.MatrixOpera(2,ref agrsout ,agrsin );//有多种重载可调用
            MWNumericArray x1 = agrsout[0] as MWNumericArray;
            MWNumericArray x2 = agrsout[1] as MWNumericArray;
            c = (double[,])x1.ToArray();//matlab函数没指定返回类型则为double
            d = (double[,])x2.ToArray();


读取图像转为灰度图像(之后要DCT变换,先将返回值设为double)

        private double[,] ReadImage(Bitmap Obj)
        {
            int i, j;
            int width = Obj.Width;
            int height = Obj.Height;
            int[,] GreyImage = new int[width, height];  //[Row,Column]
            int[] imgData = new int[width * height];
            double [,] Input = new double[Width, Height];  //[Row,Column]
            Bitmap image = Obj;
            BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                                     ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            unsafe
            {
                byte* imagePointer1 = (byte*)bitmapData1.Scan0;


                for (i = 0; i < bitmapData1.Height; i++)
                {
                    for (j = 0; j < bitmapData1.Width; j++)
                    {
                        //GreyImage[j, i] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0);
                        //Input[j, i] = (double)GreyImage[j, i];
                        GreyImage[i,j] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0);
                        Input[i,j] = (double)GreyImage[i, j];
                        //4 bytes per pixel
                        imagePointer1 += 4;
                    }//end for j
                    //4 bytes per pixel
                    imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
                }//end for i
            }//end unsafe
            image.UnlockBits(bitmapData1);
            return Input ;
        }

将字节数组转换为8位灰度图像Bitmap

public static Bitmap ToGrayBitmap(byte[] rawValues, int width, int height)
        {
            申请目标位图的变量,并将其内存区域锁定  
            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
             ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

            获取图像参数  
            int stride = bmpData.Stride;  // 扫描线的宽度  
            int offset = stride - width;  // 显示宽度与扫描线宽度的间隙  
            IntPtr iptr = bmpData.Scan0;  // 获取bmpData的内存起始位置  
            int scanBytes = stride * height;// 用stride宽度,表示这是内存区域的大小  

            下面把原始的显示大小字节数组转换为内存中实际存放的字节数组  
            int posScan = 0, posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组  
            byte[] pixelValues = new byte[scanBytes];  //为目标数组分配内存  

            for (int x = 0; x < height; x++)
            {
                下面的循环节是模拟行扫描  
                for (int y = 0; y < width; y++)
                {
                    pixelValues[posScan++] = rawValues[posReal++];
                }
                posScan += offset;  //行扫描结束,要将目标位置指针移过那段“间隙”  
            }

            用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中  
            System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
            bmp.UnlockBits(bmpData);  // 解锁内存区域  

            // 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度  
            ColorPalette tempPalette;
            using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
            {
                tempPalette = tempBmp.Palette;
            }
            for (int i = 0; i < 256; i++)
            {
                tempPalette.Entries[i] = Color.FromArgb(i, i, i);
            }
            bmp.Palette = tempPalette;
            // 算法到此结束,返回结果  
            return bmp;
        } 

嵌入水印和提取水印应注意数据类型的转换和行列扫描的顺序。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值