import cv2
import numpy as np
import matplotlib.pyplot as plt
import skimage.feature
import skimage.segmentation
def sliding_window(image, stepSize, windowSize): # 滑动窗口
for y in range(0, image.shape[0], stepSize):
for x in range(0, image.shape[1], stepSize):
yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]]) # 哪个维度超纲,哪个维度就显示原图
def mqa_hog_predict(mqa_img, z):
scale = 1
w, h = 100, 100
x, y = 0, 0
rectangles = []
z0, z1, z2, z3 = 0, 0, 0, 0
gray = cv2.cvtColor(mqa_img, cv2.COLOR_BGR2GRAY) # 灰度化
cul_num = 32
img_ku = skimage.feature.local_binary_pattern(gray, cul_num, 2, method='uniform')
img_ku = img_ku.astype(np.uint8)
hist = cv2.calcHist([img_ku], [0], None, [cul_num + 1], [0, cul_num + 1])
hist = cv2.normalize(hist, hist)
a0, res0 = svm_lbp0.predict(np.array([hist]), flags=cv2.ml.STAT_MODEL_RAW_OUTPUT)# 测试机标签、
score0 = res0[0][0]
if score0 < -0.0:
z0 += 1
print('z0', z0)
a1, res1 = svm_lbp1.predict(np.array([hist]), flags=cv2.ml.STAT_MODEL_RAW_OUTPUT) # 测试机标签、
score1 = res1[0][0]
if score1 < -0.0:
z1 += 1
print('z1', z1)
a2, res2 = svm_lbp2.predict(np.array([hist]), flags=cv2.ml.STAT_MODEL_RAW_OUTPUT) # 测试机标签、
score2 = res2[0][0]
if score2 < -0.0:
z2 += 1
print('z2', z2)
a3, res3 = svm_lbp3.predict(np.array([hist]), flags=cv2.ml.STAT_MODEL_RAW_OUTPUT) # 测试机标签、
score3 = res3[0][0]
if score3 < -0.0:
z3 += 1
print('z3', z3)
z = z0 + z1 + z2 + z3
print('z', z)
return z0, z1, z2, z3, z
svm_lbp0 = cv2.ml.SVM_create() # 草地vs all
svm_lbp0 = cv2.ml.SVM_load("E:\\ziliao\\train_gaijin\\lbp1.xml")
svm_lbp1 = cv2.ml.SVM_create() # 泥泞vs all
svm_lbp1 = cv2.ml.SVM_load("E:\\ziliao\\train_gaijin\\lbp2.xml")
svm_lbp2 = cv2.ml.SVM_create() # 水泥vs all
svm_lbp2 = cv2.ml.SVM_load("E:\\ziliao\\train_gaijin\\lbp3.xml")
svm_lbp3 = cv2.ml.SVM_create() # 雪地vs all
svm_lbp3 = cv2.ml.SVM_load("E:\\ziliao\\train_gaijin\\lbp4.xml")
# 创造一个HOG描述子和检测器
mysj = []
mysj1 = []
PosTestNum = 1697
NegTestNum = 5091
num_m, m1, m2, m3, m4, m = 0, 0, 0, 0, 0,0
fileName = 'E:\\ziliao\\val_gaijin\\caodi\\caodi (107).jpg'
# 导入正样本图片
img = cv2.imread(fileName)
m0, m1, m2, m3, m = mqa_hog_predict(img, m4)
print('.......................RESULT...........................')
if m > 1:
print("Image input problem!!")
else:
if m0 > 0:
print('cao_di')
if m1 > 0:
print('ni_di')
if m2 > 0:
print('shuini_di')
if m3 > 0:
print('xue_di')
if cv2.waitKey(0) == ord("s"):
cv2.destroyAllWindows()