c++识别试纸

#include <iostream>
#include <fstream>
#include<iomanip>
#define MAXSIZE 8000
#define column  43//像素列
#define row    56//像素行
#define Threshold 180
using namespace std;
int  test(int* ptr);
void Two_Dimention_arr_traval(int(*gary_ptr1)[column]);
void RGB_Tf_gray(int(*gary_ptr1)[column], int* ptr2);
void Two_Value_TR(int(*gary_ptr1)[column], int threshold);
int positive_ornagetive(int(*gary_ptr1)[column]);

int main()
{
	int data_gray[row][column] = { 0 };
	int data[MAXSIZE] = {0};//存储图像像素值,单通道为最小单位
	int count1=test(data);//统计data数组元素个数
	for(int i = 0; i < 7223;i++ )//便历数组元素
	{
		cout << data[i] << endl;
	}
	cout << "data[]共有" << count1 << "个元素" << endl;
	RGB_Tf_gray(data_gray, data);
	Two_Value_TR(data_gray, Threshold);
	Two_Dimention_arr_traval(data_gray);
	positive_ornagetive(data_gray);

	return 0;
}


/**
  * @name   : test()
  * @brief  : 从特定文件中读取我所需要的数据,此函数不具有统用性
  * @param  :数组头指针
  * @retval : 数组元素个数
  */
int test(int * ptr)
{
	ifstream ifs;
	ifs.open("C:\\Users\\hp-pc\\Desktop\\text3.txt", ios::in);
		if (!ifs.is_open()) {
			cout << "文件打开失败" << endl;
			return 0;
		
		}
	char c;//中间变量:暂存从文件中取出的字符
	int c_data[6];//中间数组,与下面变量i结合,完成将文本中字符转换成三通道像素值
	int i = 0;
	int count = 0;//计数:数组中
	while ((c = ifs.get()) != EOF)//循环完成文件中字符读取
	{
		if (c != ' ') {
			if (c != ',') {
				c_data[i] = (int)c - 48;//ascll转换到十进制
				i++;
			}

			if (c == ',') {
				if (i == 1) { ptr[count] = c_data[0];  count++; }
				if (i == 2) { ptr[count] = c_data[0] * 10 + c_data[1]; count++; }
				if (i == 3) { ptr[count] = c_data[0] * 100 + c_data[1] * 10 + c_data[2];  count++; }
				i = 0;
			}
		}
		cout << c << endl;
	}

	ifs.close();//文件关闭
	return count;
}
/**
  * @name   : RGB_Tf_gray()
  * @brief  : RGB三通道像素值转化灰度值(平均值法Gray=(R+G+B)/3)
  * @param  :数组头指针
  * @retval : none
  */
void RGB_Tf_gray(int (*gary_ptr1)[column],int *ptr2) 
{
	int i=0;
	int j = 0;//行
	int k = 0;//列
	
		for (j = 0; j < row; j++)
		{
			for(k=0;k<column;k++)
			{
				gary_ptr1[j][k] = (ptr2[i] + ptr2[i + 1] + ptr2[i + 2]) / 3;
				i += 3;
			}
			
		}	
}
/**
  * @name   : Two_Dimention_arr_traval()
  * @brief  : 便历二维数组,以右对齐方式输出
  * @param  :数组头指针
  * @retval : none
  */
void Two_Dimention_arr_traval(int(*gary_ptr1)[column])
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			cout <<setw(4) <<gary_ptr1[i][j] ;
			if (j == column - 1)cout << endl;
		}

	}

}
/**
  * @name   : Two_Value_TR()
  * @brief  : 像素值二值化
  * @param  :数组头指针,像素阀值
  * @retval : none
  */
void Two_Value_TR(int(*gary_ptr1)[column],int threshold)
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			if (gary_ptr1[i][j] > threshold)gary_ptr1[i][j] = 255;
			else gary_ptr1[i][j] = 0;
			
		}

	}
	
}

int positive_ornagetive(int(*gary_ptr1)[column])
{
	int i, j;
	int jump_location = 0;
	int jump_number = 0;
	for (i = 0; i < column; i++)
	{
		for (j = 0; j < row-1; j++)
		{
			if ((gary_ptr1[j][i] - gary_ptr1[j + 1][i]) != 0) {
				jump_number++; jump_location
					+= j;
			}
			
		}

	}
	jump_location = jump_location /( 2 * column);
	cout << "跳变点数为" << jump_number << endl;
	cout << "跳变位置为" << jump_location << endl;
	return 1;

}入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV是一个源的计算机视觉库,它提供了丰富的图像处理和计算机视觉算法。在C++中使用OpenCV进行小球颜色识别可以通过以下步骤实现: 1. 导入OpenCV库:在C++代码中,首先需要导入OpenCV库,可以使用`#include <opencv2/opencv.hpp>`语句来实现。 2. 读取图像:使用`cv::imread()`函数读取待处理的图像文件,并将其存储在一个`cv::Mat`对象中。 3. 转换颜色空间:将读取的图像从默认的BGR颜色空间转换为HSV颜色空间,因为HSV颜色空间对颜色的描述更加直观。可以使用`cv::cvtColor()`函数来实现。 4. 设定颜色范围:根据小球的颜色特征,设定一个颜色范围,用于在图像中提取该颜色的区域。可以使用`cv::inRange()`函数来实现。 5. 进行形态学操作(可选):根据实际情况,可以对提取到的颜色区域进行形态学操作,如腐蚀和膨胀,以去除噪声或填充空洞。可以使用`cv::erode()`和`cv::dilate()`函数来实现。 6. 寻找轮廓:使用`cv::findContours()`函数在处理后的图像中寻找轮廓。 7. 过滤轮廓:根据实际需求,可以对找到的轮廓进行一些过滤操作,如面积过滤、长宽比过滤等。 8. 绘制结果:使用`cv::drawContours()`函数将找到的轮廓绘制在原始图像上,以便观察识别结果。 下面是一些相关问题: 1. OpenCV是什么? 2. 如何在C++中使用OpenCV? 3. 什么是颜色空间转换? 4. 什么是HSV颜色空间? 5. 形态学操作在图像处理中有什么作用? 6. 如何使用OpenCV进行轮廓检测? 7. 如何对找到的轮廓进行过滤操作? 8. 如何在图像上绘制轮廓?

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值