2012-04-20 29 views
0

代碼編譯成功,但當我嘗試執行一些圖像的代碼時出現以下錯誤。sYSMALLOc:Assertion在opencv中失敗的錯誤

malloc.c:3096:SYSMALLOC:斷言`(old_top ==(((mbinptr)(((字符*)&((AV) - >倉[((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 * (爲size_t))) - 1)))& &((old_top) - >大小爲0x1 &)& &((無符號長整數)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; } 
    } 

} 

能否請你幫我找出問題所在?

+0

也許產生錯誤的圖像對OpenCV來說是無效/可接受的... – Malkocoglu 2012-04-20 06:38:41

+0

使用'new_image.at (y,x)'而不是'new_image.at (y,x)[0]解決了這個問題。 – 2012-05-01 06:37:07

回答

0

我無法重現您獲得的確切錯誤消息。在我的電腦上,您的程序停止了segmentation fault

原因是,您正在訪問灰度值圖像的像素,就像它們是RGB圖像一樣。因此,而不是

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

你需要使用

new_image.at<uchar>(y,x) 

由於灰度圖像的每個像素只有一個值,而不是3個值(紅,綠,藍)的矢量英寸在我應用這個更改後,程序運行時沒有錯誤,並生成閾值二值圖像的預期輸出。

有可能因爲這個原因你覆蓋了一些其他的opencv當前使用的內存,並且這個內存損壞會導致你的錯誤信息。

相關問題