需要提取图片中船体的外围轮廓.
import cv2
img = cv2.imread('C:/Users/Administrator/Desktop/xiangmu/test_dis/tytyt.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
ret, binary = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
grad_img = cv2.dilate(img, kernal)-cv2.erode(img, kernal)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 0, 255), 3)
cv2.imshow('lunkuo', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
这是目前的效果
1.使用opencv进行 边缘检测以及轮廓检测并添加了形态学处理
看图片外周有一圈黑色的轮廓,直接抽取黑色的颜色然后找下轮廓试试看?
1 如果允许先设置ROI 限定到目标周围
2 在轮廓结果列表里,寻找最大面积的轮廓抛弃其他小轮廓