KUMAR
KUMAR

Reputation: 25

how to access all pixels between 2 non linear points on an image

I am trying to access all the pixels between 2 non linear points. But I could not do it. To say it simple, with function cvLine i draw a line between two points, as shown in image the image below (I want to access the pixels along green line between 2 red points).

enter image description here

I tried the following:

Rect myROI(midPx, midPy, (edgPx-midPx), (midPy-edgPy)+1);    
Mat croppedImage = mask(myROI);   

It doesn't work in my case.

Can anyone help me to solve this?

I am sorry, actually i tried the same as in example, it was mistake while commenting. I tried both the methods from the example;

LineIterator it(mask, Point(midPx, midPy), Point(edgPx, edgPy), 8);
LineIterator it2 = it;
vector<Vec3b> buf(it.count);
//cout << buf<< endl;
for(int i = 0; i < it.count; i++, ++it)
{
    buf[i] = *(const Vec3b)*it;
}

// alternative way of iterating through the line
//for(int i = 0; i < it2.count; i++, ++it2)
 //{
   // cout <<it2.pos()<<","<<val<< endl;
    //buf.at<Vec3b>(Point(i,0)) = val;
//}
imshow("buf Image", buf);

but left with error at buf[i]

erreur: no match for ‘operator*’ in ‘*cv::Vec<unsigned char, 3>(((const unsigned char*)it.cv::LineIterator::operator*()))

Upvotes: 2

Views: 355

Answers (1)

Moreira
Moreira

Reputation: 586

Check out LineIterator, which is used to get each pixel of a line and allows you to process them: http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html#lineiterator

Small example (based on the link):

// grabs pixels along the line (pt1, pt2)
// from 8-bit 3-channel image to the buffer
LineIterator it(img, pt1, pt2);
vector<Vec3b> buf(it.count);

// iterate through the line
for(int i = 0; i < it.count; i++, ++it)
    buf[i] = *(const Vec3b)*it;

Upvotes: 1

Related Questions