DIP Assignment
DIP Assignment
disp(imageInof);
[height,width,numChannels] = size(img);
fprintf('Number of channels:%d\n',numChannels);
redChannel = img(:,:,1);
greenChannel =img(:,:,2);
blueChannel =img(:,:,3)
imshow(blueChannel);
title('blue Channel');
imwrite(img,'Output_image.png');
imwrite(image, 'original.jpg');
grayscale = rgb2gray(image);
imwrite(grayscale, 'grayscale.jpg');
hsvImage = rgb2hsv(image);
imwrite(hsvAsRGB, 'hsvimage.jpg');
% LAB cannot be saved directly, so normalize and convert to RGB for saving
imwrite(labAsRGB, 'labimage.jpg');
imshow(image);
if size(image,3) == 3
newImage = rgb2gray(image);
end
imshow(newImage);
yrow = 100;
xcol = 150;
pixel_value = newImage(yrow,xcol);
imshow(image);
title('RGB Image');
redvalue = image(row,col,1);
bluevalue = image(row,col,3);
% Resize image
subplot(232); imshow(resized_img);
title('Resized Image');
% Convert to grayscale
gray_img = rgb2gray(img);
subplot(223); imshow(gray_img);
title('Grayscale Image');
% Histogram Equalization
equalized_img = histeq(gray_img);
subplot(231); imshow(equalized_img);
figure
imshow(img)
title('Original Image');
% 2. Resize
figure
imshow(resized_img)
title('Resized Image');
% Convert to grayscale
gray_img = rgb2gray(img);
figure
imshow(gray_img)
title('Grayscale Image');
% Gaussian Filtering
figure
imshow(smoothed_img)
figure
imshow(img)
title('Original Image');
gray_img = rgb2gray(img);
figure
subplot(1,2,1)
imshow(edges_sobel)
subplot(1,2,2)
imshow(edges_canny)
% Convert to grayscale
grayImg = rgb2gray(img);
% Thresholding
level = 105;
% Show images
imwrite(holeImg, 'Graylevel_Thresholding_blend.png');
level = graythresh(img);
% Show images
imwrite(bwImg, 'Global_Thresholding_bw.png');
% Plot histogram
figure;
[counts, x] = imhist(img);
% Save histogram
saveas(gcf, 'Global_Thresholding_hist.png');
img = imread('img.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
level = graythresh(img);
% Show images
% Save images
imwrite(bwImg, 'Region_Labeling_bw.png');
imwrite(rgbLabel, 'Region_Labeling_rgbLabel.png');
img = imread('img.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
level = 105;
bwImg = img < level; % Foreground = white (logical 1), Background = black (logical 0)
% Fill holes in the binary image
% Show images
% Save images
img = imread('img.jpg');
BW1 = imdilate(img,se1);
% Save images
imwrite(BW1, 'Dilation_disk_10.png');
imwrite(BW2, 'Dilation_disk_20.png');
img = imread('img.jpg');
BW1 = imerode(img,se1);
% Show images
% Save images
imwrite(BW1, 'Erosion_disk_3.png');
imwrite(BW2, 'Erosion_disk_7.png');
img = imread('img.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);
h1 = [1, 0; 0, -1];
% Show results
figure,
imwrite(im2uint8(filteredImg1), 'Roberts_face_vertical.png');
imwrite(im2uint8(filteredImg2), 'Roberts_face_horizontal.png');
% Apply thresholds
figure,
% Save images
imwrite(im2uint8(logEdgeSum), 'Roberts_face_logEdgeSum.png');
imwrite(bwEdge1, 'Roberts_face_bwEdge1.png');
imwrite(bwEdge2, 'Roberts_face_bwEdge2.png');
imwrite(bwEdge3, 'Roberts_face_bwEdge3.png');
img = imread('img.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);
h = fspecial('sobel');
% Apply thresholds
% Display results
figure(4), clf;
% Save results
imwrite(im2uint8(logEdgeSum), 'Sobel_face_logEdgeSum.png');
imwrite(bwEdge1, 'Sobel_face_bwEdge1.png');
imwrite(bwEdge2, 'Sobel_face_bwEdge2.png');
imwrite(bwEdge3, 'Sobel_face_bwEdge3.png');
16. Write a program for Prewitt Edge Detection
(Horizontal & Vertical with abs & with log scaling &
thresholding)
%% Prewitt - img.jpg
img = imread('img.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);
h = fspecial('prewitt');
filteredImg1 = abs(filteredImg1);
filteredImg2 = abs(filteredImg2);
figure(1), clf;
% Save
imwrite(im2uint8(filteredImg1), 'Prewitt_bike_vertical.png');
imwrite(im2uint8(filteredImg2), 'Prewitt_bike_horizontal.png');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);
h = fspecial('prewitt');
% Display
figure(2), clf;
% Save
imwrite(im2uint8(filteredImg1), 'Prewitt_face_vertical.png');
imwrite(im2uint8(filteredImg2), 'Prewitt_face_horizontal.png');
% Load image
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);
% Create Prewitt kernels
h = fspecial('prewitt');
% Apply thresholds
% Show results
figure(3), clf;
% Save results
imwrite(im2uint8(logEdgeSum), 'Prewitt_face_logEdgeSum.png');
imwrite(bwEdge1, 'Prewitt_face_bwEdge1.png');
imwrite(bwEdge2, 'Prewitt_face_bwEdge2.png');
imwrite(bwEdge3, 'Prewitt_face_bwEdge3.png');
if size(img, 3) == 3
img = rgb2gray(img);
end
% Parameters
thresh = 0.3;
figure(1), clf;
for i = 1:numel(sigmaArray)
sigma = sigmaArray(i);
imwrite(bw, filename);
end
img = imread('img.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
sigma = sigmaArray(1);
% Display
figure(2), clf;
% Save result
imwrite(bw, filename);