첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드
TestProject.z01
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.OCR;
using Emgu.CV.Structure;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// TESSERACT
        /// </summary>
        private Tesseract tesseract;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.tesseract = new Tesseract("", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);

            this.languageValueLabel.Text = "eng : tesseract + cube";

            this.loadLanguageMenuItem.Click += loadLanguageMenuItem_Click;
            this.loadImageButton.Click      += loadImageButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 언어 로드 메뉴 항목 클릭시 처리하기 - loadLanguageMenuItem_Click(sender, e)

        /// <summary>
        /// 언어 로드 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void loadLanguageMenuItem_Click(object sender, EventArgs e)
        {
            if(this.languageOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.tesseract.Dispose();

                string filePath = Path.GetDirectoryName(this.languageOpenFileDialog.FileName);

                string language =  Path.GetFileNameWithoutExtension(this.languageOpenFileDialog.FileName).Split('.')[0];

                this.tesseract = new Tesseract(filePath, language, Tesseract.OcrEngineMode.OEM_DEFAULT);

                this.languageValueLabel.Text = String.Format("{0} : tesseract", language);
            }
        }

        #endregion
        #region 이미지 로드 버튼 클릭시 처리하기 - loadImageButton_Click(sender, e)

        /// <summary>
        /// 이미지 로드 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void loadImageButton_Click(object sender, EventArgs e)
        {
            if(this.imageOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                Bgr blueBgr = new Bgr(Color.Blue);

                try
                {
                    Image<Bgr, Byte> image = new Image<Bgr, byte>(this.imageOpenFileDialog.FileName);

                    using(Image<Gray, byte> grayImage = image.Convert<Gray, Byte>())
                    {
                        this.tesseract.Recognize(grayImage);

                        Tesseract.Charactor[] charactorArray = this.tesseract.GetCharactors();

                        foreach(Tesseract.Charactor character in charactorArray)
                        {
                            image.Draw(character.Region, blueBgr, 1);
                        }

                        this.imageBox.Image = image;

                        string result = this.tesseract.GetText();

                        this.resultTextBox.Text = result;
                    }
                }
                catch(Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요