实验2代码

This commit is contained in:
2026-04-10 11:10:36 +08:00
Unverified
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);
}