opencv getPerspectiveTransform不起作用

时间:2016-02-28 22:42:47

标签: c++ opencv

所以我正在进行一项任务,我必须根据输入图像对道路标志进行分类。所以很自然地我使用了canny函数和findContours,然后使用了aboutPolyPD来获取我将要转换的图像的角落。

但是出于某种原因,当我尝试使用getPerspectiveTransform进行下一步时,我一直收到错误。请帮忙。

错误: OpenCV错误:断言失败(0< = i&& i<(int)vv.size())在getMat_,file / home / path_to_opencv / opencv / modules / core / src /matrix.cpp,第1192行 抛出'cv :: Exception'的实例后终止调用

what():/ home / path_to_opencv/opencv/modules/core/src/matrix.cpp:1192:错误:(-215)0< = i&&我<函数getMat _

中的(int)vv.size()

中止(核心倾销)

使用的代码:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define WARPED_XSIZE 200
#define WARPED_YSIZE 300

using namespace cv;
using namespace std;

Mat src; Mat src_gray, warped_result; Mat dst;
Mat speed_80, speed_40;
int canny_thresh = 154;


#define VERY_LARGE_VALUE 100000

#define NO_MATCH    0
#define STOP_SIGN            1
#define SPEED_LIMIT_40_SIGN  2
#define SPEED_LIMIT_80_SIGN  3

RNG rng(12345);
/** @function main */
int main(int argc, char** argv)
{
    int sign_recog_result = NO_MATCH;
    speed_40 = imread("speed_40.bmp", 0);
    speed_80 = imread("speed_80.bmp", 0);

    // you run your program on these three examples (uncomment the two lines below)
    //string sign_name = "stop4";
    string sign_name = "speedsign12";
    //string sign_name = "speedsign3";
    //string sign_name = "speedsign4";
    string final_sign_input_name = sign_name + ".jpg";
    string final_sign_output_name = sign_name + "_result" + ".jpg";

    /// Load source image and convert it to gray
    src = imread(final_sign_input_name, 1);

    /// Convert image to gray and blur it
    cvtColor(src, src_gray, COLOR_BGR2GRAY);
    blur(src_gray, src_gray, Size(3, 3));
    warped_result = Mat(Size(WARPED_XSIZE, WARPED_YSIZE), src_gray.type());

    // here you add the code to do the recognition, and set the variable 
    // sign_recog_result to one of STOP_SIGN, SPEED_LIMIT_40_SIGN, SPEED_LIMIT_80_SIGN, or NO_MATCH

    // PART 1 of Assignment 2
    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Canny(src_gray, canny_output, canny_thresh, canny_thresh*2, 3);
    findContours(canny_output, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point(0, 0));

    vector<vector<Point> > contours_poly(contours.size());

    for (unsigned int i = 0; i < contours.size(); ++i) {
        approxPolyDP(Mat(contours[i]), contours_poly[i], contours_poly[i].size()*.02, true);
    }

    // Part 2 of Assignment 2
    vector<vector<Point> > transform_result(contours_poly.size());

    warped_result = getPerspectiveTransform(contours_poly, transform_result);
    warpPerspective(src, dst, warped_result, dst.size());

    //imshow("input", src);
    //imshow("output", dst);

    /*
    Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
    for(unsigned int i = 0; i< contours_poly.size(); i++ ) {
       Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
       drawContours( drawing, contours_poly, i, color, 2, 8, hierarchy, 0, Point() );
    }

    // Show in a window
    namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
    imshow( "Contours", drawing );
    //*/

    // Returning to the predetermined code.
    string text;
    if (sign_recog_result == SPEED_LIMIT_40_SIGN) text = "Speed 40";
    else if (sign_recog_result == SPEED_LIMIT_80_SIGN) text = "Speed 80";
    else if (sign_recog_result == STOP_SIGN) text = "Stop";
    else if (sign_recog_result == NO_MATCH) text = "Fail";

    int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
    double fontScale = 2;
    int thickness = 3;
    cv::Point textOrg(10, 130);
    cv::putText(src, text, textOrg, fontFace, fontScale, Scalar::all(255), thickness, 8);

    /// Create Window
    char* source_window = "Result";
    namedWindow(source_window, WINDOW_AUTOSIZE);
    imshow(source_window, src);
    imwrite(final_sign_output_name, src);

    waitKey(0);

    return(0);
}

0 个答案:

没有答案