Image and Shape Manipulation Techniques

Image and Graphics Operations

1. Grayscale Image Processing

  • a) Fill a Grayscale Array (320w x 240h)

    Fill a grayscale array to represent an image of size 320w x 240h.

    myGreyArray = zeros(240, 320);

    Note: Image dimensions are typically height x width in MATLAB, so 240 rows (height) by 320 columns (width).

  • b) Display the Grayscale Image

    Display the image.

    figure;
    image(myGreyArray);
    colormap(gray); % Ensure grayscale colormap is used
  • c) Increase Array Values and Observe Image Change

    Increase the array values by 50. Explain what happens to the image in general.

    myGreyArray = myGreyArray + 50;

    Explanation: Increasing the array values by 50 will make the image appear lighter. If the original values were 0 (black), they become 50 (dark grey). If they were 200 (light grey), they become 250 (very light grey). Values exceeding 255 (for 8-bit images) will typically be clipped to 255, resulting in pure white for those pixels.

  • d) Crop a 100×100 Section from the Image

    Crop a 100×100 section from the image as described below:

    • i. From the Top-Left Corner
      cropMyGreyArray = myGreyArray(1:100, 1:100);
    • ii. From the Middle of the Image
      % For a 240x320 image, center is (120, 160)
      % To get a 100x100 crop centered:
      % Rows: 120 - 50 + 1 = 71 to 120 + 50 = 170 (100 rows)
      % Columns: 160 - 50 + 1 = 111 to 160 + 50 = 210 (100 columns)
      cropMyGreyArray = myGreyArray(71:170, 111:210);

      Original code had an extra parenthesis and incorrect range for 100 elements. Corrected for 240×320 image.

    • iii. From the Top-Right Corner
      % For a 240x320 image, top-right 100x100:
      % Rows: 1 to 100
      % Columns: 320 - 100 + 1 = 221 to 320
      cropMyGreyArray = myGreyArray(1:100, 221:320);

      Original code had incorrect range for top-right. Corrected.

  • e) Write Grayscale Array to File

    Write the content of the grayscale array to a file.

    imwrite(myGreyArray, 'myGreyArray.jpg');

2. Color Image Processing

Repeat the above steps to represent and manipulate a color image.

  • a) Fill a Color Array (320w x 240h)

    Fill a color array to represent an image of size 320w x 240h.

    myColourArray = zeros(240, 320, 3); % Height x Width x 3 channels (RGB)
  • b) Display the Color Image

    Display the image.

    figure;
    image(uint8(myColourArray)); % Cast to uint8 for image display if values are 0-255
  • c) Increase Array Values and Observe Image Change

    Increase the array values by 50. Explain what happens to the image in general.

    myColourArray = myColourArray + 50; % Increases all RGB channel values by 50

    Explanation: Increasing the array values by 50 for a color image will make the image appear brighter and lighter overall. Each color component (Red, Green, Blue) for every pixel will be increased, shifting the colors towards white. For example, a dark red will become a lighter red, and a dark blue will become a lighter blue. Values exceeding 255 will be clipped.

  • d) Crop a 100×100 Section from the Color Image

    Crop a 100×100 section from the image as described below:

    • i. From the Top-Left Corner
      cropMyColourArray = myColourArray(1:100, 1:100, :);

      Corrected indexing from 0:100 to 1:100.

    • ii. From the Middle of the Image
      % For a 240x320 image, center is (120, 160)
      % Rows: 71 to 170
      % Columns: 111 to 210
      cropMyColourArray = myColourArray(71:170, 111:210, :);

      Corrected indexing and removed extra parenthesis.

    • iii. From the Top-Right Corner
      % For a 240x320 image, top-right 100x100:
      % Rows: 1 to 100
      % Columns: 221 to 320
      cropMyColourArray = myColourArray(1:100, 221:320, :);

      Corrected indexing.

  • e) Write Color Array to File

    Write the content of the color array to a file.

    imwrite(uint8(myColourArray), 'myColourArray.jpg'); % Cast to uint8 for imwrite

3. Basic Shape Drawing

Demonstrates drawing basic geometric shapes within a defined space.

  • a) Create a Drawing Space (500×500)

    figure;
    hrect = rectangle('Position', [0, 0, 500, 500]);
    axis equal; % Maintain aspect ratio
    axis([0 500 0 500]); % Set axis limits
  • b) Draw a Rectangle (200×100)

    hrect = rectangle('Position', [100, 100, 200, 100]);
  • c) Draw a Circle (150×150)

    hcirc = rectangle('Position', [200, 300, 150, 150], 'Curvature', [1 1]);
  • d) Draw a Triangle

    x = [50 100 80];
    y = [60 100 0];
    hpoly = patch(x, y, 'k'); % 'k' for black color

4. Shape Transformations

