山内セミナー(2018/11/28)

関連サイトと資料

色情報による追跡

色情報による追跡0a.py
import cv2
import numpy as np
import os
	
targets = [ 'Megamind000.png', 'Megamind001.png', 'Megamind002.png']
	
for fname in targets:
    bgrImg = cv2.imread(fname, cv2.IMREAD_COLOR)
    hsvImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2HSV)
    lower_purple = np.array([160,150,40])
    upper_purple = np.array([170,255,100])
    maskImg = cv2.inRange(hsvImg, lower_purple, upper_purple)

    base, ext = os.path.splitext(fname)
    savename = base + '_mask' + ext
    cv2.imwrite(savename, maskImg)
    print(fname + ' --> ' + savename)

色情報による追跡0b.py
import cv2
import numpy as np
import os
	
targets = [ 'Megamind000.png', 'Megamind001.png', 'Megamind002.png']
	
for fname in targets:
    bgrImg = cv2.imread(fname, cv2.IMREAD_COLOR)
    hsvImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2HSV)
    lower_purple = np.array([160,150,40])
    upper_purple = np.array([170,255,100])
    maskImg = cv2.inRange(hsvImg, lower_purple, upper_purple)
	
    resultImg = cv2.bitwise_and(bgrImg, bgrImg, mask= maskImg)
    base, ext = os.path.splitext(fname)
    savename = base + '_detect' + ext
    cv2.imwrite(savename, resultImg)
    print(fname + ' --> ' + savename)

画像の拡大・縮小

画像の拡大縮小1.py
import cv2
	
img = cv2.imread('messi5.jpg', cv2.IMREAD_COLOR)
	
bigImg1 = cv2.resize(img, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
	
height, width = img.shape[:2]
smallImg1 = cv2.resize(img,(width/2, height/2), interpolation = cv2.INTER_CUBIC)
	
cv2.imshow('original', img)
cv2.imshow('big1', bigImg1)
cv2.imshow('small1', smallImg1)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

messi5.jpg

単純な二値化

単純な二値化1.py
import cv2
	
img = cv2.imread('gradient.png', cv2.IMREAD_GRAYSCALE)
	
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
	
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
	
for i in range(len(titles)):
	cv2.imshow(titles[i], images[i])
	
cv2.waitKey(0)
cv2.destroyAllWindows()

gradient.png

適応的二値化

適応的二値化1.py
import cv2
	
img = cv2.imread('dave.jpg', cv2.IMREAD_GRAYSCALE)
#img = cv2.medianBlur(img,5)
	
ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
	
titles = ['Original Image', 'Global Thresholding (v = 127)', 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
	
for i in range(len(titles)):
	cv2.imshow(titles[i], images[i])
	
cv2.waitKey(0)
cv2.destroyAllWindows()

dave.jpg

大津の二値化

大津の二値化1.py
import cv2
	
img = cv2.imread('noisy2.jpg', cv2.IMREAD_GRAYSCALE)
	
ret1, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret2, th2 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
	
blur = cv2.GaussianBlur(img, (5, 5), 0)
ret3, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
	
images = [img, th1, th2, blur, th3]
titles = ['Original Noisy Image','Thresholding (v=127)', "Otsu's Thresholding", 
'Gaussian filtered Image', "Otsu's Thresholding(blured Image)"]
	
for i in range(len(titles)):
	cv2.imshow(titles[i], images[i])
	
cv2.waitKey(0)
cv2.destroyAllWindows()

noisy2.jpg

加算平均による平滑化

加算平均による平滑化1.py
import cv2
import numpy as np
	
img = cv2.imread('opencv_logo.jpg', cv2.IMREAD_COLOR)
	
kernel = np.ones((5,5), np.float32) / 25
dst = cv2.filter2D(img, -1, kernel)
blur = cv2.blur(img, (5,5))
	
images = [img, dst, blur]
titles = ['Original', 'Averaging(1)', 'Averaging(2)']
	
for i in range(len(titles)):
	cv2.imshow(titles[i], images[i])
	
cv2.waitKey(0)
cv2.destroyAllWindows()

opencv_logo.jpg

モルフォロジカルフィルター

j.png

収縮1.py
import cv2
import numpy as np
	
img = cv2.imread('j.png', cv2.IMREAD_GRAYSCALE)
	
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(img, kernel, iterations = 1)
	
images = [img, erosion]
titles = ['Original', 'Erosion']
	
for i in range(len(titles)):
	cv2.imshow(titles[i], images[i])
	
cv2.waitKey(0)
cv2.destroyAllWindows()

膨張1.py
import cv2
import numpy as np
	
img = cv2.imread('j.png', cv2.IMREAD_GRAYSCALE)
	
kernel = np.ones((5, 5), np.uint8)
dilation = cv2.dilate(img, kernel, iterations = 1)
	
images = [img, dilation]
titles = ['Original', 'Dilation']
	
for i in range(len(titles)):
	cv2.imshow(titles[i], images[i])
	
cv2.waitKey(0)
cv2.destroyAllWindows()

j2.png

オープニング1.py
import cv2
import numpy as np
	
img = cv2.imread('j2.png', cv2.IMREAD_GRAYSCALE)
	
kernel = np.ones((5, 5), np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
	
images = [img, opening]
titles = ['Original', 'Opening']
	
for i in range(len(titles)):
	cv2.imshow(titles[i], images[i])
	
cv2.waitKey(0)
cv2.destroyAllWindows()

j3.png

クロージング1.py
import cv2
import numpy as np
	
img = cv2.imread('j3.png', cv2.IMREAD_GRAYSCALE)
	
kernel = np.ones((5, 5), np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
	
images = [img, closing]
titles = ['Original', 'Closing']
	
for i in range(len(titles)):
	cv2.imshow(titles[i], images[i])
	
cv2.waitKey(0)
cv2.destroyAllWindows()

勾配

勾配1.py
import cv2
from matplotlib import pyplot as plt
	
img = cv2.imread('dave.jpg', cv2.IMREAD_GRAYSCALE)
	
laplacian = cv2.Laplacian(img, cv2.CV_64F)
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
	
plt.subplot(2,2,1), plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2), plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
	
plt.show()

box.png

勾配2.py
import cv2
import numpy as np
from matplotlib import pyplot as plt
	
img = cv2.imread('box.png', cv2.IMREAD_GRAYSCALE)
	
sobelx8u = cv2.Sobel(img, cv2.CV_8U, 1, 0, ksize=5)
	
sobelx64f = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
abs_sobel64f = np.absolute(sobelx64f)
sobel_8u = np.uint8(abs_sobel64f)
	
plt.subplot(1,3,1), plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(1,3,2),plt.imshow(sobelx8u,cmap = 'gray')
plt.title('Sobel CV_8U'), plt.xticks([]), plt.yticks([])
plt.subplot(1,3,3), plt.imshow(sobel_8u,cmap = 'gray')
plt.title('Sobel abs(CV_64F)'), plt.xticks([]), plt.yticks([])
	
plt.show()

Cannyエッジ検出

Cannyエッジ検出1.py
import cv2
from matplotlib import pyplot as plt
	
img = cv2.imread('messi5.jpg', cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(img, 100, 200)
	
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
	
plt.show()