Mashruwala Dhruvin
Mashruwala Dhruvin

Reputation: 61

How to find the length and width of detected objects by findContours function?

I have an image that contains different types of flakes. This is an example image:

Example image

In this image, all black portion is a flake and I want to find the length and the width of each flake.

I used the findContours function to detect all these flakes. I also used the arcLength function for finding the length but I don't know the return value of this function.

So, is there any method to measure the perfect length and width of an object?

Upvotes: 2

Views: 280

Answers (2)

Mauro Dorni
Mauro Dorni

Reputation: 487

As already suggested, the simplest approach is looking to the minimun rectangle that contains your flake, and this is the function to call:

Opencv minAreaRect

Upvotes: 0

Dr Yuan Shenghai
Dr Yuan Shenghai

Reputation: 1915

Depends on the accuracy you need.

Simplest way is to use opencv minrect function on each sub detection layer (just transport each detection countour[i] to a layer)

RotatedRect minAreaRect(InputArray points)

The output rect width and height is what you are looking for

enter image description here

The other tool is call convexhull. To determine this, you have to find principle direction (eigen) and find the length. If you need to find a curved length, then you have to trace alone the non-convexhull area. it will be a bit complicated

void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true )

enter image description here

Upvotes: 1

Related Questions