OpenCV:无法正确使用LineIterator,无意义的值

时间:2014-07-08 07:43:03

标签: c++ opencv

我在以下程序中遇到了一种非常奇怪的行为。我基本上在图像上迭代2个点并收集变量linePixels中的所有像素值。

我正确地从main()传递了两个点,因为我在传递给IterateLine()之前和IterateLine()之内进行了验证。我看到两者是平等的。

linePixels我总是得到114.

但如果我在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;
    }



}

像素坐标是硬编码时的输出:

First coordinates passed to IterateLine() in main() are given below
  10     15 
Second coordinates passed to IterateLine() in main() are given below
  200    150 
First coordinates received in IterateLine()  are given below
32      66
Second coordinates received in IterateLine()  are given below
230     86
No. of pixels in IterateLLine
199
pixel values in  IterateLine are
42405
41634
36237
34952
43947
26471
26985
27756
29555
35723
23901
24415
38807
25186
25186
42662
29041
29812
42405
34438
35209
42148
35980
36751
38807
28013

像素坐标未硬编码时的输出

First coordinates passed to IterateLine() in main() are given below
96      56
Second coordinates passed to IterateLine() in main() are given below
192     160
First coordinates received in IterateLine()  are given below
 96      56
Second coordinates received in IterateLine()  are given below
 192     160
No. of pixels in IterateLLine
204
pixel values in  IterateLine are
114
114
114
114
114
114
114
114
114
114
114
114
114

我的main()如下所示:

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 ; 
}  

更新

这是我的画线()

void drawline( Mat image, std::vector<Point>& points )

{

cv::namedWindow("Output Window");

cv::setMouseCallback("Output Window", onMouse, (void*)&points);
int X1=0, Y1=0, X2=0, Y2=0; 




while(1)
{
    cv::imshow("Output Window", image);

    if (points.size() > 1) //we have 2 points
    {

        for (auto it = points.begin(); it != points.end(); ++it)
        {


        }


        break;
    }
    waitKey(10);


}


 X1=points[0].x; 
X2=points[1].x; 


Y1=points[0].y; 
Y2=points[1].y; 

     //   Draw a line 

line(image,  Point(X1, Y1), Point(X2, Y2), 'r',  2, 8 );

cv::imshow("Output Window", image);


//exit image window 
while(true)
    {
        char c = cv::waitKey(10);
        if(c == 'q')
            break;
    }


destroyWindow("Output Window");




}

更新2

正如@berak指出的那样,实际上我正在迭代我绘制的线。这条线有一种颜色,这就是我得到114的原因。所以我必须让线对IterateLine()透明(但对人类可见)。任何建议如何实现这一目标?

更新3

我在新的Mat对象img1:

中再一次解读了原始图像
    Mat img = imread("sir.png", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
    drawline(img, points1);
    Mat img1 = imread("sir.png", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
    IterateLine( img1, linePixels, points1[1], points1[0], t );

但这不是一种非常有效的方法,所以我仍然在寻找最好的方法。

0 个答案:

没有答案