|
# -*- coding: utf-8 -*- |
|
""" |
|
Created on 20160801 |
|
@author: jyoshida-sci |
|
""" |
|
|
|
import cv2 |
|
import numpy as np |
|
|
|
|
|
def sobel3_xygrad_org(img): |
|
img64 = np.zeros(img.shape, np.float64) |
|
img64 = img.astype(np.float64) |
|
|
|
sobel64x = np.zeros(img.shape, np.float64) |
|
sobel64y = np.zeros(img.shape, np.float64) |
|
sobel64g = np.zeros(img.shape, np.float64) |
|
|
|
cv2.Sobel(img64, cv2.CV_64F, 1, 0, sobel64x, 3) |
|
cv2.Sobel(img64, cv2.CV_64F, 0, 1, sobel64y, 3) |
|
cv2.magnitude(sobel64x, sobel64y, sobel64g) |
|
return [sobel64x, sobel64y, sobel64g] |
|
|
|
|
|
|
|
def sobel3_xygrad(img): |
|
sobel64x, sobel64y, sobel64g = sobel3_xygrad_org(img) |
|
cv2.normalize(sobel64x, sobel64x, 0, 255, cv2.NORM_MINMAX) |
|
cv2.normalize(sobel64y, sobel64y, 0, 255, cv2.NORM_MINMAX) |
|
cv2.normalize(sobel64g, sobel64g, 0, 255, cv2.NORM_MINMAX) |
|
|
|
sobelx_disp = np.zeros(img.shape, np.uint8) |
|
sobelx_disp = sobel64x.astype(np.uint8) |
|
|
|
sobely_disp = np.zeros(img.shape, np.uint8) |
|
sobely_disp = sobel64y.astype(np.uint8) |
|
|
|
sobelg_disp = np.zeros(img.shape, np.uint8) |
|
sobelg_disp = sobel64g.astype(np.uint8) |
|
|
|
return [sobelx_disp, sobely_disp, sobelg_disp] |
|
|
|
|
|
|
|
def diff3_xygrad_org(img): |
|
kdiffx = np.array([[-1.0, 0.0, 1.0], |
|
[-1.0, 0.0, 1.0], |
|
[-1.0, 0.0, 1.0]]) |
|
|
|
kdiffy = np.array([[-1.0, -1.0, -1.0], |
|
[0.0, 0.0, 0.0], |
|
[1.0, 1.0, 1.0]]) |
|
|
|
img64 = np.zeros(img.shape, np.float64) |
|
img64 = img.astype(np.float64) |
|
|
|
diff64x = cv2.filter2D(img64, -1, kdiffx) |
|
diff64y = cv2.filter2D(img64, -1, kdiffy) |
|
diff64g = np.zeros(img.shape, np.float64) |
|
cv2.magnitude(diff64x, diff64y, diff64g) |
|
return [diff64x, diff64y, diff64g] |
|
|
|
|
|
|
|
def diff3_xygrad(img): |
|
diff64x, diff64y, diff64g = diff3_xygrad_org(img) |
|
cv2.normalize(diff64x, diff64x, 0, 255, cv2.NORM_MINMAX) |
|
cv2.normalize(diff64y, diff64y, 0, 255, cv2.NORM_MINMAX) |
|
cv2.normalize(diff64g, diff64g, 0, 255, cv2.NORM_MINMAX) |
|
|
|
diffx_disp = np.zeros(img.shape, np.uint8) |
|
diffx_disp = diff64x.astype(np.uint8) |
|
|
|
diffy_disp = np.zeros(img.shape, np.uint8) |
|
diffy_disp = diff64y.astype(np.uint8) |
|
|
|
diffg_disp = np.zeros(img.shape, np.uint8) |
|
diffg_disp = diff64g.astype(np.uint8) |
|
|
|
return [diffx_disp, diffy_disp, diffg_disp] |
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
myarray = np.random.randint(0,10,(5,6))#0~9random int numbers, 5x6 matrix |
|
|
|
sobelx_org, sobely_org, sobelg_org = sobel3_xygrad_org(myarray) |
|
diffx_org, diffy_org, diffg_org = diff3_xygrad_org(myarray) |
|
|
|
print myarray |
|
print sobelx_org |
|
print sobely_org |
|
print sobelg_org |
|
print diffx_org |
|
print diffy_org |
|
print diffg_org |
|
|
|
################ |
|
|
|
img = cv2.imread("imgage.png",cv2.CV_LOAD_IMAGE_GRAYSCALE) |
|
|
|
sobelx_disp, sobely_disp, sobelg_disp = sobel3_xygrad(img) |
|
diffx_disp, diffy_disp, diffg_disp = diff3_xygrad(img) |
|
|
|
cv2.imshow('img', img) |
|
|
|
cv2.imshow('sobelx_disp', sobelx_disp) |
|
cv2.imshow('sobely_disp', sobely_disp) |
|
cv2.imshow('sobelg_disp', sobelg_disp) |
|
|
|
cv2.imshow('diffx_disp', diffx_disp) |
|
cv2.imshow('diffy_disp', diffy_disp) |
|
cv2.imshow('diffg_disp', diffg_disp) |
|
cv2.waitKey(0) |
|
|
|
cv2.destroyAllWindows() |
outputfiles of tile.py

