• 大小: 0.27M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-06-19
  • 语言: C#
  • 标签: opencv  pe  NC  区域  识别  

资源简介

基于OpenCVsharp的C#程序用于提取文字区域

资源截图

代码片段和文件信息

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        string fileName;
        Mat mat;

        private void Form1_Load(object sender EventArgs e)
        {
            fileName = @Application.StartupPath + @“\test.png“;
            mat = new Mat(fileName);
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.Image = mat.ToBitmap();
        }

        public void findTextRegion(Mat dilation)
        {
            // 1. 查找轮廓
            OpenCvSharp.Point[][] contours;
            HierarchyIndex[] hierarchly;
            Rect biggestContourRect = new Rect();

            Cv2.FindContours(dilation out contours out hierarchly RetrievalModes.Tree ContourApproximationModes.ApproxSimple);

            // 2. 筛选那些面积小的
            int i = 0;
            foreach (OpenCvSharp.Point[] contour in contours)
            {
                double area = Cv2.ContourArea(contour);

                //面积小的都筛选掉
                if (area < 1000)
                {
                    continue;
                }

                //轮廓近似,作用很小
                double epsilon = 0.001 * Cv2.ArcLength(contour true);

                //找到最小的矩形
                biggestContourRect = Cv2.BoundingRect(contour);

                if (biggestContourRect.Height > (biggestContourRect.Width * 1.2))
                {
                    continue;
                }

                //画线
                mat.Rectangle(biggestContourRectnew Scalar(0 255 0) 2);
            }

            pictureBox1.Image = mat.ToBitmap();
            //Cv2.ImShow(“img“ mat);
        }


        public Mat preprocess(string imgPath)
        {
            Mat dilation2 = new Mat();

            //读取灰度图
            using (Mat src = new Mat(imgPath ImreadModes.Grayscale))
            {
                //1.Sobel算子,x方向求梯度
                Mat sobel = new Mat();
                Cv2.Sobel(src sobel MatType.CV_8U 1 0 3);

                //2.二值化
                Mat binary = new Mat();
                Cv2.Threshold(sobel binary 0 255 ThresholdTypes.Otsu | ThresholdTypes.Binary);

                //3. 膨胀和腐蚀操作的核函数
                Mat element1 = new Mat();
                Mat element2 = new Mat();
                OpenCvSharp.Size size1 = new OpenCvSharp.Size(30 9);
                OpenCvSharp.Size size2 = new OpenCvSharp.Size(24 6);

                element1 = Cv2.GetStructuringElement(MorphShapes.Rect size1);
                element2 = Cv2.GetStructuringElement(MorphShapes.Rect size2);

                //4. 膨胀一次,让轮廓突出
                Mat dilation = new Mat();
                Cv2.Dilate(binary dilation element2);

     

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-04-26 06:48  OpenCVSharpDemo-master\
     文件          10  2020-04-26 06:48  OpenCVSharpDemo-master\.gitignore
     目录           0  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\
     目录           0  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\
     目录           0  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\v16\
     文件       90112  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\v16\.suo
     目录           0  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\v16\Server\
     目录           0  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\v16\Server\sqlite3\
     文件           0  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\v16\Server\sqlite3\db.lock
     文件      622592  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\v16\Server\sqlite3\storage.ide
     文件       32768  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\v16\Server\sqlite3\storage.ide-shm
     文件      197792  2020-04-26 06:48  OpenCVSharpDemo-master\.vs\OpenCVSharpDemo\v16\Server\sqlite3\storage.ide-wal
     文件        1126  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo.sln
     目录           0  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\
     文件          10  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\.gitignore
     文件        3324  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Form1.Designer.cs
     文件        3702  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Form1.cs
     文件        5698  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Form1.resx
     文件        5421  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\OpenCVSharpDemo.csproj
     文件         475  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Program.cs
     目录           0  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Properties\
     文件        1286  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Properties\AssemblyInfo.cs
     文件        2772  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Properties\Resources.Designer.cs
     文件        5496  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Properties\Resources.resx
     文件        1072  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Properties\Settings.Designer.cs
     文件         242  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\Properties\Settings.settings
     文件         152  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\packages.config
     文件       79127  2020-04-26 06:48  OpenCVSharpDemo-master\OpenCVSharpDemo\test.png
     文件          71  2020-04-26 06:48  OpenCVSharpDemo-master\README.md

评论

共有 条评论