Demonstrates common transformations applied to shapes: translation, scaling, and rotation.

  • a) Move a Shape to the Top-Right Corner

    Move any of these shapes to the top-right corner. This example demonstrates moving a rectangle.

    figure;
    % Create white space
    hrect = rectangle('Position', [0, 0, 500, 500]);
    axis equal; axis([0 500 0 500]);
    
    % Create matrix containing values for standard rectangle: [X, Y, Width, Height]
    % Original: [100, 100, 200, 100]
    XY = [100, 100, 200, 100];
    
    % Display original rectangle
    hrect1 = rectangle('Position', XY, 'EdgeColor', 'b'); % Blue for original
    
    % Create transformed matrices for top-right corner (e.g., 500x500 space)
    % New X = SpaceWidth - Width
    % New Y = SpaceHeight - Height
    spaceWidth = 500;
    spaceHeight = 500;
    rectWidth = XY(3);
    rectHeight = XY(4);
    
    XT = spaceWidth - rectWidth; % New X coordinate
    YT = spaceHeight - rectHeight; % New Y coordinate
    
    % Display transformed rectangle
    hrect2 = rectangle('Position', [XT, YT, rectWidth, rectHeight], 'EdgeColor', 'r'); % Red for transformed
    title('Rectangle Moved to Top-Right Corner');

    The original code had issues with matrix indexing and was not clearly moving to the top-right. Corrected to demonstrate a clear move to the top-right corner of a 500×500 space.

  • b) Scale Up Each Shape to 1.7 Times

    Scale up each shape to 1.7 times in size. This example demonstrates scaling a rectangle.

    figure;
    % Create white space
    hrect = rectangle('Position', [0, 0, 500, 500]);
    axis equal; axis([0 500 0 500]);
    
    % Create matrix containing values for standard rectangle: [X, Y, Width, Height]
    XY = [100, 100, 200, 100];
    
    % Display original rectangle
    hrect1 = rectangle('Position', XY, 'EdgeColor', 'b'); % Blue for original
    
    % Create 1.7 scaled matrices
    XS = XY(3) * 1.7; % Scales width by 1.7
    YS = XY(4) * 1.7; % Scales height by 1.7
    
    % Display transformed rectangle (scaled from its top-left corner)
    hrect2 = rectangle('Position', [XY(1), XY(2), XS, YS], 'EdgeColor', 'r'); % Red for transformed
    title('Rectangle Scaled Up by 1.7');

    Corrected matrix indexing for width and height.

  • c) Scale Down Each Shape to 0.4 Times

    Scale down each shape to 0.4 times in size. This example demonstrates scaling a rectangle.

    figure;
    % Create white space
    hrect = rectangle('Position', [0, 0, 500, 500]);
    axis equal; axis([0 500 0 500]);
    
    % Create matrix containing values for standard rectangle: [X, Y, Width, Height]
    XY = [100, 100, 200, 100];
    
    % Display original rectangle
    hrect1 = rectangle('Position', XY, 'EdgeColor', 'b'); % Blue for original
    
    % Create 0.4 scaled matrices
    XS = XY(3) * 0.4; % Scales width by 0.4
    YS = XY(4) * 0.4; % Scales height by 0.4
    
    % Display transformed rectangle (scaled from its top-left corner)
    hrect2 = rectangle('Position', [XY(1), XY(2), XS, YS], 'EdgeColor', 'r'); % Red for transformed
    title('Rectangle Scaled Down by 0.4');

    Corrected matrix indexing for width and height.

  • d) Rotate Each Shape by 30 Degrees Clockwise

    Rotate each shape by 30 degrees clockwise. This example demonstrates rotating a polygon.

    figure;
    axis equal; axis([-100 100 -100 100]); % Adjust axis for visibility
    
    % Original polygon vertices
    xy = [0 60 80 85;
          0 70 60 50];
    x = xy(1,:);
    y = xy(2,:);
    patch(x, y, 'r', 'DisplayName', 'Original Polygon'); % Red for original
    
    % Produce 15 rotated objects (demonstrating rotation)
    for th_deg = 10:10:150
        th_rad = deg2rad(th_deg); % Convert degrees to radians
        
        % Standard rotation matrix for counter-clockwise: [cos(th) -sin(th); sin(th) cos(th)]
        % For clockwise rotation by 'th', use angle '-th'.
        RT_clockwise = [cos(-th_rad) -sin(-th_rad);
                        sin(-th_rad) cos(-th_rad)];
        
        xyp = RT_clockwise * xy;
        x_rotated = xyp(1,:);
        y_rotated = xyp(2,:);
        patch(x_rotated, y_rotated, 'b', 'DisplayName', sprintf('Rotated by %d deg', th_deg)); % Blue for rotated
    end
    title('Polygon Rotation (Clockwise)');
    legend('show');

    Corrected rotation matrix for clockwise rotation. Added deg2rad for clarity.

5. Image Negative Transform

Applies a negative transformation to an image, inverting its colors (white becomes black, black becomes white).

% Load image (assuming '1_Image.jpg' exists in the current directory)
im = imread('1_Image.jpg');

% Display original image (requires DIPimage toolbox for dipshow)
dipshow(im);
title('Original Image');

% Apply negative transform to a central region
[R, C, ~] = size(im); % Get dimensions, including channels for color images
newim = im; % Initialize newim with original image data type

% Loop through a central region (e.g., 100 pixels in from each edge)
% Note: This loop is for a specific region. For full image, remove loop.
for r = 100 : R - 100
    for c = 100 : C - 100
        % For grayscale: newim(r,c) = 255 - im(r,c);
        % For color: newim(r,c,:) = 255 - im(r,c,:);
        newim(r,c,:) = 255 - im(r,c,:); % Assumes 8-bit image (0-255)
    end
end

% Display transformed image (requires DIPimage toolbox for dipshow)
figure;
dipshow(newim);
title('Image with Negative Transform (Central Region)');

% Alternative for full image negative transform (standard MATLAB):
% newim_full = 255 - im;
% figure;
% imshow(newim_full);
% title('Full Image Negative Transform');

Added comments for clarity, corrected size for color images, and noted dipshow dependency. Clarified that the loop applies to a region.