専門ユニット2/山内研セミナー(2021/11/24)

関連サイトと資料

プログラム

import cv2
from IPython.display import Image, display
  
def imshow(title, img):
    """ndarray 配列をインラインで Notebook 上に表示する。
    """
    ret, encoded = cv2.imencode('.png', img)
    print(title)
    display(Image(encoded))
    

import cv2
import numpy as np
  
img = np.zeros((200, 200), dtype=np.uint8)
img[50:150, 50:150] = 255
imshow('original', im
    

ret, thresh = cv2.threshold(img, 127, 255, 0)
imshow('threshold', thresh)
    

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img = cv2.drawContours(color, contours, -1, (0,255,0), 2)
imshow("contours", color)
    



import cv2
import numpy as np
  
img = cv2.pyrDown(cv2.imread("hammer.jpg", cv2.IMREAD_UNCHANGED))
imshow('original', img)
    

ret, thresh = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)
imshow('threshold', thresh)
    

contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]
  
for c in contours:
  # find bounding box coordinates
  x,y,w,h = cv2.boundingRect(c)
  cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
  
  # find minimum area
  rect = cv2.minAreaRect(c)
  # calculate coordinates of the minimum area rectangle
  box = cv2.boxPoints(rect)
  # normalize coordinates to integers
  box = np.int0(box)
  # draw contours
  cv2.drawContours(img, [box], 0, (0,0, 255), 3)
  
  # calculate center and radius of minimum enclosing circle
  (x, y), radius = cv2.minEnclosingCircle(c)
  # cast to integers
  center = (int(x), int(y))
  radius = int(radius)
  # draw the circle
  img = cv2.circle(img, center, radius, (0, 255, 0), 2)
  
imshow('contours1', img)
    

cv2.drawContours(img, contours, -1, (255, 0, 0), 1)
imshow("contours2", img)
    



import cv2
import numpy as np
  
img = cv2.pyrDown(cv2.imread("hammer.jpg", cv2.IMREAD_UNCHANGED))
ret, thresh = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)
contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]
  
black = np.zeros_like(img)
for cnt in contours:
  epsilon = 0.01 * cv2.arcLength(cnt,True)
  approx = cv2.approxPolyDP(cnt,epsilon,True)
  hull = cv2.convexHull(cnt)
  cv2.drawContours(black, [cnt], -1, (0, 255, 0), 2)
  cv2.drawContours(black, [approx], -1, (255, 255, 0), 2)
  cv2.drawContours(black, [hull], -1, (0, 0, 255), 2)
  
imshow('hull', black)
    



import cv2
import numpy as np
  
img = cv2.imread('lines.jpg', cv2.IMREAD_COLOR)
imshow('original', img)
    

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imshow('gray', gray)
    

edges = cv2.Canny(gray, 50, 120)
imshow('edges', edges)
    

minLineLength = 20
maxLineGap = 5
lines = cv2.HoughLinesP(edges, 1, np.pi/180.0, 20, minLineLength, maxLineGap)
  
color = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
for [[x1, y1, x2, y2]] in lines:
  cv2.line(color, (x1, y1), (x2, y2), (0,255,0),2)
  
imshow("lines", color)
    



import cv2
import numpy as np
  
planets = cv2.imread('planet_glow.jpg')
imshow('original', planets)      
    

gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
imshow('gray', gray_img)
    

gray_img = cv2.medianBlur(gray_img, 5)
imshow('blurred image', gray_img)
    

circles = cv2.HoughCircles(gray_img,cv2.HOUGH_GRADIENT,1,120, param1=100,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
  
for i in circles[0,:]:
  # draw the outer circle
  cv2.circle(planets,(i[0],i[1]),i[2],(0,255,0),2)
  # draw the center of the circle
  cv2.circle(planets,(i[0],i[1]),2,(0,0,255),3)
  
imshow('HoughCirlces', planets)
    

プロジェクトCameo

cameo.zipをダウンロード・解凍してできた*.pyファイルを、 現在作業中のフォルダーにコピーする。 そして、下記プログラムをJupyter Noteで実行する。

from cameo import Cameo
  
Cameo().run()