首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# OpenCvSharp 模拟生成车辆运行视频

  • 25-02-19 03:20
  • 3843
  • 10340
blog.csdn.net

C# OpenCvSharp 模拟生成车辆运行视频

目录

效果

项目

代码

下载


效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace OpenCvSharp_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string image_path;

        Stopwatch stopwatch = new Stopwatch();

        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = "bg_img.jpg";
            pictureBox1.Image = new Bitmap(image_path);
        }

        Mat bgImg;
        Mat car_bgra;
        Mat car;
        Mat mask;
        Rect roi;
        Mat pos;
        int x, y, width, height, step;

        private void button6_Click(object sender, EventArgs e)
        {
            x = Convert.ToInt32(txtX.Text);
            y = Convert.ToInt32(txtY.Text);
            width = Convert.ToInt32(txtW.Text);
            height = Convert.ToInt32(txtH.Text);

            Cv2.Resize(mask, mask, new OpenCvSharp.Size(width, height));
            Cv2.Resize(car, car, new OpenCvSharp.Size(width, height));

            textBox1.Text = string.Format("x={0};y={1}", x, y);

            ShowCar();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            step = Convert.ToInt32(txtStep.Text);
            y = y + step;
            ShowCar();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            step = Convert.ToInt32(txtStep.Text);
            x = x - step;
            ShowCar();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            step = Convert.ToInt32(txtStep.Text);
            y = y - step;
            ShowCar();
        }

        VideoCapture capture;
        Mat currentFrame = new Mat();
        private void button8_Click(object sender, EventArgs e)
        {
            capture = new VideoCapture("out.mp4");

            if (!capture.IsOpened())
            {
                MessageBox.Show("打开视频文件失败");
                return;
            }
            capture.Read(currentFrame);
            if (!currentFrame.Empty())
            {
                pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
                timer1.Interval = (int)(1000.0 / capture.Fps);
                timer1.Enabled = true;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            capture.Read(currentFrame);
            if (currentFrame.Empty())
            {
                //pictureBox1.Image = null;
                timer1.Enabled = false;
                capture.Release();
                textBox1.Text = "播放完毕。";
                return;
            }
            pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
        }

        bool ShowCar()
        {

            textBox1.Text = string.Format("x={0};y={1};width={2};height={3}", x, y, width, height);
            txtX.Text = x.ToString();
            txtY.Text = y.ToString();
            txtW.Text = width.ToString();
            txtH.Text = height.ToString();

            roi = new Rect(x, y, width, height);

            if (x < 0)
            {
                x = 0;
                textBox1.Text = "位置越界";
                return false;
            }

            if (y < 0)
            {
                y = 0;
                textBox1.Text = "位置越界";
                return false;
            }

            bgImg = Cv2.ImRead(image_path, ImreadModes.Color);

            if (roi.Bottom > bgImg.Height)
            {
                textBox1.Text = "位置越界";
                bgImg.Dispose();
                return false;
            }

            if (roi.Right > bgImg.Width)
            {
                textBox1.Text = "位置越界";
                bgImg.Dispose();
                return false;
            }

            pos = new Mat(bgImg, roi);
            car.CopyTo(pos, mask);
            pictureBox2.Image = new Bitmap(bgImg.ToMemoryStream());

            pos.Dispose();
            bgImg.Dispose();

            return true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            step = Convert.ToInt32(txtStep.Text);
            x = x + step;
            ShowCar();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bgImg = Cv2.ImRead(image_path, ImreadModes.Color);
            car_bgra = Cv2.ImRead("car1.png", ImreadModes.Unchanged);
            car = Cv2.ImRead("car1.png", ImreadModes.Color);
            //透明图
            //B,G,R,A   B,G,R -> 0 黑色 255 白色
            //A为透明度 -> 255为不透明,0为全透。
            var alphaBgr = Cv2.Split(car_bgra);
            mask = alphaBgr[3];

            x = 0;
            y = 0;
            width = car.Cols;
            height = car.Rows;

            ShowCar();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            x = 1120;
            y = 0;
            width = 400;
            height = 400;

            Cv2.Resize(mask, mask, new OpenCvSharp.Size(width, height));
            Cv2.Resize(car, car, new OpenCvSharp.Size(width, height));
            Cv2.Resize(car_bgra, car_bgra, new OpenCvSharp.Size(width, height));
            bgImg = Cv2.ImRead(image_path, ImreadModes.Color);

            VideoWriter vwriter = new VideoWriter("out.mp4", FourCC.X264, 30, new OpenCvSharp.Size(bgImg.Width, bgImg.Height));

            int count = 0;
            for (int i = 0; i < 200; i++)
            {
                y = y + 10;

                width = width + 2;
                height = height + 2;

                car_bgra = Cv2.ImRead("car1.png", ImreadModes.Unchanged);
                car = Cv2.ImRead("car1.png", ImreadModes.Color);

                Cv2.Resize(car_bgra, car_bgra, new OpenCvSharp.Size(width, height));
                Cv2.Resize(car, car, new OpenCvSharp.Size(width, height));

                var alphaBgr = Cv2.Split(car_bgra);
                mask = alphaBgr[3];

                roi = new Rect(x, y, width, height);

                if (roi.Bottom > bgImg.Height)
                {
                    textBox1.Text = "位置越界";
                    break;
                }

                if (roi.Right > bgImg.Width)
                {
                    textBox1.Text = "位置越界";
                    break;
                }

                bgImg = Cv2.ImRead(image_path, ImreadModes.Color);
                pos = new Mat(bgImg, roi);
                car.CopyTo(pos, mask);

                Cv2.ImWrite("out/" + i + ".jpg", bgImg);
                vwriter.Write(bgImg);
                count++;
                //pos.Dispose();
                //bgImg.Dispose();
            }
            pictureBox2.Image = new Bitmap(bgImg.ToMemoryStream());

            //Cv2.ImWrite("result.jpg", bgImg);
            //double costTime = stopwatch.Elapsed.TotalMilliseconds;
            //textBox1.Text = $"耗时:{costTime:F2}ms";
            textBox1.Text = "生成完毕!count=" + count.ToString();
            textBox1.Text += string.Format("x={0};y={1};width={2};height={3}", x, y, width, height);
            vwriter.Release();
        }
    }
}

  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. namespace OpenCvSharp_Demo
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. string image_path;
  16. Stopwatch stopwatch = new Stopwatch();
  17. private void Form1_Load(object sender, EventArgs e)
  18. {
  19. image_path = "bg_img.jpg";
  20. pictureBox1.Image = new Bitmap(image_path);
  21. }
  22. Mat bgImg;
  23. Mat car_bgra;
  24. Mat car;
  25. Mat mask;
  26. Rect roi;
  27. Mat pos;
  28. int x, y, width, height, step;
  29. private void button6_Click(object sender, EventArgs e)
  30. {
  31. x = Convert.ToInt32(txtX.Text);
  32. y = Convert.ToInt32(txtY.Text);
  33. width = Convert.ToInt32(txtW.Text);
  34. height = Convert.ToInt32(txtH.Text);
  35. Cv2.Resize(mask, mask, new OpenCvSharp.Size(width, height));
  36. Cv2.Resize(car, car, new OpenCvSharp.Size(width, height));
  37. textBox1.Text = string.Format("x={0};y={1}", x, y);
  38. ShowCar();
  39. }
  40. private void button5_Click(object sender, EventArgs e)
  41. {
  42. step = Convert.ToInt32(txtStep.Text);
  43. y = y + step;
  44. ShowCar();
  45. }
  46. private void button7_Click(object sender, EventArgs e)
  47. {
  48. step = Convert.ToInt32(txtStep.Text);
  49. x = x - step;
  50. ShowCar();
  51. }
  52. private void button1_Click(object sender, EventArgs e)
  53. {
  54. step = Convert.ToInt32(txtStep.Text);
  55. y = y - step;
  56. ShowCar();
  57. }
  58. VideoCapture capture;
  59. Mat currentFrame = new Mat();
  60. private void button8_Click(object sender, EventArgs e)
  61. {
  62. capture = new VideoCapture("out.mp4");
  63. if (!capture.IsOpened())
  64. {
  65. MessageBox.Show("打开视频文件失败");
  66. return;
  67. }
  68. capture.Read(currentFrame);
  69. if (!currentFrame.Empty())
  70. {
  71. pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
  72. timer1.Interval = (int)(1000.0 / capture.Fps);
  73. timer1.Enabled = true;
  74. }
  75. }
  76. private void timer1_Tick(object sender, EventArgs e)
  77. {
  78. capture.Read(currentFrame);
  79. if (currentFrame.Empty())
  80. {
  81. //pictureBox1.Image = null;
  82. timer1.Enabled = false;
  83. capture.Release();
  84. textBox1.Text = "播放完毕。";
  85. return;
  86. }
  87. pictureBox1.Image = BitmapConverter.ToBitmap(currentFrame);
  88. }
  89. bool ShowCar()
  90. {
  91. textBox1.Text = string.Format("x={0};y={1};width={2};height={3}", x, y, width, height);
  92. txtX.Text = x.ToString();
  93. txtY.Text = y.ToString();
  94. txtW.Text = width.ToString();
  95. txtH.Text = height.ToString();
  96. roi = new Rect(x, y, width, height);
  97. if (x < 0)
  98. {
  99. x = 0;
  100. textBox1.Text = "位置越界";
  101. return false;
  102. }
  103. if (y < 0)
  104. {
  105. y = 0;
  106. textBox1.Text = "位置越界";
  107. return false;
  108. }
  109. bgImg = Cv2.ImRead(image_path, ImreadModes.Color);
  110. if (roi.Bottom > bgImg.Height)
  111. {
  112. textBox1.Text = "位置越界";
  113. bgImg.Dispose();
  114. return false;
  115. }
  116. if (roi.Right > bgImg.Width)
  117. {
  118. textBox1.Text = "位置越界";
  119. bgImg.Dispose();
  120. return false;
  121. }
  122. pos = new Mat(bgImg, roi);
  123. car.CopyTo(pos, mask);
  124. pictureBox2.Image = new Bitmap(bgImg.ToMemoryStream());
  125. pos.Dispose();
  126. bgImg.Dispose();
  127. return true;
  128. }
  129. private void button3_Click(object sender, EventArgs e)
  130. {
  131. step = Convert.ToInt32(txtStep.Text);
  132. x = x + step;
  133. ShowCar();
  134. }
  135. private void button2_Click(object sender, EventArgs e)
  136. {
  137. bgImg = Cv2.ImRead(image_path, ImreadModes.Color);
  138. car_bgra = Cv2.ImRead("car1.png", ImreadModes.Unchanged);
  139. car = Cv2.ImRead("car1.png", ImreadModes.Color);
  140. //透明图
  141. //B,G,R,A B,G,R -> 0 黑色 255 白色
  142. //A为透明度 -> 255为不透明,0为全透。
  143. var alphaBgr = Cv2.Split(car_bgra);
  144. mask = alphaBgr[3];
  145. x = 0;
  146. y = 0;
  147. width = car.Cols;
  148. height = car.Rows;
  149. ShowCar();
  150. }
  151. private void button4_Click(object sender, EventArgs e)
  152. {
  153. x = 1120;
  154. y = 0;
  155. width = 400;
  156. height = 400;
  157. Cv2.Resize(mask, mask, new OpenCvSharp.Size(width, height));
  158. Cv2.Resize(car, car, new OpenCvSharp.Size(width, height));
  159. Cv2.Resize(car_bgra, car_bgra, new OpenCvSharp.Size(width, height));
  160. bgImg = Cv2.ImRead(image_path, ImreadModes.Color);
  161. VideoWriter vwriter = new VideoWriter("out.mp4", FourCC.X264, 30, new OpenCvSharp.Size(bgImg.Width, bgImg.Height));
  162. int count = 0;
  163. for (int i = 0; i < 200; i++)
  164. {
  165. y = y + 10;
  166. width = width + 2;
  167. height = height + 2;
  168. car_bgra = Cv2.ImRead("car1.png", ImreadModes.Unchanged);
  169. car = Cv2.ImRead("car1.png", ImreadModes.Color);
  170. Cv2.Resize(car_bgra, car_bgra, new OpenCvSharp.Size(width, height));
  171. Cv2.Resize(car, car, new OpenCvSharp.Size(width, height));
  172. var alphaBgr = Cv2.Split(car_bgra);
  173. mask = alphaBgr[3];
  174. roi = new Rect(x, y, width, height);
  175. if (roi.Bottom > bgImg.Height)
  176. {
  177. textBox1.Text = "位置越界";
  178. break;
  179. }
  180. if (roi.Right > bgImg.Width)
  181. {
  182. textBox1.Text = "位置越界";
  183. break;
  184. }
  185. bgImg = Cv2.ImRead(image_path, ImreadModes.Color);
  186. pos = new Mat(bgImg, roi);
  187. car.CopyTo(pos, mask);
  188. Cv2.ImWrite("out/" + i + ".jpg", bgImg);
  189. vwriter.Write(bgImg);
  190. count++;
  191. //pos.Dispose();
  192. //bgImg.Dispose();
  193. }
  194. pictureBox2.Image = new Bitmap(bgImg.ToMemoryStream());
  195. //Cv2.ImWrite("result.jpg", bgImg);
  196. //double costTime = stopwatch.Elapsed.TotalMilliseconds;
  197. //textBox1.Text = $"耗时:{costTime:F2}ms";
  198. textBox1.Text = "生成完毕!count=" + count.ToString();
  199. textBox1.Text += string.Format("x={0};y={1};width={2};height={3}", x, y, width, height);
  200. vwriter.Release();
  201. }
  202. }
  203. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/138620645"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top