% EE 622 Digital Image Processing. Spring Semester % Electrical & Electronics Eng. Dep. Cukurova University %% 1. SOME DEFINITIONS % From http://www.schorsch.com/kbase/glossary/ % Brightness % (Term of photometry) % The subjective description of luminance. % A perceived characteristics of objects which does not vary directly in a simple mathematical relationship with their physical or measured brightness, which is corretly termed their luminance. % The term brightness is often misused to mean luminance. If so, it should be qualified as "measured" brightness, to distinguish between the absolute attributes ("measured" brightness) and the perceived attributes ("apparent" of "subjective" brightness) of the object in question. % Luminance % Photometric brightness is an old and deprecated term for luminance. % (Term of photometry) % The physical measure of brightness. % Luminous intensity per unit projected area of any surface, as measured from a specific direction. % Luminance (usually 'L' in formulas) is the amount of visible light leaving a point on a surface in a given direction. This "surface" can be a physical surface or an imaginary plane, and the light leaving the surface can be due to reflection, transmission, and/or emission % Standard unit of luminance is candela per square meter (cd/m2). % (also called Nits in the USA, from latin "nitere" = "to shine"). % Contrast % Luminance Contrast % Michelson Contrast % Modulation % Peak-to-Peak Contrast % Weber Contrast % (Terms of photometry/physiology) % Contrast (Luminance Contrast) is the relationship between the luminance of a brighter area of interest and that of an adjacent darker area. % Mathematically, the difference between the two luminances divided by the lower luminance . This definition is also called Weber Contrast, and is the most commonly useful one in the context of lighting. % C = (Lmax - Lmin) / Lmin % Simple Contrast values are often used in photography, to specify the difference between bright and dark parts of the picture. This definition is not useful for real-world luminances, because of their much higher dynamic range and the logarithmic response characteristics of the human eye. % Csimple = Lmax / Lmin % Peak-to-Peak Constrast (Michelson Contrast, Modulation) measures the relation between the spread and the sum of the two luminances. This definition is typically used in signal processing theory, to determine the quality of a signal relative to its noise level. In the context of vision, such noise could be caused by scattered light introduced into the view path by a translucent element partly obscuring the scene behind it. % Modulation = (Lmax - Lmin) / (Lmax + Lmin) % From http://www.answers.com/topic/contrast % Contrast % The difference in brightness between the light and dark areas of a picture, such as a photograph or video image. % SEE: http://www.crompton.com/wa3dsp/light/lumin.html. %% 2. INTRODUCTION % Reading and Displaying Images imagefile = 'building.jpg'; imagetype = 'JPEG'; % A = imread('cameraman.tif', 'TIFF'); A = imread(imagefile, imagetype); imshow(A) % set(gcf, 'NumberTitle', 'off', 'Name', ' cameraman.tif') set(gcf, 'NumberTitle', 'off', 'Name', imagefile) % Image Class c = class(A); disp(['Image class is ', c]) % uint8 to double Conversion A = double(A); % Information About Image % info = imfinfo('cameraman.tif') info = imfinfo(imagefile); % Another Way of Displaying Images figure; image(A); colormap(gray(256)) % Another Way of Obtaining Information About Image iminfo(A); % Image Grid [x, y] = meshgrid(0:15); x = x(:); y = y(:); plot(y, x, 'o', 'MarkerEdgeColor','b', 'MarkerFaceColor','b'); axis ij set(gca,'xtick',0:15, 'ytick',0:15, 'XGrid','on', 'YGrid','on', 'XLim', [-1, 16], 'YLim', [-1, 16]) xlabel('y'); ylabel('x') set(gcf, 'NumberTitle', 'off', 'Name', ' Grid') title('Dots are pixel locations of an image') %% 3. IMAGE ENHANCEMENT % Histogram and Probability Dist. % x = imread('cameraman.tif', 'tiff'); x = imread('building.jpg', 'jpeg'); info = imfinfo('building.jpg'); [counts, bins] = imhist(x); totpixels = sum(counts); p = counts/totpixels; figure; subplot(211); stemnm(bins, counts); xlabel('r'); ylabel('counts'); title('Histogram'); subplot(212); stemnm(bins, p); xlabel('r'); ylabel('p_r(r)'); title('Probability Dist.'); % Image Negative r = 0:255; s = imneg(r); figure; plot(r, s, 'k'); axis tight xlabel('r'); ylabel('s'); title('Image Negative - Transfer Function'); axis tight xneg = imneg(x); figure; subplot(121); subimage(x); axis off; title('Original Image'); subplot(122); subimage(xneg); axis off; title('Transformed Image'); set(gcf, 'Name', 'Image Negative'); % Alternative method for Image Negative y = imnegbc(x); % bit complement imshow(y); % Log Transform r = 0:255; s = imlogtran(r); figure; plot(r, s, 'k'); axis tight xlabel('r'); ylabel('s'); title('Log Transform - Transfer Function'); xlog = imlogtran(x); figure; subplot(121); subimage(x); axis off; title('Original Image'); subplot(122); subimage(xlog); axis off; title('Transformed Image'); set(gcf, 'Name', 'Log Transform'); figure; subplot(211); histg(x, 'stem'); title('Histogram of Original Image'); subplot(212); histg(xlog, 'stem'); title('Histogram of Transformed Image'); set(gcf, 'Name', 'Log Transform - Histograms'); % Power Law Transform r = 0:255; s1 = impowlaw(r, 0.4); s2 = impowlaw(r, 2.5); figure; plot(r, s1, 'k', r, s2, 'k:'); axis tight legend('\gamma = 0.4', '\gamma = 2.5'); xlabel('r'); ylabel('s'); title('Power Law Transform - Transfer Function'); xpowlaw = impowlaw(x, 0.4); figure; subplot(121); subimage(x); axis off; title('Original Image'); subplot(122); subimage(xpowlaw); axis off; title('Transformed Image'); set(gcf, 'Name', 'Power Law Transform ( gamma = 0.4 )'); figure; subplot(211); histg(x, 'stem'); title('Histogram of Original Image'); subplot(212); histg(xpowlaw, 'stem'); title('Histogram of Transformed Image'); set(gcf, 'Name', 'Power Law Transform ( gamma = 0.4 ) - Histograms'); xpowlaw = impowlaw(x, 2.5); figure; subplot(121); subimage(x); axis off; title('Original Image'); subplot(122); subimage(xpowlaw); axis off; title('Transformed Image'); set(gcf, 'Name', 'Power Law Transform ( gamma = 2.5 )'); figure; subplot(211); histg(x, 'stem'); title('Histogram of Original Image'); subplot(212); histg(xpowlaw, 'stem'); title('Histogram of Transformed Image'); set(gcf, 'Name', 'Power Law Transform ( gamma = 2.5 ) - Histograms'); % Image Bit Planes xbp = imbitplanes(x); figure colormap(gray(2)); subplot(241); subimage(xbp(:, :, 1)); axis off title('Bit Plane : 7 (MSB)'); subplot(242); subimage(xbp(:, :, 2)); axis off title('Bit Plane : 6'); subplot(243); subimage(xbp(:, :, 3)); axis off title('Bit Plane : 5'); subplot(244); subimage(xbp(:, :, 4)); axis off title('Bit Plane : 4'); subplot(245); subimage(xbp(:, :, 5)); axis off title('Bit Plane : 3'); subplot(246); subimage(xbp(:, :, 6)); axis off title('Bit Plane : 2'); subplot(247); subimage(xbp(:, :, 7)); axis off title('Bit Plane : 1'); subplot(248); subimage(xbp(:, :, 8)); axis off title('Bit Plane : 0 (LSB)'); set(gcf, 'Name', 'Image Bit Planes', 'Units','Normalized','Outerposition',[0 0 1 1]); % Image Gray Level Slicing r = 0:255; s = imthrsh(r, 150, 200, 20, 235); figure plot(r, s); axis([0 255 0 255]); xlabel('r'); ylabel('s'); title('Gray Level Slicing - Transfer Function'); x = imread('rice.png'); xgs = imthrsh(x, 130, 200, 20, 235); figure; subplot(121); subimage(x); axis off; title('Original Image'); subplot(122); subimage(uint8(xgs)); axis off; title('Transformed Image'); set(gcf, 'Name', 'Gray Level Slicing'); figure; subplot(211); histg(x, 'stem'); title('Histogram of Original Image'); subplot(212); histg(xgs, 'stem'); title('Histogram of Transformed Image'); set(gcf, 'Name', 'Gray Level Slicing - Histograms'); % Image Gray Level Slicing 2 r = 0:255; s = imgrayslice(r, 150, 200, 235); figure plot(r, s); axis([0 255 0 255]); xlabel('r'); ylabel('s'); title('Gray Level Slicing - Transfer Function'); xgs = imgrayslice(x, 150, 200, 235); figure; subplot(121); subimage(x); axis off; title('Original Image'); subplot(122); subimage(uint8(xgs)); axis off; title('Transformed Image'); set(gcf, 'Name', 'Gray Level Slicing'); figure; subplot(211); histg(x, 'stem'); title('Histogram of Original Image'); subplot(212); histg(xgs, 'stem'); title('Histogram of Transformed Image'); set(gcf, 'Name', 'Gray Level Slicing - Histograms'); % Image stretching r = 0:255; s = imstretch(r); figure plot(r, s); axis([0 255 0 255]); xlabel('r'); ylabel('s'); title('Image Stretching - Transfer Function'); x = imread('rice.png'); xstretch = imstretch(x); figure; subplot(121); subimage(x); axis off; title('Original Image'); subplot(122); subimage(uint8(xstretch)); axis off; title('Transformed Image'); set(gcf, 'Name', 'Image Stretching'); figure; subplot(211); histg(x, 'stem'); title('Histogram of Original Image'); subplot(212); histg(xstretch, 'stem'); title('Histogram of Transformed Image'); set(gcf, 'Name', 'Image Stretching - Histograms'); close all % Histiogram Equalization x = imread('pout.tif', 'tiff'); y = histequal(x); figure; subplot(121); subimage(x); axis off; title('Original Image'); subplot(122); subimage(y); axis off; title('Equalized Image'); set(gcf, 'Name', 'Histogram Equalization'); figure; subplot(211); histg(x, 'stem'); title('Histogram of Original Image'); subplot(212); histg(y, 'stem'); title('Histogram of Equalized Image'); set(gcf, 'Name', 'Histogram Equalization'); close all % Histogram Matching imr = imread('kids.tif', 'tiff'); imz = imread('cameraman.tif', 'tiff'); y = histmatch(imr, imz); figure; subplot(221); subimage(imr); axis off; title('Image - 1'); subplot(222); subimage(imz); axis off; title('Image - 2'); subplot(223); subimage(y); axis off; title('Resultant Image'); set(gcf, 'Name', 'Histogram Matching'); figure; subplot(311); histg(imr, 'stem'); title('Histogram of Image - 1'); subplot(312); histg(imz, 'stem'); title('Histogram of Image - 2'); subplot(313); histg(y, 'stem'); title('Histogram of Resultant Image'); set(gcf, 'Name', 'Histogram Matching'); % Histogram Specification x = imread('kids.tif', 'tiff'); [y, pz, n] = histspec(x); figure; subplot(121); subimage(x); axis off; title('Image'); subplot(122); subimage(y); axis off; title('Resultant Image'); set(gcf, 'Name', 'Histogram Specification'); figure; subplot(311); histg(x, 'stem'); title('Histogram of Image'); subplot(312); stemnm(n, pz*prod(size(x))); title('Target Histogram'); subplot(313); histg(y, 'stem'); title('Histogram of Resultant Image'); set(gcf, 'Name', 'Specification'); close all %% 4. COLOR IMAGE PROCESSING x = imread('peppers.png'); figure; imshow(x); % RGB to YIQ Conversion y = rgb2yiq(x); figure; subplot(221); subimage(x); axis off; title('Original Image'); subplot(222); subimage(uint8(y(:, :, 1))); axis off; title('Y Component - Perceived Luminance'); subplot(223); subimage(uint8(y(:, :, 2))); axis off; title('I Component - Color and Some Luminance Information'); subplot(224); subimage(uint8(y(:, :, 3))); axis off; title('Q Component - Color and Some Luminance Information'); set(gcf, 'Name', 'RGB to YIQ Conversion', 'Units','Normalized','Outerposition',[0 0 1 1]); % YIQ to RGB Conversion xback = yiq2rgb(y); erro =(double(xback(:)) - double(x(:))); erro = sum(erro.^2) / prod(size(x)); erro = sqrt(erro); disp(' '); disp('RGB to YIQ Conversion'); disp(['error = ', num2str(erro)]); % RGB to HSI Conversion y = rgb2hsi(x); figure; subplot(221); subimage(x); axis off; title('Original Image'); subplot(222); subimage(uint8(y(:, :, 1))); axis off; title('H Component - Hue'); subplot(223); subimage(uint8(y(:, :, 2))); axis off; title('S Component - Saturation'); subplot(224); subimage(uint8(y(:, :, 3))); axis off; title('I Component - Intensity'); set(gcf, 'Name', 'RGB to HSI Conversion', 'Units','Normalized','Outerposition',[0 0 1 1]); % HSI to RGB Conversion xback = hsi2rgb(y); erro =(double(xback(:)) - double(x(:))); erro = sum(erro.^2) / prod(size(x)); erro = sqrt(erro); disp(' '); disp('RGB to HSI Conversion'); disp(['error = ', num2str(erro)]); % Pseudo Color x = imread('checker.gif'); x = 255*x; y = pseudocolor(x); figure; subplot(121); subimage(x); axis off; subplot(122); subimage(y); axis off; % FREQUENCY DOMAIN % x = [1 1]; w = linspace(-pi, pi, 250); X = 1 + cos(w) - j*sin(w); A = (2 + 2*cos(w)).^(1/2); theta = -atan2(sin(w), 1 + cos(w)); subplot(211); plot(w, A); axis tight xlabel('w'); ylabel('A(w)'); title('Magnitude Spectrum'); subplot(212); plot(w, theta); axis tight xlabel('w'); ylabel('theta(w)'); title('Phase Spectrum'); % x = [1 1; 1 -1]; frq = linspace(-pi, pi, 250); [w1, w2] = meshgrid(frq); X = 1 + cos(w2) + cos(w1) + cos(w1 + w2) + j*(- sin(w2) + sin(w1) - sin((w1 + w2))); a = real(X); b = imag(X); A = (a.^2 + b.^2).^(1/2); theta = atan2(b, a); figure; subplot(121); contourf(frq,frq,A); axis ij xlabel('w2'); ylabel('w1'); colormap(bone) subplot(122); imagesc(frq,frq,theta); axis ij colormap(bone) xlabel('w2'); ylabel('w1'); % D = 1/32; t = (0:31)*D; x = cos(2*pi*3*(t.'))* cos(2*pi*14*t + pi/7); X = fftshift(fft2(x)); figure; freq = -1/2/D:1/2/D-1; imagesc(freq,freq,abs(X)); axis ij colormap(bone) xlabel('w2'); ylabel('w1');