cv后处理网络预测出现断开的情况

尽管网络修改可以在一定程度上提高预测结果的准确性,但是模型对于窄区域的预测效果仍然存在一些问题,其中最为关键的就是区域无法完全连续起来,会存在断开的情况,因此在模型预测结果的基础上,使用CV算法进行进一步的优化,对于该部分,当检测到mask图中轮廓大于1的时候,对轮廓排序,将轮廓的右下角和下一个轮廓的左上角相连,之后使用top-hat变换,增强暗背景中感兴趣的明亮物体,最后通过膨胀腐蚀让线条顺滑。此方法适用于一张图片中存在断开的轮廓将其连接起来。

 代码:

    def find_y(self,contours):
        min_y = float('inf')
        max_y = 0
        min_y_point = [0,0]
        max_y_point = [0,0]

        # 遍历轮廓中的点,找到最低和最高的 y 坐标及对应的点
        for contour in contours:

            for point in contour:

                x, y = point
                if y < min_y:
                    min_y = y
                    min_y_point = [x, y]
                if y > max_y:
                    max_y = y
                    max_y_point = [x, y]

        return min_y_point, max_y_point

    def find_x(self,contours):
        min_x = float('inf')
        max_x = 0
        min_x_point = [0,0]
        max_x_point = [0,0]

        # 遍历轮廓中的点,找到最低和最高的 y 坐标及对应的点
        for contour in contours:

            for point in contour:

                x, y = point
                if x < min_x:
                    min_x = x
                    min_x_point = [x, y]
                if x > max_x:
                    max_x = x
                    max_x_point = [x, y]

        return min_x_point, max_x_point

    def solution(self,image,contours):


        # 对轮廓按照 y 坐标进行排序
        sorted_contours = sorted(contours, key=lambda c: cv2.boundingRect(c)[1])
        # 如果第一个轮廓x大就对轮廓按照 x 坐标进行排序
        if cv2.boundingRect(sorted_contours[0])[0] > cv2.boundingRect(sorted_contours[1])[0]:

            sorted_contours = sorted(contours, key=lambda c: cv2.boundingRect(c)[0])
            for i in range(len(sorted_contours) - 1):

                current_contour = sorted_contours[i]
                next_contour = sorted_contours[i + 1]
                point1, _ = self.find_x(next_contour)
                # print(point1)
                _, point2 = self.find_x(current_contour)
                # print(point2)
                point1 = np.array(point1)
                point2 = np.array(point2)
                distance = np.linalg.norm(point1 - point2)
                if distance < 70:
                    cv2.line(image, point2, point1, (255, 255, 255), thickness=5)
        else:

            for i in range(len(sorted_contours) - 1):

                # img = np.zeros((1024, 1024), dtype=np.uint8)
                # patch_img = np.array(cv2.drawContours(img, sorted_contours[i], -1, 255, -1), dtype=np.uint8)
                # cv2.imwrite(f'i_{i}.png',patch_img)

                current_contour = sorted_contours[i]

                next_contour = sorted_contours[i + 1]
                point1,_ = self.find_y(next_contour)
                # print(point1)
                _,point2 = self.find_y(current_contour)

                point1 = np.array(point1)
                point2 = np.array(point2)

                distance = np.linalg.norm(point1 - point2)
                # print('distance',distance)
                if distance < 70:
                    cv2.line(image, point2, point1, (255, 255, 255), thickness=5)
        #cv2.imwrite('ima.png', image)
        kernel = np.ones((10, 10), np.uint8)

        tophat_transform = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel)   # 进行 Top-Hat 变换
        thickened_image = cv2.add(image, tophat_transform)  # 合并原始图像和变换后的图像,使细线部分变粗
        #cv2.imwrite('thickened_image.png', thickened_image)
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 返回椭圆形
        dilate = cv2.dilate(thickened_image, kernel, iterations=5)  #
        erode = cv2.erode(dilate, kernel, iterations=5)  #
        #cv2.imwrite('final.png', erode)
        contours, _ = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        return contours

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值