C# OpenCvSharp DNN 二维码增强 超分辨率

效果

项目

代码

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using OpenCvSharp.Extensions;

namespace OpenCvSharp_DNN_二维码增强
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        Bitmap bmp;
        String imgPath = "";

        const string prototxt_path = "sr.prototxt";
        const string caffe_model_path = "sr.caffemodel";

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            imgPath = ofd.FileName;
            bmp = new Bitmap(imgPath);
            pictureBox1.Image = bmp;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            var src = Cv2.ImRead(imgPath, ImreadModes.Grayscale);
            var srnet = CvDnn.ReadNetFromCaffe(prototxt_path, caffe_model_path);
            Mat blob = CvDnn.BlobFromImage(src, 1.0 / 255, src.Size(), new Scalar(0.0f), false, false);
            srnet.SetInput(blob);
            var prob = srnet.Forward();
            var dst = new Mat(prob.Size(2), prob.Size(3), MatType.CV_8UC1);
            for (int row = 0; row < prob.Size(2); row++)
            {
                for (int col = 0; col < prob.Size(3); col++)
                {
                    float pixel = prob.At<float>(0, 0, row, col) * 255;
                    dst.Set<byte>(row, col, (byte)(Math.Max(0, Math.Min(pixel, 255f))));
                }
            }
            pictureBox2.Image = BitmapConverter.ToBitmap(dst);

            // Cv2.ImShow("src", src);
            // Cv2.ImShow("dst", dst);
        }
    }
}

Demo下载

相关推荐
aqi002 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
用户5191495848453 小时前
libcurl Headers API 释放后重利用漏洞:跨请求复用头句柄导致堆内存安全风险
人工智能·aigc
踩蚂蚁3 小时前
自定义语音唤醒词:从训练到部署的完整链路实践
人工智能
用户5191495848453 小时前
CVE-2025-1094 PostgreSQL SQL注入与WebSocket劫持远程代码执行利用工具
人工智能·aigc
IT_陈寒4 小时前
SpringBoot自动配置这个坑,我踩进去又爬出来了
前端·人工智能·后端
冬奇Lab15 小时前
Agent 系列(23):Web Agent——让 Agent 真正浏览网页
人工智能·llm·agent
冬奇Lab15 小时前
每日一个开源项目(第135篇):codebase-memory-mcp - 给 AI Agent 一张代码库的知识图谱
人工智能·开源·llm