import logging
from math import cos, pi, sin
import cv2
import numpy as np
from sklearn.cluster import KMeans
from sklearn.utils import shuffle
from kd import get_rad_val
from auxiliary.find_roi_FlANN import find_roi
from auxiliary.cut_roi import polylines_cut
from auxiliary.perspective_roi import perspective_roi
import warnings
warnings.filterwarnings('ignore', category=FutureWarning)
# 旧
# def get_match_rect(template,img,method):
# '''获取模板匹配的矩形的左上角和右下角的坐标'''
# w, h = template.shape[1],template.shape[0]
# res = cv2.matchTemplate(img, template, method)
# mn_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# # 使用不同的方法,对结果的解释不同
# if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
# top_left = min_loc
# else:
# top_left = max_loc
# bottom_right = (top_left[0] + w, top_left[1] + h)
# return top_left,bottom_right
# 新
def get_match_rect(template, img, method):
'''获取模板匹配的矩形的左上角和右下角的坐标'''
# template 模版 img 原图 method 匹配方法
# 由于模板的大小和原图片中的大小不一致,所以需要对图片模板大小进行缩放,以准确匹配源图像中的尺寸
max_val_ = 0
max_loc_ = 0
w_ = 0
h_ = 0
count = 0
for dscale in np.linspace(0.1, 2, 100): # 对模板的大小进行缩放,从0.1倍到2倍大小
count += 1
# print(count)
template_re = cv2.resize(template, (int(template.shape[1] * dscale), int(template.shape[0] * dscale)))
w, h = template_re.shape[1], template_re.shape[0] # 0 为数组长度(高) 1为数组列数宽度(宽)
# print("w:{0}h:{1}".format(w,h))
try:
res = cv2.matchTemplate(img, template_re, method) / (w * h) # 滑动窗口在源图中寻找模板图,并给出每个位置的匹配相似程度
# print("res:{0}".format(res))
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 找到像素值的最大和最小值的位置
if max_val > max_val_:
max_val_ = max_val
max_loc_ = max_loc
w_ = w
h_ = h
except:
pass
max_loc = max_loc_
w = w_
h = h_
# 使用不同的方法,对结果的解释不同
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
# print("top_left:{0}".format(top_left))
bottom_right = (top_left[0] + w, top_left[1] + h)
return top_left, bottom_right
def get_circumference(top_left, bottom_right):
x1, y1 = top_left
x2, y2 = bottom_right
w = x2 - x1
h = y2 - y1
long = 2 * (w + h)
return long
def get_center_point(top_left, bottom_right):
'''传入左上角和右下角坐标,获取中心点'''
# print("{0}+{1}/2={2}".format(np.array(top_left), np.array(bottom_right),
# (np.array(top_left) + np.array(bottom_right)) / 2))
c_x, c_y = ((np.array(top_left) + np.array(bottom_right)) / 2).astype(int)
return c_x, c_y
def get_circle_field_color(img, center, r, thickness):
'''获取中心圆形区域的色值集'''
temp = img.copy().astype(np.int)
cv2.circle(temp, center, r, -100, thickness=thickness)
return img[temp == -100]
def v2_by_center_circle(img, colors):
'''二值化通过中心圆的颜色集合'''
for i in range(img.shape[0]):
for j in range(img.shape[1]):
a = img[i, j]
if a in colors:
img[i, j] = 0
else:
img[i, j] = 255
def v2_by_k_means(img):
'''使用k-means二值化'''
original_img = np.array(img, dtype=np.float64)
src = original_img.copy()
delta_y = int(original_img.shape[0] * (0.4)) # 原始图片中提取类似模板图片的高的0.2倍 (0.2需要修改的参数)
delta_x = int(original_img.shape[1] * (0.4)) # 原始图片中提取类似模板图片的宽的0.2倍 (0.2需要修改的参数)
original_img = original_img[delta_y:-delta_y, delta_x:-delta_x] # 类模板图片的中心区域,用于k-means的训练样本的区域
# uw=img[delta_y:-delta_y, delta_x:-delta_x]
# cv2.imshow("ss",uw)
h, w, d = src.shape
print('类模板的宽、高、通道数:', w, h, d)
dts = min([w, h])
print(dts)
r2 = (dts / 3) ** 2 # 距离中心位置的距离平方超过这个位置,则清除 (需要修改的参数)
print("{0}:".format(r2))
c_x, c_y = w / 2, h / 2 # 图片的中心位置
a: np.ndarray = original_img[:, :, 0:3].astype(np.uint8)
# 获取尺寸(宽度、长度、深度)
height, width = original_img.shape[0], original_img.shape[1]
depth = 3
# print(depth)
image_flattened = np.reshape(original_img, (width * height, depth))
'''
用K-Means算法进行像素的二值分类学习
'''
image_array_sample = shuffle(image_flattened, random_state=0)
estimator = KMeans(n_clusters=2, random_state=0) # k-means评估器
estimator.fit(image_array_sample) # 如果k-means处理效果不佳可以缩小算法的训练样本的区域
'''
为原始图片的每个像素进行类的分配
'''
src_shape = src.shape
new_img_flattened = np.reshape(src, (src_shape[0] * src_shape[1], depth)) # 类模板图展开
cluster_assignments = estimator.predict(new_img_flattened) # 对类模板图的每个像素点进行分类
'''
建立通过压缩调色板和类分配结果创建压缩后的图片
'''
compressed_palette = estimator.cluster_centers_ # 获取聚类中心,每行为rgb的值,两个聚类点
# print(compressed_palette)
a = np.apply_along_axis(func1d=lambda x: np.uint8(compressed_palette[x]), arr=cluster_assignments,
axis=0) # 把每个像素点变为到聚类中心点上
img = a.reshape(src_shape[0], src_shape[1], depth)
# print(compressed_palette[0, 0])
threshold = (compressed_palette[0, 0] + compressed_palette[1, 0]) / 2
img[img[:, :, 0] > threshold] = 255
img[img[:, :, 0] < threshold] = 0
cv2.imshow('K-Means Binarization', img)
cv2.waitKey(1)
cv2.destroyAllWindows()
for x in range(w):
for y in range(h):
distance = ((x - c_x) ** 2 + (y - c_y) ** 2) # 距离图片中心位置较远的区域,清空处理
if distance > r2:
pass
img[y, x] = (255, 255, 255)
cv2.imshow('sd', img)
cv2.waitKey(1)
cv2.destroyAllWindows()
return img
def dilate(img):
'''将Kmeans二值化后的图像进行膨胀处理'''
kernel = np.ones((3, 3), np.uint8)
dilation = cv2.dilate(img, kernel, iterations=1)
return dilation
def get_pointer_rad(img):
'''获取角度'''
shape = img.shape
c_y, c_x, depth = int(shape[0] *0.51), int(shape[1] *0.52), shape[2] # 寻找指针的旋转中心位置 (0.75是需要修改的参数)
# c_y, c_x, depth = shape[0]//2, shape[1]//2, shape[2] # 寻找指针的旋转中心位置 (0.75是需要修改的参数)
x1 = c_x + c_x * 0.8
src = img.copy()
freq_list = []
for i in range(0, 361): # 拟合指针的角度范围,水平向右为0度,逆时针为正方向(使用自己的数据集,需要自定义的参数)
# x = (x1 - c_x) * cos(i * pi / 180) + c_x
# y = (x1 - c_x) * sin(i * pi / 180) + c_y
x = (x1 - c_x) * cos(np.radians(i)) + c_x
y = (x1 - c_x) * sin(np.radians(i)) + c_y
temp = src.copy()
cv2.line(temp, (c_x, c_y), (int(x), int(y)), (0, 0, 255), thickness=2) # 绘制直线
t1 = img.copy()
t1[temp[:, :, 2] == 255] = 255
c = img[temp[:, :, 2] == 255]
cv2.imshow("ssd",t1)
points = c[c == False]
freq_list.append((len(points), i))
cv2.imshow('d'


无涯233
- 粉丝: 110
最新资源
- springboot250智慧校园之家长子系统录像.mp4
- springboot255基于spring boot的疫情信息管理系统录像.mp4
- 银行家算法python实现操作系统死锁避免与资源分配
- springboot254小区团购管理录像.mp4
- springboot253社区养老服务系统演示录像.mp4
- springboot257基于SpringBoot的中山社区医疗综合服务平台录像.mp4
- springboot256基于springboot+vue的游戏交易系统录像.mp4
- springboot258流浪动物救助网站录像.mp4
- springboot259交通管理在线服务系统的开发录像.mp4
- springboot261高校专业实习管理系统的设计和开发演示录像.mp4
- springboot260火锅店管理系统录像.mp4
- springboot263校园组团平台录像.mp4
- springboot264基于JAVA的民族婚纱预定系统的设计与实现录像.mp4
- springboot262基于spring boot的小型诊疗预约平台的设计与开发录像.mov
- springboot265基于Spring Boot的库存管理系统录像.mp4
- springboot266基于Web的农产品直卖平台的设计与实现录像.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


