MATLAB Answers

0

How can I write an image adapter class for a .bmp image?

I'm trying to develop an image adapter class to accommodate some very large images with blockproc. I've tested the rest of my algorithms with a .tiff image and everything worked as expected but when trying to work with a .bmp file (what will be required for my project) I was unable to successfully write an image adapter class.

Here is the image adapter class that I've written and attempted to use.

classdef bmpAdapter < ImageAdapter
    properties
        Filename
        Info
    end
    methods
        function obj = bmpAdapter(filename)
            obj.Filename = filename;
            obj.Info = imfinfo(filename);
            obj.ImageSize = [obj.Info.Height obj.Info.Width];
        end
        function result = readRegion(obj, start, count)
            result = imread(obj.Filename,'Index', [1 2 3], ...
                'Info',obj.Info,'PixelRegion', ...
                {[start(1), start(1) + count(1) - 1], ...
                [start(2), start(2) + count(2) - 1]});
        end
        function result = close(obj) %#ok
        end
    end
end

While using this adapter class with blockproc I'm receiving the following error messages:

Error encountered while executing the readRegion method of the bmpAdapter
class.  The input arguments were:
  region_start: [1  1]
  region_size : [316  348]
The cause of the error was:
Error using imagesci\private\readbmp
Too many input arguments.
Error in imread (line 434)
    [X, map] = feval(fmt_s.read, filename, extraArgs{:});
Error in bmpAdapter/readRegion (line 13)
            result = imread(obj.Filename,'Index', [1 2 3], ...
Error in internal.images.AdapterDispatcher/readRegion (line 81)
                data = obj.Adapter.readRegion(region_start, region_size);
Error in blockproc>getBlock (line 788)
    source_data = source.readRegion(...
Error in blockproc (line 282)
block_struct = getBlock(source_dispatcher,block_struct,1,1,options);

I've gone through the following tutorial but haven't had much luck. http://blogs.mathworks.com/steve/2011/09/23/dealing-with-really-big-images-image-adapters/

Based on the error messages that I'm receiving my method is properly determining the starting positions to read the image data. Any help would be greatly appreciated.

Thanks,

Chris

  0 Comments

1 Answer

Answer by Walter Roberson
on 19 May 2013
 Accepted answer

Those Name/Value pairs are defined for TIFF only. BMP is not defined with any Name/Value pairs, so you will need to strip them out before calling imread()

  2 Comments

Note: this does mean that if you want to read portions of a large BMP, then you might need to add complete BMP file format processing code, possibly written in another language.

I removed the Name/Value pairs and am now running into the issue of reading the entire image, thus generating an error. So basically I won't be able to use the imread() function. Does MATLAB provide any other means of reading the specific pixel information or will I have to write a separate function for reading the image information myself?


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!