未處理AccessViolationException
這通常指示其他內存已損壞,這里內存損壞並非物理的內存條損壞。猜想是執行到此步驟后,內存空間被清理了,沒有找到內存地址的感覺。
public static bool RecognizeCpositiveAndNegative(PictureBox pbox_disImage1, Mat tempimg)
當調用此函數后,信息傳遞給tempimg.
Mat org_被測圖片 = tempimg.Clone();
復制tempimg的內容時發生錯誤。估計tempimg的所占內存空間被清理掉了,所以就報錯。
如何解決報錯:
else if (RevString == "check") //檢查是否防反。。。
{
// SendData("ready_ok");
g_match_image = null;
g_match_image = SimpleGrab.SimpleGrab.獲取相機圖像();
//獲取的圖像為空
if (g_match_image == null)
{
SendData("error1");
this.Invoke(new EventHandler(delegate { richTextBox1.AppendText("沒有獲取到圖像" + "\r\n"); }));
return;
}
else
{
bool result = CRecognizeCpositiveAndNegative.RecognizeCpositiveAndNegative(this.pbox_disImage1, g_match_image);
tempimg的上級調用位置是g_match_image,
g_match_image = SimpleGrab.SimpleGrab.獲取相機圖像();//即執行拍照,圖像信息保存在g_match_image里面。
既然報錯。那就不要傳遞這個參數。直接在被調用函數里面拍照。就不執行tempimg.clone()了。
---------------------
//作者:txwtech
public static bool RecognizeCpositiveAndNegative(PictureBox pbox_disImage1, Mat tempimg)
{
Mat org_放正模板 = new Mat(Application.StartupPath + "\\image\\放正模板.bmp", ImreadModes.Grayscale);
Mat org_放反模板 = new Mat(Application.StartupPath + "\\image\\放反模板.bmp", ImreadModes.Grayscale);
// Mat org_被測圖片 = tempimg.Clone();
Mat org_被測圖片 = SimpleGrab.SimpleGrab.獲取相機圖像();//直接在這里拍照獲取圖片信息。
if (tempimg.NumberOfChannels !=1)
{
CvInvoke.CvtColor(org_被測圖片, org_被測圖片, ColorConversion.Bgr2Gray);
}