| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1268 人关注过本帖
标题:请问C#+openCV问题
取消只看楼主 加入收藏
高小
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2010-6-30
结帖率:82.35%
收藏
 问题点数:0 回复次数:1 
请问C#+openCV问题

请问我下边这段代码怎么总是出错呢?总是出现这样的错误:MIplImage img =(MIplImage)Marshal.PtrToStructure(src, typeof(MIplImage));-----未将对象引用设置到对象实例?

这段代码是实现IntPtr向Image<>类型的转化。
public Image ShowIplImageInWindow(IntPtr src)
        {
            MIplImage img =(MIplImage)Marshal.PtrToStructure(src, typeof(MIplImage));

            Bitmap disp = new Bitmap(img.width, img.height, PixelFormat.Format24bppRgb);
            BitmapData bmp = disp.LockBits(new Rectangle(0, 0, img.width, img.height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

            long linebytes = (img.width * 24 + 31) / 32 * 4;

            unsafe
            {
                byte* pixel = (byte*)bmp.Scan0.ToPointer();
                if (img.nChannels == 3)
                {
                    for (int i = 0; i < img.height; i++)
                    {
                        for (int j = 0, n = 0; j < img.width; j++, n++)
                        {
                            byte b = ((byte*)img.imageData + img.widthStep * i)[3 * j];
                            byte g = ((byte*)img.imageData + img.widthStep * i)[3 * j + 1];
                            byte r = ((byte*)img.imageData + img.widthStep * i)[3 * j + 2];

                            *(pixel + linebytes * (i) + n) = b;
                            n++;
                            *(pixel + linebytes * (i) + n) = g;
                            n++;
                            *(pixel + linebytes * (i) + n) = r;
                        }
                    }
                }
                else if (img.nChannels == 1)
                {
                    for (int i = 0; i < img.height; i++)
                    {
                        for (int j = 0, n = 0; j < img.width; j++, n++)
                        {
                            byte g = ((byte*)img.imageData + img.widthStep * i)[j];

                            *(pixel + linebytes * (i) + n) = g;
                            n++;
                            *(pixel + linebytes * (i) + n) = g;
                            n++;
                            *(pixel + linebytes * (i) + n) = g;
                        }
                    }
                }
                else
                {
                    return null;
                }
            }

            disp.UnlockBits(bmp);

            return (Image)disp;
        }
搜索更多相关主题的帖子: openCV 
2010-12-14 14:59
高小
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2010-6-30
收藏
得分:0 
回复 楼主 高小
我已经解决了 \(^o^)/~
2010-12-30 18:58
快速回复:请问C#+openCV问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017416 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved