C++ OpenCV:未能正确使用LineIterator,给出了无意义的值
我在下面的程序中遇到了一个非常奇怪的行为。我基本上是在一个图像上迭代2个点,然后收集可变行中的所有像素值C++ OpenCV:未能正确使用LineIterator,给出了无意义的值,c++,opencv,C++,Opencv,我在下面的程序中遇到了一个非常奇怪的行为。我基本上是在一个图像上迭代2个点,然后收集可变行中的所有像素值 void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 ) { //Am I getting correct image? cout<<"Pixel values in image in Itera
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}
我正确地传递了main中的两个点,因为我在传递到IterateLine和IterateLine内部之前进行了验证。我看到两者是平等的
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}
但我得到的线像素总是114
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}
但是如果我在IterateLine中硬编码点的开始和结束,那么我会得到有意义的值
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}
我不明白这里出了什么问题?有些东西我不知道
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}
请帮忙
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}
像素坐标未硬编码时输出
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}
我的主要工作如下:
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}
int main()
{
Mat img = imread("sir.png", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
vector<Point> points1;
vector<ushort>linePixels;
//Draw the line, and collect end and final point coordinates in points1
drawline(img, points1);
//now check the collected points
cout<<"First coordinates passed to IterateLine() in main() are given below"<<endl;
cout<<points1[0].x<<'\t'<<points1[0].y<<endl;
cout<<"Second coordinates passed to IterateLine() in main() are given below"<<endl;
cout<<points1[1].x<<'\t'<<points1[1].y<<endl;
IterateLine( img, linePixels, points1[1], points1[0], t );
return 0 ;
}
但这不是很有效的方法,所以我仍然在寻找最好的方法 @berak你的意思是我正在获取我绘制的“线”的像素值?@berak那么我如何使这条线透明,以便IterateLine看不到它?只有人类才能看到这一点line@berak请查看我的更新,我已经显示了我的drawline@berak我单击的两个点存储在vector&points中,然后我在迭代线中使用它;你可以直接复制图像,而不是读取图像两次。它将比读取磁盘上的数据更有效。不过,要注意复制Mat的不同功能。有些只复制标题,仍然引用相同的基础数据,有些则真正复制标题和新Mat对象中的数据。
void IterateLine( const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int *count1 )
{
//Am I getting correct image?
cout<<"Pixel values in image in IterateLine"<<endl;
cout<<Mat(image)<<endl; //Yes this shows meaningful values indeed
//hard coding pixel coordinates for debugging. Prints meaningful pixel values at end
p1.x=32;
p1.y=66;
p2.x=230;
p2.y=86;
//but If I do not use hard coded values as above, then it always prints 114
//Am I getting correct pixel coordinates? Let us print
cout<<"First coordinates received in IterateLine() are given below"<<endl;
cout<<p1.x<<'\t'<<p1.y<<endl;
cout<<"Second coordinates received in IterateLine() are given below"<<endl;
cout<<p2.y<<'\t'<<p2.y<<endl;
//The above indeed prints meaningful coordinates, but only when pixel values are hard coded
LineIterator it(image, p2,p1, 8);
for(int i = 0; i < it.count; i++, it++){
linePixels.push_back(image.at<ushort>(it.pos()));
}
*count1=it.count;
cout<<"No. of pixels in IterateLLine"<<endl;
cout<<count1[0]<<endl;
cout<<"pixel values in IterateLine are"<<endl;
for(int i=0;i<count1[0];i++)
{
cout<<linePixels[i]<<endl;
}
}