EfficientSAM分割对象后求其中图像中的高

1 分割对象

EfficientSAM

https://github.com/yformer/EfficientSAM

2 计算在图像中最高点即y值最小点

import os
import cv2

def read_images(folder_path):
    image_files = [f for f in os.listdir(folder_path) if
                   f.endswith(".jpg") or f.endswith(".png")]  # 获取文件夹中所有的jpg和png图片文件
    sorted_image_files = sorted(image_files, key=lambda x: int(''.join(filter(str.isdigit, os.path.splitext(x)[0]))))

    images = []  # 用来存储读取的图像
    for filename in sorted_image_files:
        # print(filename)
        image_path = os.path.join(folder_path, filename)  # 构建完整的文件路径
        image = cv2.imread(image_path)  # 使用OpenCV库读取图像文件
        # image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)

        images.append(image)  # 将图像添加到列表中

    return images

def reprocess_image(img):
    # 读入图片
    # for i in range(len(images)):
    # img = images[i]
    # 显示图片
    # 将彩色图片转换成灰度图像
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 二值化处理
    # ret, thresh = cv2.threshold(gray, 50, 255, 0)
    # cv2.imshow("Binary Image", thresh)
    # cv2.waitKey(0)

    contour_image = img.copy()
    # 直接用灰度图寻找轮廓
    contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # 二值化处理后的图像再处理轮廓
    # contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 3)
    cv2.imshow("contour_image", contour_image)
    cv2.waitKey(0)
    return contours, img

def find_highest_y_point(contours, img):
    _highest_point = (0, 0)
    max_y = img.shape[0]
    for contour in contours:
        for point in contour:
            if point[0][1] < max_y:
                max_y = point[0][1]
                _highest_point = (point[0][0], point[0][1])
                # print(highest_point[0])
                # print(highest_point[1])

    # 在图像上标记最高点
    cv2.circle(img, _highest_point, 5, (0, 0, 255), -1)
    # 显示结果
    cv2.imshow('Highest Point', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return _highest_point

if __name__ == '__main__':
    folder_path = "final-images"  # 替换为你的文件夹路径
    highest_point = (0, 0)
    real_highest_y = 300
    if(read_images(folder_path) is not None):
        print("读取成功")
        print(len(read_images(folder_path)))
        for _image in read_images(folder_path):
            contours, img = reprocess_image(_image)
            highest_point = find_highest_y_point(contours, img)
            real_highest_y = img.shape[0] - highest_point[1]
            real_highest_y *= 0.25
            # print("最高点坐标:", highest_point)
            # print("最高点x坐标:", highest_point[0])
            # print("最高点y坐标:", highest_point[1])
            print(real_highest_y)
    else:
        print("读取失败")

分割后的图像

结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值