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+= P W D / . . / i n c l u d e D E P E N D P A T H + =
PWD/../include

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

INCLUDEPATH +=

PWD/../includeDEPENDPATH+= P W D / . . / i n c l u d e D E P E N D P A T H + =
PWD/../include

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

INCLUDEPATH +=

PWD/../includeDEPENDPATH+= P W D / . . / i n c l u d e D E P E N D P A T H + =
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!"<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!”<

你可能感兴趣的:(qt用opencv进行识别时出现程序异常停止错误,求各位大神解答。)