1. 程式人生 > >qt用opencv進行識別時出現程式異常停止錯誤,求各位大神解答。

qt用opencv進行識別時出現程式異常停止錯誤,求各位大神解答。

qt用opencv進行識別時出現程式異常停止錯誤

pro檔案:

————————————————-

#

Project created by QtCreator 2018-07-28T20:39:51

#

————————————————-

QT += core gui

QT += multimedia

QT += multimediawidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = RiceData
TEMPLATE = app

SOURCES += \
main.cpp \
mainwindow.cpp

HEADERS += \
mainwindow.h

FORMS += \
mainwindow.ui

INCLUDEPATH +=C:/OpenCV/include/opencv\
C:/OpenCV/include/opencv2\
C:/OpenCV/include

LIBS += -lopencv_core\
-lopencv_imgproc\
-lopencv_highgui\
-lopencv_ml\
-lopencv_video\
-lopencv_features2d\
-lopencv_calib3d\
-lopencv_objdetect\
-lopencv_contrib\
-lopencv_legacy\
-lopencv_flann
unix|win32: LIBS += -L$$PWD/../include/ -lLibUsart

INCLUDEPATH +=

PWD/../includeDEPENDPATH+=PWD/../include

unix|win32: LIBS += -L$$PWD/../include/ -lMvCameraControl

INCLUDEPATH +=

PWD/../includeDEPENDPATH+=PWD/../include

unix|win32: LIBS += -L$$PWD/../include/ -lMvSdkExport

INCLUDEPATH +=

PWD/../includeDEPENDPATH+=PWD/../include

mainWindow.cpp檔案:

include “mainwindow.h”

include “ui_mainwindow.h”

include

define IMAGE_HEIGHT 192

define IMAGE_WIDTH 192

define IMAGE_CROP_HEIGHT 250

define IMAGE_GROP_WIDTH 350

using namespace std;
using namespace cv;

Mat image_0(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3);
Mat image_1(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3);
Mat image_2(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3);

void __stdcall ImageCallBack_0(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
void __stdcall ImageCallBack_1(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
void __stdcall ImageCallBack_2(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);

void ImageCrop_0(Mat & image);
void ImageCrop_1(Mat & image);
void ImageCrop_2(Mat & image);
void CreateDir(string dir);

CvRect Rect_image_0;
CvRect Rect_image_1;
CvRect Rect_image_2;

bool Capture_0 = false;
bool Capture_1 = false;
bool Capture_2 = false;

int lightLevel = 0;
int riceClass = 0;
int riceLabel = 0;

string outputPath = “D:/RiceDataset/”;

MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

int nRet = MV_OK;

memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
//列舉裝置
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_EnumDevices fail!"<< nRet;
}
unsigned int nIndex = 0;
if (stDeviceList.nDeviceNum > 0)
{
   InitCamera();
}
else
{
    qDebug()<<"Find No GIGE Devices!";

}

}

void __stdcall ImageCallBack_0(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (pFrameInfo)
{
Mat image(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3, pData);
//ImageCrop_0(image);
char szInfo[256] = { 0 };
SYSTEMTIME sys;
GetLocalTime(&sys);
string path = outputPath + to_string(lightLevel) + “/” + to_string(riceClass) + “/” + to_string(riceLabel) + “/picture_0”;
CreateDir(path);
sprintf_s(szInfo, 256, “%s/%d-%02d-%02d-%02d-%02d-%02d-%04d-width[%d]-height[%d].jpg”, path.c_str(), sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, image.cols, image.rows);
//sprintf_s(szInfo, 256, “./picture_0/%d-%02d-%02d-%02d-%02d-%02d-%04d-width[%d]-height[%d].jpg”, sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, image.cols, image.rows);
imwrite(szInfo, image);
resize(image, image, Size(IMAGE_WIDTH, IMAGE_HEIGHT), (0, 0), (0, 0));
image_0 = image;
Capture_0 = true;
}
}
void __stdcall ImageCallBack_1(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (pFrameInfo)
{
Mat image(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3, pData);
//Sleep(20);
//ImageCrop_1(image);
char szInfo[256] = { 0 };
SYSTEMTIME sys;
string path = outputPath + to_string(lightLevel) + “/” + to_string(riceClass) + “/” + to_string(riceLabel) + “/picture_1”;
CreateDir(path);
GetLocalTime(&sys);
sprintf_s(szInfo, 256, “%s/%d-%02d-%02d-%02d-%02d-%02d-%04d-width[%d]-height[%d].jpg”, path.c_str(), sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, image.cols, image.rows);
//sprintf_s(szInfo, 256, “./picture_1/%d-%02d-%02d-%02d-%02d-%02d-%04d-width[%d]-height[%d].jpg”, sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, image.cols, image.rows);
imwrite(szInfo, image);
resize(image, image, Size(IMAGE_WIDTH, IMAGE_HEIGHT), (0, 0), (0, 0));
image_1 = image;
Capture_1 = true;
}
}
void __stdcall ImageCallBack_2(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (pFrameInfo)
{
Mat image(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3, pData);
//Sleep(40);
//ImageCrop_2(image);
char szInfo[256] = { 0 };
SYSTEMTIME sys;
string path = outputPath + to_string(lightLevel) + “/” + to_string(riceClass) + “/” + to_string(riceLabel) + “/picture_2”;
CreateDir(path);
GetLocalTime(&sys);
sprintf_s(szInfo, 256, “%s/%d-%02d-%02d-%02d-%02d-%02d-%04d-width[%d]-height[%d].jpg”, path.c_str(), sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, image.cols, image.rows);
//sprintf_s(szInfo, 256, “./picture_2/%d-%02d-%02d-%02d-%02d-%02d-%04d-width[%d]-height[%d].jpg”, sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, image.cols, image.rows);
imwrite(szInfo, image);
resize(image, image, Size(IMAGE_WIDTH, IMAGE_HEIGHT), (0, 0), (0, 0));
image_2 = image;
Capture_2 = true;
}
}

