sYSMALLOc:opencv中的断言失败的错误 [英] sYSMALLOc: Assertion Failed error in opencv

查看:312
本文介绍了sYSMALLOc:opencv中的断言失败的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


malloc.c :3096:sYSMALLOc:Assertion`(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)))&((old_top) - > size& 0x1)&&((unsigned long)old_end& pagemask)== 0)
已中止


我的代码是:

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

using namespace cv;

///全局变量
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;

///加载图像
src = imread(argv [1],1);

///将图像转换为灰色
cvtColor(src,src_gray,CV_RGB2GRAY);

///创建新的图像矩阵
new_image = Mat :: ones(src_gray.size(),src_gray.type());

///计算像素的总和
for(int y = 0; y {
for(int x = 0 ; x< src_gray.cols; x ++)
{
sum = sum + src_gray.at< Vec3b>(y,x)[0]
}
}

///计算像素的平均值
mean = sum /(src_gray.rows * src_gray.cols);

///执行转换为二进制
for(int y = 0; y< src_gray.rows; y ++)
{
for(int x = 0 ; 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;
}
}

///创建一个窗口显示结果
namedWindow(window_name,CV_WINDOW_AUTOSIZE);

imshow(window_name,new_image);
///等待用户完成程序
while(true)
{
int c;
c = waitKey(20);
if((char)c == 27)
{break; }
}

}

问题?

解决方案

我无法重现您得到的确切错误消息。在我的电脑上,您的程序以细分错误停止。



这是因为您正在访问像素的灰度值图像,就像他们是rgb图像。

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



您需要使用

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



这可能是因为你覆盖一些其他内存opencv目前使用,并且此内存损坏,然后导致您的错误消息。


The code compiles successfully but I am getting the following error when I try to execute the code with some images.

malloc.c:3096: sYSMALLOc: Assertion `(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)' failed. Aborted

My code is:

#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; }
    }

}

Can you please help me identify the problem?

解决方案

I cannot reproduce the exact error message you get. On my computer your program stopped with a segmentation fault.

The reason for this was, that you are accessing the pixels of your gray value images as if they were rgb images. So instead of

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

you need to use

new_image.at<uchar>(y,x)

Because in a gray scale image every pixel only has a single value instead of a vector of 3 values (red, green and blue). After I applied this changes your program ran without errors and produced the expected output of an thresholded binary image.

It is possible that because of this you are overwriting some other memory opencv currently used and that this memory corruption then lead to your error message.

这篇关于sYSMALLOc:opencv中的断言失败的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