1. 求助c语言读取灰度图像并转换为二维数组完整代码
1、步骤大概这样第一步:读取图像数据到内存第二步:读取文件头第三步:读取信息头第四步:读取图像矩阵到二维数组2、例程:
FileName=fileDlg.GetFileName ();FILE *fp=fopen(FileName,"rb");//二进制读方式打开指定的图像文件fread(&FileHead, sizeof(BITMAPFILEHEADER), 1,fp); //读取文件头,文件指针自动后移fread(&InfoHead, sizeof(BITMAPINFOHEADER), 1,fp);//读取信息头,文件指针自动后移//获取图像宽、高、每像素所占位数等信息bmpWidth = InfoHead.biWidth;bmpHeight = InfoHead.biHeight;//下面完成图像数据向内存数组的存储ImageData=new unsigned char*[bmpHeight];if(InfoHead.biBitCount==24){for (int i=0;i<bmpHeight;i++){ImageData[i]=new unsigned char[(bmpWidth*3+3)/4*4];}for (int k=0;k<bmpHeight;k++ ){for(int j=0;j<(bmpWidth*3+3)/4*4;j++){fread(&ImageData[k][j],1,1,fp);//上面完成动态二维数组的申请,这里实际读取图像数据}}fclose(fp);//关闭文件
2. C语言里 灰度图像怎么转换为二维数组
1、步骤大概这样
第一步:读取图像数据到内存
第二步:读取文件头
第三步:读取信息头
第四步:读取图像矩阵到二维数组
2、例程:
FileName=fileDlg.GetFileName();
FILE*fp=fopen(FileName,"rb");//二进制读方式打开指定的图像文件
fread(&FileHead,sizeof(BITMAPFILEHEADER),1,fp);//读取文件头,文件指针自动后移
fread(&InfoHead,sizeof(BITMAPINFOHEADER),1,fp);//读取信息头,文件指针自动后移
//获取图像宽、高、每像素所占位数等信息
bmpWidth=InfoHead.biWidth;
bmpHeight=InfoHead.biHeight;
//下面完成图像数据向内存数组的存储
ImageData=newunsignedchar*[bmpHeight];
if(InfoHead.biBitCount==24)
{
for(inti=0;i<bmpHeight;i++)
{
ImageData[i]=newunsignedchar[(bmpWidth*3+3)/4*4];
}
for(intk=0;k<bmpHeight;k++)
{
for(intj=0;j<(bmpWidth*3+3)/4*4;j++)
{
fread(&ImageData[k][j],1,1,fp);//上面完成动态二维数组的申请,这里实际读取图像数据
}
}
fclose(fp);//关闭文件
3. 如何用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);
}
(3)c语言读取图片矩阵扩展阅读
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 ",size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen("qw","r");
pFile = fopen ( "qwe" , "wb" );
printf("%d==
",pFile);
printf("%d ",size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename("qwe","Groot.jpg");
return 0;
}
4. C程序:从文件中读取矩阵数据,并显示出来,利用链式存储结构。
md.row;
p->
return
sm;
}
}
sm->,col,列数,数据这样存储的。
你这个不会是稀疏矩阵吧!
typedef
struct
{
int
row,矩阵每个维度的大小自然先读出来.d
=sm->
for(i=0;md=sm->
p->md;head.md;
typedef
struct
strucSparseMat
{
int
m;head;
p=p->
return
md;
}
读矩阵,如果储存了;count=i;head.md;p,如果数据个数也储存了就最好了。
SparseMat
*readMat(FILE
*fp,fp);next->next
=NULL;next=p->pre=NULL,fp),sizeof(n);
MatNode
head.d;next=(PMatNode)malloc(sizeof(MatNode));
p->head.md){
break;
}
else
{
p->,n;
int
count,fp);
p->md;
fread(&n,1,sizeof(n),fp);
fread(&count.col;
typedef
struct
struMatNode{
matdata
md;//
}SparseMat;i++)
{
if(!readdata(fp,sm->!fp)return
NULL;
if(feof(fp)
&&
;
p=&sm->head;md.col=sm->!md)return
NULL;
fread(md,1,sizeof(md);head.md,*PMatNode;p->.row=sm->head;
if(feof(fp))return
NULL;
if(,
SparseMat
*sm)
{
int
i;
PMatNode
p;
if(!fp)return
NULL;next;
//&sp->
}MatNode,要看情况了;
double
d;
}matdata,sizeof(count),1;//i<count
&&
!feof(fp);next->pre=p;
p->,1、列号、数据还可以这样存储矩阵!
一般都是行数;
p->!sm)return
NULL;
fread(&m;
struMatNode
*pre,*nxt;
matdata
*readdata(FILE
*fp,matdata
*md){
if(啥时候矩阵,开始流行用链表做了!
行号
5. c语言如何从文件中读入矩阵,存入二维数组
#include<iostream>
using namespace std;
int mat[101][101];
int main()
{
int n,m;//行,列...
int i,j;
freopen("D:\\in.txt","r",stdin);//读文件...
cin>>n>>m;//读入矩阵行数,列数...
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>mat[i][j];
return 0;
}
6. 《C语言》中如何从文件读取矩阵
确定文件名。
打开文件,使用fopen函数。fopen("文件名", “r”)。
根据约定的文件格式,包括文件中矩阵规模,元素的类型,以及元素分隔的符号,采用fscanf函数循环读入矩阵。
判断文件是否读完,如未读完,重复第三步直到读完。
关闭文件。
7. 怎样用C语言将png图像读入数组并显示
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 ;
8. C语言如何从文本读取矩阵
文件打开后先读第一行两个数字,比如是
int row,col,i,j;
FILE *fp1 = fopen(filepath1, "r");
if (fp1 == NULL)
{printf("Can not open file!\n");return 0;}
fscanf(fp1, "%d%d",&row,&col);
然后根据这个去声明数组和确定循环
int A[row][col];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
fscanf(fp1, "%d",&A[i][j]);
9. 请问如何使用纯C语言读取文件中的图片,并将图片存储在二维数组中
1、使用双层循环语句,就可以依次把数据顺序读入到一个二维数组当中了。2、例程:#include#include#defineMAXLINE3#defineMAXCOLUMN10voidmain(void){FILE*fp;//文件指针chararr[MAXLINE][MAXCOLUMN]={0};//定义3行10列的二维数组并初始化inti=-1;if((fp=fopen("./test/filename.txt","r"))==NULL){//打开txt文件perror("Fileopenerror!\n");return;}while((fgets(arr[++i],MAXCOLUMN+1,fp))!=NULL)//读取一行并存到arr数组printf("%d:",i);//打印行号//puts(arr[i]);char*subarr=strtok(arr[i],"");//以空格为分隔符从arr[i]中获得字串while(subarr!=NULL){data[i][j]=atoi(subarr);//将字串转为int型数据存入data数组printf("%d\t",data[i][j]);//打印data[i][jsubarr=strtok(NULL,"");//继续获得arr[i]中的字串j++;//data数组列加一}printf("\n");}//循环完毕后,所有数据已在data数组中printf("\n");fclose(fp);//关闭指针}
10. C语言 从文件读取矩阵
void Read(void)
{
FILE *fp;
int i,j;
char s[MAX],ch;
if((fp=fopen("1.txt","r"))==NULL)
{
printf("can not open this file!\n");
exit(1);
}
row=0;
while(fgets(s,MAX,fp)!=NULL)//读行数
row++;
rewind(fp);//回文件起始位置
col=0;//读列数
while(ch!='\n')//(ch=fgetc(fp))!='\n'&&(ch=fgetc(fp))!='\r'
{
if(ch==' ')
col++;
ch=fgetc(fp);
}
col++;//补上最后一列因为最后一列后面没有空格
jz=(int**)malloc(row*sizeof(int*));//现在开始读数据到矩阵 先生成动态二维数组
for(i=0;i<row;i++)
jz[i]=(int*)malloc(col*sizeof(int));
rewind(fp);
for(i=0;i<row;i++)//矩阵读入数据
for(j=0;j<col;j++)
fscanf(fp,"%d",&jz[i][j]);
printf("文件中矩阵:\n");
for(i=0;i<row;i++)//显示矩阵
for(j=0;j<col;j++)
{
printf("%3d",jz[i][j]);
if(j+1==col)
printf("\n");
}
fclose(fp);
}//