自适应阈值崩溃

时间:2016-05-14 20:51:19

标签: opencv crash adaptive-threshold

我正在使用

分割字符
adaptiveThreshold(crops_gray, crops_gray_resized, 255, 
CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV,121,-35);

我也在使用

    adaptiveThreshold(crops_gray, thresh, 255,
CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 81,-80);

问题是,对于某些图像,BINARY_INV崩溃,直到我将参数设置为81,-35 对于某些图像,与BINARY

的问题相同

例如,第一个“上方”图片显示BINARY设置为81,-35BINARY_INV81,-80的结果

BINARY更改为121,-35而将BINARY_INV更改为81,-80将导致图像的第二行 - >在第二张图片中没有错误而崩溃

BINARY_INV更改为81,-35BINARY更改为121,-80会产生最后一张图片

我正在提供代码,让我自己更清楚自己在做什么。

for (int k = 0; k < j; k++) {

        cvtColor(crops[k], crops_gray, CV_RGB2GRAY);
        imshow("Crops_gray", crops_gray);

        Mat crops_gray_resized;
        resize(crops_gray, crops_gray, cvSize(160, 80));
        crops_gray_resized=crops_gray;
        imshow("img_rotated_resized ", crops_gray);

                Mat thresh;             
                imshow("crops_eq ", crops_gray);
        adaptiveThreshold(crops_gray, crops_gray_resized, 255,
                                CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV,121,-35);
        adaptiveThreshold(crops_gray, thresh, 255,
                                        CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 81,-80);
                imshow("crops_thresh ", crops_gray_resized);

        findContours(thresh, contours, hierarchy, CV_RETR_TREE,
                CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

        vector<Point2f> ContArea(contours.size());
        for (int i = 0; i < contours.size(); i++) {
            approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
            boundRect[i] = boundingRect(Mat(contours_poly[i]));

        }
        min = 0.0;
        float avg=0;
        float elem=0;
        int num=0;

        for (int i = 0; i < contours.size(); i++) {
                double a = contourArea(contours[i]);
                ratio = (float) boundRect[i].width / (float) boundRect[i].height;
                if ((a >= 40 && a < 800) && ((ratio >= min) && (ratio < 2))
                    ) {
                elem=elem+(float)boundRect[i].height;
                num++;
            }
        }
avg=(elem/(num));
printf("avg: %f",avg);
imshow("crops_thresh? ", thresh);
int l =0;
    /// Draw polygonal contour + bonding rects
    Mat drawing5 = Mat::zeros(crops_gray_resized.size(), CV_8UC3);
    for (int i = 0; i < contours.size(); i++) {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255),
                rng.uniform(0, 255));
        double a = contourArea(contours[i]);
        ratio = (float) boundRect[i].width / (float) boundRect[i].height;

        if ((a >= 40 && a < 400) && ((ratio >= min) && (ratio < 2))
                && (float)boundRect[i].height > avg) {
            printf(" a: %f ratios: %f image%d height: %d \n", a, ratio, i,
                    boundRect[i].height);

            drawContours(drawing5, contours_poly, (int) i, color, 1, 8,
                    vector<Vec4i>(), 0, Point());
            rectangle(drawing5, boundRect[i].tl(), boundRect[i].br(), color,
                    CV_FILLED, 1, 0);

            Mat cropx(crops_gray_resized(boundRect[i]));
                            crops[l] = cropx;

                            imshow("crops0", crops[l]);
                            if (l == 1)
                                imshow("crops1", crops[l]);
                            if (l == 2)
                                imshow("crops2", crops[l]);
                            if (l == 3)
                                imshow("crops3", crops[l]);
                            if (l == 4)
                                imshow("crops4", crops[l]);
                            if (l == 5)
                                imshow("crops5", crops[l]);
                            if (l == 6)
                                imshow("crops6", crops[l]);
                            if (l == 7)
                                imshow("crops7", crops[l]);
                            if (l == 8)
                                imshow("crops8", crops[l]);
                            l++;
        }
    }

    imshow("Rectangles5", drawing5);

    waitKey(0);
}

我现在对发生的事情感到十分困惑。 谁能告诉我我做错了什么?

enter image description here

enter image description here

0 个答案:

没有答案