| 查看: 3351 | 回復(fù): 11 | |||
afgh2587849木蟲 (小有名氣)
|
[交流]
【求助】matlab如何只讀取圖像的一部分? 已有7人參與
|
||
| 用matlab處理圖像的時候遇到了一個問題,圖像太大而且數(shù)目比較多,但感興趣的只是圖像中的一部分,現(xiàn)在想做的就是比如說對1024*1024的圖像只讀取200*200的這一小部分,不是先把整幅圖像讀進(jìn)來然后再取矩陣的一部分,而是一開始就只讀這一小部分圖像,請問該如何操作? |
木蟲 (小有名氣)
|
你參考下rawread函數(shù)。 function [X,map] = rawread(filename,n,m); % RAWREAD Read a Portable Bitmap file, or a raw file. % RAWREAD('imagefile.raw', xsize, ysize) reads a "raw" image file % RAWREAD('imagefile.pgm') reads a "pgm" (portable gray map) image % [X,map] = RAWREAD('imagefile.raw') returns both the image and a % color map, so that % [X,map] = rawread('imagefile.raw',sx,sy); % or [X,map] = rawread('imagefile.pgm'); % image(X) % colormap(map) % will display the result with the proper colors. % % NOTE : map is optional and could be replaced during the display by % the "colormap('gray')" command % % See also IMWRITE, IMREAD, IMAGE, COLORMAP. dot = max(find(filename == '.')); suffix = filename(dot+1:dot+3); % 提取圖像文件后綴 if strcmp(suffix,'pgm') | strcmp(suffix,'raw') % 要求是pgm格式或者raw格式 disp(sprintf('\nopens %s file\n',filename)); fp = fopen(filename,'rb','b'); % "Big-endian" byte order. if (fp<0) error(['Cannot open ' filename '.']); end if strcmp(suffix,'pgm') % Read and crack the header head = fread(fp,2,'uchar'); % pgm magic number : P5 if ~strcmp(head,'P5'), fprintf(1,'\n Magic Number : %s\n',head); % 默認(rèn)打印到窗口 else fprintf(1,'\n Bad Magic Number : %s\n',head); error('cannot continue this way, good bye cruel world'); end c = fread(fp,1,'uchar'); %reads the carriage return separating P5 from the creator precreator = fread(fp,1,'uchar'); % look for a '#' character preceeding a creator signature if precreator == '#', c = setstr(20); % any character except carriage return cr = setstr(10); % defines a carriage return while c ~= cr, c = fread(fp,1,'uchar'); creator = [creator,c]; end; fprintf(1,'\n creator : %s\n',creator); else fprintf('\n No creator signature\n'); fseek(fp,-1,'cof'); % return one char before end end if nargin <2, if strcmp(suffix,'raw') % assume image size is 256x256 disp('RAW file without size : assume image size is 256x256'); n = 256; m = 256; else % for PGM files % reads the size and depth % pgm格式開頭包括維數(shù) disp(' reads sizes'); n = fscanf(fp,'%d',1); tn = num2str(n); disp([' xsize = ' tn]); m = fscanf(fp,'%d',1); tm = num2str(m); disp([' ysize = ' tm]); p = fscanf(fp,'%d',1); tp = num2str(p); disp([' depth = ' tp]); c = fread(fp,1,'uchar'); %reads the last carriage return end; end % Creates a gray palette and scale it to [0,1]. disp(' create gray palette'); % 灰度映射表 for i=1:256, map(i,[1:3])=[i/256,i/256,i/256]; end; % Read the image disp(' Reads image data ...'); [X,l] = fread(fp,[n,m],'uchar'); % 維數(shù)錯了 if l ~= m*n l error('HSI image file is wrong length') end % Image elements are colormap indices, so start at 1. X = X'+1; fclose(fp); disp('end'); else error('Image file name must end in ''raw'' or ''pgm''.') end |
木蟲 (小有名氣)
新蟲 (初入文壇)
木蟲 (小有名氣)

木蟲 (小有名氣)
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 一志愿中南大學(xué)化學(xué)0703總分337求調(diào)劑 +3 | niko- 2026-03-27 | 3/150 |
|
|---|---|---|---|---|
|
[考研] 085600 286分 材料求調(diào)劑 +5 | 麻辣魷魚 2026-03-27 | 6/300 |
|
|
[考研] 求調(diào)劑 +6 | 白QF 2026-03-21 | 7/350 |
|
|
[有機(jī)交流]
高溫高壓反應(yīng)求助
10+4
|
chibby 2026-03-25 | 4/200 |
|
|
[考研] 一志愿南師大0703化學(xué) 275求調(diào)劑 +4 | Ripcord上岸 2026-03-27 | 4/200 |
|
|
[考研] 085600材料與化工調(diào)劑 +10 | A-哆啦Z夢 2026-03-23 | 16/800 |
|
|
[考研] 322求調(diào)劑 +4 | 我真的很想學(xué)習(xí) 2026-03-23 | 4/200 |
|
|
[考研] 292求調(diào)劑 +4 | 求求了收下我吧?/a> 2026-03-26 | 4/200 |
|
|
[考研] 329求調(diào)劑 +7 | 鈕恩雪 2026-03-25 | 7/350 |
|
|
[考研] 一志愿華理,數(shù)一英一285求A區(qū)調(diào)劑 +8 | AZMK 2026-03-25 | 10/500 |
|
|
[考研] 一志愿北化求調(diào)劑 +3 | Jsman 2026-03-22 | 3/150 |
|
|
[考研] 321求調(diào)劑 +5 | 材料cailiao 2026-03-21 | 5/250 |
|
|
[考研] 086000生物與醫(yī)藥292求調(diào)劑 +6 | 小小陳小小 2026-03-22 | 9/450 |
|
|
[考研] 一志愿上海交大生物與醫(yī)藥專碩324分,求調(diào)劑 +6 | jiajunX 2026-03-22 | 6/300 |
|
|
[考研] 334分 一志愿武理-080500 材料求調(diào)劑 +4 | 李李不服輸 2026-03-25 | 4/200 |
|
|
[考研] 網(wǎng)絡(luò)空間安全0839招調(diào)劑 +4 | w320357296 2026-03-25 | 6/300 |
|
|
[考研] 化工專碩求調(diào)劑 +3 | question挽風(fēng) 2026-03-24 | 3/150 |
|
|
[考研] 306求0703調(diào)劑一志愿華中師范 +10 | 紙魚ly 2026-03-21 | 11/550 |
|
|
[考研] 求調(diào)劑 +6 | 研研,接電話 2026-03-24 | 7/350 |
|
|
[考研] 361求調(diào)劑 +3 | Glack 2026-03-22 | 3/150 |
|