Advertisement
TUJHE_KYA_BHAI

DIP_PRACTICALS

Nov 23rd, 2022
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 5.48 KB | Source Code | 0 0
  1.         Digital  Image Processing
  2.                Practical
  3. Name: Anwesha Sanyal
  4. Roll No: 3906
  5.  
  6.  
  7. Practical-1(Reading and Displaying)
  8.  
  9. pkg load image;
  10. I=imread("Panda.jpg");
  11. imshow(I);
  12.  
  13. Practical-2 (Resize an image)
  14. resizedI = imresize(I,0.25);
  15. figure
  16. imshow(resizedI)
  17. Practical-3 (Gray scale image)
  18. J = rgb2gray(I);
  19. Figure
  20. imshow(J);
  21. Practical-4 (Black & White image)
  22. figure imshow(im2bw(I));
  23.  
  24. Practical-5 (Color temperature of image)
  25. r = size(I,1);
  26. c = size(I,2);
  27. R =zeros(r,c,3);
  28. G = zeros(r,c,3);
  29. B = zeros(r,c,3);
  30. R( : , : ,1) = I( : , : ,1);
  31. G( : , : ,2) = I( : , : ,2);
  32. B( : , : ,3) = I( : , : ,3);
  33. figure imshow(uint8(R));
  34.  
  35. figure imshow(uint8(G));
  36.  
  37. figure imshow(uint8(B));
  38.  
  39. Practical-6 (Negative of an image)
  40. Image = imread(' Panda.jpg ');
  41. J = rgb2gray(Image);
  42. subplot(1,3,1);
  43. imshow(J);
  44. negativeImage = 255-J;   subplot(1,3,2); imshow(negativeImage);
  45. for row=1:size(J,1)
  46. for col=1:size(J,2)
  47. negativeimage(row,col,:)=255-J(row,col,:); end
  48. end
  49. subplot(1,3,3); imshow(negativeimage);
  50.  
  51. Practical-7 (Stretching of an image)
  52. subplot(1,2,1) imshow(Image);
  53.  
  54. J = imadjust(Image,stretchlim(Image),[]); subplot(1,2,2);
  55. imshow(J);
  56.  
  57. Practical-8 (Applying filter on an image)
  58. lap =[0,1,0;1,-4,1;0,1,0]; output=imfilter(I,lap); subplot(2,2,1);
  59. imshow(output);
  60. filteredImage = imadd(I,output); subplot(2,2,2); imshow(filteredImage);
  61. l = [0,-1,0;-1,4,-1;0,-1,0]; output2=imfilter(I,l); subplot(2,2,3);
  62. imshow(output2);
  63.  
  64. filteredImage2 = imadd(I,output2); subplot(2,2,4); imshow(filteredImage2);
  65.  
  66. Practical-9 (Prewitt Filter)
  67. subplot(2,3,1);
  68. imshow(I);
  69. title('Original Image');
  70.  
  71. prewitt_H = [-1,-1,-1;0,0,0;1,1,1]; output_H = imfilter(I,prewitt_H); subplot(2,3,2); imshow(output_H);
  72. title('prewitt_H');
  73.  
  74.  
  75.  
  76. filteredImage_H =imadd(I,output_H);
  77. subplot(2,3,3); imshow(filteredImage_H); title('filteredImage_H');
  78. prewitt_V = [-1,0,1;-1,0,1;-1,0,1]; output_V = imfilter(I,prewitt_V); subplot(2,3,4);
  79. imshow(output_V);
  80. title('prewitt_V');
  81.  
  82.  
  83. filteredImage_V=imadd(I,output_V); subplot(2,3,5);
  84. imshow(filteredImage_V); title('filteredImage_V');
  85.  
  86.  
  87. filtered_img=imadd(I,output_H); filtered_image=imadd(filtered_img,output_V); subplot(2,3,6);
  88. imshow(filtered_image);
  89. title('prewitt');
  90.  
  91.  
  92. Practical-10 (Zooming an image)
  93. imshow(Image); title("zoomed image"); zoom(2);
  94.  
  95.  
  96. Practical-11 (Translating an image)
  97. pkg load image
  98. i = imread(' Panda.jpg ');
  99.  
  100. subplot(1,2,1);
  101.  imshow(i);
  102. title("Original image");
  103.  
  104. set(gca, "Visible", "on"); j = imtranslate(i, [20,-40]); subplot(1,2,2); imshow(j);
  105. title("translated Image");
  106.  
  107. set(gca, "Visible", "on");
  108.  
  109.  
  110. Practical-12 (Shrinking an image)
  111. f = input('Enter the shrinking factor: ');
  112. s = size(A);
  113. s1 = s/f;
  114. k = 1;
  115. l = 1;
  116. for i=1:s1
  117. for j=1:s1
  118. B(i,j) = A(k,l); l = l+f;
  119. end
  120. l = 1;
  121. k=k+f;
  122. end
  123.  
  124. subplot(1,2,1); imshow(A);
  125. title("Original image");
  126.  
  127. subplot(1,2,2); imshow(B); title("shrinkled image");
  128.  
  129.  
  130.  
  131. Practical-13 (Salt & pepper noise)
  132. pkg load image
  133. i=imread(' Panda.jpg ');
  134. subplot(2,2,1);
  135. imshow(i);
  136. title('Original Image');
  137. J = imnoise(i,'salt & pepper', 0.05);
  138. subplot(2,2,2);
  139. imshow(J);
  140. title('Noisy Image');
  141.  
  142. ##remove salt and pepper noise using averaging filter
  143. H = fspecial('average',[3,3]);
  144. Kaverage = imfilter(J,H);
  145. subplot(2,2,3);
  146. imshow(Kaverage);
  147. title('Noise Free Image');
  148.  
  149. ##remave salt and pepper noise usign median filter
  150. i = rgb2gray(J);
  151. Kmedian = medfilt2(i);
  152. subplot(2,2,4);
  153. imshow(Kmedian);
  154. title('Noise free Image after median filter');
  155.  
  156.  
  157.  
  158.  
  159.  
  160. Practical-14 (Rotating an image)
  161. j = imrotate(i, 90);
  162. imshow(j);
  163.  
  164. Practical-15
  165. subplot(1,3,1);
  166. imshow(i);
  167. title("Input image");
  168.  
  169. g = rgb2gray(i);
  170. subplot(1,3,2);
  171. imshow(g);
  172. title("Gray Imgae");
  173.  
  174. f_transform = double(g);
  175. f_transform1 = double(g);
  176. h = fspecial('prewitt');
  177. pre1 = uint8(round(filter2(h,g)));
  178. subplot(1,3,3);
  179. imshow(pre1);
  180.          title("prewitt image");
  181.  
  182.  
  183.  
  184.  
  185.  
  186. Practical-16 (Laplacian filter)
  187. subplot(1,3,1);
  188. imshow(I);
  189. title("original image");
  190.  
  191. lap = [0,1,0;1,-4,1;0,1,0];
  192. output = imfilter(I,lap);
  193.  
  194.  
  195.  
  196. subplot(1,3,2);
  197. imshow(output);
  198. title("lap image");
  199.  
  200.  
  201. filteredimage = imadd(I, output);
  202. subplot(1,3,3);
  203. imshow(filteredimage);
  204. title("filtered image");
  205.  
  206.  
  207.  
  208.  
  209.  
  210. Practical-17 (Histogram equalisation)
  211. imhist(i);
  212. J = histeq(i); imhist(J); imshow(J);
  213.  
  214. Practical-18 (Erosion of image)
  215. i = uint8(i); subplot(1,3,1); imshow(i);
  216. i = im2bw(i); i= double(i); subplot(1,3,2); imshow(i);
  217. se = [0,1,0;1,1,1;0,1,0]; se = strel("square", 3); erodedI = imerode(i, se); subplot(1,3,3); imshow(i);
  218.  
  219. Practical-19 (Edge detection of an image)
  220. I=rgb2gray(i);
  221. BW3 = edge(I,"roberts"); subplot(2,2,1); imshow(I);
  222. title("original");
  223.  
  224. BW1 = edge (I,"prewitt"); subplot(2,2,2); imshow(BW1); title("prewitt");
  225. BW2 = edge(I,"sobel"); subplot(2,2,3); imshow(BW2);
  226. title("sobel");
  227. BW3 = edge(I,"roberts"); subplot(2,2,4); imshow(BW3);
  228. title("roberts");
  229.  
  230. Practical-20 (Dilation of image)
  231. i = uint8(i);
  232. subplot(2,2,1);
  233. imshow(i);
  234.  
  235. i = im2bw(i);
  236. i= double(i);
  237. subplot(2,2,2);
  238. imshow(i);
  239.  
  240. se = [0,1,0;1,1,1;0,1,0];
  241. se = strel("square", 3);
  242. erodedI = imerode(i, se);
  243. subplot(2,2,3);
  244. imshow(i);
  245.  
  246. dilateI = imdilate(erodedI, se);
  247. subplot(2,2,4);
  248. imshow(i);
  249.  
  250.  
  251.  
  252.  
  253.  
  254. Practical-21
  255. pkg load image
  256. sth=imread(' Panda.jpg');
  257. r = size(sth,1);
  258. c = size(sth,2);
  259. sth = double(sth);
  260. sum = 0;
  261. for i = 1:r
  262.    for j = 1:c
  263.     sum = sum + sth(i,j,:);
  264.    end
  265. end
  266. disp(sum)
  267. disp(sum/(r*c))
  268. J = imadjust(sth,stretchlim(sth),[]);
  269. figure;
  270. imshow(J);
  271.  
  272.  
  273.  
  274.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement