OpenCV:无法正确传递数据给函数
OpenCV: Unable to correctly pass data to a function
使用MSVS2010在Windows上编译
在以下代码中,我尝试使用行迭代器,然后在提取像素值后,我试图将提取的数据传递给函数foo()
:
该图像为单通道16位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 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
可以看出foo()
和main()
的值是错误的。应该和IterateLine()
报告的一致。由于地址被正确传递(见输出),所以使用指针我应该从任何函数获得相同的数据。但这并没有发生。
为什么会发生这种情况,我如何正确地将数据传递给foo()
?
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 ;
}
相关文章:
- 链表,反向函数,数据结构
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 从函数返回任意简单类型的数据
- 友元函数无法访问私有数据成员 (c++)
- 访问和打印元组中的数据,并使用 C++14 使用模板函数显示数据
- 为什么我们使用在C++中返回数据结构的函数?
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 重写函数不打印基类数据
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- 接受函数在发送数据包时等待
- 使用 pybind11 调用 Python 函数时避免复制输入数据
- C++中是否有用于长数据类型的 lower_bound() 函数?
- 在线程函数中处理数据向量时进行线程竞速
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- 在 C++ 中,默认情况下构造函数为类的数据成员提供的值是多少?
- 喃喃自语/谷歌protobufs客户端读取数据函数错误