Image generation with MATLAB


Methods to generate 2D images as matrices using MATLAB are described.  3D graphics is not covered here.
Advantages of using MATLAB for image generation are:



Basics of image generation with MATLAB

(Image Processing Toolbox required)

Basic image generation

  • White square (100x100 pix)
  • image = ones([100 100]);
    imshow(image);
    By default, 0 -> black, 1 -> white.
     
  • Random-dot
  • image = rand([100 100]);
    imshow(image);
  • Sine grating
  • image = 0.5 * sin([1:100]')*ones(1,100) + 0.5*ones([100 100]);
    imshow(image);
  • Circle
  • X = ones(100,1)*[-50:49]; Y = [-50:49]'*ones(1,100);
    Z = X.^2 + Y.^2;
    % mesh(Z);
    image = zeros([100 100]);
    image(find(Z <= 50^2)) = 1;
    imshow(image);

    Index color

    Assign color by defining a color map (256x3).
    index  [R      G      B]       (R, G, B = 0 - 1)
        1    [ 0     0      0]
        2    [ 0.1  0      1]
                   :
                   :
       256 [ 0     1     0.5]

    image = ones([100 100]);    % square composed of index "1"s
    colormap = [0:1/255:1]' * ones(1,3);   % black (1)- white(256)
    colormap(1,:) = [1 0 0]; % index "1" -> red
    imshow(image, colormap);


    Image analysis and processing


    Save this image (as Pixel.jpg).
    % Load jpeg files
    orgImage = imread('Pixel.jpg', 'jpg');
    figure(1); imshow(orgImage);

    % 2D FFT
    fftImage = fftshift(fft2(orgImage));   % 2d fft
    ampImage= abs(fftImage);
    figure(2); imshow(ampImage,  [0  10000 ]);

    % Convolution (low-pass filtering)
    filter = fspecial('gaussian',[10 10], 4);  % gaussian kernel
    filterImage = conv2(orgImage, filter);     % convolution
    figure(3); imshow(filterImage, [0 250]);

    % 2D FFT of filtered image
    fftFilterImage = fftshift(fft2(filterImage));
    ampFilterImage= abs(fftFilterImage);
    figure(4); imshow(ampFilterImage,  [0  10000 ]);



    Yuki Kamitani's image tools

    Download

        YkImageTools.tar.gz
        YkImageTools.sea.hqx


    pattern


    drawing



    tool

    comtains subfunctions used in the functions in 'pattern' and 'drawing'


    Examples


    Psychophysics Toolbox (only for Mac)

    Image matrices can be used for presenting visual stimuli with Psychophysics Toolbox. Instead of imshow(imageMatrix) :

    SCREEN(windowPointer,'PutImage',imageMatrix);
    Color map can be defined by:
    SCREEN(windowPointer,'SetClut', colorMap);
    Color values in color map of Psychophysics Toolbox are 0-255, not 0-1.




    cns176 Cognition Home

    Psychophysics Toolbox Home


    written by Yukiyasu Kamitani, kamitani@percipi.caltech.edu
    Last Modified: April 16, 1999
    @