如何用c语言读取图片
#include
using namespace std;
#define Twoto1(i,j,w) i*w+j
void createimage(unsigned char *&img, int w, int h)
{img = new unsigned char[w*h];}
void delateimage(unsigned char*img)
{delete []img;}
void readimage(unsigned char*img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(&fp,fname, "rb");
if (fp == NULL){ cout << "error" << endl; return; }
size_t result;
result=fread(img , sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout << "Reading error" << endl;
return;
}
else
cout << "Reading Ok!" << endl;
fclose(fp);
}
void mobanjuanji(unsigned char image, unsigned char*image1, int w, int h, float moban[5][5])
{
for (int i = 0; i for (int j = 0; j if (iw - 3 || j>h - 3)
image1[Twoto1(i,j,w)] = 0;
else
{
float temp = 0;
for (int m = 0; m<5; m++)
for (int n = 0; n<5; n++)
{
temp += (image[Twoto1(i-2+m,j-2+n,w)] moban[m][n]);
}
if (temp>255) image1[Twoto1(i, j, w)] = 255;
else if (temp<0) image1[Twoto1(i, j, w)] = 0;
else image1[Twoto1(i, j, w)] = temp;
}
}
void saveimage(unsigned char *img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(&fp, fname, "wb");
if (fp == NULL) { cout << "error" << endl; return; }
size_t result;
result = fwrite(img, sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout << "Write error" << endl;
return;
}
else
cout << "Write Ok!" << endl;
fclose(fp);
}
void main()
{
unsigned char *img;
unsigned char *img1;
float moban[5][5] = { {0,0,0,0,0},{0, -1, 0, 1, 0 }, { 0, -2, 0, 2, 0 }, { 0, -1, 0, 1, 0 }, { 0,0,0,0,0 } };
//float moban[5][5] = { 0 };
int w = 512, h = 512;
createimage(img, w, h);
createimage(img1, w, h);
readimage(img, w, h, "E:\ss.raw");
mobanjuanji(img, img1,w, h, moban);
saveimage(img, w, h, "E:\ss_1.raw");
saveimage(img1, w, h, "E:\ss_2.raw");
delateimage(img);
delateimage(img1);
}
扩展资料
C语言实现一个图片的读出和写入
#include <stdlib.h>
#include <windows.h>
int file_size(char* filename)//获取文件名为filename的文件大小。
{
FILE *fp = fopen(filename, "rb");//打开文件。
int size;
if(fp == NULL) // 打开文件失败
return -1;
fseek(fp, 0, SEEK_END);//定位文件指针到文件尾。
size=ftell(fp);//获取文件指针偏移量,即文件大小。
fclose(fp);//关闭文件。
return size;
}
int main ()
{
int size=0;
size=file_size("qw");
printf("%d\n",size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen("qw","r");
pFile = fopen ( "qwe" , "wb" );
printf("%d==\n",pFile);
printf("%d\n",size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename("qwe","Groot.jpg");
return 0;
}
c语言读取图片原理:通过文件流的方式读入到Byte的二进制数组中,之后,使用图像分析算法将图像显示到屏幕上,要将数组中的值转换为像素。
参考代码如下:
//function definition
void ImageRead(AnsiString name,int &width,int &height,int *r,int *g,int *b)
{
//read image
FILE *fp;
if((fp=fopen(name.c_str(),"rb"))==NULL) {
printf("cannot open bmp.name\n");
return ;
}
fread(&bfType,sizeof(WORD),1,fp);
if(bfType!=0x4d42) {//该值必需是0x4D42,也就是字符'BM'
printf("the input map is not bmp type");
return ;
}
fread(&bfSize,sizeof(DWORD),1,fp);
fread(&bfReserved1,sizeof(WORD),1,fp);
fread(&bfReserved2,sizeof(WORD),1,fp);
fread(&bfOffBits,sizeof(DWORD),1,fp);
fread(&bih,sizeof(BITMAPINFOHEADER),1,fp);
width=bih.biWidth ;
height=bih.biHeight;
if(width % 4 !=0)
width=width+ (4-width % 4);
DWORD size=width*(bih.biBitCount/8)*height;
unsigned char* pData=new unsigned char[size];
fread(pData,size,1,fp);
int bmWidthBytes=width*bih.biBitCount /8;
int bmBitsPixel=bih.biBitCount ;
int nBit=bmBitsPixel/8;
int z;
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
z=height-y-1;
*(r+(y*width+x))=pData[x*nBit+2+z*bmWidthBytes];
*(g+(y*width+x))=pData[x*nBit+1+z*bmWidthBytes];
*(b+(y*width+x))=pData[x*nBit+z*bmWidthBytes];
}
}
}
void ImageWrite(AnsiString name1,int *r,int *g,int *b)
{
//read image
FILE *fw;
fw=fopen(name1.c_str(),"wb");
fwrite(&bfType,sizeof(WORD),1,fw);
fwrite(&bfSize,sizeof(DWORD),1,fw);
fwrite(&bfReserved1,sizeof(WORD),1,fw);
fwrite(&bfReserved2,sizeof(WORD),1,fw);
fwrite(&bfOffBits,sizeof(DWORD),1,fw);
fwrite(&bih,sizeof(BITMAPINFOHEADER),1,fw);
int width=bih.biWidth ;
int height=bih.biHeight;
if(width % 4 !=0)
width=width+ (4-width % 4);
DWORD size=width*(bih.biBitCount/8)*height;
unsigned char* pData=new unsigned char[size];
int bmWidthBytes=width*bih.biBitCount /8;
int bmBitsPixel=bih.biBitCount ;
int nBit=bmBitsPixel/8;
int z;
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
z=height-y-1;
pData[x*nBit+2+z*bmWidthBytes]=*(r+(y*width+x));
pData[x*nBit+1+z*bmWidthBytes]=*(g+(y*width+x));
pData[x*nBit+z*bmWidthBytes]=*(b+(y*width+x));
}
}
fwrite(pData,size,1,fw);
}
广告 您可能关注的内容 |