C++ SEGFULT后返回0;
我想使用ITK的一个简单的颜色比例程序,但我得到一个segfault后返回0;主要功能的定义。 这是我的密码C++ SEGFULT后返回0;,c++,segmentation-fault,itk,C++,Segmentation Fault,Itk,我想使用ITK的一个简单的颜色比例程序,但我得到一个segfault后返回0;主要功能的定义。 这是我的密码 #include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include <iostream> #include <string> #include <vector> #include "itkRGBPixel.h" const un
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include <iostream>
#include <string>
#include <vector>
#include "itkRGBPixel.h"
const unsigned int Dimension = 2;
typedef itk::RGBPixel< unsigned char > PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
int main(int argc, char** argv)
{
std::string input=argv[1], output=argv[2];
//allocation of the image data
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName(input);
writer->SetFileName(output);
reader->Update();
//access image
ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer output_img;
//apparently providing the spacing and origin in ITK is mandatory
ImageType::SpacingType spacing;
spacing[0] = 0.33;
spacing[1] = 0.33;
image->SetSpacing(spacing);
ImageType::PointType origin;
origin[0] = 0.0;
origin[1] = 0.0;
image->SetOrigin(origin);
writer->SetInput( reader->GetOutput() );
writer->Update();
return EXIT_SUCCESS;
}
即使有36000个无用的try/catch语句,我仍然会在main之后遇到相同的分段错误
有人能帮我吗
提前感谢您此代码适用于我:
#include <iostream>
#include <string>
#include <vector>
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRGBPixel.h"
int main(int argc, char** argv)
{
std::string input = "/home/doriad/temp/test.png";
std::string output= "output.png";
const unsigned int Dimension = 2;
typedef itk::RGBPixel< unsigned char > PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
//allocation of the reader
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(input);
reader->Update();
//access image
ImageType::Pointer image = reader->GetOutput();
// ImageType::Pointer output_img;// This line does nothing
//apparently providing the spacing and origin in ITK is mandatory
// The spacing and origin are read from the file.
// ImageType::SpacingType spacing;
// spacing[0] = 0.33;
// spacing[1] = 0.33;
// image->SetSpacing(spacing);
// ImageType::PointType origin;
// origin[0] = 0.0;
// origin[1] = 0.0;
// image->SetOrigin(origin);
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(output);
writer->SetInput( reader->GetOutput() );
writer->Update();
return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括“itkImage.h”
#包括“itkImageFileReader.h”
#包括“itkImageFileWriter.h”
#包括“itkRGBPixel.h”
int main(int argc,字符**argv)
{
std::string input=“/home/doriad/temp/test.png”;
std::string output=“output.png”;
常量无符号整数维=2;
typedef itk::RGBPixelPixelType;
typedef itk::ImageImageType;
typedef itk::ImageFileReaderReaderType;
typedef itk::ImageFileWriterWriterType;
//读者的分配
ReaderType::Pointer reader=ReaderType::New();
读卡器->设置文件名(输入);
阅读器->更新();
//访问图像
ImageType::Pointer image=reader->GetOutput();
//ImageType::指针输出\u img;//此行不执行任何操作
//显然,在ITK中提供间距和原点是强制性的
//从文件中读取间距和原点。
//ImageType::SpacingType间距;
//间距[0]=0.33;
//间距[1]=0.33;
//图像->设置间距(间距);
//ImageType::PointType原点;
//原点[0]=0.0;
//原点[1]=0.0;
//图像->设置原点(原点);
WriterType::指针编写器=WriterType::New();
writer->SetFileName(输出);
writer->SetInput(reader->GetOutput());
writer->Update();
返回退出成功;
}
当然,您必须将“输入”字符串更改为您实际拥有的文件。看起来您正在破坏堆栈。您可以发布一条消息吗?另外,您不应该使用
-1
从main
返回。任何非零数字通常都会被视为错误,如果您想确定,请返回。谢谢您的回复。我已将代码编辑为SSCCE格式。它足够简短、独立,并且可以编译。基本上,它是从一个文件中读取一个图像,然后直接在另一个文件中重写它。(我所知道的)没有别的。问题仍然存在。顺便说一句,这张图片是png图片。我正在使用Itk库!除非提供完整的构建,或者复制粘贴库的每个头文件/源文件的全部内容,否则就不可能生成sscce!好的,谢谢。我猜这个错误来自setspace或setOrigin。如果仅将itk用于非医学图像处理,为什么必须使用它?无论如何,谢谢你的帮助。问题已解决。如果您的文件没有其他说明(即,如果您阅读非医疗文件(如正常png),读者将生成您期望的间距(1)和原点(0)。您已通过接受答案正确地结束了问题。
==16671== Memcheck, a memory error detector
==16671== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==16671== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==16671== Command: ./ColorRatio clouds2.jpeg coucou.jpeg --track-origins=yes
==16671==
==16671== Conditional jump or move depends on uninitialised value(s)
==16671== at 0x4B28DD8: inflateReset2 (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==16671== by 0x4B28EC7: inflateInit2_ (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==16671== by 0x4C87E58: ??? (in /usr/lib/libITKniftiio.so.3.20.1)
==16671==
==16671== Conditional jump or move depends on uninitialised value(s)
==16671== at 0x4B28DD8: inflateReset2 (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==16671== by 0x4B28EC7: inflateInit2_ (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==16671==
==16671== Invalid read of size 4
==16671== at 0x47429BC: ??? (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
==16671== by 0x47CE4D2: (below main) (libc-start.c:226)
==16671== Address 0xfffffffc is not stack'd, malloc'd or (recently) free'd
==16671==
==16671==
==16671== Process terminating with default action of signal 11 (SIGSEGV)
==16671== Access not within mapped region at address 0xFFFFFFFC
==16671== at 0x47429BC: ??? (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
==16671== by 0x47CE4D2: (below main) (libc-start.c:226)
==16671== If you believe this happened as a result of a stack
==16671== overflow in your program's main thread (unlikely but
==16671== possible), you can try to increase the size of the
==16671== main thread stack using the --main-stacksize= flag.
==16671== The main thread stack size used in this run was 8388608.
==16671==
==16671== HEAP SUMMARY:
==16671== in use at exit: 2,352,658 bytes in 63,532 blocks
==16671== total heap usage: 74,071 allocs, 10,539 frees, 14,082,941 bytes allocated
==16671==
==16671== LEAK SUMMARY:
==16671== definitely lost: 49 bytes in 2 blocks
==16671== indirectly lost: 0 bytes in 0 blocks
==16671== possibly lost: 1,309,540 bytes in 42,156 blocks
==16671== still reachable: 1,043,069 bytes in 21,374 blocks
==16671== suppressed: 0 bytes in 0 blocks
==16671== Rerun with --leak-check=full to see details of leaked memory
==16671==
==16671== For counts of detected and suppressed errors, rerun with: -v
==16671== Use --track-origins=yes to see where uninitialised values come from
==16671== ERROR SUMMARY: 5 errors from 3 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
#include <iostream>
#include <string>
#include <vector>
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRGBPixel.h"
int main(int argc, char** argv)
{
std::string input = "/home/doriad/temp/test.png";
std::string output= "output.png";
const unsigned int Dimension = 2;
typedef itk::RGBPixel< unsigned char > PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
//allocation of the reader
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(input);
reader->Update();
//access image
ImageType::Pointer image = reader->GetOutput();
// ImageType::Pointer output_img;// This line does nothing
//apparently providing the spacing and origin in ITK is mandatory
// The spacing and origin are read from the file.
// ImageType::SpacingType spacing;
// spacing[0] = 0.33;
// spacing[1] = 0.33;
// image->SetSpacing(spacing);
// ImageType::PointType origin;
// origin[0] = 0.0;
// origin[1] = 0.0;
// image->SetOrigin(origin);
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(output);
writer->SetInput( reader->GetOutput() );
writer->Update();
return EXIT_SUCCESS;
}