Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何修复OpenCV分段错误?_C++_Opencv - Fatal编程技术网

C++ 如何修复OpenCV分段错误?

C++ 如何修复OpenCV分段错误?,c++,opencv,C++,Opencv,我正在尝试使用openCV检测红色圆形对象,并围绕该对象绘制一个圆。但是,当我使用圆函数绘制圆时,会出现分割错误。我不知道它为什么会发生,以及如何修复它?谢谢 #include <opencv/cvaux.h> #include <opencv/highgui.h> #include <opencv/cxcore.h> #include <stdlib.h> #include <cv.hpp> #include <cxcore.

我正在尝试使用openCV检测红色圆形对象,并围绕该对象绘制一个圆。但是,当我使用圆函数绘制圆时,会出现分割错误。我不知道它为什么会发生,以及如何修复它?谢谢

#include <opencv/cvaux.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>

#include <stdlib.h>
#include <cv.hpp>
#include <cxcore.hpp>
#include <highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

#include<stdio.h>
#include<math.h>
#include<opencv/cv.h>
#include<opencv/highgui.h>
#include<opencv2/objdetect/objdetect.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<vector>

using namespace cv;  // if you don want to use scope resolution operator(::) in the code to call the classes or functions from cv namespace, you need this line
using namespace std; // if you don want to use scope resolution operator(::) in the code to call the classes or functions from std namespace, you need this line


