基礎ユニット2(イメージメディア) 第12回(2019/01/10)

関連サイトと資料

サンプル画像

manmade2.png

manmade3.png

manmade5.png

manmade7.png

サンプルプログラム

createImage.py
import cv2
import numpy as np
import random
	
def createImageWithRandomLines(w, h, num):
    関数の中身は各自で考える!
	
num = 10 # 線の本数
saveFileName = 'lines_{0}.png'.format(num)
width = 750
height = 500
	
linesImg = createImageWithRandomLines(width, height, num)
	
cv2.imwrite(saveFileName, linesImg)
cv2.imshow('image with Lines({0})'.format(num), linesImg)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

lines_5.png

lines_10.png

addNoise.py
import cv2
import random
	
def addDotNoise(img, num):
    関数の中身は各自で考える!
	
num = 10000 # 雑音の個数
target = 'lines_10.png'
saveFileName = 'noisy_{0}_'.format(num) + target
	
orgImg = cv2.imread(target, cv2.IMREAD_GRAYSCALE)
noisyImg = addDotNoise(orgImg, num)
	
cv2.imwrite(saveFileName, noisyImg)
cv2.imshow('Original', orgImg)
cv2.imshow('Noisy({0})'.format(num), noisyImg)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

noisy_100_lines_5.png

noisy_10000_lines_5.png

noisy_100_lines_10.png

noisy_10000_lines_10.png

lineHough1.py
import cv2
import math
import os
	
def drawLine(img, rho, theta, lineColor):
    a = math.cos(theta)
    b = math.sin(theta)
    x0 = int(rho * a)
    y0 = int(rho * b)
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * a)
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * a)
	
    cv2.line(img, (x1, y1), (x2, y2), lineColor, 2)
	
target = 'lines_5.png'
th = 100 # 投票数の閾値
	
base, ext = os.path.splitext(target)
saveFileName = base + '_hough_{0}'.format(th) + ext
	
orgImg = cv2.imread(target, cv2.IMREAD_GRAYSCALE)
resultIimg = cv2.cvtColor(orgImg, cv2.COLOR_GRAY2BGR)
	
lines = cv2.HoughLines(orgImg, 1, math.pi / 180, th)
	
num = len(lines)
print('検出された直線の数:{0}'.format(num))
	
for i in range(num):
    line = lines[i][0]
    rho = line[0]
    theta = line[1]
    drawLine(resultIimg, rho, theta, (0, 0, 255))
	
#cv2.imwrite(saveFileName, resultIimg) # 結果を保存したい時にコメント設定を解除する
cv2.imshow('Original', orgImg)
cv2.imshow('Result(target={0},th={1})'.format(target, th), resultIimg)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

lines_5_hough_100.png

noisy_100_lines_5_hough_100.png

lineHough2.py
import cv2
import math
import os
	
def drawLine(img, rho, theta, lineColor):
    a = math.cos(theta)
    b = math.sin(theta)
    x0 = int(rho * a)
    y0 = int(rho * b)
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * a)
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * a)
	
    cv2.line(img, (x1, y1), (x2, y2), lineColor, 2)
	
target = 'manmade2.png'
th1 = 100
th2 = 200
th = 200 # 投票数の閾値
	
base, ext = os.path.splitext(target)
saveFileName = base + '_c_{0}_{1}_h_{2}'.format(th1, th2, th) + ext
	
step = 1 # 1 = エッジ抽出まで行う,  2 = ハフ変換まで行う
	
base, ext = os.path.splitext(target)
saveFileName = base + '_hough_{0}'.format(th) + ext
	
orgImg = cv2.imread(target, cv2.IMREAD_GRAYSCALE)
resultIimg = cv2.cvtColor(orgImg, cv2.COLOR_GRAY2BGR)
	
edgeImg = cv2.Canny(orgImg, th1, th2)
	
if step == 1:
    cv2.imshow('Original', orgImg)
    cv2.imshow('Canny(th1={0}, th2={1})'.format(th1, th2), edgeImg)
	
    cv2.waitKey(0)
    cv2.destroyAllWindows()
	
elif step == 2:
    lines = cv2.HoughLines(edgeImg, 1, math.pi / 180, th)
    
    num = len(lines)
    print('検出された直線の数:{0}'.format(num))
    
    for i in range(num):
        line = lines[i][0]
        rho = line[0]
        theta = line[1]
        drawLine(resultIimg, rho, theta, (0, 0, 255))
    
    cv2.imwrite(saveFileName, resultIimg) # 結果を保存したい時にコメント設定を解除する
    cv2.imshow('Original', orgImg)
    cv2.imshow('Canny(th1={0}, th2={1})'.format(th1, th2), edgeImg)
    cv2.imshow('Result(target={0},th={1})'.format(target, th), resultIimg)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()

manmade2_c_100_200_h_200.png