DIP Lab Nandan
DIP Lab Nandan
DIP Lab Nandan
MATLAB CODE:
clc
clear all
close all
a = imread('cameraman.tif');
b = imread('football.jpg');
c = rgb2gray(b);
d = imresize(c,[ 256 256]);
w= im2bw(a);
y= im2bw(d);
r = not(w);
e= and(w,y);
f= or(w,y);
s = xor(w,y);
imshow(e)
figure,imshow(f)
figure,imshow(s)
OUTPUT:
EXPERIMENT NO: 2
Aim: Program for an image enhancement using pixel operation.
1.Negative Image
2.Flip Image
3.Threshold Operaton
4.Contrast Stretching
Negative Image:
Threshold Operation:
EXPERIMENT NO: 3
AIM: Program for gray level slicing with and without background.
THEORY: Image enhancement can be done in two domain: Spatial Domain and Transform
domain. In spatial domain Image processing, there are two ways:
_ Point processing (Single pixel is processed at a time)
_ Neighborhood processing (Mask processing) Convolution of 3x3 or 5x5 or other size of mask
with image
Where, f(x, y) is original image, g(x, y) is transformed image, s = gray level of transformed
image,
Threshold operation: In threshold operation, Pixel value greater than threshold value is made
white and pixel value less than threshold value is made black.
s = 0ifr ≤ m
s = 255ifr > m
Where m = threshold
Thresholding Scan any document and use this method to make it clean
clc;
clear all;
close all;
a = imread('football.jpg');
% for better idea of matlab images, "help imdemos" in command window.
% converting the above color image into a gray level image
b = rgb2gray(a);
[m,n] = size(b);
% analyzing the above gray level image histogram using "imhist"
imshow(b); title('input image in gray level');
figure;
imhist(b);title('histogram of the input image at gray level');
% using matlab command "zeros"
c = zeros(m,n);
% initialing the for loop
for i = 1:m
for j = 1:n
% use the condition
if b(i,j) >= 227 % try with defferent intensity levels in the range from 227 to
245 for better understanding
c(i,j) = 255;
else
% c(i,j) = 0; % for without bachground
c(i,j) = b(i,j); % for with background
end
end
end
figure;
imshow(c);title('gray level sliced image from an intensity level 240 onwards');
SLICING OUTPUT:
BIT SLICING MATLAB CODE:
clc;
t = 0:1/1000:1;
x = sin(2*pi*25*t);
y = dct(x);
y2 = find(abs(y) < 0.1);
y(y2) = zeros(size(y2));
z = idct(y);
howmany = length(find(y))
subplot(2,1,1)
plot(t,x)
ax = axis;
title('Original Signal')
subplot(2,1,2)
plot(t,z)
axis(ax)
title('Reconstructed Signal')
DCT OUTPUT:
EXPERIMENT NO: 5
AIM: Program for image enhancement using histogram equalization.
THEORY: Histogram is bar-graph used to profile the occurrence of each gray level in the
image. Mathematically it is represented as h(r ) = n Where r is k grey level and n is number of
k k k
th
k
pixel having that Grey level. Histogram can tell us whether image was scanned properly or not. It
gives us idea about tonal distribution in the image. Histogram equalization can be applied to
improve appearance of the image. Histogram also tells us about objects in the image. Object in
an image have similar gray levels so histogram helps us to select threshold value for object
detection. Histogram can be used for image segmentation.
MATLAB CODE:
clc;
clear all
close all
I = imread('tire.tif');
J = histeq(I);
imshowpair(I,J,'montage')
axis off
figure,imhist(I,64)
figure,imhist(J,64)
%for 3D image
load mristack
enhanced = histeq(mristack);
% Display the first slice of data for the original image and the contrast-enhanced
image.
figure,subplot(1,2,1)
imshow(mristack(:,:,1))
title('Slice of Original Image')
subplot(1,2,2)
imshow(enhanced(:,:,1))
title('Slice of Enhanced Image')
OUTPUT:
EXPERIMENT NO: 6
AIM: Program to filter an image using averaging low pass filter in spatial domain
and median filter.
MATLAB CODE:
img = imread('cameraman.tif');
imgd = im2double(img); % imgd in [0,1]
f = ones(3,3)/9;
img1 = filter2(f, imgd);
subplot(331);imshow(img);
subplot(332);imshow(img1);
K = medfilt2(imgd);
subplot(335);imshow(K);
%color image
I = imread('hawk.png');
J = imnoise(I,'salt & pepper',0.02);
OUTPUT:
EXPERIMENT NO: 8
INPUT:
OUTPUT:
FILTERED IMAGE:
Code:
import
heapq
import os
class HeapNode:
def __init__(self, char, freq):
self.char = char
self.freq = freq
self.left = None
self.right = None
class HuffmanCoding:
def __init__(self, path):
self.path = path
self.heap = []
self.codes = {}
self.reverse_mapping = {}
def merge_nodes(self):
while(len(self.heap)>1):
node1 = heapq.heappop(self.heap)
node2 = heapq.heappop(self.heap)
heapq.heappush(self.heap, merged)
def make_codes(self):
root = heapq.heappop(self.heap)
current_code = ""
self.make_codes_helper(root, current_code)
padded_info = "{0:08b}".format(extra_padding)
encoded_text = padded_info + encoded_text
return encoded_text
def get_byte_array(self, padded_encoded_text):
if(len(padded_encoded_text) % 8 != 0):
print("Encoded text not padded properly")
exit(0)
b = bytearray()
for i in range(0, len(padded_encoded_text), 8):
byte = padded_encoded_text[i:i+8]
b.append(int(byte, 2))
return b
def compress(self):
filename, file_extension = os.path.splitext(self.path)
output_path = filename + ".bin"
frequency = self.make_frequency_dict(text)
self.make_heap(frequency)
self.merge_nodes()
self.make_codes()
encoded_text = self.get_encoded_text(text)
padded_encoded_text = self.pad_encoded_text(encoded_text)
b = self.get_byte_array(padded_encoded_text)
output.write(bytes(b))
print("Compressed")
return output_path
padded_encoded_text = padded_encoded_text[8:]
encoded_text = padded_encoded_text[:-1*extra_padding]
return encoded_text
return decoded_text
byte = file.read(1)
while(byte != ""):
byte = ord(byte)
bits = bin(byte)[2:].rjust(8, '0')
bit_string += bits
byte = file.read(1)
encoded_text = self.remove_padding(bit_string)
decompressed_text = self.decode_text(encoded_text)
output.write(decompressed_text)
print("Decompressed")
return output_path
Result:
INITIAL SIZE COMPRESSED FILE SIZE
715.3 KB 394.0 KB
Plus, the decompressed file comes out to be exactly the same as the original file,
without any data loss.
EXPERIMENT NO :10
AIM: Program for morphological image operations-erosion,
dilation, opening & closing.
MATLAB CODE: