Reputation: 848
Compiling on Windows with MSVS2010
In the following code I have tried to use Line iterator and then after extracting the pixel values along the line, I am trying to pass the extracted data to a function foo()
:
The image is a single channel 16 bit png.
int foo(unsigned short int *values, unsigned int nSamples)
{
cout<<"address in PlotMeNow"<<endl;
cout<<&values[0]<<endl;
cout<<"values in PlotMeNow"<<endl;
for(int i=0;i<5;i++){
cout<<values[i]<<endl;
}
return 0;
}
unsigned short int * IterateLine(Mat image)
{
LineIterator it(image, Point(1,1), Point(5,5), 8);
vector<ushort> buf;
for(int i = 0; i < it.count; i++, it++){
buf.push_back(image.at<ushort>(it.pos()));
}
std::vector<ushort> data(5);
data=buf;
cout<<"the address in the iterateLine() is"<<endl;
cout<<&data[0]<<endl;
cout<<"values in IterateLine"<<endl;
for(int i=0;i<5;i++){
cout<<data[i]<<endl;
}
return &data[0];
}
int main()
{
Mat img = imread("sir.png", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
unsigned short int * x ;
//Iterate through the line
x = IterateLine(img);
int n=5;
foo(&x[0], n);
cout<<"The address in main() is"<<endl;
cout<<x<<endl;
cout<<"values in main are"<<endl;
for(int i=0;i<5;i++){
cout<<x[i]<<endl;
}
getch();
return 0 ;
}
The output of the program is:
the address in the iterateLine() is
001195D8
values in IterateLine
40092
39321
39578
46003
46774
address in foo()
001195D8
values in foo()
56797
56797
56797
56797
56797
The address in main() is
001195D8
values in main() are
56797
56797
56797
56797
56797
As can be seen the values foo()
and main()
are wrong. It should be same as that reported by IterateLine()
. Since address are passed correctly (see in the output ) so using pointers I should get same data from any function. But its not happening.
Why is this happening and how do I correctly pass data to foo()
?
Upvotes: 0
Views: 104
Reputation: 39796
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p1, Point p2 )
{
LineIterator it(image, p1,p2, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
}
int main()
{
Mat img = imread("sir.png", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
vector<ushort> linePixels; // initially empty
IterateLine( img, linePixels, Point(1,1), Point(5,5) );
foo( &linePixels[0], linePixels.size() );
return 0 ;
}
Upvotes: 1