Digital Images have a finite number of coordinates, known as pixels, and Intensity levels corresponding to the pixels’ value.A continuous image is converted to digital form by sampling it in both coordinates and amplitude (intensity).

Digitizing the coordinates is known as Sampling and digitizing the amplitude values is known as Quantization.Sampling determines the spatial resolution of an image. Spatial resolution is defined as smallest discernible detail in an image.

Program to demonstrate Sampling of a Digital grayscale Image

Write an optimized Matlab code to perform the following.
i) Read a 1024 x 1024 flower.jpg image, subsample the image to sizes 512, 256,128, 64,32.
ii) Use subplot and display the subsampled images to the same size 1024 x 1024 as shown in the fig below.
iii) Obtain the above result using imresize() function.

``` clc; clear all;close all```

``` Input = imread('flower1.jpg'); figure(); subplot(2,3,1); imshow(Input); m = 1024; s = num2str(m); xlabel(s); for i = 1:5, m = m/2; J=Input(1:2^i:size(Input,1),1:2^i:size(Input,2)); subplot(2,3,i+1); imshow(J); s = num2str(m); xlabel(s); end; title('using code'); % sampling using imresize m=1024; scale=[512 256 128 64 32] figure(); subplot(2,3,1); imshow(Input); ```

```s=num2str(m); xlabel(s); for i = 1:5 m=m/2; subplot(2,3,i+1); imshow(imresize(Input,[scale(i) scale(i)])); s=num2str(m); xlabel(s); end title('using built in function'); ```
Program to demonstrate Quantization

i) Write a MATLAB program to quantize the gray levels to 128, 64, 32,16, 8, 4 and 2 levels.
ii) Obtain the above result using grayslice() function.
iii) Obtain the above result using imquantize() function and write the description of imquantize function.

``` clear;close all; Input = imread('CAT.jpg'); m = 1; figure,subplot(4,2,1); image(Input); colormap(gray(256)); xlabel(num2str(2^8));```

``` for i = 7:-1:1 m = m + 1; del = 256/(2^i); % step size % 128,64,32,16,8,4,2 levels subplot(4,2,m); J = floor(double(Input)/del)*del+(del/2); image(J); colormap(gray(256)); xlabel(num2str(2^i)); end; title(' using code'); % using built in function figure();subplot(4,2,1); image(Input); colormap(gray(256)); xlabel(num2str(2^8)); % using gray slice function for i=7:-1:1 out=grayslice(Input,(2^7)); subplot(4,2,8-i+1); image(Input); colormap(gray(256)); xlabel(num2str(2^8)); image(uint8(out)); xlabel(num2str(2^i)); end; title('using built-in function grayslice'); figure(),subplot(4,2,1); image(Input); colormap(gray(256)); xlabel(num2str(2^8)); % using imquantize function ```

```for i=7:-1:1 thres=multithresh(Input,i); valuesMax=[thres max(Input(:))]; quant=imquantize(Input,thres,valuesMax); % out=grayslice(Input,(2^7)); subplot(4,2,8-i+1); image(uint8(quant)); xlabel(num2str(2^i)); end; title('using built-in function imquantize'); ```
This bring to an end to the Sampling and Quantization.
Check out the next article on Adjacency, Connectivity and Component Labelling of Digital Image.
Go back to Getting started with Image Processing using Matlab