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

eanckbw9  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(102)

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


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



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

img_copy = cv.imread('face1.jpg')
gray = cv.cvtColor(img_copy, cv.COLOR_BGR2GRAY)
gray_invert = cv.bitwise_not(gray)

contours, hierarchy = cv.findContours(gray_invert,  cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
print("Numar de conture: {}\n".format(len(contours)))

cv.drawContours(img_copy, contours, -1, (0, 255, 0), 3)

cv.imshow('image', img_copy)
cv.waitKey(0)

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

img = cv.imread('face1.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# cleaning img
ret, threshold = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
print(threshold)
kernel = np.ones((5, 5), np.uint8)
opening = cv.morphologyEx(gray, cv.MORPH_CLOSE, kernel)

contours, hierarchy = cv.findContours(gray,  cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)

cv.drawContours(img_copy, contours, -1, (0, 255, 0), 3)

cv.imshow('image', img_copy)
cv.waitKey(0)

sqougxex

sqougxex1#

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

import cv2
import numpy as np

img = cv2.imread('box.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
threshold = 255 - threshold

# OR use inRange()
#lower=(70,60,60)
#upper=(90,80,80)
#threshold = cv2.inRange(img, lower, upper)

contours, hierarchy = cv2.findContours(threshold,  cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cv2.imwrite('box_contour.jpg', img_copy)

img_copy=img.copy()
cv2.drawContours(img_copy, contours, -1, (0, 255, 0), 3)

cv2.imshow('threshold', threshold)
cv2.imshow('image', img_copy)
cv2.waitKey(0)

字符串
x1c 0d1x的数据

相关问题