从矢量字符串中读取并拉直多个图像,得到错误:“矢量下标超出范围”[c ++]

时间:2014-12-31 10:53:23

标签: c++ opencv

嘿我尝试从文件夹加载所有图像然后拉直它们然后将所有新图像写入新文件夹。我的代码有效,但并非一切都很好。在4-5图像程序崩溃,我收到错误消息:

"调试评估失败! (...)行:1201表达式:向量下标超出范围"

这是我的代码部分:

int main()
{
system("dir Klasa_pierwsza\\*.tiff /b > lista_zdjęć.txt");
add_list_of_names_images_tiff_to_string_vector("lista_zdjęć.txt", lista);
display(lista);

std::vector<cv::Point> wektor_straighten_images2;

cv::Mat *M = new Mat[lista.size()];

for (int i = 0; i < lista.size(); i++)
{

    M[i] = imread("Klasa_pierwsza\\" + lista[i]);

    cv::cvtColor(M[i], bw, CV_BGR2GRAY);
    cv::blur(bw, bw, cv::Size(3, 3));
    cv::Canny(bw, bw, 100, 100, 3); // 200

    std::vector<cv::Vec4i> lines;
    cv::HoughLinesP(bw, lines, 1, CV_PI / 180, 70, 30, 10);
    cv::Vec4i v1 = lines[3]; // lines[2,5,10

    wektor_straighten_images2.push_back(cv::Point(v1[0], v1[1]));
    wektor_straighten_images2.push_back(cv::Point(v1[2], v1[3]));

    cv::line(M[i], cv::Point(v1[0], v1[1]), cv::Point(v1[2], v1[3]), CV_RGB(0, 255, 0)); // rysowanie linii na obrazku im0

    double angle = std::atan((double)(wektor_straighten_images2[0].y - wektor_straighten_images2[1].y) / (wektor_straighten_images2[0].x - wektor_straighten_images2[1].x)) * (180 / CV_PI);

    cv::Point2f center(M[i].cols / 2., M[i].rows / 2.);
    cv::Mat dst, r = cv::getRotationMatrix2D(center, angle, 1.0);
    cv::warpAffine(M[i], dst, r, M[i].size());

    cv::imwrite("Zapisane\\" + lista[i], dst);
}

这是我的功能&#34; add_list_of_names_images_tiff_to_string_vector&#34;

void add_list_of_names_images_tiff_to_string_vector(String k,vector<string> &wektor)
{
ifstream in(k);
    if (!in) 
    {
        cout << "Can't read.\n";
    }
char str[255];

    while (in) 
    {
        in.getline(str, 255);
        wektor.push_back(str);
    }
    wektor.pop_back();

in.close();
}

也许有人对我有好的建议。请帮忙。

为什么我这样做? :我必须创建程序来整理文件夹中的所有图像,但图像名称是未知的。

编辑:

嗯问题在这里:     cv :: Vec4i v1 = lines [3];

但是出了什么问题?

1 个答案:

答案 0 :(得分:0)

您的代码中的一个问题是,您永远不会检查您的操作结果,应该有:

M[i] = imread("Klasa_pierwsza\\" + lista[i]);
if ( M[i].empty() )
{
     cerr << "Klasa_pierwsza\\" + lista[i] << " could not be loaded !" << endl;
     continue;
}

// ...

std::vector<cv::Vec4i> lines;
cv::HoughLinesP(bw, lines, 1, CV_PI / 180, 70, 30, 10);
if ( lines.size() < 4 )
{
     cerr << "Klasa_pierwsza\\" + lista[i] << " did not have 4 lines !" << endl;
     continue;
}
cv::Vec4i v1 = lines[3]; // you have an access violation (out of bounds) here.