在这里插入代码片
```
import cv2
import numpy as np
import easygui as g
import math
np.set_printoptions(suppress=True)
def func(x,y,sigma=1):
return 100*(1/(2*np.pi*sigma))*np.exp(-((x-1)**2+(y-1)**2)/(2.0*sigma**2))
def convolution(kernal_size, image):
rows = image.shape[0]
cols = image.shape[1]
image_new = np.zeros((rows+2,cols+2))
image_new[1:rows+1,1:cols+1] = image.copy()
img_new = []
for i in range(1,rows+1):
line = []
for j in range(1,cols+1):
a = image_new[i-1:i+2,j-1:j+2]
a=np.array(a)
line.append(np.sum(np.multiply(kernal_size, a)))
img_new.append(line)
return np.array(img_new)
def non_maximum_suppression(hough):
r,l = hough.shape
for y in range(r):
for x in range(l):
if x-1>=0 and x+2 <= l and y-1 >=0 and y+2<=r:
x1 = x-1
x2 = x+2
y1 = y-1
y2 = y+2
if np.max(hough[y1:y2, x1:x2]) == hough[y, x] and hough[y, x] != 0:
pass
else:
hough[y, x] = 0
return hough
def grad_x(h):
delta_h = h.copy()
a = int(h.shape[0])
b = int(h.shape[1])
for i in range(a):
for j in range(b):
if i-1>=0 and i+1<a and j-1>=0 and j+1<b:
c = abs(int(h[i-1,j-1]) - int(h[i+1,j-1]) + 2*(int(h[i-1,j]) - int(h[i+1,j])) + int(h[i-1,j+1]) - int(h[i+1,j+1]))
if c>255:
c = 255
delta_h[i,j] = c
else:
delta_h[i,j] = 0
print ('x方向的梯度:\n %s \n' %delta_h)
return delta_h
def grad_y(h):
a = int(h.shape[0])
b = int(h.shape[1])
delta_h = h.copy()
for i in range(a):
for j in range(b):
if i-1>=0 and i+1<a and j-1>=0 and j+1<b:
c = abs(int(h[i-1,j-1]) - int(h[i-1,j+1]) + 2*(int(h[i,j-1]) - int(h[i,j+1])) + (int(h[i+1,j-1]) - int(h[i+1,j+1])))
if c > 255:
c = 255
delta_h[i,j] = c
else:
delta_h[i,j] = 0
print ('y方向的梯度:\n %s \n' %delta_h)
return delta_h
def laplace(image):
laplace = np.array([[1, 1, 1],
[1, -8, 1],
[1, 1, 1]])
image_laplace = convolution(image=image, kernal_size=laplace)
image_laplace = image_laplace * (255 / image_laplace.max())
image_laplace = convolution(image=image_laplace, kernal_size=laplace)
return image_laplace
def glcm(arr, d_x, d_y, gray_level=16):
'''计算并返回归一化后的灰度共生矩阵'''
max_gray = arr.max()
height, width = arr.shape
arr = arr.astype(np.float64)
arr = arr * (gray_level - 1) // max_gray
ret = np.zeros([gray_level, gray_level])
for j in range(height - abs(d_y)):
for i in range(width - abs(d_x)):
rows = arr[j][i].astype(int)
cols = arr[j + d_y][i + d_x].astype(int)
ret[rows][cols] += 1
ret = ret /(height * width)
return ret
def gause_image(image):
suanzi = np.fromfunction(func,(3,3),sigma=2)
image2 = convolution(image=image, kernal_size=suanzi)
image2 = (image2 / float(image2.max())) * 255
return image2
def duibidu_number(ret):
rows = ret.shape[0]
cols = ret.shape[1]
sum = 0
for i in range(rows):
for j in range(cols):
sum += (i-j)**2*ret[i][j]
return sum
def max_number(ret):
rows = ret.shape[0]
cols = ret.shape[1]
sum = 0
for i in range(rows):
for j in range(cols):
if sum <= ret[i][j]:
sum = ret[i][j]
return sum
def er_jie_ju(ret):
rows = ret.shape[0]
cols = ret.shape[1]
sum = 0
for i in range(rows):
for j in range(cols):
sum += ret[i][j]**2
return sum
def fancha_fen_matrix(ret):
rows = ret.shape[0]
cols = ret.shape[1]
sum = 0
for i in range(rows):
for j in range(cols):
sum +=(ret[i][j])/(1+(i-j)**2)
return sum
def fun1(ret):
dui_bi_du = duibidu_number(ret)
max__number = max_number(ret)
er_jieju_number = er_jie_ju(ret)
fan=fancha_fen_matrix(ret)
print('对比度为%f' % (dui_bi_du),end='\n')
print('最大概率为 %f' % (max__number),end='\n')
print('二阶矩 %f ' % (er_jieju_number),end='\n')
print('反差分为%f' % (fan))
def line_length(dict):
sum = 0
for i in range(0,len(dict),2):
x1 = dict[i][0]
y1 = dict[i][1]
if i+1 <= len(dict)-1:
x2 = dict[i+1][0]
y2 = dict[i+1][1]
sum += math.sqrt((x1-x2)**2+(y1 -y2)**2)
sum += math.sqrt((dict[0][0]-dict[len(dict)-1][0])**2+(dict[0][1]-dict[len(dict)-1][1])**2)
return sum
def area_lianma(dict,img):
area = 0
for i in range(0,len(dict),2):
x1 = dict[i][0]
y1 = dict[i][1]
if i + 1 <= len(dict) - 1:
x2 = dict[i + 1][0]
y2 = dict[i + 1][1]
area += (x2 * y1 - x1 * y2)/2
rols = img.shape[0]
cols = img.shape[1]
area_all = rols * cols
gray_point = 0
white_point = 0
for i in range(rols):
for j in range(cols):
if img[i,j] == 255:
white_point +=1
else:
img[i,j] == 0
gray_point +=1
return area_all + area
def lianma(img):
[img_h, img_w, img_channel] = img.shape
trace = []
start_x = 0
start_y = 0
img1 = img[:,:,0] * 0.11 + img[:,:,1] * 0.59 + img[:,:,2] * 0.3
img1 = img1.astype(np.uint8)
gray = img1.copy()
img3 = img[:,:,0] * 0.11 + img[:,:,1] * 0.59 + img[:,:,2] * 0.3
img3 = img3.astype(np.uint8)
gray[gray>128] = 255
gray[gray<128] =0
class getoutofloop(Exception): pass
try:
for h in range(10,img_h - 2):
for w in range(10,img_w - 2):
if gray[h, w] == 255:
start_x = w
start_y = h
raise getoutofloop
except getoutofloop:
pass
print("Start Point (%d %d)" % (start_x, start_y))
trace.append([start_x, start_y])
neighbor = [ [ 1, 1 ], [ 1, 0 ], [1,-1],[0,-1],[-1,-1],[-1,0],[-1,1],[0,1]]
neighbor_len = len(neighbor)
i = 0
cur_x = start_x + neighbor[i][0]
cur_y = start_y + neighbor[i][1]
is_contour_point = 0
try:
while not ((cur_x == start_x) and (cur_y == start_y)):
is_contour_point = 0
while is_contour_point == 0:
if gray[cur_y, cur_x] == 0:
is_contour_point = 1
trace.append([cur_x, cur_y])
i -= 2
if i < 0:
i += neighbor_len
else:
i += 1
if i >= neighbor_len:
i -= neighbor_len
cur_x = cur_x + neighbor[i][0]
cur_y = cur_y + neighbor[i][1]
except:
print("throw error")
img3 = cv2.cvtColor(img3, cv2.COLOR_GRAY2RGB)
for i in range(len(trace) - 1):
cv2.line(img3, (trace[i][0], trace[i][1]), (trace[i + 1][0], trace[i + 1][1]), (0, 0, 255), 3)
cv2.imshow("img", img3)
cv2.waitKey(10)
image2 = np.zeros((img.shape[0],img.shape[1]),np.uint8)
image2 = cv2.cvtColor(image2,cv2.COLOR_GRAY2RGB)
for i in range(len(trace)):
image2[trace[i][1],trace[i][0]] = [0,0,255]
cv2.imshow('dot image',image2)
cv2.rectangle(img3, (start_x-10, start_y-10), (start_x + 10, start_y + 10) , (0, 255, 0), 2)
cv2.imshow("img", img)
cv2.imshow('img2',img3)
line = line_length(trace)
g.msgbox('线的长度为 %d' % (line),title='线的长度')
lianma_area = area_lianma(trace,gray)
g.msgbox('链码包围的面积为 %d' % (lianma_area),title='链码面积' )
cv2.waitKey(0)
cv2.destroyWindow("img")
def harris(img):
img2 = img.copy()
img_gray = img2[:, :, 0] * 0.11 + img2[:, :, 1] * 0.59 + img2[:, :, 2] * 0.3
img_gray = img_gray.astype(np.uint8)
gray = img_gray.copy()
m = np.array(gray)
img_laplace = laplace(gray)
dx = np.array(grad_x(gray))
dy = np.array(grad_y(gray))
A = dx * dx
B = dy * dy
C = dx * dy
A1 = A
B1 = B
C1 = C
a = int(gray.shape[0])
b = int(gray.shape[1])
A1 = gause_image(A1)
B1 = gause_image(B1)
C1 = gause_image(C1)
a_rols = A1.shape[0]
a_cols = A1.shape[1]
R = np.zeros(gray.shape)
for i in range(a):
for j in range(b):
M = [[A1[i, j], C1[i, j]], [C1[i, j], B1[i, j]]]
R[i, j] = np.linalg.det(M) - 0.06 * (np.trace(M)) * (np.trace(M))
threshold = 2500
R[R < threshold] = 0
R = non_maximum_suppression(R)
print(R)
rols = R.shape[0]
cols = R.shape[1]
pix_number = []
for i in range(rols):
for j in range(cols):
if R[i, j] >= threshold:
pix_number.append((i, j))
for i in (pix_number):
cv2.circle(img2, (i[1], i[0]), 2, (0, 0, 255), 1)
cv2.imshow('harris_detection', img2)
cv2.namedWindow('R', cv2.WINDOW_NORMAL)
cv2.imshow('R', R)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
msg = "请输入您想要完成的任务(建议您第一步先打开图片)"
title = '第五次作业'
choice = ('打开图片', '退出')
a = g.buttonbox(msg=msg, title=title, choices=choice)
if a == '打开图片':
filename = g.fileopenbox(msg="请打开一个jpg文件")
img = cv2.imread(filename)
msg1 = "选择您想要实现的功能"
title1 = '第五次作业'
choice1 = ('边界线链码', '灰度共生矩阵', '角点特征','SIFT','显示原图','重新选择图片','退出')
q = 1
while q:
b = g.buttonbox(msg=msg1, title=title1, choices=choice1)
if b == '边界线链码':
lianma(img)
elif b == '灰度共生矩阵':
img_gray = img[:,:,0] * 0.11+img[:,:,1] *0.59 + img[:,:,2] *0.3
img_gray = img_gray.astype(np.uint8)
glcm_0 = glcm(img_gray, 1, 0)
fun1(glcm_0)
print(glcm_0,
end='\n\n**************************************************************************************\n\n\n')
glcm_1 = glcm(img_gray, 0, 1)
fun1(glcm_1)
print(glcm_1,
end='\n\n**************************************************************************************\n\n\n')
glcm_2 = glcm(img_gray, 1, 1)
fun1(glcm_2)
print(glcm_2,
end='\n\n**************************************************************************************\n\n\n')
glcm_3 = glcm(img_gray, -1, -1)
fun1(glcm_3)
print(glcm_3,
end='\n\n**************************************************************************************\n\n\n')
elif b == '角点特征':
harris(img)
elif b == 'SIFT':
pass
elif b == '显示原图':
cv2.imshow('原图',img)
elif b == '重新选择图片':
filename = g.fileopenbox(msg="请打开一个jpg文件")
img = cv2.imread(filename)
else:
q = 0