#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;
}入代码片
c++识别试纸
最新推荐文章于 2024-07-09 22:37:42 发布