gpt4 book ai didi

c++ - 无法解决 opencv 代码中的 malloc 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:44:37 24 4
gpt4 key购买 nike

我的代码:

  cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));
std::cout << hough_acc.size( ) << " " << hough_acc[0].size() << std::endl;

for (size_t i = 0; i < hough_acc.size(); i++) {
for (size_t j = 0; j < hough_acc[0].size(); j++) {
std::cout << hough_acc[i][j] << std::endl;
img_mat.at<int> (i,j) = hough_acc[i][j];
}
}

错误在行img_mat.at<int> (i,j) = hough_acc[i][j]; .错误是:

ps1: malloc.c:2392: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)

我检查了 Mat img_mat 的大小和 hough_acc[][]两者是平等的。 hough_acc 中的值也都是整数。不明白为什么会出现错误。

完整代码:

void hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc) {
int img_w = img_a_edges.cols;
int img_h = img_a_edges.rows;

int max_votes = 0;
int min_votes = INT_MAX;

for (size_t r = 0; r < img_h; r++) {
for (size_t c = 0; c < img_w; c++) {
if(true) {
for (size_t angle = 0; angle < 180; angle++) {
double theta = (angle * M_PI / 180);
double rho = ( (c * cos(theta)) + (r * sin(theta)) );
int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(theta)];

if (buff > max_votes) {
max_votes = buff;
}
if (buff < min_votes) {
min_votes = buff;
}
}
}
}
}

double div = static_cast<double>(max_votes) / 255;
int threshold = 10;
int possible_edge = round(static_cast<double>(max_votes) / div) - threshold;

cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));
std::cout << hough_acc.size( ) << " " << hough_acc[0].size() << std::endl;

for (size_t i = 0; i < hough_acc.size(); i++) {
for (size_t j = 0; j < hough_acc[0].size(); j++) {
std::cout << hough_acc[i][j] << std::endl;
img_mat.at<int> (i,j) = hough_acc[i][j];
}
}

imwrite("../output/ps1-­2-­b-­1.png", img_mat);
}

最佳答案

您遇到的 malloc 错误可能是由于在可疑行之前写越界造成的。有没有可能

    int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(theta)];

在 hough_acc 的边界之外写入?有些东西是“可疑的”,因为数组的大小为 180,但您以弧度计算角度,所以它应该(可能?)上升到 6。

关于c++ - 无法解决 opencv 代码中的 malloc 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41510918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com