如何使用diplib精确测量平坦电池上的接触耳定位。

huangapple go评论94阅读模式
英文:

How to accurately measure the contact ear orientation on a flat battery using diplib

问题

以下是翻译好的内容:

  1. 我们的生产线希望自动化制造平板电池,我想精确测量平板电池的接触耳朝向并将电池中心坐标与耳朝向度数发送到LinuxCNC运动控制器。我采用了一种非常天真和临时的方式,使用diplibPython绑定来实现它,我不确定它是否足够精确并且是否能够承受生产线上的粗糙光源。以下是我的代码和测试图像。
  2. 我画了一条亚像素线,连接了耳朝向的中心和主圆的中心,看起来不错。但是找到这两个点的过程非常临时,我不确定它有多精确以及对光源的敏感性以及需要调整这些参数需要多少工作。因为我还没有将此代码转换成C++并加载到我的OpenGL框架中,使用GiGE相机进行评估。
  3. 最后,我分享了从Python转换为C++的cpp代码。
  4. 我不确定二值膨胀和开运算是否会导致错误。我仍然是计算机图形的学徒,计算机图形很难。
  5. 将这些代码转换为C++并集成到基于OpenGL的测试平台对我来说非常费力,因此在执行此操作之前,我想听到更加优雅和稳健的解决方案。

希望这些翻译对您有所帮助。如果您需要任何进一步的翻译或有其他问题,请随时告诉我。

英文:

Our production line want to automate the fabrication of flat battery, I want to accurately measure the contact ear orientation of the flat battery and send the center of the battery coordinate plus the ear orientation degrees to linuxCNC motion controller. I follow a very naive and ad hoc way to implement it using python binding of diplib, I am not sure it is accurate enough and bearing the coarse light source on production line. Here is my code and test images
如何使用diplib精确测量平坦电池上的接触耳定位。

  1. import diplib as dip
  2. img = dip.ImageRead('tail2.png')
  3. dip.viewer.Show(img)
  4. img2 = dip.ColorSpaceManager.Convert(img, 'grey')
  5. # blend the ultimate results on img3
  6. img3 = img.Copy()
  7. gm = dip.Norm(dip.GradientMagnitude(img2))
  8. dip.viewer.Show(gm)
  9. # detect main circle using watershed
  10. wlab = dip.Watershed(gm, connectivity=1, maxDepth=1, flags={'correct', 'labels'})
  11. dip.viewer.Show(wlab)
  12. wlab = dip.SmallObjectsRemove(wlab, 8000)
  13. dip.viewer.Show(wlab)
  14. # select main circle lab
  15. pp = (wlab == 18179)
  16. lab = dip.Label(pp)
  17. dip.viewer.Show(lab)
  18. # lab = dip.GrowRegions(lab, connectivity=1, iterations = 3)
  19. result = dip.MeasurementTool.Measure(lab, img, features=['Center', 'Radius'])
  20. print(result)
  21. circle = dip.Image(img.Sizes(), 1, 'SFLOAT')
  22. circle.Fill(0)
  23. # draw a solid circle as a mask, subtracted from wlab(id = 18179), obtain the tail(or ear)
  24. dip.DrawBandlimitedBall(circle, diameter=result[1]['Radius'][1]*2, origin=result[1]['Center'], value=1)
  25. circle /= dip.Maximum(circle)
  26. img *= 1 - circle
  27. img += circle * dip.Create0D([0,255,0])
  28. dip.viewer.Show(img)
  29. img] = 0
  30. mymask = img.Copy()
  31. mymask[mymask == [0,255,0]] = 1000
  32. dip.viewer.Show(mymask)
  33. mainCircle = dip.Threshold(dip.ColorSpaceManager.Convert(mymask, 'grey'))[0]
  34. dip.viewer.Show(mainCircle)
  35. mainCircle = dip.Dilation(mainCircle, dip.SE(1))
  36. # obtain the ear, open by reconstruction and opening
  37. tail = pp - mainCircle
  38. dip.viewer.Show(tail)
  39. tail = dip.OpeningByReconstruction(tail,15)
  40. tail = dip.Opening(tail,5)
  41. mylab = dip.Label(tail)
  42. dip.viewer.Show(mylab)
  43. # obtain the center of the ear, connect to center of the main cirle and blend it to the original image
  44. result2 = dip.MeasurementTool.Measure(mylab, img, features=['Center'])
  45. print(result2)
  46. imgline = dip.Image(img3.Sizes(), 1, 'SFLOAT')
  47. imgline.Fill(0)
  48. dip.DrawBandlimitedLine(imgline, start = result2[1]['Center'], end = result[1]['Center'])
  49. imgline /= dip.Maximum(imgline)
  50. img3 *= 1 - imgline
  51. img3 += imgline * dip.Create0D([0,255,0])
  52. dip.viewer.Show(img3)
  53. dip.ImageWrite(img3, 'mylab.jpg')

