[英]Image Processing Outlines
我正在编写一个进行基本图像处理的程序。
请记住,图像是灰度的,而不是RGB,而且我对Python还是很陌生,所以对我做错了/正确的事情的解释会非常有用。
我正在尝试编写遵循以下规则的大纲算法:
轮廓图像中原稿的所有浅色像素必须为白色。 轮廓图像中图像边缘上的所有暗像素必须为黑色。 如果不在图像边缘上的像素是暗的,而周围的所有8个像素都是暗的,则此像素在形状的内部,并且在轮廓图像中必须为白色。 轮廓图像中所有其他深色像素必须为黑色。
到目前为止,我有这个:
def outlines(image):
"""
Finds the outlines of shapes in an image. The parameter must be
a two-dimensional list of pixels. The return value is another
two-dimensional list of pixels which describes an image showing
outlines of the shapes in the original image. Each pixel in the
return value will be either black (0) or white (255).
"""
height=len(image)
width=len(image[0])
new_image=[]
for r in range(height):
new_row=[]
index=0
for c in range(width):
if image[r][c]>128:
new_row.append(255)
if image[r][c]<=128:
new_row.append(0])
new_image.append(new_row)
有人可以告诉我如何将算法实现到我的轮廓函数中吗?
提前致谢。 编辑:这是我的大学计算机科学课的作业,我不是要别人做作业,而是因为我实际上不知道下一步是什么。 Edit2:如果有人可以向我解释一个简单的边缘检测功能,该功能类似于我需要创建的算法,我将不胜感激。
除了检查像素是否暗或清晰外,还应检查在黑暗时像素周围的其余像素是否也暗,以使该点变为白色。
检查此功能并尝试将其用于该目的:
def all_are_dark_around(image, r, c):
# range gives the bounds [-1, 0, 1]
# you could use the list directly. Probably better for this especific case
for i in range(-1,2):
for j in range(-1,2):
# if the pixel is clear return False.
# note that image[r+0][c+0] is always dark by definition
if image[r+i][c+j] <= 128:
return False
#the loop finished -> all pixels in the 3x3 square were dark
return True
建议:
r
或c
等于0
或height
或width
时,切勿检查image[r][c]
。 也就是说,当选中的像素位于边框中时,因为在这种情况下,图像中至少有一侧没有相邻像素要看,因此您将得到IndexError
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.