基礎ユニット2(イメージメディア) 第13回(2020/01/16)

関連サイトと資料

サンプル画像

manmade1.png

manmade4.png

manmade5.png

manmade6.png

サンプルプログラム

createImage.py
import cv2
import numpy as np
import random
	
def createImageWithRandomCircles(w, h, minRadius, maxRadius, num):
    関数の中身は各自で考える!
	
num = 10 # 円の個数
saveFileName = 'circles_{0}.png'.format(num)
width = 750
height = 500
r1 = 5 # 半径の下限
r2 = 100 # 半径の上限
	
circlesImg = createImageWithRandomCircles(width, height, r1, r2, num)
	
cv2.imwrite(saveFileName, circlesImg)
cv2.imshow('image with Circles({0})'.format(num), circlesImg)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

circles_5.png

circles_10.png

addNoise.py
import cv2
import random
	
def addDotNoise(img, num):
    関数の中身は各自で考える!
	
num = 100 # 雑音の個数
target = 'circles_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_circles_5.png

noisy_10000_circles_5.png

noisy_100_circles_10.png

noisy_10000_circles_10.png

circleHough1.py
import cv2
import math
import os
	
def drawCircle(img, cx, cy, radius, lineColor):
    cv2.circle(img, (cx, cy), radius, lineColor, 2)
    return img
	
target = 'circles_5.png'
dp = 1
minD = 300
param1 = 200
param2 = 1000
minR = 5
maxR = 100
	
base, ext = os.path.splitext(target)
saveFileName = base + '_hough_{0}_{1}_{2}_{3}_{4}'.format(minD, param1, param2, minR, maxR) + ext
	
orgImg = cv2.imread(target, cv2.IMREAD_GRAYSCALE)
resultIimg = cv2.cvtColor(orgImg, cv2.COLOR_GRAY2BGR)
	
circles = cv2.HoughCircles(orgImg, cv2.HOUGH_GRADIENT, dp, minD, param1, param2, minR, maxR)
	
num = len(circles[0])
print('検出された円の数:{0}'.format(num))
	
for i in range(num):
    x = circles[0][i][0]
    y = circles[0][i][1]
    r = circles[0][i][2]
    drawCircle(resultIimg, x, y, r, (0, 0, 255))
	
cv2.imwrite(saveFileName, resultIimg) # 結果を保存したい時にコメント設定を解除する
cv2.imshow('Original', orgImg)
cv2.imshow('Result(target={0},th={1})'.format(target, param2), resultIimg)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

circles_5_hough_300_200_1000_5_100.png

noisy_100_circles_5_hough_300_200_1000_5_100.png

circleHough2.py
import cv2
import math
import os
	
def drawCircle(img, cx, cy, radius, lineColor):
    cv2.circle(img, (cx, cy), radius, lineColor, 2)
    return img
	
target = 'manmade6.png'
dp = 1
minD = 10
param1 = 150
param2 = 1250
minR = 6
maxR = 8
	
base, ext = os.path.splitext(target)
saveFileName = base + '_hough_{0}_{1}_{2}_{3}_{4}'.format(minD, param1, param2, minR, maxR) + ext
	
orgImg = cv2.imread(target, cv2.IMREAD_GRAYSCALE)
resultIimg = cv2.cvtColor(orgImg, cv2.COLOR_GRAY2BGR)
	
circles = cv2.HoughCircles(orgImg, cv2.HOUGH_GRADIENT, dp, minD, param1, param2, minR, maxR)
	
num = len(circles[0])
print('検出された円の数:{0}'.format(num))
print(circles)
	
for i in range(num):
    x = circles[0][i][0]
    y = circles[0][i][1]
    r = circles[0][i][2]
    drawCircle(resultIimg, x, y, r, (0, 0, 255))
	
cv2.imwrite(saveFileName, resultIimg) # 結果を保存したい時にコメント設定を解除する
cv2.imshow('Original', orgImg)
cv2.imshow('Result(target={0},th={1})'.format(target, param2), resultIimg)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

manmade6_hough_10_150_1250_6_8.png