# 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. This is how i am creating the polyline.

polyLine.clear();
for (int i = 0; i < blobs.size(); i++){
ofPolyline tempPl;
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,

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 = tempPl.getResampledBySpacing(10);

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

ofVec3f p2(blobs[i].pts.x, blobs[i].pts.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.x,blobs[i-1].pts.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
{"ft_lang_label":"__label__en","ft_lang_prob":0.7154495,"math_prob":0.96421504,"size":661,"snap":"2022-27-2022-33","text_gpt3_token_len":161,"char_repetition_ratio":0.15829529,"word_repetition_ratio":0.0,"special_character_ratio":0.23903178,"punctuation_ratio":0.16296296,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9885839,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-05T15:14:10Z\",\"WARC-Record-ID\":\"<urn:uuid:26141250-ba3a-43f8-90d2-f1be622ce61a>\",\"Content-Length\":\"24585\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d307db61-62f6-4977-a749-6888109c2e13>\",\"WARC-Concurrent-To\":\"<urn:uuid:2482ae63-f08a-4d9d-a9e2-cb352e3cbdba>\",\"WARC-IP-Address\":\"192.237.163.93\",\"WARC-Target-URI\":\"https://forum.openframeworks.cc/t/finding-the-normals-of-a-polyline/23071\",\"WARC-Payload-Digest\":\"sha1:LOUZL32IROXXG5VXM5PPQE6LAQWAFPRL\",\"WARC-Block-Digest\":\"sha1:QHR2YPFJRXITASCRV7MN2LYVNOLTSCLD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104585887.84_warc_CC-MAIN-20220705144321-20220705174321-00739.warc.gz\"}"}