C++ sYSMALLOc:opencv中的断言失败错误

C++ sYSMALLOc:opencv中的断言失败错误,c++,opencv,C++,Opencv,代码编译成功,但当我尝试使用一些图像执行代码时,出现以下错误 malloc.c:3096:sYSMALLOc:Assertion`old_top==mbinptr char*&av->bins[1-1*2]-\uu内置结构malloc\u块的偏移量,fd&&old\u大小==0 | |无符号长旧大小>=无符号长内置结构malloc\u块的偏移量,fd_nextsize+2*sizeofsize\u t-1&~2*sizeofsize\u t-1&&old\u top->size&0x1&&uns

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

malloc.c:3096:sYSMALLOc:Assertion`old_top==mbinptr char*&av->bins[1-1*2]-\uu内置结构malloc\u块的偏移量,fd&&old\u大小==0 | |无符号长旧大小>=无符号长内置结构malloc\u块的偏移量,fd_nextsize+2*sizeofsize\u t-1&~2*sizeofsize\u t-1&&old\u top->size&0x1&&unsigned longold\u 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; }
    }

}

您能帮我确定问题吗?

我无法重现您收到的确切错误消息。在我的计算机上,您的程序因分段错误而停止

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

new_image.at<Vec3b>(y,x)[0]
你需要使用

new_image.at<uchar>(y,x)
因为在灰度图像中,每个像素只有一个值,而不是红、绿、蓝三个值的向量。在我应用了这些更改之后,您的程序运行时没有错误,并生成了阈值化二值图像的预期输出


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

可能产生错误的图像对opencv无效/不可接受…使用新的\u图像。aty,x而不是新的\u图像。aty,x[0]解决了问题。