basics (Spring 2013)



info = imfinfo(filename) returns a structure whose fields contain information about an image in a graphics file. filename is a string that specifies the name of the graphics file

filename = '2013_0115_163116AA.JPG';
info = imfinfo(filename);
% which leads us to enter:


            Filename: [1x66 char]
         FileModDate: '16-Jan-2013 22:31:05'
            FileSize: 607096
              Format: 'jpg'
       FormatVersion: ''
               Width: 2048
              Height: 1536
            BitDepth: 24
           ColorType: 'truecolor'
     FormatSignature: ''
     NumberOfSamples: 3
        CodingMethod: 'Huffman'
       CodingProcess: 'Sequential'
             Comment: {}
                Make: 'FUJIFILM'
               Model: 'FinePix6900ZOOM'
         Orientation: 1
         XResolution: 72
         YResolution: 72
      ResolutionUnit: 'Inch'
            Software: 'Digital Camera FinePix6900ZOOM Ver1.00'
            DateTime: '2013:01:15 16:31:16'
    YCbCrPositioning: 'Co-sited'
           Copyright: '    '
       DigitalCamera: [1x1 struct]
       ExifThumbnail: [1x1 struct]

ans = 

                     FNumber: 2.8000
             ExposureProgram: 'Normal program'
             ISOSpeedRatings: 100
                 ExifVersion: [4x1 double]
            DateTimeOriginal: '2013:01:15 16:31:16'
           DateTimeDigitized: '2013:01:15 16:31:16'
     ComponentsConfiguration: 'YCbCr'
      CompressedBitsPerPixel: 1.5000
           ShutterSpeedValue: 6
               ApertureValue: 3
             BrightnessValue: 3.1100
           ExposureBiasValue: 0
            MaxApertureValue: 3
                MeteringMode: 'Pattern'
                       Flash: [1x131 char]
                 FocalLength: 9.7000
                   MakerNote: [238x1 double]
             FlashpixVersion: [4x1 double]
                  ColorSpace: 'sRGB'
            CPixelXDimension: 2048
            CPixelYDimension: 1536
         InteroperabilityIFD: [1x1 struct]
       FocalPlaneXResolution: 2678
       FocalPlaneYResolution: 2674
    FocalPlaneResolutionUnit: 3
               SensingMethod: 'One-chip color area sensor'
                  FileSource: 'DSC'
                   SceneType: 'A directly photographed image'

ans = 

                       Filename: [1x66 char]
                    FileModDate: '16-Jan-2013 22:31:05'
                       FileSize: 607096
                         Format: 'jpg'
                  FormatVersion: []
                          Width: 0
                         Height: 0
                       BitDepth: 0
                      ColorType: -1
                FormatSignature: []
                    Compression: 'OJPEG'
                           Make: []
                          Model: []
                    Orientation: 1
                    XResolution: 72
                    YResolution: 72
                 ResolutionUnit: 'Inch'
                       Software: []
                       DateTime: []
          JPEGInterchangeFormat: 1102
    JPEGInterchangeFormatLength: 9225
               YCbCrPositioning: 'Co-sited'
                      Copyright: []
                  DigitalCamera: []


A = imread(filename) reads a grayscale or color image from the file specified by the string filename.

rgb = imread(filename);
whos rgb
  Name         Size                  Bytes  Class    Attributes

  rgb       1536x2048x3            9437184  uint8              


imshow(RGB) displays the truecolor image RGB.

small = imresize(rgb,0.3);


I = imcrop creates an interactive Crop Image tool associated with the image displayed in the current figure, called the target image. The Crop Image tool is a moveable, resizable rectangle that you can position interactively using the mouse.

[out rect] = imcrop;
Warning: Image is too big to fit on screen; displaying at 50% 

rect =

  657.5100  497.5100  707.9800  461.9800


imwrite(A,filename) writes the image A to the file specified by filename in the format specified the file extension.


I = rgb2gray(RGB) converts the truecolor image RGB to a grayscale intensity image I. rgb2gray converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.

g = rgb2gray(out);

imhist(I) displays a histogram for the image I above a grayscale colorbar. The number of bins in the histogram is specified by the image type. If I is a grayscale image, imhist uses a default value of 256 bins.


level = graythresh(I) computes a global threshold (level) that can be used to convert an intensity image to a binary image with im2bw. level is a normalized intensity value that lies in the range [0, 1].

%The graythresh function uses Otsu's method, which chooses the threshold
% to minimize the intraclass variance of the black and white pixels.

level = graythresh(g);
fprintf('threshold %g\n',level);
BW = im2bw(g,level);
threshold 0.521569

CC = bwconncomp(BW) returns the connected components CC found in BW. The binary image BW can have any dimension. CC is a structure with four fields.

CC = bwconncomp(~BW);
    Connectivity: 8
       ImageSize: [462 708]
      NumObjects: 21
    PixelIdxList: {1x21 cell}

L = labelmatrix(CC) creates a label matrix from the connected components structure CC returned by bwconncomp. The size of L is CC.ImageSize. The elements of L are integer values greater than or equal to 0.

L = labelmatrix(CC);

RGB = label2rgb(L) converts a label matrix into an RGB color image for the purpose of visualizing the labeled regions.

RGB = label2rgb(L);

show "inbetween" values;

msk1 = g>100;
msk2 = g<150;