【问题标题】:Crack detection using opencv使用opencv进行裂纹检测
【发布时间】:2016-10-26 12:47:59
【问题描述】:

嘿,我有一个破解检测代码,但在运行代码时我得到了 错误。这是代码,当我删除时,错误在 2.0f 和 2.0 中 这与此相关的所有错误都已解决,但新错误 DrawMarker 没有 在这个范围内声明。

#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
#define CL_GREEN  Scalar(0  , 255, 0  )
#define CL_RED    Scalar(0  , 0  , 255)
#define CL_BLU    Scalar(255, 0  , 0  )
#define CL_BLACK  Scalar(0  , 0  , 0  )     
void Morph(const cv::Mat &src, cv::Mat &dst, int operation =    cv::MORPH_OPEN, 
int kernel_type = cv::MORPH_RECT, int size = 1);
struct contourStats{
double area, perimeter, axisMin, axisMax, axisAvg, eccentricity, axisRatio, circularity;
Point2d center;
RotatedRect rr;
contourStats(){}
contourStats(const vector<Point> &contour)
{
    calculateStats(contour);
}
void calculateStats(const vector<Point> &contour)
{
    Moments m = cv::moments(contour, true);
    area = m.m00;
    center = Point2f(-1, -1);
    if (area > 0){
        center.x = cvRound(m.m10 / m.m00);
        center.y = cvRound(m.m01 / m.m00);
    }
    eccentricity = DBL_MAX;
    if ((m.m20 + m.m02) > 0)
        eccentricity = (pow((m.m20 - m.m02), 2) - 4 * m.m11 * m.m11) / pow((m.m20 + m.m02), 2);
    // axis ratio:circles have ratio=1 lines have ratio->0
    rr = minAreaRect(contour);
    axisMin = min(rr.size.height, rr.size.width);
    axisMax = max(rr.size.height, rr.size.width);
    axisAvg = (axisMin+axisMax) / 2.0;
    axisRatio = axisMax > 0 ? axisMin / axisMax : 0;
    perimeter = arcLength(contour, false);
    circularity = perimeter > 0 ? 4 * CV_PI * area / pow(perimeter, 2) : 0;
}
void printStats(const string &title)
{
    cout << endl << title << endl
        << "\tArea [px^2]: " << area << endl
        << "\tAxis[px]: " << axisMin << "/" << axisMax << endl
        << "\tAxis Ratio: " << axisRatio << endl
        << "\tCircularity: " << circularity << endl
        << "\tAbs(eccentricity): " << abs(eccentricity) << endl;
}
};

       int main(int argc, char** argv)
      {
    Mat src, gray, edges, cracks,tmp;
Mat biscuitMask;
//src = imread("../img/biscotto_rotated.jpg");
src = imread("../img/biscotto.jpg");
cvtColor(src, gray, CV_BGR2GRAY);
// GET ALL EDGES (CRACKS AND BISCUIT)
Canny(gray, edges, 100, 200, 3);
Morph(edges, edges, MORPH_CLOSE, MORPH_ELLIPSE, 5);
// GET THE BISCUIT ONLY
GaussianBlur(gray, gray, Size(7, 7), 0);
threshold(gray, biscuitMask, 0, 255, THRESH_OTSU);
// VALIDATE THE BISCUIT
vector<vector<Point> > contours;
biscuitMask.copyTo(tmp);
findContours(tmp, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
if ((contours.size() > 1) || (contours[0].size() < 6)) {
    cerr << "ERROR: invalid mask for the biscuit!";
    return 1;
}
vector<Point> biscuitContour(contours[0]);
contourStats theBiscuit(biscuitContour);
theBiscuit.printStats("BISCUIT STATS:");
if (theBiscuit.circularity < 0.85) {
    cout << "WARNING: invalid biscuit circularity!";
}
drawMarker(src, theBiscuit.center, CL_BLU, MARKER_CROSS, theBiscuit.axisAvg / 8);
circle(src, theBiscuit.center, theBiscuit.axisAvg / 2, CL_BLU, 2);
//polylines(src, biscuitContour, true, CL_GREEN); 

// GET ONLY THE CRACKS INSIDE THE BISCUIT
Morph(biscuitMask, biscuitMask, MORPH_ERODE,MORPH_ELLIPSE, 5);
cracks = (edges & biscuitMask);
imshow("cracks", cracks);

findContours(cracks, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

double minLenght = theBiscuit.axisAvg / 2;
double maxLenght = 0;
int biggest = -1;
vector<contourStats> crackStats(contours.size());
for (size_t i = 0; i < contours.size(); i++)
{
    drawContours(src, contours, i, CL_GREEN, 1); //all contours in GREEN
    crackStats[i].calculateStats(contours[i]);   //analize potential cracks
    if (crackStats[i].axisMax < minLenght)       //ignore short contours
        continue;
    drawContours(src, contours, i, CL_RED, 1);   //longer contours are cracks
    if (crackStats[i].axisMax > maxLenght)       // get longest crack
        biggest = i;
}
// DRAW AROUND BIGGER CRACKS
if (biggest > 0)  {
    cout << "Cracks Found! Biggest size: " << crackStats[biggest].axisMax <<
        "px (" << 100 * crackStats[biggest].axisMax / theBiscuit.axisMax << "%)" 
        << endl;
    RotatedRect rr = crackStats[biggest].rr;
    Point2f rect_points[4];
    rr.points(rect_points);
    //draw enclosing rectangle
    for (int j = 0; j < 4; j++) 
        line(src, rect_points[j], rect_points[(j + 1) % 4], CL_RED, 1, 8);
    //draw major axis
    Point2f pt0 = (rect_points[0] + rect_points[3]) / 2.0;
    Point2f pt1 = (rect_points[1] + rect_points[2]) / 2.0;
    Point2f pt2 = (rect_points[0] + rect_points[1]) / 2.0;
    Point2f pt3 = (rect_points[2] + rect_points[3]) / 2.0;
    double axis1 = norm(pt0 - pt1);
    double axis2 = norm(pt2 - pt3);
    if (axis1>axis2)
        line(src, pt0, pt1, CL_BLACK, 2);
    else
        line(src, pt2, pt3, CL_BLACK, 2);
}
imshow("Src", src);
waitKey(0);
return 0;
}
void Morph(const cv::Mat &src, cv::Mat &dst, int operation,int kernel_type, int size)
{
cv::Point anchor = cv::Point(size, size);
cv::Mat element = getStructuringElement(kernel_type, cv::Size(2 * size +  1, 2 * size + 1), anchor);
morphologyEx(src, dst, operation, element, anchor);
}

这是我的饼干图片文件.....

这里是修改后代码的输出

【问题讨论】:

    标签: opencv


    【解决方案1】:

    可能您使用的是不支持drawMarker 的旧版本的opencv。 Opencv 3.1 支持click

    我做了一些改变,现在它正在工作。请检查...

    #include "opencv/cv.h"
    #include "opencv/highgui.h"
    #include "opencv/cxcore.h"
    
    
    using namespace std;
    using namespace cv;
    
    #define CL_GREEN  Scalar(0  , 255, 0  )
    #define CL_RED    Scalar(0  , 0  , 255)
    #define CL_BLU    Scalar(255, 0  , 0  )
    #define CL_BLACK  Scalar(0  , 0  , 0  )     
    
    void Morph(const cv::Mat &src, cv::Mat &dst, int operation =    cv::MORPH_OPEN, 
    int kernel_type = cv::MORPH_RECT, int size = 1);
    
    struct contourStats
    {
        double area, perimeter, axisMin, axisMax, axisAvg, eccentricity, axisRatio, circularity;
        Point2d center;
        RotatedRect rr;
        contourStats(){}
    
        contourStats(const vector<Point> &contour)
        {
            calculateStats(contour);
        }
    
        void calculateStats(const vector<Point> &contour)
        {
            Moments m = cv::moments(contour, true);
            area = m.m00;
            center = Point2f(-1, -1);
            if (area > 0){
                center.x = cvRound(m.m10 / m.m00);
                center.y = cvRound(m.m01 / m.m00);
            }
            eccentricity = DBL_MAX;
            if ((m.m20 + m.m02) > 0)
                eccentricity = (pow((m.m20 - m.m02), 2) - 4 * m.m11 * m.m11) / pow((m.m20 + m.m02), 2);
            // axis ratio:circles have ratio=1 lines have ratio->0
            rr = minAreaRect(contour);
            axisMin = min(rr.size.height, rr.size.width);
            axisMax = max(rr.size.height, rr.size.width);
            axisAvg = (axisMin+axisMax) / 2.0;
            axisRatio = axisMax > 0 ? axisMin / axisMax : 0;
            perimeter = arcLength(contour, false);
            circularity = perimeter > 0 ? 4 * CV_PI * area / pow(perimeter, 2) : 0;
        }
    
        void printStats(const string &title)
        {
            cout << endl << title << endl
                << "\tArea [px^2]: " << area << endl
                << "\tAxis[px]: " << axisMin << "/" << axisMax << endl
                << "\tAxis Ratio: " << axisRatio << endl
                << "\tCircularity: " << circularity << endl
                << "\tAbs(eccentricity): " << abs(eccentricity) << endl;
        }
    };
    
    int main(int argc, char** argv)
    {
        Mat src, gray, edges, cracks,tmp;
        Mat biscuitMask;
        //src = imread("../img/biscotto_rotated.jpg");
        src = imread("D:/bis.jpg");
        cvtColor(src, gray, CV_BGR2GRAY);
        // GET ALL EDGES (CRACKS AND BISCUIT)
        Canny(gray, edges, 100, 200, 3);
        Morph(edges, edges, MORPH_CLOSE, MORPH_ELLIPSE, 5);
        // GET THE BISCUIT ONLY
        GaussianBlur(gray, gray, Size(7, 7), 0);
        threshold(gray, biscuitMask, 0, 255, THRESH_OTSU);
    
        // VALIDATE THE BISCUIT
        vector<vector<Point> > contours;
        biscuitMask.copyTo(tmp);
    
    
        findContours(tmp, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
    
        /*if ((contours.size() > 1) || (contours[0].size() < 6)) 
        {
            cerr << "ERROR: invalid mask for the biscuit!";
            return 1;
        }*/
    
        //taking largest contour
        int maxx = 0;
        int maxxIndx = -1;
    
        for(int i=0;i<contours.size();i++)
        {
            cout<<contours[i].size()<<endl;
    
            if(maxx < contours[i].size())
            {
                maxx = contours[i].size();
                maxxIndx = i;
            }
    
        }
    
        if(maxxIndx==-1)
        {
            cerr << "ERROR: invalid mask for the biscuit!";
            return 1;
        }
    
        vector<Point> biscuitContour(contours[maxxIndx]);
        contourStats theBiscuit(biscuitContour);
        theBiscuit.printStats("BISCUIT STATS:");
    
        if (theBiscuit.circularity < 0.85) 
        {
            cout << "WARNING: invalid biscuit circularity!";
        }
    
        //drawMarker(src, theBiscuit.center, CL_BLU, MARKER_CROSS, theBiscuit.axisAvg / 8);
        circle(src, theBiscuit.center, theBiscuit.axisAvg / 2, CL_BLU, 2);
        //polylines(src, biscuitContour, true, CL_GREEN); 
    
        // GET ONLY THE CRACKS INSIDE THE BISCUIT
        Morph(biscuitMask, biscuitMask, MORPH_ERODE,MORPH_ELLIPSE, 5);
        cracks = (edges & biscuitMask);
        imshow("cracks", cracks);
    
        findContours(cracks, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
    
        double minLenght = theBiscuit.axisAvg / 2;
        double maxLenght = 0;
        int biggest = -1;
        vector<contourStats> crackStats(contours.size());
        for (size_t i = 0; i < contours.size(); i++)
        {
            drawContours(src, contours, i, CL_GREEN, 1); //all contours in GREEN
            crackStats[i].calculateStats(contours[i]);   //analize potential cracks
            if (crackStats[i].axisMax < minLenght)       //ignore short contours
                continue;
            drawContours(src, contours, i, CL_RED, 1);   //longer contours are cracks
            if (crackStats[i].axisMax > maxLenght)       // get longest crack
                biggest = i;
        }
        // DRAW AROUND BIGGER CRACKS
        if (biggest > 0)  
        {
            cout << "Cracks Found! Biggest size: " << crackStats[biggest].axisMax <<
                "px (" << 100 * crackStats[biggest].axisMax / theBiscuit.axisMax << "%)" 
                << endl;
            RotatedRect rr = crackStats[biggest].rr;
            Point2f rect_points[4];
            rr.points(rect_points);
            //draw enclosing rectangle
            for (int j = 0; j < 4; j++) 
                line(src, rect_points[j], rect_points[(j + 1) % 4], CL_RED, 1, 8);
    
            for (int j = 0; j < 4; j++) 
            {
                rect_points[j].x = rect_points[j].x / 2.0;
                rect_points[j].y = rect_points[j].x / 2.0;
            }
            //draw major axis
            Point2f pt0 = (rect_points[0] + rect_points[3]);
            Point2f pt1 = (rect_points[1] + rect_points[2]);
            Point2f pt2 = (rect_points[0] + rect_points[1]);
            Point2f pt3 = (rect_points[2] + rect_points[3]);
    
            double axis1 = norm(pt0 - pt1);
            double axis2 = norm(pt2 - pt3);
            if (axis1>axis2)
                line(src, pt0, pt1, CL_BLACK, 2);
            else
                line(src, pt2, pt3, CL_BLACK, 2);
        }
    
        imshow("Src", src);
        waitKey(0);
        return 0;
    }
    
    
    void Morph(const cv::Mat &src, cv::Mat &dst, int operation,int kernel_type, int size)
    {
    cv::Point anchor = cv::Point(size, size);
    cv::Mat element = getStructuringElement(kernel_type, cv::Size(2 * size +  1, 2 * size + 1), anchor);
    morphologyEx(src, dst, operation, element, anchor);
    }
    

    【讨论】:

    • 你能上传预期的输出图像吗?
    猜你喜欢
    • 2019-11-27
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2012-02-28
    • 2012-12-08
    • 2011-03-10
    相关资源
    最近更新 更多