❶ 如何用Matlab读入并显示图片文件
imMatrix = imread('name.jpg')%jpg也可以bmp,图片格式
MATLAB图像处理工具箱支持四种基本图像类型:索引图像、灰度图像、二进制图像和RGB图像。MATLAB直接从图像文件中读取的图像为RGB图像。
它存储在三维数组中。这个三维数组有三个面,依次对应于红(Red)、绿(Green)、蓝(Blue)三种颜色,而面中的数据则分别是这三种颜色的强度
值,面中的元素对应于图像中的像素点。设所得矩阵为X三维矩阵(256,256,3) ,X(:,:,1)代表红颜色的2维矩阵
X(:,:,2)代表绿颜色的2维矩阵,
X(:,:,3)代表兰颜色的2维矩阵。[X, map]=imread('34.bmp');r=double(X(:,:,1)); %r是256
x 256的红色信息矩阵g=double(X(:,:,2)); %g是256 x 256的绿色信息矩阵b=double(X(:,:,3));
%b是256 x 256的兰色信息矩阵
索引图像数据包括图像矩阵X与颜色图数组map,其中颜色图map是按图像中颜色值进行排序后的数组。对于每个像素,图像矩阵X包含一个值,这个值就是颜
色图数组map中的索引。颜色图map为m×3双精度矩阵,各行分别指定红、绿、蓝(R、G、B)单色值,map=[RGB],R、G、B为值域为
[0,1]的实数值,m为索引图像包含的像素个数。
对于相同的数据,采用uint8格式比双精度格式节省内存空间,从而更经济。在MATLAB中
如果索引图像的颜色图小于256行,则它的图像矩阵以uint8格式存储,否则以双精度格式存储。
一:imread:从图像文件夹中读取图像。
A = imread(FILENAME,FMT) 读取图像到A,如果文件是包含一灰度图像,A是一二维矩阵,如果文件是包含一真彩色图像(RGB),A是一三维矩阵(M-by-N-by-3)。FILENAME :图像文件名;FMT:图像文件格式;
文件必须在当前目录下,或在Matlab的一路径上。如果 imread不能够找到一名称为FILENAME的文件,那么它将找一名为FILENAME.FMT的文件
[X,MAP] = imread(FILENAME,FMT) 把图像FILENAME读入与它相关的图像色彩信息写入MAP,图像色彩信息值在范围[0,1]中自动地重新调整.
[...] = imread(FILENAME)这种方式是试图得到文件的格式从文件所包含的信息。
[...] = imread(URL,...)从一Internet URL上读图像 URL 必须包含协议(即: "http://").
1.2数据类型:
TIFF的特殊语法:
[...] = imread(...,IDX)
从很多图像TIFF文件中读一个图像;IDX是一个整数值,它显示了所读图像在文件中的顺序,例如:如果 IDX是 3,
imread将读文件中的第三个图像。 如果省略了这个变量, imread将读文件中的第一个图像.
IMREAD支持的图像文件格式:JPEG TIFF GIF BMP PNG HDF PCX XWD ICO CUR RAS PBM PGM PPM
相关信息也可在Matlab中查看: imfinfo, imwrite, imformats, fread,
二:imwrite输出图像
imwrite(A,FILENAME,FMT) 把图像 A 写入图像文件 FILENAME.
imwrite(X,MAP,FILENAME,FMT) 把 X和它的相关色彩信息MAP写入FILENAME.
imwrite(...,FILENAME) 把图像写入图像文件FILENAME,并推测可能的格式用来做filename的扩展名。扩展名必须是FMT中一合法名.
imwrite(...,PARAM1,VAL1,PARAM2,VAL2,...) 不同的参数控制输出文件的各种不同特征。参数要是当前所支持的HDF,JPEG, TIFF, PNG, PBM, PGM, 和PPM 文件
三:image 显示图像.image(C) 把矩阵 C 转成一图像. C 可以是一MxN 或 MxNx3维的矩阵,且可以是包含 double,
uint8,或 uint16 数据.image是用来显示附标图像,即显示的图像上有x,y坐标轴的显示,可以看到图像的像素大小。但可以加上axis
off命令即可把坐标去掉。
imshow只是显示图像。用colormap来定义图像显示用的颜色查找表,比如用colormap(pink),可以把黑白图像显示成带粉红色的图像。
图像像素矩阵的数据类型:(1)显示真彩色图像像素三维矩阵X,如果是uint8类型,要求矩阵的数据范围为0-255,(2)如果是double型,则其数据范围为0-1,要不就会出错或者出现空白页。
类型转换:(1)如果你原来的数值是uint8,在运算中转换为double后,实际要显示的数值没有改变的话,只要用uint8(X)就可转换为
uint8型,如果不想转换频繁,也可在显示时用X/255来转换为符合0-1double类型范围要求的数值显示。(2)如果显示索引图像(二维矩
阵),如果索引图像像素数值是double型,则它的取值范围为1-length(colormap),数值起点为1,则矩阵中数值为1的对应
colormap中第一行数据,如果索引图像像素数值是uint8,则取值范围为0-255,数值起点为0,则矩阵中数值为0的对应colormap中第
一行数据,所以索引图像这两个数据类型之间的转换,要考虑到+1或-1。直接用uint8或double转换则会查找移位,产生失真情况。uint16数
据类型与uint8类似,取值范围为0-65536。
四:其它常用图像操作:
图像显示于屏幕有imshow( ), image( )函数;
图像进行裁剪imcrop( );
图像的插值缩放imresize( )函数实现;
旋转用 imrotate( )实现。
五:具体的操作
下面通过运用图像处理工具箱中的有关函数对下图(nice.bmp)进行一些变换。见后面的transfer.m内容!
变换前图片:(nice.bmp)
变换后所得图片:newpic.bmp
例,在电脑F\picture下有一彩色图像文件nice.bmp,则可由下述语句读取:
下面是对图像 nice.bmp以y轴为对称轴所做的一个对称变换。
% Transfer1.m
clear all
figure
[x,map]=imread('F:\picture\nice.bmp');% 所得x为一375x420x3的矩阵
[w1,w2,w3]=size(x); % 375 X 420
w22=floor(w2/2);
image(x); %显示出图像
title('HELLO! @This is the first pose of me')%则显示出图像nice.bmp
axis off; % 去掉图像中的坐标
colormap(map); % colormap(),图像查找表函数。函数结构为colormap(map),设置当前的图像查找表到map。
imwrite(x,map,'nice.bmp')
for i=1:w1
for j=1:w22 % 图像关于y轴对折
t=x(i,j);
x(i,j)=x(i,w2-j+1);
x(i,w2-j+1)=t;
end
end
figure
image(x);
axis off
title('HELLO!!@@ Can you find any difference of my two picture! ') colormap(map);
imwrite(x,map,'newpic.bmp') %把x写到nepic2.bmpz中去
% Transfer1.m文件中包含了最基本也是最常用的对读像处理的命令。
在对图像处理的整个过程中,实质上是对[x,map]=imread(‘figure')函数中所得x矩阵的各种变换!
❷ c语言,怎样读取一个BMP图片
#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改变图像亮度
typedef struct BMP
{
//14字节
unsigned short bfType; //文件标识 2字节 必须为BM
unsigned int bfSize; //文件大小 4字节
unsigned short bfReserved1; //保留,每字节以"00"填写 2字节
unsigned short bfReserved2; //同上 2字节
unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节
//40字节
unsigned int biSize; //表示本结构的大小 4字节
int biWidth; //位图的宽度 4字节
int biHeight; //位图的高度 4字节
unsigned short biPlanes; //永远为1 , 2字节
unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节
unsigned int biCompression; //压缩说明 4字节
unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节
int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节
int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节
unsigned int biClrUsed; //位图使用的颜色索引数 4字节
unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节
} BMP;
int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif
//image_rw.c文件
#include<stdio.h>
#include<stdlib.h>
#include"image.h"
void image_info(FILE *file)
{
int times=3; //输入文件名次数。
char bmp_name[10]; //文件名
printf("\nplease enter a file name for reading:");
do
{
if (times<3)
{
printf("\nplease enter a file name for reading again:");
}
fflush(stdin);
gets(bmp_name);
//printf("\n%s",bmp_name);
file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for reading! ",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//读取图像信息
fseek(file,0L,0); //读取图像文件类型
fread(&bmp,sizeof(BMP),1,file);
printf("\n bmp tpye: %u",bmp.bfType);
printf("\n bmp size: %u",bmp.bfSize);
printf("\n bmp reserved1: %u",bmp.bfReserved1);
printf("\n bmp reserved2: %u",bmp.bfReserved2);
printf("\n bmp offBits: %u",bmp.bfOffBits);
printf("\n bmp bisize: %u",bmp.biSize);
printf("\n bmp biWidth: %d",bmp.biWidth);
printf("\n bmp biHeight: %d",bmp.biHeight);
printf("\n bmp biplans: %u",bmp.biPlanes);
printf("\n bmp biBitCount: %u",bmp.biBitCount);
printf("\n bmp biCompression: %u",bmp.biCompression);
printf("\n bmp biSizeImage: %u",bmp.biSizeImage);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
printf("\n bmp biClrUsed: %u",bmp.biClrUsed);
printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);
line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数
//printf("dfsa%u",bmp.line_byte);
//bmp.imagedata=NULL;
imagedata=(unsigned char*)malloc(bmp.biSizeImage);
fseek(file,(long)bmp.bfOffBits,0);
fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//保存图像
void image_save(FILE *file)
{
int times=3; //输入文件名次数。
char bmp_name[10]; //文件名
//int i; //记录数据区个数
printf("\nplease enter a file name for writeing:");
do
{
if (times<3)
{
printf("\nplease enter a file name for writeing again:");
}
fflush(stdin);
gets(bmp_name);
printf("\n%s",bmp_name);
file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for writing",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//写文件头
printf("\n%s",bmp_name);
fseek(file,0L,0); //图像文件类型
fwrite(&(bmp.bfType),sizeof(short),1,file);
printf("\n bmp tpye: %d",bmp.bfType);
fseek(file,2L,0); //图像文件大小
fwrite(&(bmp.bfSize),sizeof(int),1,file);
printf("\n bmp size: %d",bmp.bfSize);
fseek(file,6L,0); //图像文件保留字1
fwrite(&(bmp.bfReserved1),sizeof(short),1,file);
printf("\n bmp reserved1: %d",bmp.bfReserved1);
fseek(file,8L,0); //图像文件保留字2
fwrite(&(bmp.bfReserved2),sizeof(short),1,file);
printf("\n bmp reserved2: %d",bmp.bfReserved2);
fseek(file,10L,0);//数据区的偏移量
fwrite(&(bmp.bfOffBits),sizeof(short),1,file);
printf("\n bmp offBits: %d",bmp.bfOffBits);
fseek(file,14L,0);//文件头结构大小
fwrite(&(bmp.biSize),sizeof(int),1,file);
printf("\n bmp bisize: %d",bmp.biSize);
fseek(file,18L,0);//图像的宽度
fwrite(&(bmp.biWidth),sizeof(int),1,file);
printf("\n bmp biWidth: %d",bmp.biWidth);
fseek(file,22L,0);//图像的高度
fwrite(&(bmp.biHeight),sizeof(int),1,file);
printf("\n bmp biHeight: %d",bmp.biHeight);
fseek(file,24L,0);//图像的面数
fwrite(&(bmp.biPlanes),sizeof(short),1,file);
printf("\n bmp biplans: %d",bmp.biPlanes);
fseek(file,28L,0);//图像一个像素的字节数
fwrite(&(bmp.biBitCount),sizeof(short),1,file);
printf("\n bmp biBitCount: %d",bmp.biBitCount);
fseek(file,30L,0);//图像压缩信息
fwrite(&(bmp.biCompression),sizeof(short),1,file);
printf("\n bmp biCompression: %d",bmp.biCompression);
fseek(file,34L,0);//图像数据区的大小
fwrite(&(bmp.biSizeImage),sizeof(int),1,file);
printf("\n bmp biSizeImage: %d",bmp.biSizeImage);
fseek(file,38L,0);//水平分辨率
fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
fseek(file,42L,0);//垂直分辨率
fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
fseek(file,46L,0);//颜色索引数
fwrite(&(bmp.biClrUsed),sizeof(int),1,file);
printf("\n bmp biClrUsed: %d",bmp.biClrUsed);
fseek(file,50L,0);//重要颜色索引数
fwrite(&(bmp.biClrImportant),sizeof(int),1,file);
printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);
fseek(file,(long)(bmp.bfOffBits),0);
fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//pixProcess.c文件
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"image.h"
//灰度化
void image_gray()
{
int i,j;
unsigned char tmp;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));
imagedata[i*line_byte+j*3+0]=tmp;
imagedata[i*line_byte+j*3+1]=tmp;
imagedata[i*line_byte+j*3+2]=tmp;
//printf("\nnidsfh%d %d",i,j);
}
}
}
//二值化
void image_binarization()
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if ((*(imagedata+i*line_byte+j))<128)
{
imagedata[i*line_byte+j]=0;
}
else
{
imagedata[i*line_byte+j]=255;
}
}
}
}
void image_opposite() //反相
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);
}
}
}
void image_channel() //抽取RGB通道
{
int i,j;
char rgb;
printf("\nplease enter a char(r/g/b): ");
fflush(stdin);
scanf("%c",&rgb);
if (rgb=='b')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j+1]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else if(rgb=='g')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+1]=0;
}
}
}
}
void image_bright()//改变图像亮度
{
int level;
int i,j;
printf("\n please enter the level of brightness[-255 to 255] :");
fflush(stdin);
scanf("%d",&level);
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if (level>=0)
{
if ((imagedata[i*line_byte+j]+level)>255)
imagedata[i*line_byte+j]=255;
else
imagedata[i*line_byte+j]+=level;
}
else
{
if ((imagedata[i*line_byte+j]-abs(level))<0)
imagedata[i*line_byte+j]=0;
else
imagedata[i*line_byte+j]+=level;
}
}
}
}
//void image_create() //创建一幅24位BMP图像文件。
//{
//main.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include"image.h"
BMP bmp;
int main()
{
FILE *file=NULL;
int choose;
char gono;
do
{
image_info(file); //imagedata已经分配了动态内存,但是没有释放
printf("\n 1.image_opposite");
printf("\n 2.image_gray");
printf("\n 3.image_binarization");
printf("\n 4.image_channel");
printf("\n 5.image_brightness");
//printf("6.image_opposite");
//printf("7.image_opposite");
printf("\nchoose your options:");
fflush(stdin);
scanf("%d",&choose);
switch(choose)
{
case 1:
image_opposite();
image_save(file);
free(imagedata);
break;
case 2:
image_gray();
image_save(file);
free(imagedata);
break;
case 3:
image_binarization();
image_save(file);
free(imagedata);
break;
case 4:
image_channel();
image_save(file);
free(imagedata);
break;
case 5:
image_bright();
image_save(file);
free(imagedata);
break;
default:
printf("\n wrong choose!");
}
printf("\nlet's go on?(y/n):");
fflush(stdin);
scanf("%c",&gono);
if (gono=='n')
{
printf("\nbye bye!");
break;
}
}
while(1);
return 0;
}
❸ 关于matlab的imread函数
首先你用whos I命令,抄查看一下变量I的结构。
imread读取了tiff格式的图片之后,得到的是一个M x N x 4的矩阵(M,N是图片大小),这一点和读取JPG等格式的图片不同。
也就是说,可能不是imread读取过程中产生的问题,而是imshow现实过程中出现的问题。
如果你确定是imread过程中产生的问题,你可以help imread,查看一下imread的详细使用方法。
当读取tiff图片时,imread其实是有几个参数的(index,info等),你可以在文档中查看一下,如何设置这几个参数。
此外,如果你不想仔细研究一下imread和imshow对于tiff格式图片的特殊处理方法,也可以考虑先对图片格式进行转换:建议使用ImageMagick中的convert命令,当然你也可以在matlab中使用system等命令进行批量处理。
❹ 利用Matlab中的imread怎么读取图片
方法/步骤
1、在matlab软件中,读取图像数据(载入)利用的是imread函数,主要有以下4种方式:
A = imread(filename, fmt)
[X, map] = imread(...)
[...] = imread(filename)
[...] = imread(URL,...)
[...] = imread(...,Param1,Val1,Param2,Val2...)
作为初步以及最为常见的方式,采取第一种讲解。
如下图所示即为将载入的图片,图片格式(jpg) :
❺ 用c语言如何读取和保存jpg图片文件
#include <stdio.h>
#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;
}
(5)读图像文件函数是什么扩展阅读:
c语言读取TXT文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
int main()
{
char buf[MAX_LINE]; /*缓冲区*/
FILE *fp; /*文件指针*/
int len; /*行字符个数*/
if((fp = fopen("test.txt","r")) == NULL)
{
perror("fail to read");
exit (1) ;
}
while(fgets(buf,MAX_LINE,fp) != NULL)
{
len = strlen(buf);
buf[len-1] = '