5月份为应付老师的作业写的简单的图片识别的程序

 做的是一个简单的图片识别程序  用来识别摄像头在纸上拍到的图像。注释很详细  整个项目将上传到本人的资源里面去。

 

 关键类

//bmp.h

#ifndef _BMP_H_
#define _BMP_H_

#include <iostream>
#include <list>
using namespace std;

struct BMPPoint
{
 int x,y;
};

struct Bound
{
 unsigned char data;
 int up,low,left,right;
 list<BMPPoint> *points;
};

class MapDO
{
public:
 /*--------位图转化为灰度图--------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 ---------------------------------------------*/
 int ChangeGray(unsigned char *pSrcDib, int width, int height);

 //--------二阶微分算子提取轮廓---------------
 int Laplacian1(unsigned char *pSrcDib, int width, int height);
 int Laplacian2(unsigned char *pSrcDib, int width, int height);
 int Laplacian3(unsigned char *pSrcDib, int width, int height);
 int Laplacian4(unsigned char *pSrcDib, int width, int height);
 int Laplacian5(unsigned char *pSrcDib, int width, int height);
 int Laplacian6(unsigned char *pSrcDib, int width, int height);
 int Laplacian7(unsigned char *pSrcDib, int width, int height);
 int Laplacian8(unsigned char *pSrcDib, int width, int height);
 //--------二阶微分算子提取轮廓---------------

 //--------用Prewitt提取轮廓------------------
 int Prewitt(unsigned char *pSrcDib, int width, int height);
 
 /*--------把二值图像细化---------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 ---------------------------------------------*/
 int Thinning(unsigned char *pSrcDib, int width, int height);

 /*--------把二值图像-------------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 thresh  阀值
 ---------------------------------------------*/
 int Threshold(unsigned char *pSrcDib, int width, int height, int thresh);

 /*--------中值滤波法去除噪声-----------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度 
  -------------------------------------------*/
 int Median(unsigned char *pSrcDib, int width, int height);

 /*---------二值图像收缩处理-------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 ---------------------------------------------*/
 int Erosion(unsigned char *pSrcDib, int width, int height);

 /*---------二值图像膨胀处理-------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 ---------------------------------------------*/
 int Diation(unsigned char *pSrcDib, int width, int height);

 /*-------标记图像-----------------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 count 被标记个数
 labels  标记数组
 bounds 保存每个对象的边框
 --------------------------------------------*/
 int Label(unsigned char *pSrcDib, int width, int height, unsigned char *lb, int &count, list<Bound> &bounds);

 /*-------框住所要的对象-----------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 bounds  标记数组
 --------------------------------------------*/
 int bounding(unsigned char *pSrcDib, int width, int height, list<Bound> &bounds);

 int Rotation(unsigned char *pSrcDib, int width, int height, float deg);

private:
 /*--------用二阶微分提取轮廓------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 c[]  算子
 amp     图像增益
 ---------------------------------------------*/
 int Second_order_differential(unsigned char *pSrcDib, int width, int height, int c[], float amp);

 /*--------用Prewitt提取轮廓-------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 amp     图像增益
 ---------------------------------------------*/
 int Prewitt(unsigned char *pSrcDib, int width, int height, float amp );

 /*--------检查连接性---------------------------
 inb  输入像素
 ---------------------------------------------*/
 int Cconc(int inb[]);

 /*-------求出中值滤波法中9个元素的中间值------
 c  橡树
 --------------------------------------------*/
 int Median_value(int c[]);

 /*-------标记图像-----------------------------
 pSrcDib 图片数据指针
 width   图片橡树宽度
 height  图片橡树高度
 x       识别位置
 y  识别位置
 label   标记值
 bound 该对象的边框和橡树
 --------------------------------------------*/
 int Labeling(unsigned char *pSrcDib, int width, int height,int x, int y, int label, Bound &bound);
};
#endif

 

 

 

 

//ComputerKnow.h

#ifndef _COMPRTERKNOW_H_
#define _COMPRTERKNOW_H_

#include "bmp.h"
#include <deque>

class ComputerKnow
{
public:
 /*----------------构造函数---------------------------
  pSrcDib  数据源
  width   图片宽度
  height   图片高度
  ---------------------------------------------------*/
  ComputerKnow(unsigned char* pSrcDib, int width, int height)
  {
   if (pSrcDib == NULL || width < 0 || height < 0)
   {
    throw string("error! please send in right!");
   }
   this->pSrcDib = pSrcDib;
   this->width = width;
   this->height = height;
  }

