【python计算机视觉编程——8.图像内容分类】

2024-09-08 10:28

本文主要是介绍【python计算机视觉编程——8.图像内容分类】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

python计算机视觉编程——8.图像内容分类

  • 8.图像内容分类
    • 8.1 K邻近分类法(KNN)
      • 8.1.1 一个简单的二维示例
      • 8.1.2 用稠密SIFT作为图像特征
      • 8.1.3 图像分类:手势识别
    • 8.2贝叶斯分类器
      • 用PCA降维
    • 8.3 支持向量机
      • 8.3.2 再论手势识别
    • 8.4 光学字符识别
      • 8.4.2 选取特征
      • 8.4.3 多类支持向量机
      • 8.4.4 提取单元格并识别字符
      • 8.4.5 图像校正

8.图像内容分类

8.1 K邻近分类法(KNN)

import numpy as np
class KnnClassifier(object):def __init__(self,labels,samples):""" 使用训练数据初始化分类器 """self.labels = labelsself.samples = samplesdef classify(self,point,k=3):""" 在训练数据上采用k近邻分类,并返回标记 """# 计算所有训练数据点的距离dist = np.array([L2dist(point,s) for s in self.samples])# 对他们进行排序ndx = dist.argsort()# 用字典存储k近邻votes = {}for i in range(k):label = self.labels[ndx[i]]votes.setdefault(label,0)votes[label] += 1return max(votes, key=lambda x: votes.get(x))def L2dist(p1,p2):return np.sqrt( sum( (p1-p2)**2) )

8.1.1 一个简单的二维示例

from numpy.random import randn
import picklen=200class_1=0.6*randn(n,2)
class_2=1.2*randn(n,2)+np.array([5,1])
labels=np.hstack((np.ones(n),-np.ones(n)))# print(class_1)
with open('points_normal.pkl','wb') as f:pickle.dump(class_1,f)pickle.dump(class_2,f)pickle.dump(labels,f)class_1=0.6*randn(n,2)
r=0.8*randn(n,1)+5
angle=2*np.pi*randn(n,1)
class_2=np.hstack((r*np.cos(angle),r*np.sin(angle)))
labels=np.hstack((np.ones(n),-np.ones(n)))with open('points_ring.pkl','wb') as f:pickle.dump(class_1,f)pickle.dump(class_2,f)pickle.dump(labels,f)
with open('points_normal.pkl','rb') as f:class_1=pickle.load(f)class_2=pickle.load(f)labels=pickle.load(f)model=KnnClassifier(labels,np.vstack((class_1,class_2)))# with open('points_ring.pkl','rb') as f:
#     class_1=pickle.load(f)
#     class_2=pickle.load(f)
#     labels=pickle.load(f)# print(model.classify(class_1[0]))
from pylab import *def plot_2D_boundary(plot_range,points,decisionfcn,labels,values=[0]):"""    Plot_range is (xmin,xmax,ymin,ymax), points is a listof class points, decisionfcn is a funtion to evaluate, labels is a list of labels that decisionfcn returns for each class, values is a list of decision contours to show. """clist = ['b','r','g','k','m','y'] # colors for the classes# evaluate on a grid and plot contour of decision functionx = arange(plot_range[0],plot_range[1],.1)y = arange(plot_range[2],plot_range[3],.1)xx,yy = meshgrid(x,y)xxx,yyy = xx.flatten(),yy.flatten() # lists of x,y in gridzz = array(decisionfcn(xxx,yyy)) zz = zz.reshape(xx.shape)# plot contour(s) at valuescontour(xx,yy,zz,values) # for each class, plot the points with '*' for correct, 'o' for incorrectfor i in range(len(points)):d = decisionfcn(points[i][:,0],points[i][:,1])correct_ndx = labels[i]==dincorrect_ndx = labels[i]!=dplot(points[i][correct_ndx,0],points[i][correct_ndx,1],'*',color=clist[i])plot(points[i][incorrect_ndx,0],points[i][incorrect_ndx,1],'o',color=clist[i])axis('equal')
def classify(x,y,model=model):return np.array([model.classify([xx,yy]) for (xx,yy) in zip(x,y)])
plot_2D_boundary([-6,6,-6,6],[class_1,class_2],classify,[1,-1])
show()

在这里插入图片描述

8.1.2 用稠密SIFT作为图像特征

