❶ 如何用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] = '