首页 新闻 会员 周边

C#调用vc++ dll 传递参数的问题(Bitmap 转换为 opencv mat ),保存后图片不一样。

0
悬赏园豆:15 [已解决问题] 解决于 2014-06-16 14:50

vc++ 代码

bool Recognize(Point_2F *arr,uchar* b)
{

Mat src=cv::Mat(415,770,CV_8UC3,b);

/*for (int i=0;i<src.rows;i++) 
{
memcpy(src.ptr(i),b+i*src.cols,src.cols);
}*/
cv::imwrite("D:\\my_2_testfiles\\111_before.png",src);

// 保存的照片和原来的照片不一样而且大小也不一样?

}

C#代码

 [DllImport("rectanglepoints.dll", EntryPoint = "Recognize", CallingConvention = CallingConvention.Cdecl)]
        static extern byte Recognize(IntPtr arr, IntPtr b);
{ 
 // Create a new bitmap.
            Bitmap bmp = new Bitmap("D:\\my_2_testfiles\\111.png");

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);
            // Get the address of the first line.
            IntPtr ptrImg = bmpData.Scan0;
bool ret;
    ret = Convert.ToBoolean(Recognize(IntPtr.Zero, ptrImg));   

 // Unlock the bits.
            bmp.UnlockBits(bmpData);

            Console.Read();

        } 

照片附加

原图:

 

调用vc++ dll后保存的图片

亲福的主页 亲福 | 初学一级 | 园豆:40
提问于:2014-06-12 17:41
< >
分享
最佳答案
0

每行有padding缘故

每行第像素第字节对齐4字节边界上(无法理解句理解每行第像素第字节地址被4整除)
opencv没有做把padding也当作有效数据会偏

 
Bitmap对象Lock以后,除了scan0应该还会给你一个stride,这就是每一行所占的字节数(包括最后的padding)

opencv那边我可以算是没玩过,只有知道opencv那边到底数据是怎么要求的(比如有没有padding啊如何padding之类)才有办法从bitmap构建符合它要求的数据
你找找opencv那边每一条扫描线占用的字节数怎么计算吧
亲福 | 初学一级 |园豆:40 | 2014-06-16 14:48
其他回答(1)
0

你好,请问你的问题最后是和解决的呢,可否将代码贴出来

空山疯语 | 园豆:202 (菜鸟二级) | 2015-03-31 21:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册