OpenCV检测从SolidWorks导出为jpg的2D对象的线条

eanckbw9  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(238)

我已经在SW中做了一个平行六面体,并将其中一个面导出为jpg文件。现在我正在尝试检测面的线条并在其上生成点。


的数据
我的代码检测img的外部线并放置一些随机点。



这是我的代码,其中face1.jpg是初始照片。

  1. img_copy = cv.imread('face1.jpg')
  2. gray = cv.cvtColor(img_copy, cv.COLOR_BGR2GRAY)
  3. gray_invert = cv.bitwise_not(gray)
  4. contours, hierarchy = cv.findContours(gray_invert, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
  5. print("Numar de conture: {}\n".format(len(contours)))
  6. cv.drawContours(img_copy, contours, -1, (0, 255, 0), 3)
  7. cv.imshow('image', img_copy)
  8. cv.waitKey(0)

字符串
同样基于其中一条评论,我做了阈值和形态学处理,得到两个轮廓,一个是洞的图像,一个是物体左角的点。

  1. img = cv.imread('face1.jpg')
  2. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  3. # cleaning img
  4. ret, threshold = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
  5. print(threshold)
  6. kernel = np.ones((5, 5), np.uint8)
  7. opening = cv.morphologyEx(gray, cv.MORPH_CLOSE, kernel)
  8. contours, hierarchy = cv.findContours(gray, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
  9. cv.drawContours(img_copy, contours, -1, (0, 255, 0), 3)
  10. cv.imshow('image', img_copy)
  11. cv.waitKey(0)

sqougxex

sqougxex1#

使用cv2.inRange()在Python/OpenCV中对灰色框进行阈值化或大津阈值化,但获得外部轮廓。如果框在白色背景上为黑色,则反转图像。

  1. import cv2
  2. import numpy as np
  3. img = cv2.imread('box.jpg')
  4. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  5. ret, threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  6. threshold = 255 - threshold
  7. # OR use inRange()
  8. #lower=(70,60,60)
  9. #upper=(90,80,80)
  10. #threshold = cv2.inRange(img, lower, upper)
  11. contours, hierarchy = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  12. cv2.imwrite('box_contour.jpg', img_copy)
  13. img_copy=img.copy()
  14. cv2.drawContours(img_copy, contours, -1, (0, 255, 0), 3)
  15. cv2.imshow('threshold', threshold)
  16. cv2.imshow('image', img_copy)
  17. cv2.waitKey(0)

字符串
x1c 0d1x的数据

展开查看全部

相关问题