基于Python来获取用户手机设备使用情况
前言
本博客为模式识别作业的记录,实现批感知器算法、Ho Kashyap算法和MSE多类扩展方法,可参考教材[ 1 ] color{#0000FF}{[1]}[1]。所用数据如下如所示:
批感知器算法
从a = 0 mathbf a=0a=0开始迭代,分类ω 1 omega_1ω1和ω 2 omega_2ω2并计算最终的解向量,记录下收敛的步数。
import cv2 import numpy as np from imutils import contours from matplotlib import pyplot as plt # 定义绘图函数 def imshow(name, img): cv2.imshow(name, img) cv2.waitKey(0) cv2.destroyAllWindows() def num_cnts_sort(list,right=1,up=0): # 传入的是找到的轮廓,返回的是排序好的轮廓外接矩阵的(x,y,w,h) # up=1表示从上往下,right=1表示从左往右,-1表示反过来 reverse = False if up==-1 or right== -1: reverse = True if up == 0: # 左右方向排序 权重选x i = 0 if right == 0: i = 1 # 找到的轮廓用外接矩形框起来 cv2.boundingRect(c)返回x,y,w,h boundingBoxs = [cv2.boundingRect(c) for c in list] # sorted(输入序列,排序规则,reverse=True由小到大否则由大到小) # lambda 匿名函数 输入序列的每个元素 输出b[i] boxs = sorted(boundingBoxs,key= lambda b: b[i],reverse=reverse ) return boxs def num_resize(img,w_size=0,h_size=0): (h,w)=img.shape[0:2] # size返回总元素个数 和matlab不一样 if h_size != 0: r = h_size/float(h) w_size = int(r*w) if w_size != 0: r = w_size/float(w) h_size = int(r*h) resized = cv2.resize(img,(w_size,h_size)) return resized # 读取模板图片 img_num = cv2.imread("images/ocr_a_reference.png") # cv2.cvtColor获得图像的副本 img_num_gray = cv2.cvtColor(img_num, cv2.COLOR_BGR2GRAY) imshow("img_num",img_num) # cv2.threshold(输入图像,阈值,赋值,方法) 这里方法是高于阈值取0,低于阈值取255 # cv2.threshold返回两个值 第二个值是我需要的处理后的图像 img_num_bin = cv2.threshold(img_num_gray,10,255,cv2.THRESH_BINARY_INV)[1] imshow("img_num_bin",img_num_bin) # 获取轮廓 # cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),cv2.RETR_EXTERNAL只检测外轮廓,cv2.CHAIN_APPROX_SIMPLE只保留终点坐标 # 返回的list中每个元素都是图像中的一个轮廓 num_cnts_list, _ =cv2.findContours(img_num_bin.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) """ cv2.drawContours(img_num, num_cnts_list, -1, (0,0,255), 2) imshow("draw_img_num",img_num_bin) """ # 对轮廓排序 并且返回论廓外接矩形的坐标 num_rect_list = num_cnts_sort(num_cnts_list) # 验证排序正确 """ for num_rect in num_rect_list: (x,y,w,h)=num_rect num_rect_img = cv2.rectangle(img_num.copy(),(x,y),(x+w,y+h),(255,0,0),2) imshow("num_rect_img",num_rect_img) """ # 把图片和数字对应 num_rect_dic = {} for (i,num_rect) in enumerate(num_rect_list): (x, y, w, h) = num_rect # 对图片像素点操作x,y要对调,因为dim=0存的是行 是x方向的像素信息 num_rect_item = img_num_bin[y:y+h,x:x+w] num_rect_item = cv2.resize(num_rect_item,(57,88)) # 把数字和截下来的图像对应 num_rect_dic[i]=num_rect_item imshow("num_rect_item", num_rect_item) # 对银行卡图像预处理 # 读取图像 bank_img = cv2.imread("images/credit_card_01.png") bank_img = num_resize(bank_img,h_size=200) bank_img_gray = cv2.cvtColor(bank_img,cv2.COLOR_BGR2GRAY) # bank_img_gray = num_resize(bank_img_gray,h_size=200) # bank_img = cv2.resize(bank_img,bank_img_gray.shape) imshow("bank_img",bank_img) imshow("bank_img_gray",bank_img_gray) # 定义卷积核 rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3)) # 矩形卷积核 sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) # 顶帽操作 突出明亮的部分 bank_img_tophat = cv2.morphologyEx(bank_img_gray,cv2.MORPH_TOPHAT, rectKernel) imshow("bank_img_tophat",bank_img_tophat) # 对x方向边缘检测分支 然后二值化 def branch1(bank_img_tophat): # X方向边缘检测处理 横线太浅 y方向边缘检测可能会消失 bank_img_grad = cv2.Sobel(bank_img_tophat, cv2.CV_32F, 1, 0, ksize=-1) bank_img_grad_abs = np.absolute(bank_img_grad) (max, min) = (np.max(bank_img_grad_abs), np.min(bank_img_grad_abs)) bank_img_grad_abs = (255 * (bank_img_grad_abs - min) / (max - min)) bank_img_grad_abs = bank_img_grad_abs.astype("uint8") imshow("bank_img_grad_abs", bank_img_grad_abs) return bank_img_grad_abs bank_img_grad_abs = branch1(bank_img_tophat) # 腐蚀与闭操作 bank_img_close = cv2.morphologyEx(bank_img_grad_abs,cv2.MORPH_DILATE,sqKernel) bank_img_close = cv2.morphologyEx(bank_img_close,cv2.MORPH_CLOSE,sqKernel) imshow("bank_img_close",bank_img_close) bank_img_close= cv2.morphologyEx(bank_img_close,cv2.MORPH_CLOSE,sqKernel) # 二值化 cv2.THRESH_OTSU会选择合适的阈值进行二值化 cv2.threshold返回的是两个元素 第二个是处理后的图像 bank_img_close_bin = cv2.threshold(bank_img_close, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] imshow("double-close",bank_img_close_bin ) # 获取轮廓 bank_img_gray1 = bank_img_gray.copy() bank_img_contour,_ = cv2.findContours(bank_img_close_bin,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) """ cv2.drawContours(bank_img_gray,bank_img_contour,-1,(0,0,255),3) imshow("contours",bank_img_gray) """ # 通过以下代码找到一组银行卡上的轮廓 计算大概的比例和长度 """ (x,y,w,h) = cv2.boundingRect(bank_img_contour[4]) bank_img_draw = bank_img_gray bank_img_draw = cv2.rectangle(bank_img_draw,(x,y),(x+w,y+h),(0,0,255),2) imshow("1",bank_img_draw) print("w="+str(w)+"h="+str(h),"r="+str(w/float(h))) """ # 获取轮廓外接矩形 并过滤不合格的轮廓 bank_img_real_contour=[] for contour in bank_img_contour: (x, y, w, h) = cv2.boundingRect(contour) r = w / float(h) if r > 2.5 and r < 4.0: if w > 50 and w < 80 and h > 10 and h < 30: bank_img_real_contour.append(contour) # 画出来看看 img_draw = cv2.cvtColor(bank_img,1) bank_draw = cv2.rectangle(img_draw, (x, y), (x + w, y + h), (0, 128, 128), 2) imshow("s", bank_draw) # 4个一组 获取对应二值图像 bank_img_list = [] # 把4组从左往右排序 返回每组的(x,y,w,h) contour_list = num_cnts_sort(bank_img_real_contour) for contour in contour_list: (x, y, w, h) = contour # 把每组的灰度图像填充5个像素截取下来 bank_img = bank_img_gray[(y - 5):(y + 5 + h), (x - 5):(x + 5 + w)] # 二值化 bank_img = cv2.threshold(bank_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] imshow("bank_img", bank_img) bank_img_list.append(bank_img) # 获取每个数字进行模板匹配 grade = [] for img in bank_img_list: # 对包含4个数字的图片进行轮廓检测 bank_contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 对每个数字排序 返回的是每个轮廓外接矩形的(x,y,w,h) bank_cont_rec = num_cnts_sort(bank_contours) for i, rec in enumerate(bank_cont_rec): (x, y, w, h) = rec num = img[y:(y + h), x:(x + w)] # 缩放到和模板一样大小 roi = cv2.resize(num, (57, 88)) item = 0 # 字典num_rect_dic存有数字和对应图像 for num in range(10): # 模板匹配 num_img = num_rect_dic[num] # 模板匹配 result = cv2.matchTemplate(roi, num_img, cv2.TM_CCOEFF) (_, score, _, _) = cv2.minMaxLoc(result) # 记下最大值,最贴近正确值得对应的 num if score > item: item = score max = num grade.append(str(max)) # cv2.putText(图像, 文字, 左下角坐标, 字体, 大小, 颜色, 字体粗细) cv2.putText(img_draw, "".join(grade), (contour_list[0][0], contour_list[0][1] - 15), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 1) imshow("bank", img_draw) # .join把序列的字符串和前面的拼在一起 print("银行卡号为" + "".join(grade))