C# OpenCV学习笔记二之图像读写的两种方法
2014-10-28 21:16
561 查看
原文地址:
http://www.2cto.com/kf/201109/105560.html
图像显示在控件loadPictureBox上
方法一
//读取图像001.jpg
IntPtr img = CvInvoke.cvLoadImage("001.jpg", Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR);
//IntPtr转换为Image,详细见IntPtr2Image方法
loadPictureBox.Image = IntPtr2Image(img);
//显示图像窗口
CvInvoke.cvShowImage("view", img);
//窗口保留2000毫秒,即2秒
CvInvoke.cvWaitKey(2000);
//关闭窗口
CvInvoke.cvDestroyWindow("view");
//保存图像
CvInvoke.cvSaveImage("002.jpg", img);
//释放
CvInvoke.cvReleaseImage(ref img);
private Image IntPtr2Image(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;
}
方法二
Image<Bgr, Byte> img = new Image<Bgr, byte>("001.jpg");
loadPictureBox.Image = img.ToBitmap();
作者“边城骆驼 -- 每天进步百分之一”
http://www.2cto.com/kf/201109/105560.html
图像显示在控件loadPictureBox上
方法一
//读取图像001.jpg
IntPtr img = CvInvoke.cvLoadImage("001.jpg", Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR);
//IntPtr转换为Image,详细见IntPtr2Image方法
loadPictureBox.Image = IntPtr2Image(img);
//显示图像窗口
CvInvoke.cvShowImage("view", img);
//窗口保留2000毫秒,即2秒
CvInvoke.cvWaitKey(2000);
//关闭窗口
CvInvoke.cvDestroyWindow("view");
//保存图像
CvInvoke.cvSaveImage("002.jpg", img);
//释放
CvInvoke.cvReleaseImage(ref img);
private Image IntPtr2Image(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;
}
方法二
Image<Bgr, Byte> img = new Image<Bgr, byte>("001.jpg");
loadPictureBox.Image = img.ToBitmap();
作者“边城骆驼 -- 每天进步百分之一”
相关文章推荐
- 【OpenCV学习笔记 023】两种图像分割方法比较
- OpenCV2学习笔记(四):两种图像分割方法比较
- C# OpenCV学习笔记三之图像捕捉及其灰度转换方法
- opencv学习笔记(1)----图像读写;添加噪声
- c# opencvsharp学习笔记(3)兴趣区域ROI,图像叠加,操作像素点
- C# OpenCV学习笔记五之图像轮廓
- OpenCv学习笔记(六)----图像空间缩减,OpenCv中的计时函数和OpenCv中操作图像单个像素点的方法
- opencv学习笔记1::访问图像中像素的三类方法(用指针,迭代器,动态地址)代码及用时检测
- Opencv 基础学习三 (OpenCV实现图像合并主要有两种方法)
- C# OpenCV学习笔记四之图像二值化
- opencv学习笔记之对灰度图像遍历的三种方法
- 【OpenCV学习笔记】之四:二值图像细化方法/骨架提取----基于2.0 Mat接口
- opencv学习笔记之对灰度图像遍历的三种方法
- CLR Via C# 学习笔记(4) 方法 构造函数
- C#.NET学习笔记---C#中方法参数的传递方式: 值类型(默认),ref类型,out类型和params类型
- C#两种获取灰度图像的方法
- 【转】.NET深入学习笔记(2):C#中判断空字符串的4种方法性能比较与分析
- Visual C# 技术内幕学习笔记 第二章 C#基础(3) 继承和方法
- CLR Via C# 学习笔记(6) 方法参数相关(out ref params)
- CLR Via C# 学习笔记(4) 方法 构造函数