您的位置:首页 > 文旅 > 旅游 > 利用OpenCvSharp进行图像相关操作

利用OpenCvSharp进行图像相关操作

2024/10/6 16:28:21 来源:https://blog.csdn.net/weixin_45114627/article/details/140931154  浏览:    关键词:利用OpenCvSharp进行图像相关操作

前言

程序设计过程,有时也需要对图像进行一些简单操作,C#没有现成的图像处理库,但有人对OpenCV进行了包装,我们可以很方便的使用OpenCvSharp对图像进行操作。当然了,这也需要使用的人员进行一些研究,但相对于C++版本,它已经非常友好了。

1、显示图像

代码:

private void button1_Click(object sender, EventArgs e){//读取图像为灰度图panda = new Mat("1.jpg", ImreadModes.Color);//把Mat格式的图片转换成BitmapBitmap bitmap = BitmapConverter.ToBitmap(panda);//显示图片pictureBox2.Image = bitmap;}

效果:

2、绘制元素

代码:

 private void DrawObject(){//画圆方法Cv2.Circle(panda, new OpenCvSharp.Point(100, 100), 100, new Scalar(0, 69, 255), 5);  //以宽度画圆 不填充Cv2.Circle(panda, new OpenCvSharp.Point(300, 100), 100, new Scalar(0, 69, 255), -1); //当为负数的时候,进行画整圆 填充,C++用FILLED进行代替//画矩形Cv2.Rectangle(panda, new OpenCvSharp.Point(100, 260), new OpenCvSharp.Point(400, 400), new Scalar(141, 218, 86), 3);//画线Cv2.Line(panda, new OpenCvSharp.Point(200, 300), new OpenCvSharp.Point(350, 400), new Scalar(87, 176, 174), 3);//绘制文字Cv2.PutText(panda, "Please advise!", new OpenCvSharp.Point(200, 500), HersheyFonts.HersheyDuplex, 0.75, new Scalar(193, 242, 75));Random random = new Random();Vec3b vec = new Vec3b(255, 255, 255);for (int i = 0; i < 3000; i++)//30000个点{int row = random.Next(panda.Rows);int col = random.Next(panda.Cols);//char white = (char)255;panda.Set(row, col, vec);}//把Mat格式的图片转换成BitmapBitmap bitmap = BitmapConverter.ToBitmap(panda);//显示图片pictureBox2.Image = bitmap;}

效果:

3、图像数据操作

代码:

private void Test(){Console.WriteLine("开始生成图片:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));int imageWidth = 1024;int imageHeight = 128;ushort[] orginImgData = new ushort[imageHeight * imageWidth];string filename = "123.bmp";Random random = new Random();for (int i = 0; i < imageWidth; i++){for (int j = 0; j < imageHeight; j++){orginImgData[i * imageHeight + j] = (ushort)random.Next(0, 65535);}}GenImage(imageWidth, imageHeight, orginImgData, filename);}private void GenImage(int imageWidth, int imageHeight, ushort[] orginImgData, string filename){int row = (int)imageWidth;int col = (int)imageHeight;Mat picMat = new Mat(row, col, MatType.CV_16UC1);int r = 0;int c = 0;unsafe{for (r = 0; r < row; r++){IntPtr p = picMat.Ptr(r);ushort* pp = (ushort*)p.ToPointer();for (c = 0; c < col; c++){// picMat.Set<ushort>(r,c, orginImgData[r * col + c]);pp[c] = orginImgData[r * col + c];}}Cv2.ImWrite(filename, picMat);}picMat.Dispose();Console.WriteLine("结束生成图片:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));}

效果:

4、操作摄像头

代码:

 Task.Run(() =>{//VideoCapture cap = new VideoCapture(1, VideoCaptureAPIs.DSHOW);VideoCapture cap = new VideoCapture(0, VideoCaptureAPIs.ANY);//  VideoCapture cap = new VideoCapture(path, VideoCaptureAPIs.ANY);Mat img = new Mat();while (true){cap.Read(img);if (!img.Empty()){//把Mat格式的图片转换成BitmapBitmap bitmap = BitmapConverter.ToBitmap(img);//显示图片pictureBox1.Image = bitmap;}else{Console.WriteLine("ERROR|NO IMG");break;}Thread.Sleep(1);}});

效果:

5、播放电影

代码:

 Task.Run(() =>{string path = @"D:\狄仁杰之亢龙有悔.mkv";//VideoCapture cap = new VideoCapture(1, VideoCaptureAPIs.DSHOW);// VideoCapture cap = new VideoCapture(0, VideoCaptureAPIs.ANY);VideoCapture cap = new VideoCapture(path, VideoCaptureAPIs.ANY);Mat img = new Mat();while (true){cap.Read(img);if (!img.Empty()){//Cv2.ImShow("Video", img);//把Mat格式的图片转换成BitmapBitmap bitmap = BitmapConverter.ToBitmap(img);//显示图片pictureBox1.Image = bitmap;}else{Console.WriteLine("ERROR|NO IMG");break;}Thread.Sleep(1);}});

效果:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com