Finding the normals of a polyline

Hi,
I am working on a project at the moment that uses openCv for the blob detection. I have created a polyline around the blobs but I want to create points on the polyline and get the normals of the polyline. The idea is that when the computer detects a blob it produces a noise growth ( in the direction of the normal angle) which starts from the polyline outlining the blob.

much appreciate any help. :slight_smile:

This is how i am creating the polyline.

polyLine.clear();
for (int i = 0; i < blobs.size(); i++){
ofPolyline tempPl;
tempPl.addVertices(blobs[i].pts);
tempPl.addVertex(blobs[i].pts[0]);
tempPl = tempPl.getResampledBySpacing(10);
polyLine.push_back(tempPl);
}

Hi Sabay,

You have to compute the normals per segment. To do so, as you already have the points (blobs[i].pts), you can compute them directly.

For each point, get the next point (this are the points forming the segment), and compute the normal of the line bewteen the points ( normal ( point2 - point1 ) )

Hi Hennio,

Thank you for your help.
This is what i’ve done.
I get a bad access error for ofVec3f p1

polyLine.clear();
for (int i = 0; i < blobs.size(); i++){
    ofPolyline tempPl;
    tempPl.addVertices(blobs[i].pts);
    tempPl.addVertex(blobs[i].pts[0]);
    tempPl = tempPl.getResampledBySpacing(10);
  
    ofVec3f p1(blobs[i-1].pts[0].x,blobs[i-1].pts[0].y);

    ofVec3f p2(blobs[i].pts[0].x, blobs[i].pts[0].y);
    
    float dx = p2.y-p1.y;
    float dy = p2.x-p1.x;
    
    ofVec3f norm(dx, -dy);
    norm.normalize();
    norm *=30;
    ofVec3f center ((p1.x+p2.x)*0.5, (p1.y+p2.y)*0.5);
    ofSetColor(200, 0, 0);
    ofNoFill();
    ofVec3f end (center.x,center.y);
    end+(norm);
    
    ofDrawLine(center.x, center.y,end.x,end.y);
    
    
    polyLine.push_back(tempPl);

At line

ofVec3f p1(blobs[i-1].pts[0].x,blobs[i-1].pts[0].y);

You are accessing blobs[i-1] when i = 0 in the first iteration, which is blobs[-1], and is not a valid index.

I recommend to use a different for loop to compute the normals:

for( i = 0; i < blobs.size(); i++)
{
    // build polyline
}

for( i = 1; i < blobs.size(); i++)
{
    // compute normals using blobs[i-1] and blobs[i]
}

Hi Hennio,

Thank you for your help :slight_smile: