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