[英]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.