首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++优先机会例外

C++优先机会例外
EN

Stack Overflow用户
提问于 2013-11-17 12:26:44
回答 1查看 1.5K关注 0票数 0

我已经用c++写了一个形状检测代码。我正在使用Visual Studio 2013速成版桌面版。

当我运行程序时,它会给出以下错误。

Final.exe: 0xC0000005中0x54EE3C77 (opencv_imgproc244d.dll)处的第一次机会异常:访问冲突读取位置0x05958000。

如果存在针对此异常的处理程序,则程序可以安全地继续运行。

以下是代码示例。

代码语言:javascript
复制
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <thread>  
#include <cv.h>
#include <highgui.h>
#include <windows.h>




void shapeDetectionNew(IplImage* img)
{

try
{

    //converting the original image into grayscale
    IplImage* imgGrayScale = cvCreateImage(cvGetSize(img), 8, 1);
    cvCvtColor(img, imgGrayScale, CV_BGR2GRAY);

    //thresholding the grayscale image to get better results
    cvThreshold(imgGrayScale, imgGrayScale, 128, 255, CV_THRESH_BINARY);

    CvSeq* contour;  //hold the pointer to a contour
    CvSeq* result;   //hold sequence of points of a contour
    CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours

    //finding all contours in the image
    cvFindContours(imgGrayScale, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));

    //***********************************
    cv::Mat img = cv::imread("F:\\My Works\\Opencv\\Shape_Detection_Images\\shape.jpg");

    //convert IplImage to Mat
    //cv::Mat img(iplImg);

    cv::Mat gray;
    cv::cvtColor(img, gray, CV_BGR2GRAY);

    // Use Canny instead of threshold to catch squares with gradient shading
    cv::Mat bw;
    cv::Canny(gray, bw, 0, 50, 5);

    // Find contours
    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(bw.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    std::vector<cv::Point> approx;
    cv::Mat dst = img.clone();

    //***********************************

    for (int i = 0; i < contours.size(); i++)
    {
        // Approximate contour with accuracy proportional
        // to the contour perimeter
        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true)*0.02, true);

        // Skip small or non-convex objects 
        if (std::fabs(cv::contourArea(contours[i])) < 100 || !cv::isContourConvex(approx))
            continue;

        if (approx.size() == 3)
        {
            setLabel(dst, "TRI", contours[i]);    // Triangles
        }
        else if (approx.size() >= 4 && approx.size() <= 6)
        {
            // Number of vertices of polygonal curve
            int vtc = approx.size();

            // Get the cosines of all corners
            std::vector<double> cos;
            for (int j = 2; j < vtc + 1; j++)
                cos.push_back(angle(approx[j%vtc], approx[j - 2], approx[j - 1]));

            // Sort ascending the cosine values
            std::sort(cos.begin(), cos.end());

            // Get the lowest and the highest cosine
            double mincos = cos.front();
            double maxcos = cos.back();

            // Use the degrees obtained above and the number of vertices
            // to determine the shape of the contour
            if (vtc == 4 && mincos >= -0.1 && maxcos <= 0.3)
                setLabel(dst, "RECT", contours[i]);
            else if (vtc == 5 && mincos >= -0.34 && maxcos <= -0.27)
                setLabel(dst, "PENTA", contours[i]);
            else if (vtc == 6 && mincos >= -0.55 && maxcos <= -0.45)
                setLabel(dst, "HEXA", contours[i]);
        }
        else
        {
            // Detect and label circles
            double area = cv::contourArea(contours[i]);
            cv::Rect r = cv::boundingRect(contours[i]);
            int radius = r.width / 2;

            if (std::abs(1 - ((double)r.width / r.height)) <= 0.2 &&
                std::abs(1 - (area / (CV_PI * std::pow((double)radius, 2)))) <= 0.2)
                setLabel(dst, "CIR", contours[i]);
        }
    }

    cv::imshow("Shape_Detection", dst);


}
catch (int e)
{
    throw e;
}
}

int main()
{
    try
    {

    // Create CvCapture object to grab data from the webcam
    CvCapture* pCapture;

    // Start capturing data from the webcam
    pCapture = cvCaptureFromCAM(1);

    // Define the IplImage pointers we're going to use as globals
    IplImage* pFrame;

    IplImage* pProcessedFrame;
    IplImage* tempFrame;
    pFrame = cvQueryFrame(pCapture);

    //Create the low threshold slider
    // Format: Slider name, window name, reference to variable for slider, max value of slider, callback function
    cvCreateTrackbar("Low Threshold", "Edge_Detection", &lowSliderPosition, maxLowThreshold, onLowThresholdSlide);

    // Create the high threshold slider
    cvCreateTrackbar("High Threshold", "Edge_Detection", &highSliderPosition, maxHighThreshold, onHighThresholdSlide);

    // Create a greyscale image which is the size of our captured image
    pProcessedFrame = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);

    // Create a frame to use as our temporary copy of the current frame but in grayscale mode
    tempFrame = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);

    char keypress;
    bool quit = false;
    int counterCheck = 1;

    while (quit == false)
    {


        // Make an image from the raw capture data
        // Note: cvQueryFrame is a combination of cvGrabFrame and cvRetrieveFrame
        pFrame = cvQueryFrame(pCapture);

        // Draw the original frame in our window
        cvShowImage("Live_Cam", pFrame);




        shapeDetectionNew(pFrame);



    } // End of while loop

    cvDestroyAllWindows();

}
catch (Exception ex)
{

}

return 1;
}
EN

回答 1

Stack Overflow用户

发布于 2013-11-17 22:18:09

您不是在检查

代码语言:javascript
复制
 pFrame = cvQueryFrame(pCapture);

不为空。如果是,最终会将其传递给cvCvtColor(),从而引发异常。

完全不同的问题:如果不随后调用waitKey()imshow()将不会显示任何内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20027189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档