实验2代码

This commit is contained in:
2026-04-10 11:10:36 +08:00
parent 2f5d73de02
commit 3d0e94ce32
7 changed files with 5855 additions and 0 deletions

57
lab-2/lab-2.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include <iostream>
#define H 1210
#define W 1240
using namespace std;
int main()
{
unsigned char *grey;
grey = new unsigned char[H * W];
FILE *ftest;
fopen_s(&ftest, "E:\\test.raw", "rb");
fread(grey, sizeof(unsigned char), H * W, ftest);
fclose(ftest);
int i,j;
int max=0;
int min=255;
for(i=0;i<H;i++)
{
for(j=0;j<W;j++)
{
if(grey[i*W+j]>max)
{
max=grey[i*W+j];
}
if(grey[i*W+j]<min)
{
min=grey[i*W+j];
}
}
}
float k = 255.0/(max-min);
float b = -k*min;
unsigned char *newgrey;
newgrey = new unsigned char[H * W];
for(i=0;i<H;i++)
{
for(j=0;j<W;j++)
{
newgrey[i*W+j] = (unsigned char)(k*grey[i*W+j]+b);
}
}
FILE *fnew;
fopen_s(&fnew, "E:\\Digital image process\\lab-2\\第二次上机实验材料\\变换后.raw", "wb");
if(fnew)
{
fwrite(newgrey, sizeof(unsigned char), H * W, fnew);
}
else
{
cout << "创建文件失败" << endl;
return 0;
}
fclose(fnew);
fclose(ftest);
}

56
lab-2/lab2-2.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include<iostream>
#define H 256
#define W 256
int main()
{
unsigned char* grey;
grey = new unsigned char[H * W];
FILE* fp;
fopen_s(&fp, "E:\\Digital image process\\lab-2\\第二次上机实验材料\\椒盐噪声.raw", "rb");
if (fp)
{
fread(grey, sizeof(unsigned char), H * W, fp);
fclose(fp);
}
int temph = 3, tempw = 3;
unsigned char* newgrey;
newgrey = new unsigned char[temph * tempw];
unsigned char* result;
result = new unsigned char[H * W];
int i, j, k, q, m, n;
unsigned char g;
for (i = temph / 2; i < (H - temph / 2); i++)
{
for (j = tempw / 2; j < (W - tempw / 2); j++)
{
for (k = 0; k < temph; k++)
{
for (q = 0; q < tempw; q++)
{
newgrey[k * tempw + q] = grey[(i - temph / 2 + k) * W + (j - tempw / 2 + q)];
}
}
for (m = 0; m < temph * tempw - 1; m++)
{
for (n = m; n < temph * tempw; n++)
{
if (newgrey[m] > newgrey[n])
{
g = newgrey[m];
newgrey[m] = newgrey[n];
newgrey[n] = g;
}
}
}
result[i * W + j] = newgrey[temph * tempw / 2];
}
}
FILE* pfile;
fopen_s(&pfile, "E:\\Digital image process\\lab-2\\第二次上机实验材料\\中值滤波结果.raw", "wb");
if (pfile)
{
fwrite(result, sizeof(unsigned char), H * W, pfile);
fclose(pfile);
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.