void ImageCrop_0(Mat & image)
{

Mat pBinary_temp;
pBinary_temp.create(image.rows, image.cols, CV_8UC1);
cvtColor(image, pBinary_temp, CV_BGR2GRAY);
IplImage pBinary(pBinary_temp);
double dConArea;
CvSeq *pContour = NULL;
CvSeq *pConInner = NULL;
CvMemStorage *pStorage = NULL;
// 執行條件
if (pBinary.height != 0 && pBinary.width != 0)
{
    // 查詢所有輪廓
    pStorage = cvCreateMemStorage(0);
    cvFindContours(&pBinary, pStorage, &pContour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    // 填充所有輪廓
    cvDrawContours(&pBinary, pContour, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 2, CV_FILLED, 8, cvPoint(0, 0));
    // 外輪廓迴圈
    int wai = 0;
    int nei = 0;
    for (; pContour != NULL; pContour = pContour->h_next)
    {
        wai++;
        CvRect rect = cvBoundingRect(pContour, 0);
        if (rect.width > 20 && rect.height > 20)
        {
            cout <<"0--->"<< "rect.x:" << rect.x << " " << "rect.y:" << rect.y << " " << "rect.width:" << rect.width << " " << "rect.height:" << rect.height << endl;
            //Mat ROI(image, Rect(rect.x, rect.y, rect.width, rect.height));
            //ROI.copyTo(image);
            ////Rect_image_0 = rect;
            //Range R1;
            //R1.start = rect.x - (IMAGE_CROP_WIDTH - rect.width) / 2;
            //R1.end = rect.x + rect.width + (IMAGE_CROP_WIDTH - rect.width);
            //cout << "R1_0" << " " << rect.x << rect.y << " " << R1.start << " " << R1.end << endl;
            //Range R2;
            //R2.start = rect.y - (IMAGE_CROP_HEIGHT - rect.height) / 2;
            //R2.end = rect.y + rect.height + (IMAGE_CROP_HEIGHT - rect.height);
            //image = image(R2, R1);
        }
    }
    //printf("wai = %d, nei = %d", wai, nei);
    cvReleaseMemStorage(&pStorage);
    pStorage = NULL;
}

}
void ImageCrop_1(Mat & image)
{

Mat pBinary_temp;
pBinary_temp.create(image.rows, image.cols, CV_8UC1);
cvtColor(image, pBinary_temp, CV_BGR2GRAY);
IplImage pBinary(pBinary_temp);
double dConArea;
CvSeq *pContour = NULL;
CvSeq *pConInner = NULL;
CvMemStorage *pStorage = NULL;
// 執行條件
if (pBinary.height != 0 && pBinary.width != 0)
{
    // 查詢所有輪廓
    pStorage = cvCreateMemStorage(0);
    cvFindContours(&pBinary, pStorage, &pContour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    // 填充所有輪廓
    cvDrawContours(&pBinary, pContour, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 2, CV_FILLED, 8, cvPoint(0, 0));
    // 外輪廓迴圈
    int wai = 0;
    int nei = 0;
    for (; pContour != NULL; pContour = pContour->h_next)
    {
        wai++;
        CvRect rect = cvBoundingRect(pContour, 0);
        if (rect.width > 20 && rect.height > 20)
        {
            cout << "1--->"<<"rect.x:" << rect.x << " " << "rect.y:" << rect.y << " " << "rect.width:" << rect.width << " " << "rect.height:" << rect.height << endl;
            //Mat ROI(image, Rect(rect.x, rect.y, rect.width, rect.height));
            //ROI.copyTo(image);
            ////Rect_image_1 = rect;
            //Range R1;
            //R1.start = rect.x - (IMAGE_CROP_WIDTH - rect.width) / 2;
            //R1.end = rect.x + rect.width + (IMAGE_CROP_WIDTH - rect.width);
            //cout << "R1_1" << " " << rect.x << rect.y << " " << R1.start << " " << R1.end << endl;
            //Range R2;
            //R2.start = rect.y - (IMAGE_CROP_HEIGHT - rect.height) / 2;
            //R2.end = rect.y + rect.height + (IMAGE_CROP_HEIGHT - rect.height);
            //image = image(R2, R1);
        }
    }
    //printf("wai = %d, nei = %d", wai, nei);
    cvReleaseMemStorage(&pStorage);
    pStorage = NULL;
}

}
void ImageCrop_2(Mat & image)
{

Mat pBinary_temp;
pBinary_temp.create(image.rows, image.cols, CV_8UC1);
cvtColor(image, pBinary_temp, CV_BGR2GRAY);
IplImage pBinary(pBinary_temp);
double dConArea;
CvSeq *pContour = NULL;
CvSeq *pConInner = NULL;
CvMemStorage *pStorage = NULL;
// 執行條件
if (pBinary.height != 0 && pBinary.width != 0)
{
    // 查詢所有輪廓
    pStorage = cvCreateMemStorage(0);
    cvFindContours(&pBinary, pStorage, &pContour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    // 填充所有輪廓
    cvDrawContours(&pBinary, pContour, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 2, CV_FILLED, 8, cvPoint(0, 0));
    // 外輪廓迴圈
    int wai = 0;
    int nei = 0;
    for (; pContour != NULL; pContour = pContour->h_next)
    {
        wai++;
        CvRect rect = cvBoundingRect(pContour, 0);
        //rectangle(matImage, cvPoint(20, 200), cvPoint(200, 300), Scalar(255, 0, 0), 1, 1, 0);
        if (rect.width > 20 && rect.height > 20)
        {
            cout <<"2--->"<< "rect.x:" << rect.x << " " << "rect.y:" << rect.y << " " << "rect.width:" << rect.width << " " << "rect.height:" << rect.height << endl;
            //Mat ROI(image, Rect(rect.x, rect.y, rect.width, rect.height));
            //ROI.copyTo(image);
            ////Rect_image_2 = rect;
            //Range R1;
            //R1.start = rect.x - (IMAGE_CROP_WIDTH - rect.width) / 2;
            //R1.end = rect.x + rect.width + (IMAGE_CROP_WIDTH - rect.width);
            //cout << "R1_2" << R1.start << " " << R1.end << endl;
            //Range R2;
            //R2.start = rect.y - (IMAGE_CROP_HEIGHT - rect.height) / 2;
            //R2.end = rect.y + rect.height + (IMAGE_CROP_HEIGHT - rect.height);
            ////image = Mat(image, R2, R1);
            //image = image(R2, R1);
        }
    }
    //printf("wai = %d, nei = %d", wai, nei);
    cvReleaseMemStorage(&pStorage);
    pStorage = NULL;

}

}

void CreateDir(string dir)
{
int m = 0, n;
string str1=dir;
string str2;
str2 = str1.substr(0, 2);//獲取磁碟位置
str1 = str1.substr(3, str1.size());
while (m >= 0)
{
m = str1.find(‘/’);
str2 += ‘/’ + str1.substr(0, m);
n = _access(str2.c_str(), 0);
if (n == -1)
{
_mkdir(str2.c_str());
}
str1 = str1.substr(m + 1, str1.size());
}
}

void MainWindow::InitCamera()
{
int nRet = MV_OK;

//選擇裝置並建立控制代碼
nRet = MV_CC_CreateHandle(&Camera_0, stDeviceList.pDeviceInfo[0]);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_CreateHandle fail!"<<nRet;

}
nRet = MV_CC_CreateHandle(&Camera_1, stDeviceList.pDeviceInfo[1]);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_CreateHandle fail!"<<nRet;

}
nRet = MV_CC_CreateHandle(&Camera_2, stDeviceList.pDeviceInfo[2]);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_CreateHandle fail!"<<nRet;

}
//註冊回撥
nRet = MV_CC_RegisterImageCallBackForBGR(Camera_0, ImageCallBack_0, NULL);
if (MV_OK != nRet)
{
    qDebug()<<"error: RegisterImageCallBackForBGR fail"<<nRet;

}
nRet = MV_CC_RegisterImageCallBackForBGR(Camera_1, ImageCallBack_1, NULL);
if (MV_OK != nRet)
{
    qDebug()<<"error: RegisterImageCallBackForBGR fail"<<nRet;

}
nRet = MV_CC_RegisterImageCallBackForBGR(Camera_2, ImageCallBack_2, NULL);
if (MV_OK != nRet)
{
    qDebug()<<"error: RegisterImageCallBackForBGR fail"<<nRet;

}
// 開啟裝置
nRet = MV_CC_OpenDevice(Camera_0);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_OpenDevice fail!"<<nRet;

}
nRet = MV_CC_OpenDevice(Camera_1);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_OpenDevice fail!"<<nRet;

}
nRet = MV_CC_OpenDevice(Camera_2);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_OpenDevice fail!"<<nRet;

}
// 設定引數
// 關閉觸發
//nRet = MV_CC_SetEnumValue(Camera_0, "TriggerMode", MV_TRIGGER_MODE_ON);
//if (MV_OK != nRet)
//{
//  printf("Set TriggerMode failed[%x]!\n", nRet);
//  return -1;
//}
//nRet = MV_CC_SetEnumValue(Camera_1, "TriggerMode", MV_TRIGGER_MODE_ON);
//if (MV_OK != nRet)
//{
//  printf("Set TriggerMode failed[%x]!\n", nRet);
//  return -1;
//}
//nRet = MV_CC_SetEnumValue(Camera_2, "TriggerMode", MV_TRIGGER_MODE_ON);
//if (MV_OK != nRet)
//{
//  printf("Set TriggerMode failed[%x]!\n", nRet);
//  return -1;
//}
// 使用連續模式
/*nRet = MV_CC_SetEnumValue(Camera_0, "AcquisitionMode", MV_ACQ_MODE_CONTINUOUS);
    if (MV_OK != nRet)
    {
    printf("Set AcquisitionMode failed[%x]!\n", nRet);

    }
    nRet = MV_CC_SetEnumValue(Camera_1, "AcquisitionMode", MV_ACQ_MODE_CONTINUOUS);
    if (MV_OK != nRet)
    {
    printf("Set AcquisitionMode failed[%x]!\n", nRet);

    }
nRet = MV_CC_SetEnumValue(Camera_2, "AcquisitionMode", MV_ACQ_MODE_CONTINUOUS);
    if (MV_OK != nRet)
    {
    printf("Set AcquisitionMode failed[%x]!\n", nRet);

    }
    */
