图片切割:
1 // 核心代码: 2 char root_path[150]; // 数字图片的路径 3 char pic_path[100]; 4 sprintf(root_path, "E:\4_大三下\机器视觉\实验图片\8\8-1.bmp"); 5 IplImage *inputImg = cvLoadImage(root_path,1); 6 int block = 4; 7 int nWidth = inputImg->width / block; 8 int nHeight = inputImg->height / block; 9 for(int i = 0;i < block;i++) 10 for(int j = 0; j < block;j++) { 11 cvResetImageROI(inputImg); 12 cvSetImageROI(inputImg, cvRect(i * nWidth, j * nHeight, nWidth, nHeight)); 13 IplImage * img = cvCreateImage(cvSize(nWidth, nHeight), 8, 3); 14 cvCopy(inputImg,img); 15 sprintf(pic_path, "E:\4_大三下\机器视觉\%d-%d.bmp", i, j); 16 cvSaveImage(pic_path, img); 17 }
实现数字图像的特征提取算法,求0~9共十个数字的平均特征向量;
实现数字图像的识别算法,任意输入三个不同的数字图像,将数字图像和识别结果输出到屏幕;
1 #include "stdio.h" 2 #include "math.h" 3 #include "vector" 4 #include "cv.h" 5 #include "highgui.h" 6 7 using namespace std; 8 9 // 函数功能,计算一张图片某一个矩形块(以x,y为原点, width,height为宽度和高度)中黑色像素点的个数 10 int ImageBlackCount(IplImage *Img, int x,int y,int width,int height) 11 { 12 int nCount = 0; 13 char *data = (char *)Img->imageData; // 新图象数据信息 14 int wp = Img->widthStep; 15 for(int i = 0;i < height;i++) 16 for(int j = 0;j < width;j++) 17 { 18 int r = data[(x + i) * wp + 3 * (y + j)]; 19 if(r == 0) 20 nCount++; 21 } 22 return nCount; 23 } 24 25 // 函数功能,返回一张图片的特征向量 26 vector<int> ImageVec(IplImage *Img) 27 { 28 vector<int> temp(16,0); 29 int nwidth = Img->width / 4; 30 int nheight = Img->height / 4; // 分成16块 31 int x = 0, y = 0; 32 for(int k = 1;k <= 16;k++) 33 { 34 temp[k - 1] = ImageBlackCount(Img, x, y, nwidth, nheight); 35 y += nwidth; 36 if(k % 4 == 0) 37 { 38 x += nheight; 39 y = 0; 40 } 41 } 42 return temp; 43 } 44 45 // 计算欧氏距离 46 double CalDist(vector<int> test,vector<int> train) 47 { 48 double dist = 0; 49 if(test.size() == train.size()) 50 for(int i = 0;i < test.size();i++) 51 dist += (train[i] - test[i]) * (train[i] - test[i]); 52 return sqrt(dist); 53 } 54 55 int main(int arge,char * argv[]) 56 { 57 char root_path[150]; // 数字图片的路径 58 vector< vector<int> > vec; 59 // 模板库--平均特征向量求解 60 int block = 0; 61 for(int i = 0;i < 10;i++) 62 { 63 vector<int> avg(16,0); 64 for(int j = block +1; j <= block + 5;j++) { 65 sprintf(root_path, "E:\4_大三下\机器视觉\实验图片\%d\%d-%d.bmp", i, i, j); 66 IplImage *inputImg = cvLoadImage(root_path,1); 67 vector<int> temp = ImageVec(inputImg); 68 for(int k = 0;k < 16;k++) 69 avg[k] += temp[k]; 70 } 71 printf("数字%d的平均特征向量:",i); 72 for(int k = 0;k < 16;k++) { 73 avg[k] = avg[k] / 5;//block; 74 printf("%d ",avg[k]); // 一个数字的平均特征向量 75 } 76 putchar(10); 77 vec.push_back(avg); 78 } 79 80 // 剩余数字的测试匹配 81 char pic_path[150]; 82 vector< vector<int> > Mat; 83 for(int i2 = 0;i2 < 10;i2++) 84 { 85 vector<int> mat_test(10,0); 86 for(int j2 = 6 - block; j2 <= 10 - block;j2++) { 87 sprintf(pic_path, "E:\4_大三下\机器视觉\实验图片\%d\%d-%d.bmp", i2, i2, j2); 88 IplImage *test = cvLoadImage(pic_path,1); 89 vector<int> temp = ImageVec(test); 90 int cls = 0; 91 double d = 0, min = CalDist(temp, vec[0]); 92 for(int n = 0;n < 10;n++) 93 { 94 d = CalDist(temp, vec[n]); 95 if(min > d) 96 { 97 min = d; 98 cls = n; 99 } 100 } 101 mat_test[cls]++; 102 } 103 Mat.push_back(mat_test); 104 } 105 double rate = 0; 106 // 混淆矩阵 107 printf(" 0 1 2 3 4 5 6 7 8 9 "); 108 for(int i3 = 0;i3 < 10;i3++) 109 { 110 rate += Mat[i3][i3]; 111 printf("%d: ",i3); 112 for(int j3 = 0; j3 < 10;j3++) 113 printf("%d ",Mat[i3][j3]); 114 putchar(10); 115 } 116 printf("总识别率为:%.2lf%% ",rate * 2); 117 return 0; 118 }
2021-06-04