我的emgu cv(c#)代码仅适用于一个图像,而其他图像则崩溃

时间:2017-03-18 15:13:08

标签: c# emgucv

我创建了一个c#程序,用于将图像转换为二进制,binary_inverse等。

porgram不知何故只适用于我先用过的图像,但每当我尝试其他图像时都会崩溃。

如果有人能帮我找到问题,我感激不尽。

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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.CV.CvEnum;

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

        private Image<Bgr, Byte> ori;
        private Image<Gray, Byte> edited;

        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                pB.ImageLocation = openFileDialog1.FileName ;
                ori = new Image <Bgr,Byte> (openFileDialog1.FileName)   ;
                edited = new Image<Gray, Byte>(ori.Width, ori.Height)   ;


            }


         }

        private void button2_Click(object sender, EventArgs e)
        {
            CvInvoke.cvCvtColor(ori, edited, COLOR_CONVERSION.CV_BGR2GRAY);
            pB.Image = edited.ToBitmap();
            groupBox1.Enabled = true;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            pB.Image = ori.ToBitmap();
            groupBox1.Enabled = false;
            button1.Enabled = false;
            button2.Enabled = true;
        }
        private void resetui()
        {
            button1.Enabled = true;
            button2.Enabled = true;
            groupBox1.Enabled = false;

        }
        private void CalcThresh()
        {
            if (cB.SelectedIndex == 0)
            {
                pB.Image = edited.ToBitmap();
                return;
            }

            Image<Gray, byte> temp = new Image<Gray, Byte>(edited.Height, edited.Width);
            double threshold = tB.Value;
            double maxval = (double) max.Value;
            THRESH mode = THRESH.CV_THRESH_BINARY;

            switch (cB.SelectedIndex)
            {
                case 1:
                    mode = THRESH.CV_THRESH_BINARY ;
                    break ;
                case 2:

                    mode = THRESH.CV_THRESH_BINARY_INV ; break ;
                case 3:
                    mode = THRESH.CV_THRESH_TOZERO; break ;
                case 4 :
                    mode = THRESH.CV_THRESH_TOZERO_INV; break ;

                case 5:
                    mode = THRESH.CV_THRESH_TRUNC; break ;
            }

            CvInvoke.cvThreshold(edited, temp, threshold, maxval, mode );
            pB.Image = temp.ToBitmap();


        }

        private void cB_SelectedIndexChanged(object sender, EventArgs e)
        {
            CalcThresh();
        }

        private void max_ValueChanged(object sender, EventArgs e)
        {
            CalcThresh();
        }

        private void tB_Scroll(object sender, EventArgs e)
        {
            CalcThresh();
        }




    }
}

继承人使用的第一张图片

This the first image that i used

它的工作正常如下:

enter image description here

但是对于其他图像,只要我选择了一种模式,它就会崩溃:

enter image description here

这张让我的节目崩溃的图片之一:

enter image description here

表格deisgn的样子如下:

enter image description here

我正在研究这个测试,所以如果有人可以帮我找到问题我真的

欣赏它。

之前的

1 个答案:

答案 0 :(得分:1)

替换它:

Image<Gray, byte> temp = new Image<Gray, Byte>(edited.Height, edited.Width);

有了这个:

Image<Gray, byte> temp = new Image<Gray, byte>(edited.Width, edited.Height);