python實作 影像處理 hough

import os
from PIL import Image, ImageFont, ImageDraw

def show(title, img):
    font_color = (224, 60, 138)
    font_size = 25
    font = ImageFont.truetype("AppleGothic.ttf", font_size)
    tmp_img = img.copy()
    d = ImageDraw.Draw(tmp_img)
    d.text((20, 20), title, font=font, fill=128)
    tmp_img.show()

def convolution(x, y, arr, pixels, pixels2):

    p = int(pixels[x-1, y-1]*arr[0][0] + pixels[  x, y-1]*arr[1][0] + pixels[x+1, y-1]*arr[2][0]\
          + pixels[x-1, y  ]*arr[0][1] + pixels[  x, y  ]*arr[1][1] + pixels[x+1, y  ]*arr[2][1]\
          + pixels[x-1, y+1]*arr[0][2] + pixels[  x, y+1]*arr[1][2] + pixels[x+1, y+1]*arr[2][2])

    pixels2[x, y] = p


def image_add(pixels, pixels2):
    image = Image.new('L', size, "black")
    pixels3 = image.load()
    for x in range(size[0]-2):
        for y in range(size[1]-2):
            pixels3[x, y] = pixels[x, y]+pixels2[x, y]
    return image

def thresholding(pixels,thr):
    image = Image.new('L', size, "black")
    pixels3 = image.load()
    for x in range(size[0]-2):
        for y in range(size[1]-2):
            if pixels[x, y] > thr:
                pixels3[x, y] = 255
            else:
                pixels3[x, y] = 0
    return image
from math import hypot, pi, cos, sin
def hough(im, ntx=255, mry=360):
    "Calculate Hough transform."
    pim = im.load()
    nimx, mimy = im.size
    mry = int(mry/2)*2          #Make sure that this is even
    him = Image.new("I", (ntx, mry), 0)
    phim = him.load()
 
    rmax = hypot(nimx, mimy)
    dr = rmax / (mry/2)
    dth = pi / ntx
 

    max_intensity = 0
    for jx in range(nimx):
        for iy in range(mimy):
            col = pim[jx, iy]
            # if col == 255: continue
            for jtx in range(ntx):
                th = dth * jtx
                r = jx*cos(th) + iy*sin(th)
                iry = mry/2 + int(r/dr+0.5)
                # iry = mry/2 + int(r/dr+0.5)
                phim[jtx, iry] += 1
    

    for jx in range(ntx):
        for iy in range(mry):
            if phim[jx, iy] > max_intensity:
                max_intensity = phim[jx, iy]

    normalization = max_intensity/255
    for jx in range(ntx):
        for iy in range(mry):
            phim[jx, iy] = int(phim[jx, iy]/normalization)
    return him
 
 
# 載入原圖
im_O = Image.open(os.path.abspath('Hough.jpg'))
im = im_O.convert('L')
size = im.size
show("0 原圖",im)

im1 = Image.new( 'L', size, "black")

pixels = im.load() 

# X方向一階微分
arr = [[ -1.,  0,   1],
       [ -2.,  0.,  2],
       [ -1.,  0.,  1]]
im3_x = Image.new( 'L', size, "black")
pixels2 = im3_x.load() 
for i in range(size[0]-2):
  for j in range(size[1]-2):
     convolution(i+1,j+1,arr,pixels,pixels2)
 
# Y方向一階微分
arr = [[  1.,  2,   1],
       [  0.,  0.,  0],
       [ -1., -2., -1]]
im3_y = Image.new( 'L', size, "black")
pixels3 = im3_y.load()
for i in range(size[0]-2):
  for j in range(size[1]-2):
     convolution(i+1,j+1,arr,pixels,pixels3)

# XY方像取絕對值相加
for x in range(size[0]-2):
    for y in range(size[1]-2):
        pixels2[x,y] = abs(pixels2[x,y])
        pixels3[x,y] = abs(pixels3[x,y])
im3 = image_add(pixels2,pixels3)
show("1 一階微分",im3)

im4 = thresholding(im3.load(),150)
show("2 二值化",im4)

him = hough(im)
show("",him)

phim = him.load()
draw = ImageDraw.Draw(im_O)
for jx in range(him.size[0]):
    for iy in range(him.size[1]):
        if phim[jx, iy] > 240:
            draw.line((100,200, 150,300), fill=128)

show("",im_O)
Show Comments