Cannot return copy

N

none

In the below code the call:

DisplayImage(job.GetResultImage());

just shows a black image even though it looks fine when its created.



int main() {

typedef MyImg MyImgType;
typedef Job<MyImgType> JobType;

MyImageApp<MyImgType> app;
JobType job;
app.Run(job);

// This only shows a black image!
DisplayImage(job.GetResultImage());
return 0;

}




// ============== MyImageApp ======================

template<typename F>
class MyImageApp {
public:


void Run(JobType & job) {

// The 'processor' instance will die when this function returns.
Processor<F> processor;
processor.Process();

// Not possble to copy the image from the processor into the job!?
job.SetResultImage(processor.GetWarpedImage());
}

};



// ============== Processor =======================

template <typename FI>
class Processor {
public:
typedef FI FixedImageType;
typedef itk::ResampleImageFilter< FixedImageType, FixedImageType > ResampleFilterType;

WarpProcessor ();
virtual ~WarpProcessor ();
void Process();

// Returning a copy not a reference!
typename FixedImageType::pointer GetImage(){return this->imageW;}

private:
typename FixedImageType::pointer imageW;

};

template <typename FI>
void
Processor<FI>::process() {
// Computing stuff

// ...

// Copying the result into the imageW instance
CopyImageToImage<typename FixedImageType::pointer, FixedImageType>(
resampler->GetOutput(),
imageW);

// image looks fine when viewed here!
DisplayImage(imageW)

}




// ================== Job =======================

template<typename F>
class Job {
public:
typedef F FixedImageType;

// Return copy!
typename FixedImageType::pointer GetResultImage() {return this->imageR;}
void SetResultImage(typename FixedImageType::pointer image) {this->imageR = image;}

private:
typename FixedImageType::pointer imageR;

};




Any ideas on why the image looks fine when calling:

DisplayImage(imageW)

in Processor::process() and pitch black when calling:

DisplayImage(job.GetResultImage());

in main?
 
J

Jonathan Lee

   // Return copy!
   typename FixedImageType::pointer GetResultImage() {return this->imageR;}

From this I would guess that you're not actually returning
a copy, you're returning a pointer. If the destructor is called
for the image that is being pointed to, the data is lost.
(Or, at least practice it should be.)

So, giving that you're passing things around by pointer instead
of by value, I expect that the thing pointed to doesn't exist
anymore. That's just a guess, though.

--Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top