Multimedia Lab 1
Multimedia Lab 1
University
Information & Communication
Engineering
Lab Report
Output:
2. Differential Encoding
Program:
#include <iostream>
#include <vector>
#include <cstring>
// Store differences
for (int i = 1; i < len; ++i) {
int diff = (int)input[i] - (int)input[i - 1];
encoded.push_back(diff);
}
return encoded;
}
int main() {
char text[MAX_LEN];
// Encoding
vector<int> encoded = differentialEncode(text);
// Decoding
char decodedText[MAX_LEN];
differentialDecode(encoded, decodedText);
return 0;
}
Output:
3. Static Huffman Encoding:
Program:
#include <iostream>
#include <string>
#include <iomanip>
// Swap function
void swap(Node*& a, Node*& b) {
Node* temp = a;
a = b;
b = temp;
}
int main() {
int n;
cout << "Enter the number of symbols: ";
cin >> n;
Node* nodes[MAX];
bool symbolUsed[256] = {false};
float totalProb = 0;
// Input symbols and their probabilities
for (int i = 0; i < n; i++) {
char sym;
float prob;
if (symbolUsed[(unsigned char)sym]) {
cout << "Duplicate symbol! Try again." << endl;
i--;
continue;
}
cout << "Enter probability for '" << sym << "': ";
cin >> prob;
totalProb += prob;
if (totalProb > 1.001) {
cout << "Total probability exceeds 1.0. Try again." << endl;
return 1;
}
cout << "\nFinal Encoded Binary String:\n" << encoded << endl;
// Cleanup
freeTree(root);
return 0;
}
Output:
4. Dynamic Huffman Encoding:
Program:
#include<bits/stdc++.h>
if (symbolTable.find(c) == symbolTable.end()) {
// First time seeing this character
code = getCode(NYT); // Send NYT path
code += bitset<8>(c).to_string(); // Send raw 8-bit char
internal->left = NYT;
internal->right = newLeaf;
internal->parent = NYT->parent;
if (NYT->parent) {
if (NYT->parent->left == NYT)
NYT->parent->left = internal;
else
NYT->parent->right = internal;
} else {
root = internal;
}
NYT->parent = internal;
newLeaf->parent = internal;
symbolTable[c] = newLeaf;
return 0;
}
Output:
5. Arithmetic Coding:
Program:
#include <bits/stdc++.h>
using namespace std;
return ranges;
}
int main() {
string text;
if (text.empty()) {
cout << "Empty input." << endl;
return 0;
}
map<char, SymbolRange> ranges = calculateProbabilities(text);
return 0;
}
Output:
6. LZ Encoding:
Program:
#include <bits/stdc++.h>
using namespace std;
return encoded;
}
int main() {
string input;
cout << "Enter text to compress using LZ77: ";
getline(cin, input);
displayEncoded(compressed);
return 0;
}
Output:
7. LZW Encoding:
Program:
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
if (!current.empty()) {
encodedOutput.push_back(dictionary[current]);
}
return encodedOutput;
}
int main() {
string input;
cout << "Enter text to compress using LZW: ";
getline(cin, input);
return 0;
}
Output:
Discussion:
RLE is best for highly repetitive text but limited elsewhere. Differential Encoding suits
numerical sequences within text. Static Huffman is efficient for known distributions, while
Dynamic Huffman adapts to changing ones. Arithmetic Coding offers superior compression but
is complex. LZ and LZW shine in general-purpose text compression, with LZW being simpler
but less flexible. In practice, LZ-based methods dominate modern tools (e.g., ZIP), while
Huffman and Arithmetic Coding are used in specialized contexts like JPEG or PDF. Combining
these methods (e.g., LZ with Huffman) often yields optimal results for diverse text data.
Experiment No: 02
Experiment Name: Implementation of Fundamental MATLAB Functions for Image
Processing and Analysis.
Theoretical Background:
Image processing is a fundamental area in digital signal processing and computer vision,
enabling the manipulation and analysis of visual data to extract meaningful information or
enhance image quality. MATLAB provides a powerful and user-friendly environment for
implementing and testing a wide range of image processing functions.
In this lab, various built-in MATLAB functions were used to perform key image processing
tasks. Each function plays a significant role in different stages of the image processing pipeline:
Basic Functions like imread, imshow, and imwrite are essential for loading, displaying,
and saving images. These form the foundation for any image manipulation task.
Preprocessing Functions such as imresize, imcrop, imrotate, imfilter, and imgaussfilt help
in preparing images for analysis by resizing, rotating, blurring, or enhancing them.
Color and Format Conversion Functions like im2gray, rgb2gray, and im2bw are used to
convert images into suitable formats, especially grayscale, for further processing like
edge detection or segmentation.
Analysis and Feature Extraction Functions such as imhist, edge, hough, impixel, and
bwconncomp are crucial for analyzing image content, detecting edges, counting objects,
and studying textures.
Transform and Frequency Domain Functions like dct2 and idct2 allow for image
representation in the frequency domain, which is essential in image compression and
filtering applications.
Advanced Feature Functions like gabor and imgaborfilt are used in texture segmentation
and feature extraction, particularly in applications like pattern recognition.
Utility Functions such as montage, imsave, and immovie assist in visualization, saving,
and creating image sequences or animations.
Each of these functions plays a unique role in enabling efficient, accurate, and flexible image
processing workflows. Understanding and applying them is critical for solving real-world
problems in medical imaging, remote sensing, machine vision, and multimedia applications.
Implementation:
1) Imshow
Code:
img = imread('rgb.jpg');
imshow(img);
Output:
2) Imread
Description: Reads an image from a file into the MATLAB workspace.
Usage: Input image for processing.
Code:
Img = imread(‘image_name.png’);
3) Imresize
Description: Resizes an image to a specified size.
Usage: Scale image dimensions up or down.
Code:
img = imread('rgb.png');
resizedImg = imresize(img, 0.5); % Resize to 50%
imshow(resizedImg);
Output:
4) Imcrop
Description: Crops a Rectangular portion of an image.
Usage: Extract region of interest (ROI).
Code:
img = imread('rgb.jpg');
croppedImg = imcrop(img, [100, 100, 150, 150]); % [x y width height]
imshow(croppedImg);
5) Imwrite
Description: Writes an image to a file.
Usage: Save processed images.
Code:
img = imread('image_name.png');
imwrite(img, 'saved_image.png');
6) Imfinfo
Description: Retrieves metadata about an image file.
Usage: Image properties (size, type, etc.).
Code:
info = imfinfo('rgb.png');
disp(info);
Output:
7) im2gray
Description: Converts an RGB image to grayscale.
Usage: Simplify processing, especially for edge detection, etc.
Code:
img = imread('rgb.png');
grayImg = im2gray(img);
imshow(grayImg);
8) rgb2gray
Description: Older function to convert RGB to grayscale.
Usage: Similar to im2gray
Code:
img = imread('peppers.png');
grayImg = rgb2gray(img);
imshow(grayImg);
9) imhist
Description: Displays histogram of image pixel intensities.
Usage: Analyze contrast, brightness, and dynamic range.
Code:
img = rgb2gray(imread('peppers.png'));
imhist(img);
10) im2frame
Description: Converts image to a movie frame.
Usage: Create animations or videos.
Code:
img = imread('peppers.png');
frame = im2frame(img);
11) edge
Description: Detects edges using various methods (e.g., Sobel, Canny).
Usage: Feature detection, segmentation.
Code:
img = rgb2gray(imread('peppers.png'));
edges = edge(img, 'Canny');
imshow(edges);
12) hough
Description: Performs Hough transform to detect lines.
Usage: Line detection in edge-detected images.
Code:
img = edge(rgb2gray(imread('peppers.png')), 'Canny');
[H, theta, rho] = hough(img);
imshow(H, [ ]);
13) impixel
Description: Returns pixel values from specified coordinates.
Usage: Inspect pixel intensity manually.
Code:
img = imread('peppers.png');
imshow(img);
val = impixel(img, 100, 100);
disp(val);
14) bwconncomp
Description: Finds connected components in binary image.
Usage: Object counting, segmentation.
Code:
bw = im2bw(imread('coins.png'));
cc = bwconncomp(bw);
disp(cc.NumObjects);
15) immse
Description: Computes Mean Squared Error (MSE) between two images.
Usage: Image quality evaluation.
Code:
img1 = imread('peppers.png');
img2 = imresize(img1, 0.5);
img2 = imresize(img2, size(img1(:,:,1)));
mse = immse(img1, img2);
disp(mse);
16) dct2
Description: Computes 2D Discrete Cosine Transform.
Usage: Frequency analysis, JPEG compression.
Code:
img = rgb2gray(imread('peppers.png'));
dctImg = dct2(img);
imshow(log(abs(dctImg)), []);
17) idct2
Description: Computes 2D Inverse DCT.
Usage: Reconstruct image from DCT.
Code:
reconstructedImg = idct2(dctImg);
imshow(uint8(reconstructedImg));
18) imfilter
Description: Applies custom filter to image.
Usage: Blurring, sharpening, edge detection.
Code:
img = rgb2gray(imread('peppers.png'));
h = fspecial('sobel');
filtered = imfilter(img, h);
imshow(filtered);
19) imgaussfilt
Description: Applies Gaussian smoothing filter.
Usage: Noise reduction.
Code:
img = imread('peppers.png');
blurred = imgaussfilt(img, 2);
imshow(blurred);
20) gabor
Description: Creates Gabor filter.
Usage: Texture analysis, feature extraction.
Code:
g = gabor(4, 90); % wavelength 4, orientation 90 degrees
21) imgaborfilt
Description: Applies Gabor filter to image.
Usage: Texture segmentation, edge orientation.
Code:
img = rgb2gray(imread('peppers.png'));
g = gabor(4, 90);
filteredImg = imgaborfilt(img, g);
imshow(filteredImg, []);
22) immovie
Description: Creates movie from frame array.
Usage: Video creation.
Code:
img = imread('peppers.png');
frames = repmat(im2frame(img), 1, 10);
movie(frames);
23) montage
Description: Displays multiple images as a montage.
Usage: Compare or visualize many images.
Code:
img1 = imread('peppers.png');
img2 = imresize(img1, 0.5);
montage({img1, img2});
24) imsave
Description: Opens a dialog box to save the displayed image.
Usage: Manual image saving via GUI.
Code:
img = imread('peppers.png');
imshow(img);
imsave;
25) imrotate
Description: Rotates an image by specified angle.
Usage: Data augmentation, alignment.
Code:
img = imread('peppers.png');
rotated = imrotate(img, 45);
imshow(rotated);
26) imerase
Description: Erases parts of an image using a mask.
Usage: Region removal.
Code:
img = imread('peppers.png');
mask = false(size(img(:,:,1)));
mask(100:200, 100:200) = true;
result = imerase(img, mask);
imshow(result);
Discussion:
In this experiment, we explored a wide range of MATLAB functions used in image processing,
gaining practical experience with tools essential for manipulating, analyzing, and enhancing
digital images. Through functions like imread, imshow, imresize, imfilter, and edge, we learned
how to load, visualize, modify, and extract important features from images. The experiment
highlighted the importance of each function in different stages of image processing, from basic
operations to advanced feature extraction. Overall, this hands-on practice has provided a strong
foundation for understanding digital image processing techniques and their applications in real-
world scenarios.
Experiment No: 03
Experiment Name: A program to implement different functions on an audio.
Theoretical background:
Multimedia communication involves the integration of various media types, including audio.
Audio processing is a critical aspect that encompasses tasks like recording, playback, and
retrieving information about audio files. The provided Python script demonstrates audio
operations, including recording, playing/pausing music, and retrieving information about an
audio file.
1. audioread
Description: audioread is used to read audio data from a file (such as .wav, .mp3, or .flac). It
returns the audio samples and the sample rate.
Usage: Load an audio signal into MATLAB for processing or analysis.
Code:
[y, Fs] = audioread('audiofile.wav'); % y: audio signal, Fs: sample rate
sound(y, Fs); % play the audio
2. audiowrite
Description: audiowrite writes audio data to a file. It's useful for saving modified or
generated audio signals.
Usage: Save processed audio signals to a file.
Code:
audiowrite('audio1.mp3', y, Fs); % Write signal y to a new file
3. sound
Description: sound plays an audio signal in the MATLAB environment. It sends the signal to
the default audio output device.
Usage: Listen to audio data directly from MATLAB.
Code:
sound(y, Fs); % Play audio signal y at sampling rate Fs
4. fft
Description: fft (Fast Fourier Transform) converts a time-domain signal to its frequency-
domain representation. It’s crucial for analyzing the frequency content of audio signals.
Usage: Frequency analysis, noise reduction, filtering.
Code:
y = y(:,1); % Use one channel if stereo
Y = fft(y); % Frequency-domain representation
f = (0:length(Y)-1)*Fs/length(Y); % Frequency vector
plot(f, abs(Y)); title('Frequency Spectrum');
xlabel('Frequency (Hz)'); ylabel('|Amplitude|');
5. filter
Description: filter applies a digital filter to the audio signal using specified coefficients. It’s
used for signal enhancement, noise reduction, and equalization.
Usage: Apply low-pass, high-pass, or custom-designed filters.
Code:
[b, a] = butter(4, 0.4, 'low'); % 4th-order low-pass Butterworth filter
filtered_audio = filter(b, a, y);
sound(filtered_audio, Fs); % Play filtered signal
Discussion:
In this experiment, we explored key MATLAB functions used in audio signal processing,
including audioread, audiowrite, sound, fft, and filter. These functions allowed us to load,
play, analyze, and manipulate audio data effectively. By applying transformations and filters,
we gained a practical understanding of time and frequency domain processing. This
experiment reinforced fundamental concepts in digital audio processing and demonstrated
how MATLAB serves as a powerful tool for real-time audio analysis, enhancement, and
modification.