Files
Digital-image-process/lab-2/lab-2.cpp
2026-04-10 11:10:36 +08:00

57 lines
1.2 KiB
C++

#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);
}