Skip to content

Commit f932268

Browse files
committed
All codes
1 parent ea07b03 commit f932268

File tree

112 files changed

+1973
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1973
-0
lines changed

Project 1/Problem1.pdf

20 KB
Binary file not shown.
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
clc;clear;
2+
%% Write a computer program for computing the histogram of an image.
3+
fig1 = imread('data/Fig1.jpg');
4+
fig2 = imread('data/Fig2.jpg');
5+
figure('Name', 'Original Histogram');
6+
set(gcf, 'position', [0 0 1024 512]);
7+
subplot(1, 2, 1);
8+
[L1 D1] = myImhist(fig1);
9+
title('Fig 1');
10+
subplot(1, 2, 2);
11+
[L2 D2] = myImhist(fig2);
12+
title('Fig 2');
13+
14+
%% Implement the histogram equalization technique.
15+
figure('Name', 'Transformation of Fig. 1');
16+
set(gcf, 'position', [0 0 960 512]);
17+
subplot(1, 2, 1);
18+
imshow(fig1, []);
19+
R1 = myEqualization(L1, D1);
20+
for i = 1:size(fig1, 1)
21+
for j = 1:size(fig1, 2)
22+
fig1(i, j) = R1(fig1(i, j) + 1);
23+
end
24+
end
25+
subplot(1, 2, 2);
26+
imshow(fig1, []);
27+
28+
figure('Name', 'Transformation of Fig. 2');
29+
set(gcf, 'position', [0 0 1024 512]);
30+
subplot(1, 2, 1);
31+
imshow(fig2, []);
32+
R2 = myEqualization(L2, D2);
33+
for i = 1:size(fig2, 1)
34+
for j = 1:size(fig2, 2)
35+
fig2(i, j) = R2(fig2(i, j) + 1);
36+
end
37+
end
38+
subplot(1, 2, 2);
39+
imshow(fig2, []);
40+
41+
figure('Name', 'Transformation Function');
42+
subplot(1, 2, 1);
43+
plot(R1);
44+
subplot(1, 2, 2);
45+
plot(R2);
46+
47+
figure('Name', 'Equalized Histogram');
48+
set(gcf, 'position', [0 0 1024 512]);
49+
subplot(1, 2, 1);
50+
[L1 D1] = myImhist(fig1);
51+
%title('Fig 1');
52+
subplot(1, 2, 2);
53+
[L2 D2] = myImhist(fig2);
54+
%title('Fig 2');

Project 1/src/data/Fig1.jpg

47.9 KB
Loading

Project 1/src/data/Fig2.jpg

84.9 KB
Loading

Project 1/src/myEqualization.m

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function [ output_distribution ] = myEqualization( L, distribution )
2+
%MYEQUALIZATION equalizes the histogram
3+
% It returns a mapping that equalizes the original distribution
4+
5+
output_distribution = distribution;
6+
for i = 2:size(distribution);
7+
output_distribution(i) = output_distribution(i-1) + distribution(i);
8+
end
9+
output_distribution = output_distribution * (L - 1) / sum(distribution(:));
10+
11+
12+
end
13+

Project 1/src/myImhist.m

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function [ L, distribution ] = myImhist( fig )
2+
%MYIMHIST creates the histogram of a given image.
3+
% The function returns the number of pixels for each gray level,
4+
% and draws a figure automatically.
5+
L = 8;
6+
if L <= max(max(fig))
7+
L = 256;
8+
end
9+
distribution = zeros(L, 1);
10+
for i = 1:size(fig, 1)
11+
for j = 1:size(fig, 2)
12+
distribution(fig(i, j) + 1, 1) = distribution(fig(i, j) + 1, 1) + 1;
13+
end
14+
end
15+
% distribution = table(:, 2);
16+
distribution = distribution ./ size(fig, 1) ./ size(fig, 2)
17+
s = size(distribution);
18+
b = bar(distribution,'FaceColor', 'blue', 'EdgeColor', 'blue','LineWidth',0.6);
19+
set(gca,'XLim',[0 L]);
20+
set(gca,'XTick',[0:L/4:L]);
21+
set(gca,'XTickLabel',[0:L/4:L]);
22+
ylabel('Occurance');
23+
colormap(gray);
24+
c = colorbar('Location', 'southoutside', 'FontSize',10, 'Ticks', 0:64:256, 'TickLabels', {'0', '64', '128', '192', '256'});
25+
c.Label.String = 'Intensity';
26+
27+
28+
end
29+

Project 2/problem2.pdf

19.9 KB
Binary file not shown.

Project 2/src/data/final_result.png

162 KB
Loading

Project 2/src/data/laplace_result.png

169 KB
Loading
110 KB
Loading
142 KB
Loading
Loading

Project 2/src/data/skeleton_orig.png

114 KB
Loading

Project 2/src/data/skeleton_orig.tif

237 KB
Binary file not shown.
118 KB
Loading

Project 2/src/data/sobel_grad.png

184 KB
Loading

