【问题标题】:sYSMALLOc: Assertion Failed error in opencvsysMALLOc:opencv 中的断言失败错误
【发布时间】:2012-04-20 14:47:08
【问题描述】:

代码编译成功,但是当我尝试使用一些图像执行代码时出现以下错误。

malloc.c:3096: sSYSMALLOc: 断言`(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof (size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' 失败。 中止

我的代码是:

#include "opencv2/modules/imgproc/include/opencv2/imgproc/imgproc.hpp"
#include "opencv2/modules/highgui/include/opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// Global variables
int const min_BINARY_value = 0;
int const max_BINARY_value = 255;

Mat src, src_gray, new_image;
const char* window_name = "Web Safe Colors";

/**
 * @function main
 */
int main( int argc, char** argv )
{
  double sum=0, mean=0;

  /// Load an image
  src = imread( argv[1], 1 );

  /// Convert the image to Gray
  cvtColor( src, src_gray, CV_RGB2GRAY );

  /// Create new image matrix
  new_image = Mat::ones( src_gray.size(), src_gray.type() );

  /// Calculate sum of pixels
  for( int y = 0; y < src_gray.rows; y++ )
  { 
    for( int x = 0; x < src_gray.cols; x++ )
    { 
      sum = sum + src_gray.at<Vec3b>(y,x)[0];
    }
  }

  /// Calculate mean of pixels
  mean = sum / (src_gray.rows * src_gray.cols);

  /// Perform conversion to binary
  for( int y = 0; y < src_gray.rows; y++ )
  { 
    for( int x = 0; x < src_gray.cols; x++ )
    { 
      if(src_gray.at<Vec3b>(y,x)[0] <= mean)
        new_image.at<Vec3b>(y,x)[0] = min_BINARY_value;
      else
        new_image.at<Vec3b>(y,x)[0] = max_BINARY_value;
    }
  }

  /// Create a window to display results
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );

  imshow( window_name, new_image );
  /// Wait until user finishes program
  while(true)
    {
      int c;
      c = waitKey( 20 );
      if( (char)c == 27 )
    { break; }
    }

}

你能帮我找出问题吗?

【问题讨论】:

  • 可能产生错误的图像对 OpenCV 无效/不能被 OpenCV 接受...
  • 使用new_image.at&lt;uchar&gt;(y,x) 而不是new_image.at&lt;Vec3b&gt;(y,x)[0] 解决了这个问题。

标签: c++ opencv


【解决方案1】:

我无法重现您收到的确切错误消息。在我的计算机上,您的程序以 segmentation fault 停止。

这样做的原因是,您正在访问灰度值图像的像素,就好像它们是 rgb 图像一样。所以不是

new_image.at<Vec3b>(y,x)[0]

你需要使用

new_image.at<uchar>(y,x)

因为在灰度图像中,每个像素只有一个值,而不是 3 个值(红色、绿色和蓝色)的向量。在我应用此更改后,您的程序运行没有错误,并产生了阈值二进制图像的预期输出。

可能正因为如此,您正在覆盖当前使用的其他一些内存 opencv,并且这种内存损坏会导致您的错误消息。

【讨论】:

    猜你喜欢
    • 2012-11-28
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    相关资源
    最近更新 更多