  /*---------------识别---------------------------
  rect  矩形个数
  triangle 三角形个数
  round  圆形个数
  ----------------------------------------------*/
  int Knowing(int &rect, int &triangle, int &round);
private:
 /*----------------识别到底是什么----------------
 识别是矩形 还是三角形 还是圆形
 返回值 1 矩形 , 2 三角形 , 3 圆形 , 0 无法识别
 res  保存单个图形个指针
 up  最上边界值
 low  最下边界值
 left 最左边界值
 right 最右边界值
 ----------------------------------------------*/
 int KnowWhat(unsigned char* res, int up, int low, int left, int right, int label);

 /*------------测试所在范围的像素点个数----------
 res  保存单个图形个指针
 d  起始队列
 up  最上边界值
 low  最下边界值
 left 最左边界值
 right 最右边界值
 ----------------------------------------------*/
 int TestPoint(unsigned char* res, deque<BMPPoint> d, int label, int up, int lwow, int left, int right);
public:
 unsigned char* pSrcDib;
 int width;
 int height;
private:
 MapDO mapDo; //基础图像算法
};

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
Sure, 我可以和您介绍一下如何基于KNN的图片识别的Python程序。以下是程序示例和注释: ```python import numpy as np import cv2 import os class KNN: def __init__(self, k): self.k = k def predict(self, dataset, labels, sample): # 计算所有样本与待测样本的欧式距离 dist = np.sqrt(np.sum(np.square(dataset - sample), axis=1)) # 将距离递增排序,并按照排序后的索引值提取对应的标签值 sorted_dist_indices = np.argsort(dist) class_count = {} # 统计前k个最近样本的标签及出现次数 for i in range(self.k): vote_label = labels[sorted_dist_indices[i]] class_count[vote_label] = class_count.get(vote_label, 0) + 1 # 返回出现次数最多的标签值 max_count = 0 for key, value in class_count.items(): if value > max_count: max_count = value max_index = key return max_index def load_dataset(dataset_path): # 加载数据集 images = [] labels = [] for root, dirs, files in os.walk(dataset_path): for file in files: # 获取图片路径和对应标签值 image_path = os.path.join(root, file) label = int(os.path.basename(root)) # 读取图片 image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # 将图片转换成向量形式 image_vector = np.reshape(image, [-1]) images.append(image_vector) labels.append(label) # 转换为数组形式 images = np.array(images) labels = np.array(labels) return images, labels def main(): # 数据集路径 dataset_path = './dataset/' # 加载数据集 dataset, labels = load_dataset(dataset_path) # 定义KNN分类器,K值为5 knn = KNN(5) # 读取待测图片 test_image = cv2.imread('./test/0.jpg', cv2.IMREAD_GRAYSCALE) # 将待测图片转换成向量形式 test_image_vector = np.reshape(test_image, [-1]) # 预测待测图片的标签值 predict_label = knn.predict(dataset, labels, test_image_vector) print('Predicted label:', predict_label) if __name__ == '__main__': main() ``` 以上程序是基于KNN算法实现的简单图片识别程序程序首先会加载位于`./dataset/`目录下的数据集,数据集的每个子目录分别代表一种图像分类,下面依次列举各文件夹中图片分类。 - `./dataset/0/`: 包含0-9中数字0的各种变形 - `./dataset/1/`: 包含0-9中数字1的各种变形 - `./dataset/2/`: 包含0-9中数字2的各种变形 - `./dataset/3/`: 包含0-9中数字3的各种变形 - `./dataset/4/`: 包含0-9中数字4的各种变形 - `./dataset/5/`: 包含0-9中数字5的各种变形 - `./dataset/6/`: 包含0-9中数字6的各种变形 - `./dataset/7/`: 包含0-9中数字7的各种变形 - `./dataset/8/`: 包含0-9中数字8的各种变形 - `./dataset/9/`: 包含0-9中数字9的各种变形 程序中`load_dataset`函数负责将数据集中的图片加载进来,转换成向量形式。其中,每个图像文件在加载时,使用cv2的imread函数获得相应的像素矩阵,并将其转换成向量形式,最后存储到images数组和labels数组中。 接着,程序会读取待测图片(`./test/0.jpg`),并将其转换为向量形式,利用KNN算法对其进行分类,最终输出预测的标签值。 以上程序只是一个简单的例子,您可以在其基础上加入更多的图像特征提取方法、分类器等,以提高识别率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值