Dip Programs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

3)Program for edge detection algorithm:

clc;
close all;
originalImage = imread('onion.png');
grayImage = rgb2gray(originalImage);

sobelHorizontal = [-1,0,1;-2,0,2;-1,0,1];
sobelvertical =[-1,-2,-1;0,0,0;1,2,1];
[rows,cols] = size(grayImage);
gradientMagnitude = zeros(rows,cols);
for i = 2:rows-1 for j = 2:cols-1
gx = sum(sum(double(grayImage(i-1:i+1, j-1:j+1)) .* sobelHorizontal));
gy = sum(sum(double(grayImage(i-1:i+1, j-1:j+1)) .* sobelvertical));

gradientMagnitude(i,j) = sqrt(gx^2 + gy^2);


end
end
gradientMagnitude = uint8(255 * (gradientMagnitude /
max(max(gradientMagnitude))));
subplot(3,1,1);imshow(originalImage);title('original Image');
subplot(3,1,2);imshow(grayImage);title('original grayscale Image');
subplot(3,1,3);imshow(gradientMagnitude);title('Edge Detection Image');
Output:
4)Program sharpen image gradient mask:
clc;
close all;
originalImage = imread('C:\Users\SYSTEM25\Desktop\computer.jpg');
originalImage = im2double(originalImage);

laplaciankernel = [0,-1,0;-1,4,-1;0,-1,0];

[rows,cols,~] = size(originalImage);
sharpenedImage = zeros(rows,cols,3);

for channel = 1:3 for i=2: rows-1


for j = 2:cols-1
convolution = sum(sum(originalImage(i-1:i+1, j-1:j+1, channel)
.*laplaciankernel));
sharpenedImage(i,j,channel) = originalImage(i,j,channel) + convolution;
end
end
end
sharpenedImage = max(0, min( sharpenedImage,1));subplot(1,2,1);
imshow(originalImage);title('Original image');
subplot(1,2,2);imshow( sharpenedImage);title(‘sharpened Image');

output:
5)Program for Display of an Image, negative of an
Image(Binary&Gray scale):
clc;
close all;
input_image=imread('C:\Users\SYSTEM25\Desktop\computer.jpg');
subplot(1,2,1);
imshow(input_image);
title('Original Image');

negative_image=255-input_image;

subplot(1,2,2);
imshow(negative_image);
title('negative of Original Image');

Output:
7)Program for implementation of transformation of an
image:
clc;
close all;
original_image=imread('C:\Users\SYSTEM25\Desktop\rose2.jpg');
input_image=rgb2gray(original_image);
subplot(2,3,2);
imshow(input_image);
title('Original Image');

%Image Naegative Transform


negative_image=255-input_image;
subplot(2,3,4);
imshow(negative_image);
title('Negative Image');

%Logarithmic Transform
L=255;
c=L/log10(1+L);
log_transform=c*log10(1+double(input_image));
subplot(2,3,5);
imshow(uint8(log_transform));
title('Log Transformation');

%Power Law Transform


gamma=1.1;
power_law_transform=double(input_image).^gamma;
subplot(2,3,6);
imshow(uint8(power_law_transform));
title('Power Law Transformation');
xlabel(sprintf('gamma = %g',gamma));

Output:
8)program for contrast stretching of a low contrast
image,Histogram,andHistogram Equualization:
clc;
close all;
inputImage=imread('C:\Users\SYSTEM25\Desktop\e.jpg');
if size(inputImage,3)==3
inputImage = rgb2gray(inputImage);end
grayImage=im2double(inputImage);
%Apply contrast stretching to enhance the image contrast

