使用python的opencv将图像分割后的mask轮廓叠加在原图上


问题

  • 原始数据是原图像以及分割后的白色背景黑色mask块,要将mask的轮廓画在原图上,方便可视化分割结果
  • 图像和mask如下:
  • 原图像:0.bmp-586kB
  • mask:0.bmp-257.1kB

实现方法

  • 先将原图和mask分别用cv2库读入,因为这里的数据集mask和原图尺寸不一致,所以需要resize。
  • 读入后使用cv2的轮廓检测函数findContours()检测出mask的轮廓,因为findContours()函数是检测黑底白色对象的轮廓,所以需要转换一波。
  • 转换后的轮廓使用cv2的drawContours()函数画在原图上。
  • 实现代码:
def union_image_mask(image_path, mask_path, num):
    # 读取原图
    image = cv2.imread(image_path)
    # print(image.shape) # (400, 500, 3)
    # print(image.size) # 600000
    # print(image.dtype) # uint8

    # 读取分割mask,这里本数据集中是白色背景黑色mask
    mask_2d = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
    # 裁剪到和原图一样大小
    mask_2d = mask_2d[0:400, 0:500]
    h, w = mask_2d.shape
    cv2.imshow("2d", mask_2d)

    # 在OpenCV中,查找轮廓是从黑色背景中查找白色对象,所以要转成黑色背景白色mask
    mask_3d = np.ones((h, w), dtype='uint8')*255
    # mask_3d_color = np.zeros((h,w,3),dtype='uint8')
    mask_3d[mask_2d[:, :] == 255] = 0
    cv2.imshow("3d", mask_3d)
    ret, thresh = cv2.threshold(mask_3d, 127, 255, 0)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnt = contours[0]
    cv2.drawContours(image, [cnt], 0, (0, 255, 0), 1)
    # 打开画了轮廓之后的图像
    cv2.imshow('mask', image)
    k = cv2.waitKey(0)
    if k == 27:
        cv2.destroyAllWindows()
    # 保存图像
    # cv2.imwrite("./image/result/" + str(num) + ".bmp", image)

实现结果

  • Snipaste_2019-10-22_20-08-17.png-163.9kB

附录

  • 参考资料:opencv python 图像轮廓/检测轮廓/绘制轮廓

  • 有关findContours()和drawContours()两个函数的详情可在上面的参考链接中查看。

  • 可以在cv2.drawContours(image, [cnt], 0, (0, 255, 0), 1)这一行代码中修改函数参数,改动轮廓的颜色和大小。

GitHub 加速计划 / opencv31 / opencv
238
21
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
6950bedb Use Mat::total() in Darknet IO 2 天前
105a7747 doc: update image codec configuration reference (add AVIF, fix JPEG XL) #28393 This PR updates the build configuration documentation to: - Add AVIF to the supported format list (it was missing). - Clarify that some codecs (AVIF, JPEG XL) do not support BUILD_* options because OpenCV does not bundle their source code. - Update the description to include "write" capabilities for these formats. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake 2 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