from PIL import Image
import os
def process_image_dsift(imagename,resultname,size=20,steps=10,force_orientation=False,resize=None):im=Image.open(imagename).convert('L')if resize!=None:im=im.resize(resize)m,n=im.sizeif imagename[-3:]!='pgm':im.save('tmp.pgm')imagename='tmp.pgm'scale=size/3.0x,y=meshgrid(range(steps,m,steps),range(steps,n,steps))xx,yy=x.flatten(),y.flatten()frame=np.array([xx,yy,scale*ones(xx.shape[0]),zeros(xx.shape[0])])savetxt('tmp.frame',frame.T,fmt='%03.3f')if force_orientation:cmmd=str("sift "+imagename+" --output="+resultname+" --read-frames=tmp.frame --orientations")else:cmmd=str("sift "+imagename+" --output="+resultname+" --read-frames=tmp.frame")os.system(cmmd)print('processed',imagename,'to',resultname)
def read_features_from_file(filename):f=loadtxt(filename)
#     print("Array shape:", f.shape)  # 打印数组的形状以调试#因为前四列是兴趣点的坐标,尺度和方向角度,后128列是用整数数值表示的描述子return f[:,:4],f[:,4:]
def plot_features(im,locs,circle=False):def draw_circle(c,r):t=arange(0,1.01,.01)*2*pix=r*cos(t)+c[0]y=r*sin(t)+c[1]plot(x,y,'b',linewidth=2)imshow(im)if circle:for p in locs:draw_circle(p[:2],p[2])else:plot(locs[:,0],locs[:,1],'ob')axis('off')
process_image_dsift('sun.jpg','sun.sift',90,40,True)
l,d=read_features_from_file('sun.sift')im=np.array(Image.open('sun.jpg'))
plot_features(im,l,True)
show()

在这里插入图片描述

8.1.3 图像分类:手势识别

def get_imlist(path,endIdentifier):return [os.path.join(path,f) for f in os.listdir(path) if f.endswith(endIdentifier)]
imlist=get_imlist(r'.\train','.ppm')
imtestlist=get_imlist(r'.\test','.ppm')
for filename in imlist:featfile=filename[:-3]+'dsift'process_image_dsift(filename,featfile,10,5,resize=(50,50))
for filename in imtestlist:featfile=filename[:-3]+'dsift'process_image_dsift(filename,featfile,10,5,resize=(50,50))
imlist = [r'./train/C-uniform01.ppm', r'./train/B-uniform01.ppm',r'./train/A-uniform01.ppm', r'./train/Five-uniform01.ppm',r'./train/Point-uniform01.ppm', r'./train/V-uniform01.ppm']
featlist=[]
for filename in imlist:featfile=filename[:-3]+'dsift'featlist.append(featfile)
# print(imlist)
# print(featlist)for index,item in enumerate(imlist):dirpath, filename = os.path.split(imlist[index])im = array(Image.open(imlist[index]).resize((50, 50)))titlename = filename[:-14]subplot(2, 3, index + 1)l,d = read_features_from_file(featlist[index])plot_features(im, l, True)title(titlename)

在这里插入图片描述

def read_gesture_features_labels(path):# create list of all files ending in .dsiftfeatlist = [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.dsift')]# read the featuresfeatures = []for featfile in featlist:#  print(index)l,d = read_features_from_file(featfile)features.append(d.flatten())features = array(features)# create labelslabels = [featfile.split('/')[-1][0] for featfile in featlist]return features,array(labels)
features,labels=read_gesture_features_labels(r'.\train\\')
test_features,test_labels=read_gesture_features_labels(r'.\test\\')classnames=np.unique(labels)
k=1
knn_classifier=KnnClassifier(labels,features)
res=array([knn_classifier.classify(test_features[i],k) for i inrange(len(test_labels))])acc=sum(1.0*(res==test_labels))/len(test_labels)
print('Accuracy:',acc)
def print_confusion(res,labels,classnames):n=len(classnames)class_ind=dict([(classnames[i],i) for i in range(n)])confuse=zeros((n,n))for i in range(len(test_labels)):confuse[class_ind[res[i]],class_ind[test_labels[i]]]+=1print('Confusion matrix for')print(classnames)print(confuse)

8.2贝叶斯分类器

