前言:
windows平臺使用OpenCV的findContours函式時,會出現崩潰問題,上次使用opencv3.10時,發現了這個問題,
網上找了很多解決方案都不行,最后參考這位大佬解決了這個問題,
最近把專案所用的opencv版本更新到了4.5,沒想到這個bug,還沒有被解決,由于opencv的API發生了變動,大佬提供的代碼不兼容opencv4.5,我進行了一些改動,
主要是包含了一些必備的頭檔案和兩個資料型別轉換,
code
#include "opencv2/opencv.hpp"
#include <core/types_c.h>
#include <core/core_c.h>
#include <imgproc/imgproc_c.h>
#pragma once
#include <vector>
using namespace std;
using namespace cv;
/*************************
@author: Lyu Shuai
@Time: 21/08/2021
@Source: https://blog.csdn.net/amani_liu/article/details/87932141
@Func: 解決使用OpenCV的findContours函式時,會出現崩潰問題
some changes have been made to fit opencv4.5
********************************/
void myFindContours(const Mat& src, vector<vector<Point>>& contours, vector<Vec4i>& hierarchy,int retr, int method , Point offset);
void myFindContours(const Mat& src, vector<vector<Point>>& contours, vector<Vec4i>& hierarchy,
int retr = RETR_LIST, int method = CHAIN_APPROX_SIMPLE, Point offset = Point(0, 0))
{
CvMat c_image = cvMat(src);
//Mat c_image = src;
MemStorage storage(cvCreateMemStorage());
CvSeq* _ccontours = 0;
//cvFindContours(&c_image, storage, &_ccontours, sizeof(CvContour), retr, method, CvPoint(offset));
cvFindContours(&c_image, storage, &_ccontours, sizeof(CvContour), retr, method, cvPoint(offset.x, offset.y));
if (!_ccontours)
{
contours.clear();
return;
}
Seq<CvSeq*> all_contours(cvTreeToNodeSeq(_ccontours, sizeof(CvSeq), storage));
int total = (int)all_contours.size();
contours.resize(total);
SeqIterator<CvSeq*> it = all_contours.begin();
for (int i = 0; i < total; i++, ++it)
{
CvSeq* c = *it;
((CvContour*)c)->color = (int)i;
int count = (int)c->total;
int* data = new int[count * 2];
cvCvtSeqToArray(c, data);
for (int j = 0; j < count; j++)
{
contours[i].push_back(Point(data[j * 2], data[j * 2 + 1]));
}
delete[] data;
}
hierarchy.resize(total);
it = all_contours.begin();
for (int i = 0; i < total; i++, ++it)
{
CvSeq* c = *it;
int h_next = c->h_next ? ((CvContour*)c->h_next)->color : -1;
int h_prev = c->h_prev ? ((CvContour*)c->h_prev)->color : -1;
int v_next = c->v_next ? ((CvContour*)c->v_next)->color : -1;
int v_prev = c->v_prev ? ((CvContour*)c->v_prev)->color : -1;
hierarchy[i] = Vec4i(h_next, h_prev, v_next, v_prev);
}
storage.release();
}
總結
大佬太強了,膜拜,學習!
大佬原文鏈接:
https://blog.csdn.net/amani_liu/article/details/87932141
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295761.html
標籤:其他