// 開始抓圖

nRet = MV_CC_StartGrabbing(Camera_0);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_StartGrabbing fail!"<<nRet;

}
nRet = MV_CC_StartGrabbing(Camera_1);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_StartGrabbing fail!"<<nRet;

}
nRet = MV_CC_StartGrabbing(Camera_2);
if (MV_OK != nRet)
{
    qDebug()<<"MV_CC_StartGrabbing fail!"<<nRet;

}
/*
while (1)
    {
        Capture_0 = false;
        Capture_1 = false;
        Capture_2 = false;
        cvNamedWindow("capture_image_0", CV_WINDOW_AUTOSIZE);
        cvNamedWindow("capture_image_1", CV_WINDOW_AUTOSIZE);
        cvNamedWindow("capture_image_2", CV_WINDOW_AUTOSIZE);

        imshow("capture_image_0", image_0);
        imshow("capture_image_1", image_1);
        imshow("capture_image_2", image_2);
        while (!Capture_0 || !Capture_1 || !Capture_2)
        {
        cvWaitKey(2);
        }
    }
    */

//顯示影象

HWND MainWindowID1=(HWND)this->ui->label_2->winId();
nRet=MV_CC_Display(Camera_0,MainWindowID1);
HWND MainWindowID2=(HWND)this->ui->label_3->winId();
nRet=MV_CC_Display(Camera_1,MainWindowID2);
HWND MainWindowID3=(HWND)this->ui->label_4->winId();
nRet=MV_CC_Display(Camera_2,MainWindowID3);

}

void MainWindow::CloseCamera()
{
int nRet=MV_OK;
//結束抓圖
nRet = MV_CC_StopGrabbing(Camera_0);
if (MV_OK != nRet)
{
qDebug()<<”MV_CC_StopGrabbing fail!”<