效果
项目
代码
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);
}
}
}