I draw a subpixels line between the centers of ear and the main circle, looks good. But the procedure of finding the two points is so ad hoc, I am not sure how accurate it is and how sensitive it is to the light source and how many effort should be involved tuning those parameters. For I have not converted this code into C++ and load into my OpenGL framework and evaluate using a GiGE camera
如何使用diplib精确测量平坦电池上的接触耳定位。

I am not sure binary dilation, opening could contribute errors, I am still a Apprentice in computer graphics, computer graphics is hard
如何使用diplib精确测量平坦电池上的接触耳定位。

Convert those code to C++ and integrate into my OpenGL based test platform is so labor intensive for me, before I do this, I want to hear from some more elegant and robust solution

Finally, I share the cpp code converted from python

  1. Mat orientation(Mat input) {
  2. try {
  3. dip::Image gray, battery;
  4. dip::Image src = dip_opencv::MatToDip(input);
  5. src.SetColorSpace("RGB");
  6. src.ResetPixelSize();
  7. dip::Gauss(src, gray, {1});
  8. gray = dip::MinimumTensorElement(gray);
  9. dip::Threshold(gray, battery);
  10. battery = dip::Label(battery, 0, 0, 0, {"remove"});
  11. battery = dip::SmallObjectsRemove(battery, 80000);
  12. battery.Convert(dip::DT_BIN);
  13. battery = dip::FillHoles(battery);
  14. dip::Image body = dip::Opening(battery, 60);
  15. dip::Image ear = battery - body;
  16. ear = dip::OpeningByReconstruction(ear, 7);
  17. dip::Image lab = dip::Convert(body, dip::DT_UINT8);
  18. lab.At(ear) = 2;
  19. lab.Convert(dip::DT_UINT8);
  20. lab.At(lab == 2) = 100;
  21. lab.At(lab == 1) = 160;
  22. dip::MeasurementTool msr;
  23. dip::Measurement sizes = msr.Measure(lab, {}, {"Center"}, {}, 1);
  24. std::cout << sizes << '\n';
  25. dip::Image imgline = dip::Image(src.Sizes(), 1, dip::DT_DFLOAT);
  26. imgline.Fill(0);
  27. dip::DrawBandlimitedLine(imgline, {sizes[100]["Center"][0], sizes[100]["Center"][1]}, {sizes[160]["Center"][0], sizes[160]["Center"][1]});
  28. imgline /= dip::Maximum(imgline);
  29. dip::Image output = src.Copy();
  30. output *= 1 - imgline;
  31. dip::Image my0d({0,255,0});
  32. output += imgline * my0d;
  33. return dip_opencv::CopyDipToMat(output);
  34. }
  35. catch (dip::Error const& e) {
  36. DIP_ADD_STACK_TRACE(e);
  37. std::cout << "exception: waterPreview" << '\n';
  38. return input;
  39. }
  40. }

