我想在Python中获取掩码的边缘,该边缘将用于深度学习损失函数中的标签。

huangapple go评论84阅读模式
英文:

I want to get the edge of the mask in python, which will be used for labels in deep learning loss function

问题

  1. 这是我的当前代码,处理数据需要很长时间。
  2. 我想加快这个过程,谢谢!
英文:

Mask example:
我想在Python中获取掩码的边缘,该边缘将用于深度学习损失函数中的标签。

Mask edge example:
我想在Python中获取掩码的边缘,该边缘将用于深度学习损失函数中的标签。

This is my current code, which takes a long time to process the data.
I want to speed up this process, thanks!

  1. for b_index in range(masks_np.shape[0]):
  2. for s_index in range(masks_np.shape[1]):
  3. imageio.imwrite('./mask_seg_vis/'+s_number+"_"+str(s_index)+str(b_index)+'.png', masks_np[b_index][s_index][0])
  4. unique_values= np.unique(masks_np[b_index][s_index][0].reshape(-1))
  5. mask_edge = np.zeros(masks_np[b_index][s_index][0].shape)
  6. for v_index in range(len(unique_values)):
  7. itemindex_1 = np.where(masks_np[b_index][s_index][0] == unique_values[v_index])
  8. itemindex_0 = np.where(masks_np[b_index][s_index][0] != unique_values[v_index])
  9. v_mask = copy.deepcopy(masks_np[b_index][s_index][0])
  10. v_mask[itemindex_1] = 255
  11. v_mask[itemindex_0] = 0
  12. for x in range(v_mask.shape[0]):
  13. for y in range(v_mask.shape[1]):
  14. if v_mask[x][y] == 0:
  15. continue
  16. if x == 0 or y == 0 or x == v_mask.shape[0]-1 or y == v_mask.shape[1]-1:
  17. continue
  18. if v_mask[x-1][y] == 255 and v_mask[x+1][y] == 255 and v_mask[x][y-1] == 255 and v_mask[x][y+1] == 255:
  19. continue
  20. mask_edge[x][y]=255
  21. imageio.imwrite('./mask_seg_vis/'+s_number+"_"+str(s_index)+str(b_index)+'_edge.png', mask_edge)

答案1

得分: 0

  1. 您可以使用Python/OpenCV中的Canny边缘检测几乎获得相同的结果。
  2. 输入:
  3. [![输入图像描述][1]][1]
  4. import cv2
  5. import numpy as np
  6. # 以灰度图像读取输入
  7. img = cv2.imread('mask_grays.png', cv2.IMREAD_GRAYSCALE)
  8. # 进行Canny边缘检测
  9. canny = cv2.Canny(img, 0, 0)
  10. # 保存结果
  11. cv2.imwrite('mask_grays_canny.png', canny)
  12. # 显示结果
  13. cv2.imshow('canny', canny)
  14. cv2.waitKey(0)
  15. Canny结果:
  16. [![Canny结果图像描述][2]][2]
  17. [1]: https://i.stack.imgur.com/Cxq62.png
  18. [2]: https://i.stack.imgur.com/mpqBd.png
英文:

You can get nearly the same result using Canny edge detection in Python/OpenCV.

Input:

我想在Python中获取掩码的边缘,该边缘将用于深度学习损失函数中的标签。

  1. import cv2
  2. import numpy as np
  3. # read the input as grayscale
  4. img = cv2.imread('mask_grays.png', cv2.IMREAD_GRAYSCALE)
  5. # do canny edge detection
  6. canny = cv2.Canny(img, 0,0)
  7. # save results
  8. cv2.imwrite('mask_grays_canny.png', canny)
  9. # show results
  10. cv2.imshow('canny', canny)
  11. cv2.waitKey(0)

Canny Result:

我想在Python中获取掩码的边缘,该边缘将用于深度学习损失函数中的标签。

huangapple
  • 本文由 发表于 2023年2月14日 03:22:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定