【发布时间】:2021-05-11 15:21:21
【问题描述】:
我致力于在 python 中实现区域增长算法。但是当我在输出上运行此代码时,我得到没有错误的黑色图像。在输入图像上使用 CV 阈值函数,对于种子值,我使用鼠标单击将 x,y 值存储在元组中。
def get8n(x, y, shape):
out = []
if y-1 > 0 and x-1 > 0:
out.append( (y-1, x-1) )
if y-1 > 0 :
out.append( (y-1, x))
if y-1 > 0 and x+1 < shape[1]:
out.append( (y-1, x+1))
if x-1 > 0:
out.append( (y, x-1))
if x+1 < shape[1]:
out.append( (y, x+1))
if y+1 < shape[0] and x-1 > 0:
out.append( ( y+1, x-1))
if y+1 < shape[0] :
out.append( (y+1, x))
if y+1 < shape[0] and x+1 < shape[1]:
out.append( (y+1, x+1))
return out
def region_growing(img, seed):
list = []
outimg = np.zeros_like(img)
list.append((seed[0], seed[1]))
while(len(list)):
pix = list[0]
outimg[pix[0], pix[1]] = 255
for coord in get8n(pix[0], pix[1], img.shape):
if img[coord[0], coord[1]] > 0:
outimg[coord[0], coord[1]] = 255
list.append((coord[0], coord[1]))
list.pop(0)
return outimg
def on_mouse(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
print 'Seed: ' + str(x) + ', ' + str(y)
clicks.append((y,x))
clicks = []
image = cv2.imread('lena.jpg', 0)
ret, img = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)
cv2.namedWindow('Input')
cv2.setMouseCallback('Input', on_mouse, 0, )
cv2.imshow('Input', img)
cv2.waitKey()
seed = clicks[-1]
cv2.imshow('Region Growing', region_growing(img, seed))
cv2.waitKey()
cv2.destroyAllWindows()
【问题讨论】:
-
既然您询问的是读取和显示图像,您能否包含读取/操作输入图像和显示输出图像的代码?
-
输入图片有全部代码
def on_mouse(event, x, y, flags, params): if event == cv2.EVENT_LBUTTONDOWN: print 'Seed: ' + str(x) + ', ' + str(y) clicks.append((y,x)) clicks = [] image = cv2.imread('lenna.jpg', 0) ret, img = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY) cv2.namedWindow('Input') cv2.setMouseCallback('Input', on_mouse, 0, ) cv2.imshow('Input', img) cv2.waitKey() seed = clicks[-1] cv2.imshow('Region Growing', region_growing(img, seed)) cv2.waitKey() cv2.destroyAllWindows() -
将代码添加到问题中,格式正确 - 这样不可读。
-
你希望你的代码做什么?现在,它似乎在您最后单击的位置绘制了一个带有白色像素的空白图像。如果单击输入的白色区域,它将进入无限循环。另一方面,这意味着输出图像不是黑色的,而是有一个白色像素。
-
我想通过种子像素(点击)的所有相邻像素和值大于0的像素设置为255。只要有大于0的相邻像素就继续。并分配它们到将成为输出图像的区域