add this line after dip::SmallObjectsRemove, greatly smooth the ear finding procedure

  1. battery = dip::FillHoles(battery);

如何使用diplib精确测量平坦电池上的接触耳定位。

pretty impressive, diplib is really light unbias, works very well on our production line
如何使用diplib精确测量平坦电池上的接触耳定位。

This is what I really want, the light source on production line is really sucks
如何使用diplib精确测量平坦电池上的接触耳定位。

答案1

得分: 1

我已简化您的代码如下。我认为这样更简单,虽然可能不会更精确,但通常更简单意味着更稳健。

  1. import diplib as dip
  2. img = dip.ImageRead('tail2.png')
  3. # 稍微降噪
  4. gray = dip.Gauss(img, 1)
  5. # 假定将图像中显示的电池转换为灰色
  6. gray = dip.MinimumTensorElement(gray)
  7. # 二值化,假定电池被较暗的背景包围
  8. # 保留未连接到图像边缘的最大片段
  9. battery = dip.Threshold(gray)[0]
  10. battery = dip.Label(battery, boundaryCondition=["remove"], mode="largest") > 0
  11. # 通过开操作将耳朵与身体分开
  12. body = dip.Opening(battery, 60)
  13. ear = dip.OpeningByReconstruction(battery - body, 7)
  14. # 获取身体和耳朵的中心
  15. lab = dip.Convert(body, "UINT8")
  16. lab[ear] = 2 # 身体的标签=1,耳朵的标签=2
  17. msr = dip.MeasurementTool.Measure(lab, features=['Center'])
  18. # 绘制线(与原始帖子中的代码相同)
  19. imgline = dip.Image(img.Sizes(), 1, 'SFLOAT')
  20. imgline.Fill(0)
  21. dip.DrawBandlimitedLine(imgline, start=msr[1]['Center'], end=msr[2]['Center'])
  22. imgline /= dip.Maximum(imgline)
  23. img *= 1 - imgline
  24. img += imgline * dip.Create0D([0,255,0])
  25. img.Show()

关于 battery = dip.Threshold(gray)[0] 这一行,您可能想尝试其他阈值方法,特别是 "triangle" 或 "background" 方法可能更合适。

英文:

I simplified your code as follows. I don't think it'll be more precise, but simpler usually means more robust.

  1. import diplib as dip
  2. img = dip.ImageRead('tail2.png')
  3. # A little bit of noise reduction
  4. gray = dip.Gauss(img, 1)
  5. # Conversion to gray assumes the battery shows white in the image
  6. gray = dip.MinimumTensorElement(gray)
  7. # Binarization assumes the battery is surrounded by darker background
  8. # We're keeping the largest segment not connected to the image edge
  9. battery = dip.Threshold(gray)[0]
  10. battery = dip.Label(battery, boundaryCondition=["remove"], mode="largest") > 0
  11. # We separate the ear by opening
  12. body = dip.Opening(battery, 60)
  13. ear = dip.OpeningByReconstruction(battery - body, 7)
  14. # Obtain the center of the body and ear
  15. lab = dip.Convert(body, "UINT8")
  16. lab[ear] = 2 # label for body = 1, label for ear = 2
  17. msr = dip.MeasurementTool.Measure(lab, features=['Center'])
  18. # Draw the line (code unchanged from OP)
  19. imgline = dip.Image(img.Sizes(), 1, 'SFLOAT')
  20. imgline.Fill(0)
  21. dip.DrawBandlimitedLine(imgline, start=msr[1]['Center'], end=msr[2]['Center'])
  22. imgline /= dip.Maximum(imgline)
  23. img *= 1 - imgline
  24. img += imgline * dip.Create0D([0,255,0])
  25. img.Show()

For the line battery = dip.Threshold(gray)[0], you might want to try other threshold methods. In particular the "triangle" or "background" methods could be good here.

huangapple
  • 本文由 发表于 2023年4月6日 23:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定