例子

這是一個非常簡單的影象處理和計算機視覺 Python 練習系列,旨在介紹這些主題,幾乎沒有實用性。對於任何新手的錯誤,我很抱歉,它仍處於開發階段。在本系列中,為了簡單起見,我將限制我們可以用於 PNG 檔案的數字影象,我還將討論影象壓縮的主題。

如果你還沒有這樣做,請克隆儲存庫,或者你可以通過 Github 在此處下載它 :

git clone https://github.com/Skorkmaz88/compvis101

你可以使用兩個檔案,其中一個是 tutorial0.py,另一個是 readingImages.ipynb,第二個是 ipython 筆記本,如果你喜歡使用它你可以這樣做。但是兩個檔案正在做同樣的事情。

程式碼在評論中有解釋,我是

 # libs
import png

# We create a greyscale image as described in our text.
# To do that simply, we create a 2D array in python. 
# x and y, x being horizontal and y being vertical directions.

x  = []
y = []
# Play around with these pixels values to get different grayscale images, they shoud be 
# in range of 0 - 255. 
white = 255
gray = 128
black = 0
width  = 100
height = 300

# Add 100 x 100 rectangle as just white(255) valued pixels
for i in range(0, 100):
    for j in range(0,100):
        y.append(white); # Pixel (i,j) is being set to a value, rest is coding trick to nest two lists 
    x.append(y)
    y = []
    
# Add 100 x 100 rectangle as just mid-gray(128) valued pixels
for i in range(0, 100):
    for j in range(0,100):
        y.append(gray);
    x.append(y)
    y = []

# Add 100 x 100 rectangle as just black(0) valued pixels
for i in range(0, 100):
    for j in range(0,100):
        y.append(black);
    x.append(y)
    y = []

# output image file 
f = open('out.png', 'wb')
w = png.Writer(width, height , greyscale=True, bitdepth=8)
w.write(f, x)
f.close()
# If everything went well, you should have 3 vertically aligned rectangles white, gray and black 
# Check your working folder

# PART 2
# Read a grayscale image and convert it to binary

# This time we will binarize a grayscale image, to do that we will read pixels and according to threshold we set
# we will decide if that pixel should be white or black 

# This file is originally 8 bit png image, can be found in github repository, you should use only this type of
# images if you want to change the image.
f = open('./img/lenaG.png', 'r')

r=png.Reader(file=f)
# You will the details about the image, for now pay attention to size and bitdepth only.
img = r.read()

width = img[0]
height = img[1]
# Threshold value for binarizing images, 
threshold = 128
print "Input image size is: "+ str(width)+ " pixels as  width, " + str(height) + " pixels as height"

f_out = open('lenaBinary.png', 'wb')
w = png.Writer(width, height , greyscale=True, bitdepth=1)

pixels = img[2]
 
x = []
y = []

# Let's traverse the Lena image 
for row in pixels:
    for pixel in row:
        p_value =  pixel
        # Now here we binarize image in pixel level
        if p_value > threshold:
            p_value = 1
        else:
            p_value = 0
            
        y.append(p_value);
    x.append(y)
    y = []

w.write(f_out, x)
f_out.close()

如果一切順利,恭喜! 你從頭開始建立影象,並在現有影象上執行第一個畫素級轉換。檢查你的工作資料夾以檢視新影象