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

関連サイトと資料

講義中に試したプログラム

test1.py
import cv2
    
img = cv2.imread('messi5.jpg')
    
px = img[50,100]
print(px)
    
blue = img[50,100,0]
print(blue)
    
img[50,100] = [255,255,255]
print(img[50,100])

for y in range(10,101):
    for x in range(20,111):
        img[y,x] = [57, 236, 237]
    
cv2.imshow('image', img)
    
cv2.waitKey(0)
cv2.destroyAllWindows()

test2.py
import cv2
    
img = cv2.imread('messi5.jpg')
    
ball = img[280:340, 330:390]
print(ball.shape)
cv2.imwrite('ball.png', ball)
cv2.imshow('Ball', ball)
    
img[273:333, 100:160] = ball
cv2.imshow('image', img)
    
cv2.waitKey(0)
cv2.destroyAllWindows()

test3.py
import cv2
    
img1 = cv2.imread('ml.png')
img2 = cv2.imread('opencv_logo.jpg')
    
dst = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
    
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

test4.py
import cv2
    
cap = cv2.VideoCapture('Megamind.avi')
    
for i in range(3):
    ret, frame = cap.read()
    if frame is not None:
        fname = "Megamind{0:03d}.png".format(i)
        print(fname)
        cv2.imwrite(fname, frame)
    
cap.release()

test5.py
import cv2
import numpy as np
    
def convert(red, green, blue):
    c1 = np.uint8([[[blue, green, red ]]])
    hsv_c1 = cv2.cvtColor(c1, cv2.COLOR_BGR2HSV)
    print('r={0}, g={1}, b={2}  =>  h={3}, s={4}, v={5}'.format(c1[0,0,2], c1[0,0,1], c1[0,0,0], hsv_c1[0,0,0], hsv_c1[0,0,1], hsv_c1[0,0,2]))
    
r = 0
g = 0
b = 255
convert(r, g, b)

ピクセル値へのアクセスと変更

ピクセル値へのアクセスと変更1.py
import cv2
	
img = cv2.imread('messi5.jpg')
	
px = img[100,100]
print(px)
	
blue = img[100,100,0]
print(blue)
	
img[100,100] = [255,255,255]
print(img[100,100])

messi5.jpg

ピクセル値へのアクセスと変更1b.py
import cv2
	
img = cv2.imread('messi5.jpg')
	
px = img[100,100]
print(px)
	
blue = img[100,100,0]
print(blue)
	
img[100,100] = [255,255,255]
print(img[100,100])
	
for y in range(10,101):
	for x in range(20,111):
		img[y,x] = [57, 236, 237]
	
cv2.imshow('image', img)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

ピクセル値へのアクセスと変更2.py
import cv2
	
img = cv2.imread('messi5.jpg')
	
print(img.item(10,10,2))
	
img.itemset((10,10,2),100)
print(img.item(10,10,2))

関心領域(ROI; Region of Interest)

関心領域1.py
import cv2
	
img = cv2.imread('messi5.jpg')
	
ball = img[280:340, 330:390]
cv2.imshow('Ball', ball)
	
img[273:333, 100:160] = ball
cv2.imshow('image', img)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

関心領域1b.py
import cv2
	
img = cv2.imread('messi5.jpg')
	
ball = img[280:340, 330:390]
cv2.imshow('Ball', ball)
	
img[273:333, 100:160] = ball
	
img[273:333, 50:110] = ball
	
cv2.imshow('image', img)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

画像の加算

画像の加算1.py
import numpy as np
import cv2
	
x = np.uint8([250])
y = np.uint8([10])
	
print(cv2.add(x,y))
	
print(x + y)

画像のブレンド

画像のブレンド1.py
import cv2
	
img1 = cv2.imread('ml.png')
img2 = cv2.imread('opencv_logo.jpg')
	
dst = cv2.addWeighted(img1, 0.7, img2, 0.3, 0)
	
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

ml.png

opencv_logo.jpg

ブレンド結果

画像のブレンド1b.py
import cv2
	
img1 = cv2.imread('ml.png')
img2 = cv2.imread('opencv_logo.jpg')
	
dst = cv2.addWeighted(img1, 0.5, img2, 0.5, 128)
	
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

ブレンド結果

ビットごとの演算

ビットごとの演算1.py
import cv2
	
img1 = cv2.imread('messi5.jpg')
img2 = cv2.imread('opencv_logo_half.jpg')
	
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols]
	
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 25, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
	
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
	
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols] = dst
	
cv2.imshow('result', img1)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

opencv_loggo_half.jpg

ビットごとの演算結果

ビットごとの演算1b.py
import cv2
	
img1 = cv2.imread('messi5.jpg')
img2 = cv2.imread('opencv_logo_half.jpg')
	
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols]
	
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 25, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
	
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
	
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols] = dst
	
cv2.imshow('img2', img2)
cv2.imshow('img2gray', img2gray)
cv2.imshow('mask', mask)
cv2.imshow('mask_inv', mask_inv)
cv2.imshow('roi', roi)
cv2.imshow('img1_bg', img1_bg)
cv2.imshow('img2_fg', img2_fg)
cv2.imshow('result', img1)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

色関連リストアップ

色関連リストアップ1.py
import cv2
	
flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
print(flags)

色情報による追跡

対象動画はこれです。

動画からサンプル画像を抽出1.py
import cv2
	
cap = cv2.VideoCapture('Megamind.avi')
	
for i in range(3):
	ret, frame = cap.read()
	if frame is not None:
		fname = "Megamind{0:03d}.png".format(i)
		print(fname)
		cv2.imwrite(fname, frame)
	
cap.release()

Megamind000.png

Megamind001.png

Megamind002.png

BGRからHSVへ変換1.py
import cv2
import numpy as np
	
def convert(red, green, blue):
	c1 = np.uint8([[[blue, green, red ]]])
	hsv_c1 = cv2.cvtColor(c1, cv2.COLOR_BGR2HSV)
	print('r={0}, g={1}, b={2}  =>  h={3}, s={4}, v={5}'.format(c1[0,0,2], c1[0,0,1], c1[0,0,0], hsv_c1[0,0,0], hsv_c1[0,0,1], hsv_c1[0,0,2]))
	
r = 0
g = 0
b = 255
convert(r, g, b)

色情報による追跡1.py
import cv2
import numpy as np
	
cap = cv2.VideoCapture('Megamind.avi')
i = 0
while(cap.isOpened()):
	ret, frame = cap.read()
	if frame is None:
		break
	
	height, width = frame.shape[:2]
	imgResult = np.zeros((height, width*3, 3), np.uint8)
	imgResult[:, 0:width] = frame
	
	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
	
	lower_purple = np.array([160,150,40])
	upper_purple = np.array([170,255,100])
	mask = cv2.inRange(hsv, lower_purple, upper_purple)
	for j in range(3):
		imgResult[:, width:width*2, j] = mask
	
	res = cv2.bitwise_and(frame, frame, mask= mask)
	imgResult[:, width*2:width*3] = res
	
	fname = "Megamind_purple_{0:03d}.png".format(i)
	print fname
	cv2.imwrite(fname, imgResult)
	
	i = i + 1
	
cap.release()
cv2.destroyAllWindows()