import numpy as np 
import pickle
from pylab import *
class BayesClassifier(object):def __init__(self):""" Initialize classifier with training data. """self.labels = []    # class labelsself.mean = []        # class meanself.var = []        # class variancesself.n = 0            # nbr of classesdef train(self,data,labels=None):""" Train on data (list of arrays n*dim). Labels are optional, default is 0...n-1. """if labels==None:labels = range(len(data))self.labels = labelsself.n = len(labels)for c in data:self.mean.append(np.mean(c,axis=0))self.var.append(np.var(c,axis=0))def classify(self,points):""" Classify the points by computing probabilities for each class and return most probable label. """# compute probabilities for each classest_prob = np.array([gauss(m,v,points) for m,v in zip(self.mean,self.var)])print('est prob',est_prob.shape,self.labels)# get index of highest probability, this gives class labelndx = est_prob.argmax(axis=0)est_labels = np.array([self.labels[n] for n in ndx])return est_labels, est_prob
def gauss(m,v,x):""" Evaluate Gaussian in d-dimensions with independent mean m and variance v at the points in (the rows of) x. http://en.wikipedia.org/wiki/Multivariate_normal_distribution """if len(x.shape)==1:n,d = 1,x.shape[0]else:n,d = x.shape# covariance matrix, subtract meanS = np.diag(1/v)x = x-m# product of probabilitiesy = np.exp(-0.5*np.diag(np.dot(x,np.dot(S,x.T))))# normalize and returnreturn y * (2*np.pi)**(-d/2.0) / (np.sqrt(np.prod(v)) + 1e-6)
with open('points_normal.pkl','rb') as f:class_1=pickle.load(f)class_2=pickle.load(f)labels=pickle.load(f)bc=BayesClassifier()
bc.train([class_1,class_2],[1,-1])
print(bc.classify(class_1[:10])[0])def classify(x,y,bc=bc):points=np.vstack((x,y))return bc.classify(points.T)[0]plot_2D_boundary([-6,6,-6,6],[class_1,class_2],classify,[1,-1])

在这里插入图片描述

用PCA降维

from PIL import Image
from numpy import *def pca(X):"""    Principal Component Analysisinput: X, matrix with training data stored as flattened arrays in rowsreturn: projection matrix (with important dimensions first), variance and mean."""# get dimensionsnum_data,dim = X.shape# center datamean_X = X.mean(axis=0)X = X - mean_Xif dim>num_data:# PCA - compact trick usedM = dot(X,X.T) # covariance matrixe,EV = linalg.eigh(M) # eigenvalues and eigenvectorstmp = dot(X.T,EV).T # this is the compact trickV = tmp[::-1] # reverse since last eigenvectors are the ones we wantS = sqrt(e)[::-1] # reverse since eigenvalues are in increasing orderfor i in range(V.shape[1]):V[:,i] /= Selse:# PCA - SVD usedU,S,V = linalg.svd(X)V = V[:num_data] # only makes sense to return the first num_data# return the projection matrix, the variance and the meanreturn V,S,mean_Xdef center(X):"""    Center the square matrix X (subtract col and row means). """n,m = X.shapeif n != m:raise Exception('Matrix is not square.')colsum = X.sum(axis=0) / nrowsum = X.sum(axis=1) / ntotalsum = X.sum() / (n**2)#centerY = array([[ X[i,j]-rowsum[i]-colsum[j]+totalsum for i in range(n) ] for j in range(n)])return Y
V,S,m=pca(features)V=V[:50]
features=np.array([np.dot(V,f,-m) for f in features])
# test_features=np.array([np.dot(V,f,-m) for f in test_features])bc=BayesClassifier()
blist=[features[where(labels==c)[0]] for c in classnames]
bc.train(blist,classnames)
# res=bc.classify(test_features)[0]
acc=sum(1.0*(res==test_labels))/len(test_labels)
print('Accuracy:',acc)

8.3 支持向量机

import pickle
from svmutil import *
with open('points_normal.pkl','rb') as f:class_1=pickle.load(f)class_2=pickle.load(f)labels=pickle.load(f)class_1=map(list,class_1)
class_2=map(list,class_2)
labels=list(labels)
samples=class_1+class_2prob=svm_problem(labels,samples)
param=svm_parameter('-t 2')m=svm_train(prob,param)res=svm_predict(labels,samples,m)
with open('points_normal_test.pkl','rb') as f:class_1=pickle.load(f)class_2=pickle.load(f)labels=pickle.load(f)class_1=map(list,class_1)
class_2=map(list,class_2)def predict(x,y,model=m):return array(svm_predict([0]*len(x),zip(x,y),model)[0])plot_2D_boundary([-6,6,-6,6],[class_1,class_2],classify,[1,-1])
show()

8.3.2 再论手势识别

features=map(list,features)
test_features=map(list,test_features)transl={}
for i,c in enumerate(classnames):transl[c],trans[i]=i,cprob=svm_problem(convert_labels(labels,transl),features)
param=svm_paramter('-t 2')m=svm_train(prob,param)res=svm_predict(covert_labels(labels,transl),features,m)res=svm_predict(convert_labels(test_labelss,transl),test_features,m)[0]
res=convert_labels(res,transl)
acc=sum(1.0*(res==test_labels))/len(test_labels)
print('Accuracy:',acc)print_confusion(res,test_labels,classnames)

8.4 光学字符识别

8.4.2 选取特征

def compute_feature(im):norm_im=imresize(im,(30,30))norm_im=norm_im[3:-3,3:-3]return norm_im.flatten()
def load_ocr_data(path):imlist=[os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]labels=[int(imfile.split('/')[-1][0]) for imfile in imlist]features=[]for imname in imlist:im =array(Image.open(imname).convert('L'))features.append(compute_feature(im))return array(features),labels

8.4.3 多类支持向量机

from svmutil import *
features,labels=load_ocr_data('training/')test_features,test_labels=load_ocr_data('testing/')features=map(list,features)
test_features=map(list,test_features)prod=svm_problem(labels,features)
param=svm_parameter('-t 0')m=svm_train(prob,param)res=svm_predict(labels,features,m)res=svm_predict(test_labels,test_features,m)

8.4.4 提取单元格并识别字符

from scipy.ndimage import measurementsdef find_sudoku_edges(im,axis=0):trim=1*(im<128)s=trim.sum(axis=axis)s_labels,s_nbr=measurements.label(s>(0.5*max(s)))m=measurements.center_of_mass(s,s_labels,range(1,s_nbr+1))x=[int(x[0]) for x in m]if lem(x)==4:dx=diff(x)x=[x[0],x[0]+dx[0]/3,x[0]+2*dx[0]/3,x[1],x[1]+dx[1]/3,x[1]+2*dx[1]/3,x[2],x[2]+dx[2]/3,x[2]+2*dx[2]/3,x[3]]if len(x)==10:return xelse:raise RuntimeError('Edges not detected')
imname='sudokus/sudoku18.jpg'
vername='sudokus/sudoku18.sud'
im=array(Image.open(imname).convert('L'))x=find_sudoku_edges(im,axis=0)
y=find_sudoku_edges(im,axis=1)crops=[]
for col in range(9):for row in range(9):crop=im[y[col]:y[col+1],x[row]:x[row+1]]crops.append(compute_feature(crop))
res=svm_predict(loadtxt(vername),map(list,crops),m)[0]
res_im=array(res).reshape(9,9)print('Result:')
print(res_im)

8.4.5 图像校正

from scipy import ndimageimname='sudoku8.jpg'
im=array(Image.open(imname).convert('L'))figure()
imsshow(im)
gray()
x=ginput(4)fp=array([array([p[1],p[0],1]) for p in x]).T
tp=array([[0,0,1],[0,1000,1],[1000,1000,1],[1000,0,1]]).TH=H_from_points(tp,fp)def warpfcn(x):x=array([x[0],x[1],1])xt=dot(H,x)xt=xt/xt[2]return xt[0],xt[1]im_g=ndimage.geometric_transform(im,warpfcn,(1000,1000))

这篇关于【python计算机视觉编程——8.图像内容分类】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1147854

相关文章

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

nudepy,一个有趣的 Python 库!

更多资料获取 📚 个人网站:ipengtao.com 大家好,今天为大家分享一个有趣的 Python 库 - nudepy。 Github地址:https://github.com/hhatto/nude.py 在图像处理和计算机视觉应用中,检测图像中的不适当内容(例如裸露图像)是一个重要的任务。nudepy 是一个基于 Python 的库,专门用于检测图像中的不适当内容。该

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip