整个宽度的轮廓宽度测量 [英] Contour width measurement along its entire lendth
问题描述
我正在做一个测量轮廓宽度的项目.我已经从图像中检测到轮廓(参见图像1).下一步是测量coutour沿其长度的宽度(如图2所示).请给我建议任何想法.非常感谢您的帮助!
I am working on a project to measure width of contour. I have detected the contour from the image (see image 1). The next step is to measure the width of the coutour along its length (like shown in image 2). Please suggest me any ideas. I truly appreciate your help!
谢谢!
我有一个计算轮廓的功能,然后下一步是沿着选定轮廓的长度测量其轮廓的宽度.下面是示例代码.
I have a function that calculates contours and then next step is to measure the width from selected contours along their length. Below is the sample code.
...
// image is read, thresholded and canny edges are detected. That image is input to a function that computes contours from the image.
///Below is the code in the contour function
cv::Mat src_contour= inputImage.clone(); // input image is cloned for contour detection
cv::Mat maskContour = cv::Mat::zeros(src_contour.size(), CV_8UC3);
std::vector<std::vector<cv::Point> > contours; // stores contours points. Each contour is stored in a vector and there are number of vectors for number of contours
cv::RNG rng(12345); // random number used for random colours of contours
cv::findContours( src_contour, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
int nc=contours.size();// nc: total number of countours detected
std::vector<int>areas(nc); // stores area of each contour in a vector
std::vector<double>arclens(nc); // stores arc length (perimeter) of each contour in a vector
std::vector<double>aspect_ratio(nc); // Apect ratio of the contour , width/height
std::vector<cv::Rect> r(nc) ; // Vector of rectangles,
int min_area=15000 ; // Minimum area for contour selection
int min_arclen=1000; // Minimum Arc length used for filtering contours
double min_aspRatio= 2; // Minimum Aspect ratio used for filtering , deafutl 2.0
double max_aspRatio=4; // Maximum aspect rario used for filtering , default 4.0
for (int i=0; i< nc; i++) // Loop iterates through contours , calculates properties and draws selected contours
{
areas[i]=cv::contourArea(contours[i],false); // Area of each contour is stored in a vector, false: any contour, true: closed contour
arclens[i]=cv::arcLength(contours[i],false); // Arclength of each contour is stored in a vector
r[i]=cv::boundingRect(contours[i]); // Stores bounding rect for each contour in a vector r
aspect_ratio[i]=float(r[i].width)/r[i].height; // Aspect ratio of each contour is stored in a vector
if ((areas[i] > min_area) && (arclens[i] > min_arclen) && (aspect_ratio[i] > min_aspRatio && aspect_ratio[i] < max_aspRatio))
{
cv::drawContours(maskContour, contours, i, cv::Scalar(255,255,255), CV_FILLED); // creates mask from contours (filterd by criteria), fills them
}
}
// maskContour image is the image of selected contours filled , I have access to all the points on the contour. From the selected contours
// need to compute width of contours
.....
// Now contour width measurement is required
推荐答案
我认为distanceTransform
(OpenCV的)和skeleton
(也许是你自己的)会起作用.
I think distanceTransform
(of OpenCV) and skeleton
(maybe by yourself) will work.
主要思想:
- 将您的灰度图像阈值化,然后执行
distanceTransform
以获得dist-map - 找到dist-map的骨架,宽度是骨架值的两倍.
- Threshold you gray image, then do
distanceTransform
to get dist-map - Find the skeleton of the dist-map, the width is twice of the skeleton value.
距离图如下.
The distance map is as follow.
然后尝试find the skeleton
,double the dist value
来获取宽度.
Then you try to find the skeleton
, double the dist value
to get the width.
使用C ++代码更新:
Update with C++ code:
int main() {
// read as gray and threshold
Mat gray, threshed, dist;
gray = imread("img01.png", 0);
threshold(gray, threshed, 100, 255, THRESH_BINARY);
imshow("threshed", threshed);
//distanceTransform
distanceTransform(threshed, dist, DIST_L2, 3);
// normalize for display
Mat dst;
normalize(dist, dst, 255, 0, NORM_MINMAX,CV_8UC1);
imshow("dst", dst);
waitKey();
return 0;
}
这篇关于整个宽度的轮廓宽度测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!