英文:
I want to get the edge of the mask in python, which will be used for labels in deep learning loss function
问题
这是我的当前代码,处理数据需要很长时间。
我想加快这个过程,谢谢!
英文:
This is my current code, which takes a long time to process the data.
I want to speed up this process, thanks!
for b_index in range(masks_np.shape[0]):
for s_index in range(masks_np.shape[1]):
imageio.imwrite('./mask_seg_vis/'+s_number+"_"+str(s_index)+str(b_index)+'.png', masks_np[b_index][s_index][0])
unique_values= np.unique(masks_np[b_index][s_index][0].reshape(-1))
mask_edge = np.zeros(masks_np[b_index][s_index][0].shape)
for v_index in range(len(unique_values)):
itemindex_1 = np.where(masks_np[b_index][s_index][0] == unique_values[v_index])
itemindex_0 = np.where(masks_np[b_index][s_index][0] != unique_values[v_index])
v_mask = copy.deepcopy(masks_np[b_index][s_index][0])
v_mask[itemindex_1] = 255
v_mask[itemindex_0] = 0
for x in range(v_mask.shape[0]):
for y in range(v_mask.shape[1]):
if v_mask[x][y] == 0:
continue
if x == 0 or y == 0 or x == v_mask.shape[0]-1 or y == v_mask.shape[1]-1:
continue
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:
continue
mask_edge[x][y]=255
imageio.imwrite('./mask_seg_vis/'+s_number+"_"+str(s_index)+str(b_index)+'_edge.png', mask_edge)
答案1
得分: 0
您可以使用Python/OpenCV中的Canny边缘检测几乎获得相同的结果。
输入:
[![输入图像描述][1]][1]
import cv2
import numpy as np
# 以灰度图像读取输入
img = cv2.imread('mask_grays.png', cv2.IMREAD_GRAYSCALE)
# 进行Canny边缘检测
canny = cv2.Canny(img, 0, 0)
# 保存结果
cv2.imwrite('mask_grays_canny.png', canny)
# 显示结果
cv2.imshow('canny', canny)
cv2.waitKey(0)
Canny结果:
[![Canny结果图像描述][2]][2]
[1]: https://i.stack.imgur.com/Cxq62.png
[2]: https://i.stack.imgur.com/mpqBd.png
英文:
You can get nearly the same result using Canny edge detection in Python/OpenCV.
Input:
import cv2
import numpy as np
# read the input as grayscale
img = cv2.imread('mask_grays.png', cv2.IMREAD_GRAYSCALE)
# do canny edge detection
canny = cv2.Canny(img, 0,0)
# save results
cv2.imwrite('mask_grays_canny.png', canny)
# show results
cv2.imshow('canny', canny)
cv2.waitKey(0)
Canny Result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论