├── README.md ├── black_white_image_creation ├── BerkeleyTower.png ├── BerkeleyTower_bw.png ├── bw.png └── thresholding.m ├── component_labeling_algorithm ├── connect4dfs.m ├── dfs.m ├── intermediate.png ├── pattern.png └── result.png ├── contrast_stretching ├── map_contrast_linear.png ├── contrast_stretching.m ├── map.png └── map_non_linear_contrast.png ├── edge_linking ├── bw.jpeg ├── bw_edge.jpeg ├── edge_linking.m └── filterify.m ├── filters ├── BerkeleyTower.png ├── filterify.m ├── filters.m └── text.jpg ├── global_thresholding ├── global_t.m ├── low1.jpg ├── low1_global.jpg ├── low2.jpg └── low2_global.jpg ├── gradient ├── filterify.m ├── gradients.m ├── xray.jpg ├── xray_Alpha_prewitt.jpg ├── xray_Alpha_sobel.jpg ├── xray_M_prewitt.jpg └── xray_M_sobel.jpg ├── grayscale ├── grayscale.m ├── map.png └── map_bw.png ├── histogram_equalization ├── histogram_eq.m ├── map.png └── map_hist_eq.png ├── image_intensity_and_contrast_variation ├── BerkeleyTower.png ├── BerkeleyTower_contrast.png ├── BerkeleyTower_intensity.png └── intensity_variation.m ├── image_inversion ├── BerkeleyTower.png ├── BerkeleyTower_inverted.png ├── Rachmaninoff.jpg ├── Rachmaninoff_bw_inverted.jpg └── invert.m ├── intensity_transform ├── downtown.png ├── downtown_c1.png ├── downtown_c2.png ├── downtown_c3.png ├── downtown_c4.png ├── downtown_c5.png ├── downtown_g1.png ├── downtown_g2.png ├── downtown_g3.png ├── downtown_l1.png ├── downtown_l2.png ├── downtown_l3.png ├── downtown_l4.png ├── downtown_l5.png ├── downtown_negative.png └── transform.m ├── intensity_transformation ├── intensity.m ├── map.png ├── map_gamma0.4.png ├── map_gamma3.png ├── map_intensity_plus_50.png └── map_negative.png └── scaling_translation ├── box.png ├── scaled_box.png ├── scaling_translation.m └── translated_box.png /README.md: -------------------------------------------------------------------------------- 1 | #MATLAB 2 | 3 | Multiple example of image processing in MATLAB. 4 | -------------------------------------------------------------------------------- /black_white_image_creation/BerkeleyTower.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/black_white_image_creation/BerkeleyTower.png -------------------------------------------------------------------------------- /black_white_image_creation/BerkeleyTower_bw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/black_white_image_creation/BerkeleyTower_bw.png -------------------------------------------------------------------------------- /black_white_image_creation/bw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/black_white_image_creation/bw.png -------------------------------------------------------------------------------- /black_white_image_creation/thresholding.m: -------------------------------------------------------------------------------- 1 | %black and white image creation using zeros function 2 | im = zeros(500); 3 | for row = 200:250 4 | for col = 200:300 5 | im(row,col) = 255; 6 | end 7 | end 8 | 9 | imwrite(im, 'bw.png'); 10 | 11 | %grayscale image creation using rgb2gray 12 | img = imread('BerkeleyTower.png'); 13 | img = rgb2gray(img); 14 | [rows, cols] = size(img); 15 | 16 | %set threshold value 17 | thresh = 100; 18 | 19 | %thresholding to create binary b and w image 20 | for row = 1:rows 21 | for col = 1:cols 22 | if img(row,col) > thresh 23 | img(row,col) = 255; 24 | else 25 | img(row,col) = 0; 26 | end; 27 | end; 28 | end; 29 | 30 | imwrite(img, 'BerkeleyTower_bw.png'); 31 | -------------------------------------------------------------------------------- /component_labeling_algorithm/connect4dfs.m: -------------------------------------------------------------------------------- 1 | img = imread('pattern.png'); 2 | img = rgb2gray(img); 3 | [rows, cols] = size(img); 4 | 5 | %set threshold value as 127% 6 | thresh = 127; 7 | 8 | %thresholding to convert image to black and white% 9 | for row = 1:rows 10 | for col = 1:cols 11 | if img(row,col) > thresh 12 | img(row,col) = 255; 13 | % img(row,col) = 0; 14 | else 15 | img(row,col) = 0; 16 | % img(row,col) = 255; 17 | end; 18 | end; 19 | end; 20 | 21 | imwrite(img,'intermediate.png'); 22 | 23 | colour = 10; 24 | for row = 1:rows 25 | for col = 1:cols 26 | if img(row,col) == 255 27 | if colour >= 255 28 | colour = 10; 29 | end 30 | img = dfs(img,row,col,colour); 31 | colour = colour + 10; 32 | end 33 | end 34 | end 35 | 36 | imwrite(img, copper, 'result.png'); 37 | imagesc(img); -------------------------------------------------------------------------------- /component_labeling_algorithm/dfs.m: -------------------------------------------------------------------------------- 1 | function img = dfs(I,i,j,colour) 2 | 3 | stack = java.util.Stack(); 4 | [rows, cols] = size(I); 5 | 6 | stack.push([i j]); 7 | 8 | while ~stack.isEmpty 9 | v = stack.pop; 10 | x = v(1); 11 | y = v(2); 12 | if I(x,y) == 255 13 | I(x,y) = colour; 14 | 15 | if x+1 <= rows 16 | stack.push([x+1 y]); 17 | end 18 | if y+1 <= cols 19 | stack.push([x y+1]); 20 | end 21 | if x-1 >= 1 22 | stack.push([x-1 y]); 23 | end 24 | if y-1 >= 1 25 | stack.push([x y-1]); 26 | end 27 | end 28 | end 29 | img = I; -------------------------------------------------------------------------------- /component_labeling_algorithm/intermediate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/component_labeling_algorithm/intermediate.png -------------------------------------------------------------------------------- /component_labeling_algorithm/pattern.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/component_labeling_algorithm/pattern.png -------------------------------------------------------------------------------- /component_labeling_algorithm/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/component_labeling_algorithm/result.png -------------------------------------------------------------------------------- /contrast_stretching/ map_contrast_linear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/contrast_stretching/ map_contrast_linear.png -------------------------------------------------------------------------------- /contrast_stretching/contrast_stretching.m: -------------------------------------------------------------------------------- 1 | %contrast stretching and stem histogram 2 | img = imread('map.png'); 3 | img = rgb2gray(img); 4 | [rows, cols] = size(img); 5 | 6 | intensity_low = 190; 7 | intensity_high = 230; 8 | 9 | img2 = img; 10 | 11 | %linear and enhance 12 | for row = 1:rows 13 | for col = 1:cols 14 | if img2(row,col) > intensity_low && img2(row,col) < intensity_high 15 | img2(row,col) = 50; 16 | end 17 | end 18 | end 19 | 20 | imwrite(img2, ' map_contrast_linear.png'); 21 | 22 | xaxis = linspace(0,255,50); 23 | yaxis = imhist(img2,50); 24 | stem(xaxis,yaxis); 25 | title('linear contrast stretching'); 26 | 27 | %non linear contrast stretching (enhace specified region and make other 28 | %region constant 29 | img2 = img; 30 | 31 | for row = 1:rows 32 | for col = 1:cols 33 | if img2(row,col) > intensity_low && img2(row,col) < intensity_high 34 | img2(row,col) = 50; 35 | else 36 | img2(row,col) = 240; 37 | end 38 | end 39 | end 40 | 41 | imwrite(img2,'map_non_linear_contrast.png'); 42 | 43 | yaxis = imhist(img2, 50); 44 | figure, stem(xaxis,yaxis); 45 | title('non linear contrast stretching'); -------------------------------------------------------------------------------- /contrast_stretching/map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/contrast_stretching/map.png -------------------------------------------------------------------------------- /contrast_stretching/map_non_linear_contrast.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/contrast_stretching/map_non_linear_contrast.png -------------------------------------------------------------------------------- /edge_linking/bw.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/edge_linking/bw.jpeg -------------------------------------------------------------------------------- /edge_linking/bw_edge.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/edge_linking/bw_edge.jpeg -------------------------------------------------------------------------------- /edge_linking/edge_linking.m: -------------------------------------------------------------------------------- 1 | %horizontal edge linking 2 | 3 | i = imread('bw.jpeg'); 4 | 5 | %using sobel filter 6 | filter_x = [-1 -2 -1; 0 0 0; 1 2 1]; 7 | filter_y = [-1 0 1; -2 0 2; -1 0 1]; 8 | 9 | %applying sobel filter 10 | i_x = filterify(i, filter_x, 'corr'); 11 | i_y = filterify(i, filter_y, 'corr'); 12 | 13 | M = abs(i_x) + abs(i_y); 14 | M = im2uint8(M); 15 | Temp = M; 16 | 17 | %defining threshold 18 | th = 6; 19 | 20 | [rows,cols] = size(M); 21 | 22 | %thresholding 23 | for row = 1:rows 24 | for col = 1:cols 25 | if M(row,col) < 150 26 | M(row,col) = 0; 27 | else 28 | M(row,col) = 255; 29 | end 30 | end 31 | end 32 | 33 | %edge linking algorithm 34 | marker1 = -1; 35 | count = 0; 36 | for row = 1:rows 37 | for col = 1:cols 38 | if M(row,col) == 255 39 | if marker1 ~= -1 40 | marker2 = col; 41 | if count <= th 42 | M(row,marker1:marker2) = 255; 43 | marker1 = marker2; 44 | count = 0; 45 | else 46 | marker1 = marker2; 47 | count = 0; 48 | end 49 | else 50 | marker1 = col; 51 | count = 0; 52 | end 53 | else 54 | count = count + 1; 55 | end 56 | end 57 | end 58 | 59 | %save file to disk 60 | imwrite(M,'bw_edge.jpeg'); 61 | 62 | imshow(M); 63 | figure, imshow(Temp); -------------------------------------------------------------------------------- /edge_linking/filterify.m: -------------------------------------------------------------------------------- 1 | function [ image ] = filterify( img, fil, type ) 2 | %FILTERIFY clone of imfilter 3 | % img is the input image 4 | % fil is the input filter(can be of variable size) 5 | % type can either be 'conv' or 'corr' where 'conv' signifies convolution 6 | % and 'corr' signifies correlation 7 | % there is no padding in this function so the if the filter is a 3x3 8 | % filter, 1 row and 1 column would be omitted and for 5x5, 2 rows and 2 9 | % columns would be omitted in the calculation and so on... 10 | 11 | s = size(img); 12 | t = size(s); 13 | 14 | if t(2) == 3 15 | img = im2double(rgb2gray(img)); 16 | else 17 | img = im2double(img); 18 | end 19 | 20 | fil = im2double(fil); 21 | 22 | %correlation or convolution 23 | %if conv then convolution otherwise correlation 24 | if strcmp(type,'conv') 25 | fil = rot90(fil,2); 26 | end 27 | 28 | [image_rows,image_cols] = size(img); 29 | image = zeros(image_rows,image_cols); 30 | [filter_rows,filter_cols] = size(fil); 31 | 32 | diff_rows = ceil(filter_rows/2); 33 | diff_cols = ceil(filter_cols/2); 34 | 35 | for row = 1:diff_rows 36 | image(row,:) = img(row,:); 37 | end 38 | 39 | for col = 1:diff_cols 40 | image(:,col) = img(:,col); 41 | end 42 | 43 | for row = image_rows-diff_rows:image_rows 44 | image(row,:) = img(row,:); 45 | end 46 | 47 | for col = image_cols-diff_cols:image_cols 48 | image(:,col) = img(:,col); 49 | end 50 | 51 | for row = diff_rows:image_rows-diff_rows+1 52 | for col = diff_cols:image_cols-diff_cols+1 53 | sum = 0; 54 | r = row-1; 55 | c = col-1; 56 | for i = 1:filter_rows 57 | for j = 1:filter_cols 58 | sum = sum + img(r+i-diff_rows+1,c+j-diff_cols+1)*fil(i,j); 59 | end 60 | end 61 | image(row,col) = sum; 62 | end 63 | end 64 | 65 | %scaling matrix to uint8 standards 66 | %min value corresponds to 0 67 | %max value corresponds to 255 68 | %image = image - min(image(:)); 69 | %image = image ./ max(image(:)); 70 | %image = image ./ 255; 71 | 72 | %im2uint8 does automatic scaling! comment out the following line to see 73 | %normalized output, the line is currently commented out to see the 74 | %difference between convolution and correlation in the test script 75 | %otherwise all values above 1 are normalized to 255 76 | %image = im2uint8(image); 77 | end -------------------------------------------------------------------------------- /filters/BerkeleyTower.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/filters/BerkeleyTower.png -------------------------------------------------------------------------------- /filters/filterify.m: -------------------------------------------------------------------------------- 1 | function [ image ] = filterify( img, fil, type ) 2 | %FILTERIFY clone of imfilter 3 | % img is the input image 4 | % fil is the input filter(can be of variable size) 5 | % type can either be 'conv' or 'corr' where 'conv' signifies convolution 6 | % and 'corr' signifies correlation 7 | % there is no padding in this function so the if the filter is a 3x3 8 | % filter, 1 row and 1 column would be omitted and for 5x5, 2 rows and 2 9 | % columns would be omitted in the calculation and so on... 10 | 11 | s = size(img); 12 | t = size(s); 13 | 14 | if t(2) == 3 15 | img = im2double(rgb2gray(img)); 16 | else 17 | img = im2double(img); 18 | end 19 | 20 | fil = im2double(fil); 21 | 22 | %correlation or convolution 23 | %if conv then convolution otherwise correlation 24 | if strcmp(type,'conv') 25 | fil = rot90(fil,2); 26 | end 27 | 28 | [image_rows,image_cols] = size(img); 29 | image = zeros(image_rows,image_cols); 30 | [filter_rows,filter_cols] = size(fil); 31 | 32 | diff_rows = ceil(filter_rows/2); 33 | diff_cols = ceil(filter_cols/2); 34 | 35 | for row = 1:diff_rows 36 | image(row,:) = img(row,:); 37 | end 38 | 39 | for col = 1:diff_cols 40 | image(:,col) = img(:,col); 41 | end 42 | 43 | for row = image_rows-diff_rows:image_rows 44 | image(row,:) = img(row,:); 45 | end 46 | 47 | for col = image_cols-diff_cols:image_cols 48 | image(:,col) = img(:,col); 49 | end 50 | 51 | for row = diff_rows:image_rows-diff_rows+1 52 | for col = diff_cols:image_cols-diff_cols+1 53 | sum = 0; 54 | r = row-1; 55 | c = col-1; 56 | for i = 1:filter_rows 57 | for j = 1:filter_cols 58 | sum = sum + img(r+i-diff_rows+1,c+j-diff_cols+1)*fil(i,j); 59 | end 60 | end 61 | image(row,col) = sum; 62 | end 63 | end 64 | 65 | %scaling matrix to uint8 standards 66 | %min value corresponds to 0 67 | %max value corresponds to 255 68 | %image = image - min(image(:)); 69 | %image = image ./ max(image(:)); 70 | %image = image ./ 255; 71 | 72 | %im2uint8 does automatic scaling! comment out the following line to see 73 | %normalized output, the line is currently commented out to see the 74 | %difference between convolution and correlation in the test script 75 | %otherwise all values above 1 are normalized to 255 76 | %image = im2uint8(image); 77 | end -------------------------------------------------------------------------------- /filters/filters.m: -------------------------------------------------------------------------------- 1 | img = imread('text.jpg'); 2 | 3 | %applying sobel filter using my algorithm using correlation 4 | filter = fspecial('sobel'); 5 | my_img = filterify(img,filter,'corr'); 6 | 7 | %applying sobel filter using imfilter algorithm 8 | filter_img = imfilter(img,filter,'replicate'); 9 | 10 | %comparing the 2 images 11 | imshow(my_img); 12 | title('My filter function'); 13 | figure, imshow(filter_img); 14 | title('Default filter function'); 15 | 16 | %code to compare correlation and convolution 17 | img = zeros(5); 18 | img(3,3) = 1; 19 | 20 | filter = [1 2 3;4 5 6;7 8 9]; 21 | 22 | disp('Correlation:'); 23 | x = filterify(img,filter,'corr'); 24 | disp(x); 25 | 26 | disp('Convolution:'); 27 | y = filterify(img,filter,'conv'); 28 | disp(y); -------------------------------------------------------------------------------- /filters/text.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/filters/text.jpg -------------------------------------------------------------------------------- /global_thresholding/global_t.m: -------------------------------------------------------------------------------- 1 | img = imread('low1.jpg'); 2 | %img = rgb2gray(img); 3 | 4 | %no of pixels on each intensity from 0 to 255 5 | hist = imhist(img); 6 | [hist_size,a] = size(hist); 7 | 8 | 9 | %starting threshold 10 | %splitting point to divide image into 2 classes c1 & c2 11 | thresh = 190; 12 | delta = 5; 13 | 14 | c1_min = 0; 15 | c1_max = 0; 16 | c2_min = 0; 17 | c2_max = 0; 18 | 19 | while 1 20 | %find min and max intensity values for each class 21 | for i = 1:thresh 22 | if hist(i) > 0 23 | c1_min = i; 24 | break; 25 | end 26 | end 27 | 28 | for i = 1:thresh 29 | if hist(i) > 0 30 | c1_max = i; 31 | end 32 | end 33 | 34 | for i = thresh+1:hist_size 35 | if hist(i) > 0 36 | c2_min = i; 37 | break; 38 | end 39 | end 40 | 41 | for i = thresh+1:hist_size 42 | if hist(i) > 0 43 | c2_max = i; 44 | end 45 | end 46 | 47 | %calculate mean thresholds for each class 48 | m1 = (c1_min + c1_max - 2) / 2; 49 | m2 = (c2_min + c2_max - 2) / 2; 50 | 51 | new_thresh = (m1 + m2) / 2; 52 | 53 | delta_t = abs(new_thresh - thresh); 54 | thresh = floor(new_thresh); 55 | 56 | if delta_t <= delta 57 | break 58 | end 59 | end 60 | 61 | 62 | %apply thresholding using new threshold 63 | [rows,cols] = size(img); 64 | 65 | for row = 1:rows 66 | for col = 1:cols 67 | if img(row,col) >= thresh 68 | img(row,col) = 255; 69 | else 70 | img(row,col) = 0; 71 | end 72 | end 73 | end 74 | 75 | imwrite(img, 'low1_global.jpg'); 76 | %imwrite(img, 'low2_global.jpg'); 77 | imshow(img); -------------------------------------------------------------------------------- /global_thresholding/low1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/global_thresholding/low1.jpg -------------------------------------------------------------------------------- /global_thresholding/low1_global.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/global_thresholding/low1_global.jpg -------------------------------------------------------------------------------- /global_thresholding/low2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/global_thresholding/low2.jpg -------------------------------------------------------------------------------- /global_thresholding/low2_global.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/global_thresholding/low2_global.jpg -------------------------------------------------------------------------------- /gradient/filterify.m: -------------------------------------------------------------------------------- 1 | function [ image ] = filterify( img, fil, type ) 2 | %FILTERIFY clone of imfilter 3 | % img is the input image 4 | % fil is the input filter(can be of variable size) 5 | % type can either be 'conv' or 'corr' where 'conv' signifies convolution 6 | % and 'corr' signifies correlation 7 | % there is no padding in this function so the if the filter is a 3x3 8 | % filter, 1 row and 1 column would be omitted and for 5x5, 2 rows and 2 9 | % columns would be omitted in the calculation and so on... 10 | 11 | s = size(img); 12 | t = size(s); 13 | 14 | if t(2) == 3 15 | img = im2double(rgb2gray(img)); 16 | else 17 | img = im2double(img); 18 | end 19 | 20 | fil = im2double(fil); 21 | 22 | %correlation or convolution 23 | %if conv then convolution otherwise correlation 24 | if strcmp(type,'conv') 25 | fil = rot90(fil,2); 26 | end 27 | 28 | [image_rows,image_cols] = size(img); 29 | image = zeros(image_rows,image_cols); 30 | [filter_rows,filter_cols] = size(fil); 31 | 32 | diff_rows = ceil(filter_rows/2); 33 | diff_cols = ceil(filter_cols/2); 34 | 35 | for row = 1:diff_rows 36 | image(row,:) = img(row,:); 37 | end 38 | 39 | for col = 1:diff_cols 40 | image(:,col) = img(:,col); 41 | end 42 | 43 | for row = image_rows-diff_rows:image_rows 44 | image(row,:) = img(row,:); 45 | end 46 | 47 | for col = image_cols-diff_cols:image_cols 48 | image(:,col) = img(:,col); 49 | end 50 | 51 | for row = diff_rows:image_rows-diff_rows+1 52 | for col = diff_cols:image_cols-diff_cols+1 53 | sum = 0; 54 | r = row-1; 55 | c = col-1; 56 | for i = 1:filter_rows 57 | for j = 1:filter_cols 58 | sum = sum + img(r+i-diff_rows+1,c+j-diff_cols+1)*fil(i,j); 59 | end 60 | end 61 | image(row,col) = sum; 62 | end 63 | end 64 | 65 | %scaling matrix to uint8 standards 66 | %min value corresponds to 0 67 | %max value corresponds to 255 68 | %image = image - min(image(:)); 69 | %image = image ./ max(image(:)); 70 | %image = image ./ 255; 71 | 72 | %im2uint8 does automatic scaling! comment out the following line to see 73 | %normalized output, the line is currently commented out to see the 74 | %difference between convolution and correlation in the test script 75 | %otherwise all values above 1 are normalized to 255 76 | %image = im2uint8(image); 77 | end -------------------------------------------------------------------------------- /gradient/gradients.m: -------------------------------------------------------------------------------- 1 | i = imread('xray.jpg'); 2 | i = rgb2gray(i); 3 | 4 | %define prewitt filter 5 | prewitt_x = [-1 -1 -1; 0 0 0; 1 1 1]; 6 | prewitt_y = [-1 0 1; -1 0 1; -1 0 1]; 7 | 8 | %define sobel filter 9 | sobel_x = [-1 -2 -1; 0 0 0; 1 2 1]; 10 | sobel_y = [-1 0 1; -2 0 2; -1 0 1]; 11 | 12 | %applying prewitt filter 13 | i_p_x = filterify(i,prewitt_x,'corr'); 14 | i_p_y = filterify(i,prewitt_y,'corr'); 15 | 16 | %M matrix 17 | M_prewitt = abs(i_p_x) + abs(i_p_y); 18 | 19 | %alpha matrix in degrees 20 | Alpha_prewitt = atand(i_p_y/i_p_x); 21 | 22 | %save to disk 23 | imwrite(M_prewitt,'xray_M_prewitt.jpg'); 24 | imwrite(Alpha_prewitt, 'xray_Alpha_prewitt.jpg'); 25 | 26 | imshow(M_prewitt); 27 | figure, imshow(Alpha_prewitt); 28 | 29 | %applying sobel filter 30 | i_s_x = filterify(i,sobel_x,'corr'); 31 | i_s_y = filterify(i,sobel_y,'corr'); 32 | 33 | %M matrix 34 | M_sobel = abs(i_s_x) + abs(i_s_y); 35 | 36 | %alpha matrix in degrees 37 | Alpha_sobel = atand(i_s_y/i_s_x); 38 | 39 | %save to disk 40 | imwrite(M_sobel,'xray_M_sobel.jpg'); 41 | imwrite(Alpha_sobel, 'xray_Alpha_sobel.jpg'); 42 | 43 | figure, imshow(M_sobel); 44 | figure, imshow(Alpha_sobel); -------------------------------------------------------------------------------- /gradient/xray.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/gradient/xray.jpg -------------------------------------------------------------------------------- /gradient/xray_Alpha_prewitt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/gradient/xray_Alpha_prewitt.jpg -------------------------------------------------------------------------------- /gradient/xray_Alpha_sobel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/gradient/xray_Alpha_sobel.jpg -------------------------------------------------------------------------------- /gradient/xray_M_prewitt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/gradient/xray_M_prewitt.jpg -------------------------------------------------------------------------------- /gradient/xray_M_sobel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/gradient/xray_M_sobel.jpg -------------------------------------------------------------------------------- /grayscale/grayscale.m: -------------------------------------------------------------------------------- 1 | %grayscale image plot stem histogram 2 | img = imread('map.png'); 3 | img = rgb2gray(img); 4 | 5 | xaxis = linspace(0,255,50); 6 | yaxis = imhist(img,50); 7 | 8 | imwrite(img,'map_bw.png'); 9 | 10 | stem(xaxis,yaxis); 11 | title('grayscale'); -------------------------------------------------------------------------------- /grayscale/map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/grayscale/map.png -------------------------------------------------------------------------------- /grayscale/map_bw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/grayscale/map_bw.png -------------------------------------------------------------------------------- /histogram_equalization/histogram_eq.m: -------------------------------------------------------------------------------- 1 | img = imread('map.png'); 2 | img = rgb2gray(img); 3 | 4 | img = histeq(img); 5 | 6 | imwrite(img, 'map_hist_eq.png'); 7 | 8 | xaxis = linspace(0,255,50); 9 | yaxis = imhist(img,50); 10 | 11 | stem(xaxis,yaxis); 12 | title('histogram equilization'); -------------------------------------------------------------------------------- /histogram_equalization/map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/histogram_equalization/map.png -------------------------------------------------------------------------------- /histogram_equalization/map_hist_eq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/histogram_equalization/map_hist_eq.png -------------------------------------------------------------------------------- /image_intensity_and_contrast_variation/BerkeleyTower.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/image_intensity_and_contrast_variation/BerkeleyTower.png -------------------------------------------------------------------------------- /image_intensity_and_contrast_variation/BerkeleyTower_contrast.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/image_intensity_and_contrast_variation/BerkeleyTower_contrast.png -------------------------------------------------------------------------------- /image_intensity_and_contrast_variation/BerkeleyTower_intensity.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/image_intensity_and_contrast_variation/BerkeleyTower_intensity.png -------------------------------------------------------------------------------- /image_intensity_and_contrast_variation/intensity_variation.m: -------------------------------------------------------------------------------- 1 | %increase or decrease intensity by multiplying pixel values by a number 2 | img = imread('BerkeleyTower.png'); 3 | %increase intensity 4 | intensity_factor = 2; 5 | 6 | %decrease intensity 7 | %intensity_factor = 1/2; 8 | 9 | img = img * intensity_factor; 10 | 11 | imwrite(img, 'BerkeleyTower_intensity.png'); 12 | 13 | %contrast variation 14 | img = imread('BerkeleyTower.png'); 15 | img = rgb2gray(img); 16 | [rows,cols] = size(img); 17 | 18 | for row = 1:rows 19 | for col = 1:cols 20 | if img(row,col) > 60 && img(row,col) < 80 21 | img(row,col) = 180; 22 | elseif img(row,col) < 60 && img(row,col) > 80 23 | img(row,col) = 30; 24 | end 25 | end 26 | end 27 | 28 | imwrite(img, 'BerkeleyTower_contrast.png'); -------------------------------------------------------------------------------- /image_inversion/BerkeleyTower.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/image_inversion/BerkeleyTower.png -------------------------------------------------------------------------------- /image_inversion/BerkeleyTower_inverted.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/image_inversion/BerkeleyTower_inverted.png -------------------------------------------------------------------------------- /image_inversion/Rachmaninoff.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/image_inversion/Rachmaninoff.jpg -------------------------------------------------------------------------------- /image_inversion/Rachmaninoff_bw_inverted.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/image_inversion/Rachmaninoff_bw_inverted.jpg -------------------------------------------------------------------------------- /image_inversion/invert.m: -------------------------------------------------------------------------------- 1 | %black and white invert 2 | img = imread('Rachmaninoff.jpg'); 3 | img = rgb2gray(img); 4 | [rows, cols] = size(img); 5 | 6 | for row = 1:rows 7 | for col = 1:cols 8 | img(row,col) = 255 - img(row,col); 9 | end 10 | end 11 | 12 | imwrite(img, 'Rachmaninoff_bw_inverted.jpg'); 13 | 14 | %color invert 15 | img = imread('BerkeleyTower.png'); 16 | [rows, cols, colours] = size(img); 17 | 18 | for row = 1:rows 19 | for col = 1:cols 20 | for colour = 1:colours 21 | img(row,col,colour) = 255 - img(row,col,colour); 22 | end 23 | end 24 | end 25 | 26 | imwrite(img, 'BerkeleyTower_inverted.png'); 27 | -------------------------------------------------------------------------------- /intensity_transform/downtown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown.png -------------------------------------------------------------------------------- /intensity_transform/downtown_c1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_c1.png -------------------------------------------------------------------------------- /intensity_transform/downtown_c2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_c2.png -------------------------------------------------------------------------------- /intensity_transform/downtown_c3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_c3.png -------------------------------------------------------------------------------- /intensity_transform/downtown_c4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_c4.png -------------------------------------------------------------------------------- /intensity_transform/downtown_c5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_c5.png -------------------------------------------------------------------------------- /intensity_transform/downtown_g1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_g1.png -------------------------------------------------------------------------------- /intensity_transform/downtown_g2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_g2.png -------------------------------------------------------------------------------- /intensity_transform/downtown_g3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_g3.png -------------------------------------------------------------------------------- /intensity_transform/downtown_l1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_l1.png -------------------------------------------------------------------------------- /intensity_transform/downtown_l2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_l2.png -------------------------------------------------------------------------------- /intensity_transform/downtown_l3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_l3.png -------------------------------------------------------------------------------- /intensity_transform/downtown_l4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_l4.png -------------------------------------------------------------------------------- /intensity_transform/downtown_l5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_l5.png -------------------------------------------------------------------------------- /intensity_transform/downtown_negative.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transform/downtown_negative.png -------------------------------------------------------------------------------- /intensity_transform/transform.m: -------------------------------------------------------------------------------- 1 | img = imread('downtown.png'); 2 | img = rgb2gray(img); 3 | 4 | %negative 5 | neg_img = imcomplement(img); 6 | imwrite(neg_img, 'downtown_negative.png'); 7 | 8 | %gamma transformations 9 | %gamma = 1 10 | g1 = imadjust(img,[],[],1); 11 | %gamma = 3 12 | g2 = imadjust(img,[],[],3); 13 | %gamma = 0.4 14 | g3 = imadjust(img,[],[],0.4); 15 | 16 | imwrite(g1, 'downtown_g1.png'); 17 | imwrite(g2, 'downtown_g2.png'); 18 | imwrite(g3, 'downtown_g3.png'); 19 | 20 | %log transforms 21 | im = im2double(img); 22 | 23 | %c = 1 24 | l1 = 1*log(1+im); 25 | %c = 2 26 | l2 = 2*log(1+im); 27 | %c = 5 28 | l3 = 4*log(1+im); 29 | 30 | imwrite(l1, 'downtown_l1.png'); 31 | imwrite(l2, 'downtown_l2.png'); 32 | imwrite(l3, 'downtown_l3.png'); 33 | 34 | %log on domain 35 | %log on domain [0,1] 36 | f = im; 37 | c = 1/log(1+1); 38 | l4 = c*log(1+f); 39 | 40 | %log on domain [0,255] 41 | f = im * 255; 42 | c = 1/log(1+255); 43 | l5 = c*log(1+f); 44 | 45 | imwrite(l4, 'downtown_l4.png'); 46 | imwrite(l5, 'downtown_l5.png'); 47 | 48 | %contrast stretching transform by changing E 49 | m = mean2(im); 50 | %E = 4 51 | c1 = 1./(1+(m./(im+eps)).^4); 52 | %E = 5 53 | c2 = 1./(1+(m./(im+eps)).^5); 54 | %E = 10 55 | c3 = 1./(1+(m./(im+eps)).^10); 56 | 57 | imwrite(c1, 'downtown_c1.png'); 58 | imwrite(c2, 'downtown_c2.png'); 59 | imwrite(c3, 'downtown_c3.png'); 60 | 61 | %contrast stretching transform by changing m with constant e = 4 62 | %m = 0.3 63 | c4 = 1./(1+(0.3./(im+eps)).^4); 64 | %m = 0.6 65 | c5 = 1./(1+(0.6./(im+eps)).^4); 66 | 67 | imwrite(c4, 'downtown_c4.png'); 68 | imwrite(c5, 'downtown_c5.png'); 69 | -------------------------------------------------------------------------------- /intensity_transformation/intensity.m: -------------------------------------------------------------------------------- 1 | %intensity transformations and stem histogram 2 | 3 | img = imread('map.png'); 4 | img = rgb2gray(img); 5 | [rows, cols] = size(img); 6 | 7 | %add 50 to intensity 8 | img2 = img; 9 | 10 | for row = 1:rows 11 | for col = 1:cols 12 | img2(row,col) = img2(row,col) + 50; 13 | end 14 | end 15 | 16 | imwrite(img2, 'map_intensity_plus_50.png'); 17 | 18 | xaxis = linspace(0,255,50); 19 | yaxis = imhist(img2,50); 20 | stem(xaxis,yaxis); 21 | title('intensity plus 50'); 22 | 23 | %image negative 24 | img2 = img; 25 | 26 | for row = 1:rows 27 | for col = 1:cols 28 | img2(row,col) = 255 - img2(row,col); 29 | end 30 | end 31 | 32 | imwrite(img2, 'map_negative.png'); 33 | 34 | yaxis = imhist(img2,50); 35 | figure, stem(xaxis,yaxis); 36 | title('image negative'); 37 | 38 | %gamma transformation 39 | img2 = img; 40 | 41 | %gamma = 3 42 | g = imadjust(img2,[],[],3); 43 | imwrite(g, 'map_gamma3.png'); 44 | yaxis = imhist(g,50); 45 | figure, stem(xaxis,yaxis); 46 | title('gamma = 3'); 47 | 48 | %gamma = 0.4 49 | g = imadjust(img2,[],[],0.4); 50 | imwrite(g, 'map_gamma0.4.png'); 51 | yaxis = imhist(g,50); 52 | figure, stem(xaxis,yaxis); 53 | title('gamma = 0.4'); -------------------------------------------------------------------------------- /intensity_transformation/map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transformation/map.png -------------------------------------------------------------------------------- /intensity_transformation/map_gamma0.4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transformation/map_gamma0.4.png -------------------------------------------------------------------------------- /intensity_transformation/map_gamma3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transformation/map_gamma3.png -------------------------------------------------------------------------------- /intensity_transformation/map_intensity_plus_50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transformation/map_intensity_plus_50.png -------------------------------------------------------------------------------- /intensity_transformation/map_negative.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/intensity_transformation/map_negative.png -------------------------------------------------------------------------------- /scaling_translation/box.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/scaling_translation/box.png -------------------------------------------------------------------------------- /scaling_translation/scaled_box.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/scaling_translation/scaled_box.png -------------------------------------------------------------------------------- /scaling_translation/scaling_translation.m: -------------------------------------------------------------------------------- 1 | %generate image 2 | img = zeros(1000); 3 | for i = 100:200 4 | for j = 100:300 5 | img(i,j) = 255; 6 | end 7 | end 8 | 9 | i = img; 10 | imwrite(img, 'box.png'); 11 | 12 | %scaling 13 | scaling_factor = 3; 14 | for row = 100:200 15 | for col = 100:300 16 | img(scaling_factor*row, scaling_factor*col) = img(row,col); 17 | img(row,col) = 0; 18 | end 19 | end 20 | 21 | imwrite(img, 'scaled_box.png'); 22 | 23 | %translation 24 | translation_index_x = 400; 25 | translation_index_y = 600; 26 | for row = 100:200 27 | for col = 100:300 28 | i(row+translation_index_x,col+translation_index_y) = i(row,col); 29 | i(row,col) = 0; 30 | end 31 | end 32 | 33 | imwrite(i, 'translated_box.png'); -------------------------------------------------------------------------------- /scaling_translation/translated_box.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaiverma/MATLAB/b5ae39849ad3c68be9de54a740fd535df310b121/scaling_translation/translated_box.png --------------------------------------------------------------------------------