宇宙中的深海光年 2022-09-16 01:01 采纳率: 100%
浏览 114
已结题

linux环境下的opencv中findcontours函数出现段错误问题

问题遇到的现象和发生背景

在linux下(centos7)使用opencv4.5.5版本的findcontours函数(轮廓检测),出现段错误(吐核)问题。
麻烦请问下这个该怎么解决呢?谢谢

用代码块功能插入代码,请勿粘贴截图

define _GLIBCXX_USE_CXX11_ABI 0

#include <stdio.h>
#include "myadd.h"
#include "opencv2/opencv.hpp"
#include <math.h>

using namespace std;
using namespace cv;

void add(char const *fpath)
{
Mat image,cannyimg;
image = imread(fpath);
imshow("img",image);
waitKey(0);
Mat gray;
cvtColor(image,gray,COLOR_BGR2GRAY);
imshow("img1",gray);
waitKey(0);
int c=image.cols, r=image.rows;
printf("c = %d, r = %d\n",c,r);
Canny(gray,cannyimg,20,120);
RNG rng(0);
Mat dst = Mat::zeros(image.size(),CV_8UC3);

    std::vector<std::vector<cv::Point>> contours;
    std::vector<cv::Vec4i> ContourTree;
    findContours(cannyimg, contours, ContourTree, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

    printf("no fail!\n");

    for(size_t i=0;i<contours.size();i++)
    {
          Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255));
          drawContours(dst, contours, i, color, 1, 8, ContourTree, 0, Point(0,0));
    }
    imshow("tupian",dst);
    waitKey(0);

}

运行结果及报错内容

段错误(吐核)

我的解答思路和尝试过的方法

findcontours函数使用错误,或者说vector存在问题。

我想要达到的结果

使报错消失

  • 写回答

4条回答 默认 最新

  • 赵4老师 2022-09-16 17:28
    关注

    仅供参考:

    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/imgproc/imgproc_c.h"
    #include "opencv2\highgui\highgui_c.h"
    using namespace std;
    using namespace cv;
    Mat img,smallImg,gray,bw;
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
    int threshval=128;
    Rect r;
    Rect maxrect,brect;
    int idx,n;
    const static Scalar colors[15]={
        CV_RGB(  0,  0,128),
        CV_RGB(  0,128,  0),
        CV_RGB(  0,128,128),
        CV_RGB(128,  0,  0),
        CV_RGB(128,  0,128),
        CV_RGB(128,128,  0),
        CV_RGB(128,128,128),
        CV_RGB(160,160,160),
        CV_RGB(  0,  0,255),
        CV_RGB(  0,255,  0),
        CV_RGB(  0,255,255),
        CV_RGB(255,  0,  0),
        CV_RGB(255,  0,255),
        CV_RGB(255,255,  0),
        CV_RGB(255,255,255),
    };
    Scalar color;
    void gamma_correct(Mat& img, Mat& dst, double gamma) {
        Mat tmp;
    
        img.convertTo(tmp, CV_32FC1, 1.0/255.0, 0.0);
        pow(tmp, gamma, tmp);
        tmp.convertTo(dst , CV_8UC1,     255.0, 0.0);
    }
    int main() {
        cvNamedWindow("display",1);
        img=imread("image.jpg",1);
        r.x     =img.cols*1/10;
        r.y     =img.rows*1/5;
        r.width =img.cols*8/10;
        r.height=img.rows*3/5;
        smallImg=img(r);
        cvtColor(smallImg,gray,CV_BGR2GRAY);
    //  medianBlur(gray,gray,5);
        equalizeHist(gray,gray);
        gamma_correct(gray,gray,4.0);
        imshow("display",gray);
        waitKey(0);
    
        bw=(gray>threshval);
        imshow("display",bw);
        waitKey(0);
    
    //  Mat Structure1=getStructuringElement(MORPH_RECT   ,Size(6,6));
        Mat Structure1=getStructuringElement(MORPH_ELLIPSE,Size(6,6));
        dilate(bw,bw,Structure1, Point(-1,-1));
        imshow("display",bw);
        waitKey(0);
    
        Mat Structure0=getStructuringElement(MORPH_RECT   ,Size(3,3));
        Mat Structure0=getStructuringElement(MORPH_ELLIPSE,Size(3,3));
        erode(bw,bw,Structure0,Point(-1,-1));
        imshow("display",bw);
        waitKey(0);
    
        findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
        if (!contours.empty()&&!hierarchy.empty()) {
            idx=0;
            n=0;
            vector<Point> approx;
            for (;idx>=0;idx=hierarchy[idx][0]) {
                color=colors[idx%15];
    //          drawContours(smallImg,contours,idx,color,1,8,hierarchy);
                approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
                const Point* p = &approx[0];
                int m=(int)approx.size();
                polylines(smallImg, &p, &m, 1, true, color);
                circle(smallImg,Point(p[0].x,p[0].y),3,color);
                circle(smallImg,Point(p[1].x,p[1].y),2,color);
                for (int i=2;i<m;i++) circle(smallImg,Point(p[i].x,p[i].y),1,color);
                n++;
                if (1==n) {
                    maxrect=boundingRect(Mat(contours[idx]));
                } else {
                    brect=boundingRect(Mat(contours[idx]));
                    CvRect mr;
                    mr.x=maxrect.x;
                    mr.y=maxrect.y;
                    mr.width=maxrect.width;
                    mr.height=maxrect.height;
                    CvRect br;
                    br.x=brect.x;
                    br.y=brect.y;
                    br.width=brect.width;
                    br.height=brect.height;
                    maxrect=cvMaxRect(&mr,&br);
                }
            }
            circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
        }
        imshow("display",smallImg);
        waitKey(0);
        cvDestroyWindow("display");
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月19日
  • 已采纳回答 9月19日
  • 修改了问题 9月16日
  • 赞助了问题酬金10元 9月16日
  • 展开全部

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效