标记针脚
检测元件针脚并标记(不灵活)基础太浅,记录一下
代码import cv2
import numpy as np
img = cv2.imread('17.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray",gray)
#定义ROI
mask = np.zeros(gray.shape, dtype=np.uint8)
#cv2.imshow("mask", mask)
mask[0:121, 134:481] =255 # 高起始:高结束, 长起始:长结束 ,且左上角为起始点
#cv2.imshow("masked", mask)
res = cv2.bitwise_and(gray, mask)
cv2.imshow("res", res)
#二值化
ret, binary = cv2.threshold(res,0, 255 , cv2.THRESH_BINARY_INV | cv2.THRESH_TRIANGLE) # 自适应二值化
#cv2.imshow("binary", binary)
#引脚加mask
mask = np.zeros(gray.shape, dtype=np.uint8)
mask[0:121, 134:481] =255 # 高起始:高结束, 长起始:长结束 ,且左上角为起始点
res_line = cv2.bitwise_and(binary, mask)
#cv2.imshow("res_line",res_line)
#开运算
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
opening_line = cv2.morphologyEx(res_line, cv2.MORPH_OPEN, kernel)
cv2.imshow("opening_line", opening_line)
#canny边缘检测
low_threshold = 60
high_threshold = 200
canny_image = cv2.Canny(res, low_threshold, high_threshold)
cv2.imshow("canny_image",canny_image)
#画出引脚边界
contours, hierarchy = cv2.findContours(opening_line, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours_line = cv2.drawContours(img, contours, -1, (255,0,0), 3)
#cv2.imshow("contours_line",contours_line)
#判断是否异常,画出警示边界
contours, hierarchy = cv2.findContours(res, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours_img = cv2.drawContours(img, contours, -1, (0,0,255), 3)#红色框代表引脚异常,绿色反之
cv2.imshow("contours_img",contours_img)
cv2.waitKey()
cv2.destroyAllWindows()
代码二(小改)
import cv2
import numpy as np
img = cv2.imread('3.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray",gray)
#定义ROI
mask = np.zeros(gray.shape, dtype=np.uint8)
#cv2.imshow("mask", mask)
mask[0:186, 235:509] =255 # 高起始:高结束, 长起始:长结束 ,且左上角为起始点
#cv2.imshow("masked", mask)
res = cv2.bitwise_and(gray, mask)
#cv2.imshow("res", res)
#二值化
#ret, binary = cv2.threshold(res,0, 255 , cv2.THRESH_BINARY_INV | cv2.THRESH_TRIANGLE) # 自适应二值化
ret, binary = cv2.threshold(res, 220, 255, cv2.THRESH_BINARY)#自定义阈值
cv2.imshow("binary", binary)
#引脚加mask
mask = np.zeros(gray.shape, dtype=np.uint8)
mask[0:186, 235:509] =255 # 高起始:高结束, 长起始:长结束 ,且左上角为起始点
res_line = cv2.bitwise_and(binary, mask)
#cv2.imshow("res_line",res_line)
#开运算
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
opening_line = cv2.morphologyEx(res_line, cv2.MORPH_OPEN, kernel)
cv2.imshow("opening_line", opening_line)
#canny边缘检测
low_threshold = 60
high_threshold = 200
canny_image = cv2.Canny(res, low_threshold, high_threshold)
cv2.imshow("canny_image",canny_image)
#画出引脚边界
contours, hierarchy = cv2.findContours(opening_line, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours_line = cv2.drawContours(img, contours, -1, (255,0,0), 3)
#cv2.imshow("contours_line",contours_line)
#判断是否异常,画出警示边界
contours, hierarchy = cv2.findContours(res, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours_img = cv2.drawContours(img, contours, -1, (0,255,0), 3)#红色框代表引脚异常,绿色反之
cv2.imshow("contours_img",contours_img)
cv2.waitKey()
cv2.destroyAllWindows()
图片:
原图:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)