2014년 8월 20일 수요일

Image analysis with Python

1. matplotlib ( is a python 2D plotting library )
 - site (api)
http://matplotlib.org/api/pyplot_api.html

2. opencv (is an open source computer vision and machine learning software library.)
 - site (api)
http://opencv.org/
 - template matching
http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html#which-are-the-matching-methods-available-in-opencv

3. NumPy (is the fundamental package for scientific computing with Python.)
http://www.numpy.org/

4. sample code
# personal issue
import matplotlib
matplotlib.use('TkAgg')
# begin
import cv2
import numpy as np
from matplotlib import pyplot as plt

pic = '/home/fox/park.JPG'
compare_pic = '/home/fox/car.JPG'

img_rgb = cv2.imread(pic)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(compare_pic,0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.7   # wanna find a best matching image => np.amax(res)
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
   cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)

cv2.imwrite('/home/fox/res3.png',img_rgb)
plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_rgb,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.show()

up to bottom
 - car.JPG , park.JPG, best_one.JPG, density_0.6.JPG

















0 개의 댓글:

댓글 쓰기