minIntensity=min(grayImage(:));
maxIntensity=max(grayImage(:));
contrastStretchedImage=(grayImage-minIntensity)/(maxIntensity-minIntensity);
%Computer the histogram of the original and contrast-stretched images
originalHistogram=computeHistogram(grayImage);
contrastStretchedHistogram=computeHistogram(contrastStretchedImage);
%perfrom histogram equalization to enhance the image contrast
histEqualizedImage=histogramEqualization(grayImage);
%compute the histogram of the equalized image
equalizedHistogram = computeHistogram(histEqualizedImage);
subplot(3,2,1);imshow(grayImage);title('Original Image');
subplot(3,2,2);bar(originalHistogram);title('Original Image');
subplot(3,2,3);imshow(contrastStretchedImage);title('contrast Stretched
Image');
subplot(3,2,4);bar(contrastStretchedHistogram);title('Histogram(contrast
Stretched Image)');

subplot(3,2,5);imshow(histEqualizedImage);title('Histogram Equalized
Image');
subplot(3,2,6);bar(equalizedHistogram);title('Histogram Equalized Image');

function histogram = computeHistogram(image)


histogram=zeros(256,1);
[rows,cols]=size(image);
for i=1:rows
for j=1:cols
intensity =
round(image(i,j)*255)+1;histogram(intensity)=histogram(intensity)+1;
end
end
end
function equalizedImage = histogramEqualization(image)
histogram = computeHistogram(image);
%compute the cumulative distribution function(CDF)
cdf=cumsum(histogram)/sum(histogram);
%perform histogram equalization
[rows,cols]=size(image);
equalizedImage = zeros(rows,cols);
for i=1:rows
for j=1:cols
intensity=round(image(i,j)*255)+1;equalizedImage(i,j)=cdf(intensity);
end
end
end
9)program for display of bit plane of an Image:
clc;
close all;
input_image=imread('C:\Users\SYSTEM25\Desktop\rose.jpg');
[row,col]=size(input_image);
%extract 7th bit plane
for i=1:row
for j=1:col
bit_7(i,j)=bitand(input_image(i,j),250);
end
end
%extract 6th bit plane
for i=1:row
for j=1:col bit_6(i,j)=bitand(input_image(i,j),100);
end
end
%extract 5th bit plane
for i=1:row
for j=1:col bit_5(i,j)=bitand(input_image(i,j),50);
end
end
%extract 4th bit plane
for i=1:row
for j=1:col bit_4(i,j)=bitand(input_image(i,j),16);
end
end
%extract 3th bit plane
for i=1:row
for j=1:col bit_3(i,j)=bitand(input_image(i,j),8);
end
end
%extract 2th bit plane
for i=1:row
for j=1:col bit_2(i,j)=bitand(input_image(i,j),4);
end
end
%extract 1th bit plane
for i=1:row
for j=1:col bit_1(i,j)=bitand(input_image(i,j),2);
end
end
%extract 0th bit plane
for i=1:row
for j=1:col bit_0(i,j)=bitand(input_image(i,j),1);
end
end
subplot(4,3,1),imshow(input_image),title('Original Image');
subplot(4,3,5),imshow(bit_7),title('7th bit plane');
subplot(4,3,6),imshow(bit_6),title('6th bit plane');
subplot(4,3,7),imshow(bit_5),title('5th bit plane');
subplot(4,3,8),imshow(bit_4),title('4th bit plane');
subplot(4,3,9),imshow(bit_3),title('3th bit plane');
subplot(4,3,10),imshow(bit_2),title('2th bit plane');
subplot(4,3,11),imshow(bit_1),title('1th bit plane');
subplot(4,3,12),imshow(bit_0),title('0th bit plane');
Output:
12)program for Implementation of Image smoothing Filters:
clc;
close all;
inputImage = imread('C:\Users\SYSTEM25\Desktop\rose.jpg');
inputImage = im2double(inputImage);
%set the kernel size for the filter(e.g,3*3or4*4)
kernelSize = 5;
meanFiltered = meanFilter(inputImage,kernelSize);
medianFiltered = medianFilter(inputImage,kernelSize);
subplot(1,3,1);imshow(inputImage);title('Original Image');
subplot(1,3,2);imshow(meanFiltered);title('Mean Filtered Image');
subplot(1,3,3);imshow(medianFiltered);title('Median Filtered Image');
function outputImage = meanFilter(inputImage,kernelSize)
[rows,cols] = size(inputImage);
outputImage = zeros(rows,cols);
padSize = floor(kernelSize/2);
paddedImage = padarray(inputImage,[padSize,padSize],'replicate');
for y =1:rows
for x=1:cols
neighbors = paddedImage(y:y + kernelSize - 1, x:x + kernelSize - 1);
outputImage(y,x) = mean(neighbors(:));
end
end
end
function outputImage = medianFilter(inputImage,kernelSize)
[rows,cols] = size(inputImage);
outputImage = zeros(rows,cols);
padSize = floor(kernelSize / 2);
paddedImage = padarray(inputImage,[padSize,padSize],'replicate');
for y = 1:rows
for x = 1:cols
neighbors = paddedImage(y:y + kernelSize - 1, x:x + kernelSize - 1);
outputImage(y,x) = mean(neighbors(:));
end
end
end

OUTPUT:
16)program for Implementation of Image Intensity slicing
technique for image enhancement:
clc;
close all;
input_image=imread('C:\Users\SYSTEM25\Desktop\doremon.jpg');
if size(input_image,3)==3
input_image=rgb2gray(input_image);
end
lower_threshold=120;
upper_threshold=200;
new_intensity=255;
enhanced_image=intensitySlicing(input_image,lower_threshold,upper_threshold,n
ew_intensity);
subplot(1,2,1);imshow(input_image);title('Original Image');
subplot(1,2,2);imshow(enhanced_image);title('Enhanced Image');
function
enhanced_image=intensitySlicing(input_image,lower_threshold,upper_threshold,n
ew_intensity)
[height,width]=size(input_image);
enhanced_image=zeros(height,width);
for y=1:height
for x=1:width
intensity=input_image(y,x);
if intensity >=lower_threshold && intensity <=upper_threshold
enhanced_image(y,x)=new_intensity;
else
enhanced_image(y,x)=intensity;
end
end
end
enhanced_image=cast(enhanced_image,class(input_image));
end

