Skip to content

Commit abebf1b

Browse files
committed
first commit
0 parents  commit abebf1b

10 files changed

+171
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# edge-detection-image-processing

draw_initials.m

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
% funtion to draw my initials on my photo
2+
function my_im = draw_initials(im)
3+
% Add my initials (letter M), with triple lines to make it obvious
4+
for i=30:270
5+
% left vertical line: set red and green to zero to make blue line
6+
im(i,30,1:2)=0;
7+
im(i,31,1:2)=0;
8+
im(i,32,1:2)=0;
9+
% right vertical line: set green to 255 to make green
10+
im(i,270,2)=255;
11+
im(i,269,2)=255;
12+
im(i,268,2)=255;
13+
if (i <= 150) % 150 is the mid-point between 30 and 270
14+
% left diagonal: set red to zero to make cyan
15+
im(i,i,1)=0;
16+
im(i+1,i,1)=0;
17+
im(i+2,i,1)=0;
18+
% right diagonal: set all rgb to zero make black
19+
im(i,270-i+30,1:3)=0;
20+
im(i+1,270-i+30,1:3)=0;
21+
im(i+2,270-i+30,1:3)=0;
22+
end
23+
end
24+
my_im = im;
25+
end

get_gray_edge.m

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function [gray_seg, gray_edge] = get_gray_edge(grayImg)
2+
[row, col, ~] = size(grayImg);
3+
%{
4+
row = 824, col = 922, dim = 1
5+
when I checked the improfile of the grayscale, my face had values of
6+
between 110 and 170
7+
%}
8+
thresh1 = 110;
9+
thresh2 = 170;
10+
11+
for i=1:row
12+
for j=1:col
13+
if (grayImg(i,j)>thresh1 && grayImg(i,j)<thresh2)
14+
grayImg(i,j)=255; % for skin tone
15+
else
16+
grayImg(i,j)=0; % for background
17+
end
18+
end
19+
end
20+
se = strel('disk', 2);
21+
erodedMask = imerode(grayImg, se);
22+
gray_seg = imdilate(erodedMask, se);
23+
gray_edge = edge(gray_seg,"roberts");
24+
end

get_hsv_edge.m

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
% Edge detection using threshold of HSV color space
2+
function [hsv_edge, imFace] = get_hsv_edge(im, hsvImg)
3+
% copy the image, to preserve the original
4+
im1 = hsvImg;
5+
[row, col, ~] = size(im1);
6+
imFace = zeros(row,col);
7+
%{
8+
row = 824, col = 922, dim = 3
9+
when I checked the improfile of the picture, my face had
10+
1. h value btw 0.02 and 0.07, while the background had h value above
11+
0.5
12+
2. s value of btw 0.28 and 0.42 while the background had s value below
13+
0.2
14+
3. v value of btw 0.5 and 0.7 while the background was either above or
15+
below that range.
16+
%}
17+
18+
hueThreshold = 0.08;
19+
saturationThreshold = 0.2;
20+
21+
for i=1:row
22+
for j=1:col
23+
if (im1(i,j,1)<hueThreshold && im1(i,j,2)>saturationThreshold)
24+
% 1 for my face and 0 for the background
25+
imFace(i,j) = im(i,j,1);
26+
end
27+
end
28+
end
29+
hsv_edge = edge(imFace,"roberts");
30+
end

get_kmeans_edge.m

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function [edges, overlay] = get_kmeans_edge(hsvImg)
2+
img = uint8(hsvImg);
3+
% Perform k-means segmentation on the HSV image
4+
k = 6; % Number of clusters
5+
[L, ~] = imsegkmeans(img, k);
6+
7+
% Extract the binary mask for the first cluster
8+
mask = (L==1);
9+
10+
% Covert label to RGB image to visualize the binary mask
11+
maskRgb = label2rgb(mask);
12+
13+
% Overlay the mask on top of the rgb image
14+
overlay = labeloverlay(maskRgb, mask);
15+
16+
edges = edge(rgb2gray(overlay), 'roberts');
17+
end

myCW3.m

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function myCW3()
2+
im = imread("photo1.jpg");
3+
4+
[~, ~, dim] = size(im);
5+
if dim > 1
6+
% convert to gray image, if the image is not gray
7+
grayImg1 = rgb2gray(im);
8+
else
9+
grayImg1 = im;
10+
end
11+
12+
% 1. draw my initials
13+
my_im = draw_initials(im);
14+
15+
% 2. Matlab inbuilt method
16+
edge_rob = edge(grayImg1, "roberts");
17+
18+
% 3. Using convolution mask
19+
myedge = my_edge(grayImg1);
20+
21+
% convert to hsv
22+
hsvImg = rgb2hsv(im);
23+
24+
% 4. Edge detection using HSV and thresholds
25+
[hsv_edge, hvs_seg] = get_hsv_edge(im, hsvImg);
26+
27+
% 5. Edge detection using Gray image and thresholds
28+
[gray_seg, gray_edge] = get_gray_edge(grayImg1);
29+
30+
% 6. Edge detection using k-means
31+
[edgeKM, overlay] = get_kmeans_edge(hsvImg);
32+
33+
% 7. Edge detection using SVM
34+
svm_edge = get_svm_edge(im);
35+
36+
% Larger image with my initials
37+
figure(2), imshow(my_im), title("Original with initials");
38+
39+
% Subplots of all results
40+
figure(1),
41+
subplot(3,4,1), imshow(im);
42+
title("1. Original")
43+
subplot(3,4,2), imshow(edge_rob);
44+
title("2. Default Roberts")
45+
subplot(3,4,3), imshow(myedge);
46+
title("3. My Conv Edge")
47+
subplot(3,4,4), imshow(hvs_seg);
48+
title("4. HSV Segment")
49+
subplot(3,4,5), imshow(hsv_edge);
50+
title("5. HSV Edge: Roberts")
51+
subplot(3,4,6), imshow(edge(hvs_seg,"sobel"));
52+
title("6. HSV Edge: Sobel")
53+
subplot(3,4,7), imshow(gray_seg);
54+
title("7. Gray Segment")
55+
subplot(3,4,8), imshow(gray_edge);
56+
title("8. Gray Edge: Roberts")
57+
subplot(3,4,9), imshow(edge(gray_seg,"sobel"));
58+
title("9. Gray Edge: Sobel")
59+
subplot(3,4,10), imshow(overlay);
60+
title("10. K-Means Overlay")
61+
subplot(3,4,11), imshow(edgeKM);
62+
title("11. K-Means Edge")
63+
subplot(3,4,12), imshow(svm_edge);
64+
title("12. SVM Edge")
65+
end

my_edge.m

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function myedge = my_edge(im)
2+
% Using convolution mask
3+
h = [
4+
1, 0, 1;
5+
0, 1, 0;
6+
-1, -1, -1;
7+
];
8+
% my work
9+
myedge = conv2(im, h);

photo1.jpg

46.1 KB
Loading

submission.pdf

1.73 MB
Binary file not shown.

0 commit comments

Comments
 (0)