int main(int argc, char* argv[]){

VideoCapture capWebcam(0);  //use scope resolution operator :: because VideoCapture is a class under namespace of cv
                                //use VideoCapture class to instantiate an object called capWebcam; here used the constructor of the object immediately to
                                //grab the only (0) camera

if(capWebcam.isOpened()==false){  //check whether the camera is detected and successfully grabbed


    printf("Error: camera not detected!!\n");
    cout<<"Error: camera not detected!!\n"<<endl;
    return(1);
}


    Mat matOriginal;   // matrix object used to store image from webcam
    Mat matProcessed;

    vector<Vec3f> vecCircles;  //declare a 3-element vector of type floats, this will be the pass by reference(i.e. a pointer) output of HoughCicles()

    vector<Vec3f>::iterator itrCircles;  //iterator for circles vector  just a counter, but has the same data type from the itrCircles' data member

    namedWindow("Original"); //window for original image
    namedWindow("Processed"); //window for Processed image

    char charCheckForEscKey =0;

    while(charCheckForEscKey!=27){   //as long as ESC is not pressed, stays in the while

            if(capWebcam.read(matOriginal) == false){   //check to see whether  the image read from webcam correctly

                    cout<<"Error: image frame not read!!\n"<<endl;
                break;
            } //

    inRange(matOriginal,     //this time we don't need to pass a pointer; we pass the image as an object instead
            Scalar(0,0,175),    //specify the lower bound of BGR we want to keep
            Scalar(100,100,256), //upper bound of BGR
            matProcessed);    //return the processed image to another object


    GaussianBlur(matProcessed,matProcessed,Size(9,9),1.5,1.5);  //take matProcessed image and blur by Gaussian filter(9x9 window with std of 1.5 in both x,y direction) and return to same object

    HoughCircles(matProcessed,
                 vecCircles,   //use vector element to receive the x,y,radius of the detected circle
                 CV_HOUGH_GRADIENT,  //algorithms used to detect circles
                 2,                   //size of image divided by this value = "accumulator resolution"
                 matProcessed.rows/4,  //min distance between the centers of two detected circles
                 100,       //upper pixel value threshold for canny edge detection to interpret as edge
                 50,        //lower pixel value threshold for canny edge detection to interpret as edge
                 10,        //min radius of a circle can be detected
                 400);      //max radius of a circle can be detected

     for(itrCircles = vecCircles.begin();itrCircles != vecCircles.end();itrCircles++)  //retrieve the x,y and radius of the detected circles from vecCircles object one by one


        cout<< "circle position x = " << (*itrCircles)[0]  //because itrCircles is a pointer(pass by reference), to get the value need to use * to dereference
                            << ",y = " << (*itrCircles)[1]
                            << ",r = " << (*itrCircles)[2] << "\n" << endl;


        // draw the center of detected circle in green
        circle(matOriginal,
               Point((int)(*itrCircles)[0],(int)(*itrCircles)[1]),
               3,
               Scalar(0,255,0),
               CV_FILLED);


        // draw the circumference of detected circle
        circle(matOriginal,
               Point((int)(*itrCircles)[0],(int)(*itrCircles)[1]),
               (int)(*itrCircles)[2],
               Scalar(0,0,255),
               3);


    imshow("Original",matOriginal);  //show the original mat(image)   in Original window
    imshow("Processed",matProcessed);// show the processed mat(image) in Processed window

    charCheckForEscKey = waitKey(10);  // delay 10 ms to allow a time gap to listen to any key pressed

} // end while


 return(0);
} // end main
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;//如果不想在代码中使用作用域解析运算符(::)从cv命名空间调用类或函数,则需要此行
使用命名空间std;//如果不想在代码中使用作用域解析运算符(::)从std命名空间调用类或函数,则需要此行
int main(int argc,char*argv[]){
VideoCapture capWebcam(0);//使用范围解析运算符::因为VideoCapture是cv命名空间下的一个类
//使用VideoCapture类实例化一个名为capWebcam的对象;这里使用该对象的构造函数立即
//抓取唯一(0)个摄像头
如果(capWebcam.isOpened()==false){//检查是否检测到摄像头并成功抓取
printf(“错误:未检测到摄像头!!\n”);

cout崩溃是由于for循环中缺少括号造成的,因此用于绘制的迭代器没有正确初始化。您应该执行以下操作:

for(itrCircles = vecCircles.begin();itrCircles != vecCircles.end();itrCircles++) 
{
    // your functions
}
我可以建议删除迭代器并使用foreach循环吗

for (const auto& circ : vecCircles)
{
     // your functions
}
这里是完整的示例,它清除了所有无用的东西(尤其是无用的标题)

#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(){
视频捕获网络摄像头(0);
如果(capWebcam.isOpened()==false){

cout崩溃是由于for循环中缺少括号造成的,因此用于绘制的迭代器没有正确初始化。您应该执行以下操作:

for(itrCircles = vecCircles.begin();itrCircles != vecCircles.end();itrCircles++) 
{
    // your functions
}
我可以建议删除迭代器并使用foreach循环吗

for (const auto& circ : vecCircles)
{
     // your functions
}
这里是完整的示例,它清除了所有无用的东西(尤其是无用的标题)

#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(){
视频捕获网络摄像头(0);
如果(capWebcam.isOpened()==false){

能否从运行调试器开始,在代码中查找崩溃。在调试器中运行时,当崩溃发生时,调试器将停止,并允许您检查和遍历函数调用堆栈,以及检查变量的值。如果调试器未在代码中停止,请遍历调用堆栈,直到到达代码,然后重试e检查导致崩溃的表达式中涉及的变量。至少,请将问题中的代码缩小到有问题的部分,并告诉我们崩溃发生的位置和变量值。您是否在调试模式下编译并链接到发行库,或者反之亦然?谢谢您的建议!!我是OpenCV的新手。我是使用调试模式,当我想要绘制检测到的圆的中心时,它会显示在第一个圆函数处发生的错误。感谢Miki修复了我的错误。您可以从运行调试器开始,在代码中查找崩溃。当您在调试器中运行时,调试器将在崩溃发生时停止,并允许您检查和遍历函数调用堆栈,并检查变量的值。如果调试器没有在代码中停止,请沿着调用堆栈走,直到到达代码,然后在那里检查导致崩溃的表达式中涉及的变量。至少,请将问题中的代码缩小到有问题的部分,并告诉我们崩溃发生的位置和原因变量值。您是否在调试模式下编译并链接到发行版库,反之亦然?谢谢您的建议!!我是OpenCV的新手。我确实使用了调试模式,它显示了当我想绘制检测到的圆的中心时,在第一个圆函数中出现的错误。感谢Miki修复了我的错误。非常感谢!!我是OpenCV的新手。非常感谢anks!!我是OpenCV新手。