Output:
17)program for canny edge detection Algorithm:
clc;
close all;

inputImage = imread('onion.png');
if size(inputImage,3)==3
inputImage = rgb2gray(inputImage);
end
cannyEdges = edge(inputImage,'canny');
subplot(1,2,1);imshow(inputImage);title('Original Image');
subplot(1,2,2);imshow(cannyEdges);title('canny Edges');

Output:
18)program to obtain histogram equalization concept:
clc;
close all;
input_image = imread('cameraman.tif');
if size(input_image,3)==3
input_image = rgb2gray(input_image);
end
subplot(2,2,1);imshow(input_image);title('Original Image');
subplot(2,2,3);imhist(input_image);axis tight;title('Bfr Histogram
Equilization');
[rows,cols] = size(input_image);
total_no_pixel=rows*cols;
% calculate the histogram of two input image
histogram = zeros(1,256);
for i = 1:rows
for j = 1:cols
histogram(input_image(i,j)+1) = histogram(input_image(i,j)+1)+1;
end
end
%calcuating probalilty
for i = 1 : 256
histogram(i) = histogram(i) / total_no_pixel;
end
temp = histogram(1);
for i = 2:256
temp = temp+histogram(i);
histogram (i) = temp;
end

%mapping
for i = 1:rows
for j = 1:cols
input_image(i,j) = round(histogram(input_image(i,j)+1) *255);
end
end
subplot(2,2,2);imshow(input_image);title('Equalized Image');
subplot(2,2,4);imhist(input_image);axis tight;title('Afr Histogram Equalized
Image');
Output:
19)program to Implement smoothing or averaging filter in
spatial domain:
clc;
close all;
originalImage=imread('C:\Users\SYSTEM25\Desktop\doremon.jpg');
if size(originalImage,3)==3
originalImage=rgb2gray(originalImage);
end
grayImage=im2double(originalImage);
filterSize=5;
paddingSize=floor(filterSize/2);
[rows,cols]=size(grayImage);
smoothedImage=zeros(rows,cols);

for row=1:rows
for col=1:cols
neighborhood=grayImage(max(row-
paddingSize,1):min(row+paddingSize,rows),max(col-
paddingSize,1):min(col+paddingSize,cols));
smoothedValue=mean(neighborhood(:));
smoothedImage(row,col)=smoothedValue;

end
end
subplot(1,2,1);imshow(grayImage);
title('Original Image');
subplot(1,2,2);imshow(smoothedImage);
title('Smoothed Image');
Output:
22)Program to compute the edges in the image using
Roberts,sobel,Canny,Log,prewitt edge detection methods:
clc;
close all;
inputImage=imread('C:\Users\SYSTEM25\Desktop\computer.jpg');
if size(inputImage,3)==3
inputImage=rgb2gray(inputImage);
end
cannyEdges=edge(inputImage,'Canny');
sobelEdges=edge(inputImage,'sobel');
robertEdges=edge(inputImage,'roberts');
perwittEdges=edge(inputImage,'prewitt');
logEdges=edge(inputImage,'Log');

subplot(3,3,1);imshow(inputImage);title('Original Image');
subplot(3,3,2);imshow(robertEdges);title('Roberts Edges');
subplot(3,3,3);imshow(sobelEdges);title('Sobel Edges');
subplot(3,3,4);imshow(cannyEdges);title('Canny Edges');
subplot(3,3,5);imshow(perwittEdges);title('Perwitt Edges');
subplot(3,3,6);imshow(logEdges);title('Log Edges');

Output:
25) Program to separate the RGB plane from the color
Image:-->
clc;
clear all;
close all;
input_image=imread('C:\Users\SYSTEM25\Desktop\doremon.jpg');

red_color=input_image;
green_color=input_image;
blue_color=input_image;
red_color(:,:,2)=0;
red_color(:,:,3)=0;

green_color(:,:,1)=0;
green_color(:,:,3)=0;

blue_color(:,:,1)=0;
blue_color(:,:,2)=0;

subplot(2,3,2);
imshow(input_image);
title('Original RGB Image');

subplot(2,3,4);
imshow(red_color);
title('Red Color Plane');

subplot(2,3,5);
imshow(green_color);
title('Green Color Plane');

subplot(2,3,6);
imshow(blue_color);
title('Blue Color Plane');

Output:

You might also like