0

I want to find the inner and outer diameter of the spring washer I calculated that I can create two ellipses with the fitellipse function of opencv and find the diameter with the help of these ellipses, but I cannot create two ellipses, only one ellipse is formed. How can I create two ellipses or is there any other way to find inner and outer diameters?

std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::Mat thresh_output;        
double diameter[10000]{0};
int thresh=ui->cam_one_threshold->value();        
cv::Mat openCvImage=cv::imread("deneme_calib.png");  
cv::cvtColor(openCvImage, openCvImage, cv::COLOR_BGRA2GRAY);  
cv::blur(openCvImage, openCvImage, cv::Size(3, 3));
cv::threshold(openCvImage,thresh_output, thresh, thresh*3, cv::THRESH_BINARY_INV);
cv::findContours(thresh_output, contours, hierarchy, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);        
cv::cvtColor(openCvImage, openCvImage, cv::COLOR_GRAY2RGB);        
for (int i = 0; i < (int)contours.size(); i++)        
{            
   {                
    cv::drawContours(openCvImage, contours, i, cv::Scalar(0, 255, 0), 2, 8, hierarchy, 0, cv::Point());
    diameter[i] =2*std::sqrt(cv::contourArea(contours[i]) / (M_PI));            
    cv::ellipse(openCvImage,cv::fitEllipse(contours[i]),cv::Scalar(0,0,255),1);
   }    
}

enter image description here

  • After the first ellipse: for each contour point that is close to the ellipse contour: remove it from the list. Then use fitEllipse again on the remaining points. – Micka May 21 '23 at 18:52
  • 3
    MV approach: bounding box to get center, then run a bunch of rays outward for 1D sample series, find gradients (edges), use those points for fitting. discard if the ray has anything but a rising and falling edge (i.e. it's not cutting the bulk of the ring but the gap of the ring). also discard outliers of the point sets for inner and outer ring. for the 1D samples, you'd want to use `remap()` (calculate samples for all rays at once, allows precalc) or `warpAffine()` (taking a ray at a time). for the gradient (local extrema) stuff... idk, I'd do it with python, where there is numpy and scipy – Christoph Rackwitz May 21 '23 at 19:18
  • 1
    very related: https://stackoverflow.com/questions/61375393/washer-measurement-with-diplib – Christoph Rackwitz May 21 '23 at 19:20
  • here's a bit of visualization: https://i.stack.imgur.com/M8OiR.png and a lot of code to do it. for the center: median blur, threshold, bounding box. for the measuring: some np.meshgrid and remap, some np.gradient, ad-hoc NMS (morphology, boolean ops, np.diff, np.nonzero), more polar geometry, ellipse fit, rejecting outliers, another fit, some drawing – Christoph Rackwitz May 21 '23 at 22:28
  • you might want to revisit your previous question and use diplib. looks like the important stuff is implemented there already. – Christoph Rackwitz May 21 '23 at 22:34
  • > only one ellipse is formed --> if so, you will be able to split contour points to 2 groups : inside or outside of the ellipse. And then fit ellipse for each group. (Of cause, you may need to do some other work for example removing some unnecessary points for fitting) – fana May 23 '23 at 09:04

0 Answers0