Project 2/src/enhance.m

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
clc; clear;
2+
%% Read the original image
3+
% (a) The original image
4+
fig_original = double(imread('data/skeleton_orig.tif')) / 255;
5+
figure;
6+
subplot(2, 4, 1);
7+
imshow(fig_original);
8+
imwrite(fig_original, 'data/skeleton_orig.png');
9+
10+
% (b) The Laplacian of the original image
11+
laplace_result = imfilter([-1 -1 -1; -1 8 -1; -1 -1 -1], fig_original);
12+
subplot(2, 4, 2);
13+
imshow(remap(laplace_result, 0, 1));
14+
imwrite(remap(laplace_result, 0, 1), 'data/laplace_result.png');
15+
16+
% (c) The sharpened image using Laplacian
17+
sharpened_laplace_result = fig_original + laplace_result;
18+
subplot(2, 4, 3);
19+
imshow(sharpened_laplace_result);
20+
imwrite(sharpened_laplace_result, 'data/sharpened_laplace_result.png');
21+
22+
% (d) The Sobel gradient of the original image
23+
sobel_grad = abs(imfilter([-1 -2 -1; 0 0 0; 1 2 1], fig_original)) + abs(imfilter([-1 0 1; -2 0 2; -1 0 1], fig_original));
24+
subplot(2, 4, 4);
25+
imshow(sobel_grad);
26+
imwrite(sobel_grad, 'data/sobel_grad.png');
27+
28+
% (e) The smoothed Sobel gradient
29+
smoothed_sobel_grad = imfilter(ones(5)/25, sobel_grad);
30+
subplot(2, 4, 5);
31+
imshow(smoothed_sobel_grad);
32+
imwrite(smoothed_sobel_grad, 'data/smoothed_sobel_grad.png');
33+
34+
% (f) Mask image formed by the product of (c) and (e)
35+
product_laplace_sobel = sharpened_laplace_result .* smoothed_sobel_grad;
36+
subplot(2, 4, 6);
37+
imshow(product_laplace_sobel);
38+
imwrite(product_laplace_sobel, 'data/product_laplace_sobel.png');
39+
40+
% (g) Sharpened image obtained by the sum of (a) and (f)
41+
sharpened_image = max(fig_original + product_laplace_sobel, 0);
42+
subplot(2, 4, 7);
43+
imshow(sharpened_image);
44+
imwrite(sharpened_image, 'data/sharpened_image.png');
45+
46+
% (h) Final result obtained by applying a powerlaw transformation to (g)
47+
final_result = power(sharpened_image, 0.5);
48+
subplot(2, 4, 8);
49+
imshow(final_result);
50+
imwrite(final_result, 'data/final_result.png');

Project 2/src/imfilter.m

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function [ g ] = imfilter( w, x )
2+
%IMFILTER conducts the filter operation given filter w and matrix x
3+
% Paddings are automatically computered, with stepsize = 1.
4+
[h_x, w_x] = size(x);
5+
[h_w, w_w] = size(w);
6+
h_w_half = (h_w - 1) / 2;
7+
w_w_half = (w_w - 1) / 2;
8+
g = zeros(h_x, w_x);
9+
for i = 1 : h_x
10+
for j = 1 : w_x
11+
g(i, j) = 0;
12+
for k = -h_w_half : h_w_half
13+
for l = -w_w_half : w_w_half
14+
if i + k > 0 && i + k <= h_x && j + l > 0 && j + l <= w_x
15+
g(i, j) = g(i, j) + w(k + h_w_half + 1, l + w_w_half + 1) * x(i + k, j + l);
16+
end
17+
end
18+
end
19+
end
20+
end
21+
end
22+

Project 2/src/remap.m

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function [ output ] = remap( input, lb, ub )
2+
%REMAP remaps a matrix to the interval [lb, ub]
3+
% lb = lower bound, ub = upper bound
4+
if max(max(input)) ~= min(min(input))
5+
output = (input - min(min(input))) / (max(max(input)) - min(min(input))) * (ub - lb) + lb;
6+
else
7+
output = input - input + (ub + lb) / 2;
8+
end
9+
end
10+

Project 3/problem3.pdf

19.8 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function [ output ] = butterworth_highpass_filter( m, n, D0, N )
2+
%BUTTERWORTH_HIGHPASS_FILTER The Butterworth highpass filter
3+
% H(u, v) = 1 - (1 / (1 + (D(u, v) / D0)^2n))
4+
u = [0:(m-1) -m:-1];
5+
v = [0:(n-1) -n:-1];
6+
[V, U] = meshgrid(v, u);
7+
D = sqrt((V.^2 + U.^2));
8+
output = 1 - (1 ./ (1 + (D ./ D0).^(2 * N)));
9+
end
10+
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function [ output ] = butterworth_lowpass_filter( m, n, D0, N )
2+
%BUTTERWORTH_LOWPASS_FILTER The Butterworth lowpass filter
3+
% H(u, v) = 1 / (1 + (D(u, v) / D0)^2n)
4+
u = [0:(m-1) -m:-1];
5+
v = [0:(n-1) -n:-1];
6+
[V, U] = meshgrid(v, u);
7+
D = sqrt((V.^2 + U.^2));
8+
output = 1 ./ (1 + (D ./ D0).^(2 * N));
9+
end
10+
245 KB
Binary file not shown.

0 commit comments

Comments
 (0)