[英]My emgu cv (c#) code only works on one image and crash for other image
I created ac# program for convert image to binary,binary_inverse and such . 我创建了一个ac#程序,用于将图像转换为binary,binary_inverse等。
the porgram somehow only works for the image i used first but crashed whenever i tried other image . porgram某种程度上仅适用于我首先使用的图像,但是每次尝试其他图像时都会崩溃。
I appreciate if someone could help me find the problem . 如果有人可以帮助我找到问题,我将不胜感激。
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();
}
}
}
Heres the first image i used 这是我使用的第一张图片
It works fine like this for example : 例如,它可以正常工作:
But for other image it crash whenever i picked a mode : 但是对于其他图像,只要我选择了一个模式,它就会崩溃:
This one of the pic that made my program crashed : 这一张使我的程序崩溃的图片:
This how the form deisgn looks like : 这样的形式deisgn看起来像:
Im studying this for test so if someone can help me find the problem i really 我正在研究此测试,以便有人能帮助我找到问题所在
appreciate it. 欣赏它。
Thx Before 在此之前
Replace this: 替换为:
Image<Gray, byte> temp = new Image<Gray, Byte>(edited.Height, edited.Width);
With this: 有了这个:
Image<Gray, byte> temp = new Image<Gray, byte>(edited.Width, edited.Height);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.