在MATLAB中将位图图像加速保存为位图图像

时间:2009-07-21 14:20:36

标签: image matlab performance

我的计算机上的图像文件夹中有bmp图像。我将它从1.bmp命名为100.bmp。所有尺寸均为576 * 768

我逐一阅读这些数字。我从所有数百幅图像中选择矩形区域。矩形区域的像素坐标垂直从182变为281,水平变化426到639.我保存了代表图像之间像素值交换的图形。矩形区域中的所有像素坐标到另一个文件

我的文件如下:

pixvalue=zeros(100);
j=1 ;% will represent pixel coordinate sequence 
%  find pizel coordinates in rectangular region 
 for y=182:281
       for x=426:639
           for i=1:100
          % read images
        s = sprintf('C:\\images\\%d.bmp', i);  
        A = imread(s);
        A=double(A);
        pixvalue(i)= A(y,x);
        end
       s2=sprintf('%d.plot', j);
      f=figure('visible','off'),
      plot(pixvalue);
      xlabel('images');
      ylabel('pixvalue');
      title(s2);
      s3=sprintf('C:\\registration\\%d.bmp', j);

      %% save figures as 8 bit bitmap to file
      print(f,'-dbmp256',s3);
      j=j+1;
       end
   end

不幸的是,这段代码一直在努力工作!我怎样才能加速呢?

最诚挚的问候......

3 个答案:

答案 0 :(得分:1)

你正在阅读相同的图像(281-182)*(639-426)次..

也许你应该在这个循环之前读取所有图像一次。将它存储在某个变量中。

比你应该做的那样......

类似的东西:

for i=1:100
    % read images
    s = sprintf('C:\\images\\%d.bmp', i);  
    A(i) = imread(s);
end

for x=...
    for y=...
        for i=1:100
            pixvalue(i)= A(i, y, x);
        end
    end

        ..
        ..

实际上我不太记得matlab语法,但你必须在这个大循环之前在一个循环中读取所有图像。在这里我更正了代码。

比在大循环中使用A(i)而不是A.

PS。顺便说一句,我优化它,好像前面的代码工作..我现在没有matlab尝试..

答案 1 :(得分:1)

您的代码可以分为两部分。首先,您要加载图像数据并从每个图像的子区域保存像素值。这可以使用以下代码完成:

subRegion = zeros(100,214,100);  % 3-D matrix to store image subregions
for i = 1:100,
  s = ['C:\images\' int2str(i) '.bmp'];      % Create file name string
  A = double(imread(s));  % Load image and convert to double precision
  subRegion(:,:,i) = A(182:281,426:639);    % Store subregion of image
end

接下来,您似乎想要绘制所有图像中每个像素的值,并将绘图输出到文件。这是很多的图像文件(21,400!),并且运行需要一段时间。如果您确定要这样做,可以采用以下方法:

j = 1;  % Pixel index
for y = 1:100,  % Loop over rows
  for x = 1:214,  % Loop over columns
    hFigure = figure('Visible','off');
    data = subRegion(y,x,:);  % Get pixel value from all 100 images
    plot(data(:));
    xlabel('images');
    ylabel('pixvalue');
    title(['Plot ' int2str(j)]);
    outFile = ['C:\registration\' int2str(j) '.bmp'];
    print(hFigure,'-dbmp256',outFile);  % Save figure
    j = j+1;
  end
end

答案 2 :(得分:1)

我制作了一个关于如何处理目录中文件子集的视频。这应该涵盖关于循环目录的部分。

http://blogs.mathworks.com/videos/2008/02/26/matlab-basics-getting-a-directory-listing/