c# - 如何训练程序使用 PCA C# 识别人脸?
问题描述
问题
我有一组两个文件夹,第一个文件夹包含train images
用于训练程序识别它们的图像,第二个文件夹中包含相似的图像test images
。所有图像都包含个人的面孔。我应该使用的算法是Principal Component Analysis
. 两个文件夹各有 200 张图像。
脚步
我读了这个的matlab实现,它说第一步是:
(一世)。阅读训练文件夹中图像的尺寸。就我而言,它是92x112
. . 该文件夹有 20 个人的图像,每个人有 10 个图像,因此总共有 200 个。
(二)。将MXN像素的图像转换为1XMN的一维向量,即 1x112x92*200,并表示此变量将用于在后面的部分中计算协方差矩阵。
(iii)。提示用户输入训练图像的数量,在这种情况下为200
。
(四)。提示用户要保留的特征值个数,特征值是图像经过 PCA 后图像的新维度。
(五)。初始化一个矩阵数据变量,该变量是训练文件夹集中图像的尺寸和测试文件夹中的图像数量的乘积。在这种情况下,它应该是92x112x200
㈥。第六步是创建一个循环来读取训练数据集中的所有图像,将它们全部转换为灰度,并将它们转换为一维向量变量。
(vii) 我们计算所有图像的平均值并创建一个循环以从每十张图像中减去平均值。
(viii)计算协方差矩阵并用它来得到eigen values
和eigen vectors
(ix) 对特征向量进行降序排序,得到最高的十个值。
先到此为止,文章很长。我决定用 C# 复制它。
代码
namespace PCA{
public partial class Form1:Form{
/*store the dimensions of the image in a
variable*/
int width=112;
int height=92;
//store the number of train_images
int train_images=200;
/*store the one dimension vector of the images in training folder in a variable*/
training[,] myarray=new
int[1,height*width*train_images];
/*store the number of eigen values to keep*/
int eigen_values=50;
/*initialize the matrix data*/
int[,] matrix_data=new
int[width*height,train_images];
/*initialize the mean of the images
*/
int image_mean=0;
/*
initialize the image array variable*/
Image[] training= new Image[train_images];
/*method to call training process*/
public static void trainImages(){
/*read the images in the training
folder into grayscale using a loop
am having trouble here
*/
for(int i=0;i<train_images;i++){
/*the folder containing the images
is in same location as script
images take the naming format below:
S1_1.jpg-image1
S1_2.jpg-image2
S1_3.kpg-image3
S1 is a person and the _2 and _3 up to 10 are different images of the same person
*/
if(i>=0 &&i<10){
//load the specific file name
training_images[i]=Image.fromFile("training/S1_"+(i+1).ToString()+".jpg");
}
/*I will implement the rest of the loops later for reading the images*/
}
//compute the mean of all images
mean_image=height*width/train_images;
//convert the image array to grayscale
List<Image> gray= new List<Image>();
foreach(Image c in training_images){
Bitmap bmp=new Bitmap(c);
Color p;
for(y=0;y<112;y++){
for(x=0;x<92;x++){
p=bmp.getPixel(x,y);
//get the pixel values
int a=p.A;
int r=p.R;
int b=p.B;
int g=p.G;
//Get the average
int av=(r+b+g)/3;
//set the new gray pixel
bmp.setPixel(x,y,Color.fromArgb(a,av,av, av)); }
}
gray.Add(bmp);
}
/*convert the grayscale image array into a one-dimension vector
not sure if vector and array mean the same thing in C#
*/
}
}
目标
有人可以帮我完成这个,以便我设计一个完美的程序,可以训练自己识别测试文件夹中的图像和训练文件夹中的图像谢谢!
解决方案
推荐阅读
- javascript - CORS 策略已阻止访问获取:请求的资源上不存在“Access-Control-Allow-Origin”标头
- bash - 如何将 Unicode 字符串转换为 Unicode 转义序列?
- reactjs - 如何在 React 中显示倒数计时器
- security - elasticsearch 在正在运行的集群上应用安全性
- xamarin - Xamarin 表单的 SeeMore 功能
- php - 未找到 Laravel 7.11.0 类“\App\Models\User”
- solr - Solr 服务器未在 Windows 上启动
- c# - 将文件发送到 api 并获得响应
- docker - 如何在 docker 桌面上修改 nodport 范围(Windows)
- python - 有没有办法使用输入数据/变量来使用 pynput 键盘测试键盘输入