Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can convert a picture to a vector of pixels, and perform PCA on that vector. This might be easier than trying to manually find descriptors. You can use numPy and sciPy in python. For example:</p> <pre><code>import scipy.io from numpy import * #every row in the *.mat file is 256*256 numbers representing gray scale values #for each pixel in an image. i.e. if XTrain.mat has 1000 lines than each line #will be made up of 256*256 numbers and there would be 1000 images in the file. #The following loads the image into a sciPy matrix where each row is a vector #of length 256*256, representing an image. This code will need to be switched #out if you have a different method of storing images. Xtrain = scipy.io.loadmat('Xtrain.mat')["Xtrain"] Ytrain = scipy.io.loadmat('Ytrain.mat')["Ytrain"] Xtest = scipy.io.loadmat('Xtest.mat')["Xtest"] Ytest = scipy.io.loadmat('Ytest.mat')["Ytest"] learn(Xtest,Xtrain,Ytest,Ytrain,5) #this lowers the dimension from 256*256 to 5 def learn(testX,trainX,testY,trainY,n): pcmat = PCA(trainX,n) lowdimtrain=mat(trainX)*pcmat #lower the dimension of trainX lowdimtest=mat(testX)*pcmat #lower the dimension of testX #run some learning algorithm here using the low dimension matrices for example trainset = [] knnres = KNN(lowdimtrain, trainY, lowdimtest ,k) numloss=0 for i in range(len(knnres)): if knnres[i]!=testY[i]: numloss+=1 return numloss def PCA(Xparam, n): X = mat(Xparam) Xtranspose = X.transpose() A=Xtranspose*X return eigs(A,n) def eigs(M,k): [vals,vecs]=LA.eig(M) return LM2ML(vecs[:k]) def LM2ML(lm): U=[[]] temp = [] for i in lm: for j in range(size(i)): temp.append(i[0,j]) U.append(temp) temp = [] U=U[1:] return U </code></pre> <p>In order to classify your image you can used k-nearest neighbors. i.e. you find the k nearest images and label your image with by majority vote over the k nearest images. For example:</p> <pre><code>def KNN(trainset, Ytrainvec, testset, k): eucdist = scidist.cdist(testset,trainset,'sqeuclidean') res=[] for dists in eucdist: distup = zip(dists, Ytrainvec) minVals = [] sumLabel=0; for it in range(k): minIndex = index_min(dists) (minVal,minLabel) = distup[minIndex] del distup[minIndex] dists=numpy.delete(dists,minIndex,0) if minLabel == 1: sumLabel+=1 else: sumLabel-=1 if(sumLabel&gt;0): res.append(1) else: res.append(0) return res </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload