Digital Image Processing and Analysis Laboratory 1: Operations on Images
Total Page:16
File Type:pdf, Size:1020Kb
Digital image processing and analysis Laboratory 1: Operations on images Vedrana Baliceviˇ c,´ Pavle Prentasiˇ c´ 2014. 1.1 Unary operations on the image Unary operations on the image are mathematical operations whose input is a pixel intensity value, and the output is a changed intensity value of the same pixel. The operation is performed for all of the pixels in the image, meaning that an unary operator produces an image for each given input image. A scheme of an unary operation is given with Fig.1.1. Figure 1.1: Unary image operations scheme Unary operation can be any mathematical function that changes the intensity values of the pixels in an image. Codomain of the function does not have to be equal to its domain. Therefore, if we want to obtain a meaningful display of an image after performing an unary operation on it, the resulting image will need to be rescaled. Images whose pixel values are given with a class double should be scaled to the interval [0;1], whereas the images whose pixel values are given with a class integer should be scaled to the interval [1;255]. MATLAB function imagesc() does an automatic scaling to the required interval and displays it. An example >> [img, map] = imread(’medalja_kamenita_vrata.png’); >> img = double(img); % most of the functions require input arguments of a class double >> imgU = sqrt(img); % performing an unary operation >> max(imgU(:)) % display the maximal value ans = 15.9687 % maximal value is not 255 anymore >> imagesc(imgU) % scale and display the image simultaneously >> colormap(gray) % change the color palette 1 1.1.1 Problems 1. Compare the function imagesc with the functions image and imshow. What should you do to obtain the same results with all functions? 2. How would you do this mathematically? (Write the mathematical expressions). 3. Read the image uskoci1:png. Create a grayscale image out of this image (function rgb2gray()). Perform the following unary operations on this image: logarithmic function, square and square root, and compare what happened to the darker, and what happened to the lighter parts of the image. 4. If we denote the unary operation with Ux;x = 1;2;3, and scaling of the brightness with N, write an expression for the transfer function of the system given with the Fig.1.1. (Transfer function is a composition of the functions: Hx = N ◦ Hx.) 5. Display the shape of the transfer function for each of the given unary operations. 1.2 Binary operations Binary operations are the operations that take two input images to perform a mathematical operation. The scheme of the binary operation is given in Fig. 1.2. Figure 1.2: Binary image operations scheme Binary options require the input images sizes to be equal. The options for this are following: To decrease the size of the bigger image, to increase the size of the smaller image, or to change the size of the both images. Also, the colorspace of both images needs to be the same (RGB, HSV or other; or black and white), ie. both images need to be indexed and have the same color palette. An example >> [img1, map] = imread(’medalja_kamenita_vrata.png’); >> [img2, map] = imread(’medalja_dubrovnik.png’); >> whos Name Size Bytes Class Attributes img1 600x600 360000 uint8 % The dimensions of the 1st image are: 600x600 img2 545x548 298660 uint8 % The dimensions of the 1st image are: 545x548 map 0x0 0 double 2 >> img2 = imresize(img2,[600 600],’bilinear’); >> size(img2) ans = 600 600 % now the first image is also 600x600 >> img = double(img1)+double(img2); % summation of the images >> imagesc(img), colormap(gray) \begin{verbatim} 1.2.1 Problems 1. Pick two grayscale images with different sizes from the USC-SIPI image database. Try several binary operations on them: summation, multiplication, subtraction. Display the results and explain them. 2. Try the same thing in the RGB images. Display the results. Explain what happened. 1.2.2 Digital angiography The most common binary operation in the practice is the subtraction of the images. It is used with a purpose of emphasizing the contrast. In this type of applications, one of the images usually contains a regular scene (the background), while the other one contains an objects (or more of them) which we want to emphasize, positioned in front of the same background. Assuming that the background didn’t change, the subtraction of the images results in a new images whose pixel values are the largest exactly in the places where our objects of interest are positioned. 1.2.3 Problems 1. Read the images angio0:ti f and angio1:ti f . These images contain a head scan before and after the contrast injection. Subtract these two images and display the result. What did you obtain in the resulting image? 1.2.4 Linear convolution The examples shown before represent non-memory systems, due to the fact that the system outputs are de- pendant only on the current state (current pixel, and not its neighborhood). This section gives the examples of image processing using the memory systems. In digital image processing we use a concept of linear spatially invariant system (LSI). In image pro- cessing time invariance is not particulary relevant since usually we only have the spatial coordinates as the varaiables, and not the time variable. However, similar to linear time invariant systems (LTI), the response of the LSI systems can be calculated by means of the linear convolution. Mask represents an impulse response of the FIR system and is usually a small square (for example masks dimensions are 3 × 3, 5 × 5 and 7 × 7). The reason for this is a number of operations needed to determine the 2D convolution of the signal and the impulse response od the system, which is a double summation of the columns and the rows of the matrix). An example of convolution calculation is given further in the text. 3 An example [img,map] = imread(’medalja_kamenita_vrata.png’); >> msk = [1 0 -1; % defining the mask 2 0 -2; 1 0 -1]/4; >> imgC = conv2(img,msk); % calculating the convolution >> imshow(imgC) % showing the result Convolution of an image and a mask is performed by calling the function conv2(). For the calculation, MATLAB assumes that the intensity values of the pixels outside of the images are equal to zero, ie. the image is expanded with zeroes. If the image dimensions are Mx ×Nx and the mask dimensions are Mh ×Nh, the dimensions of their convolution will be (Mx + Mh − 1) × (Nx + Nh − 1). An example >> [img,map]=imread(’medalja_dubrovnik.png’); % read the image >> msk = [1 0 -1; % define the mask 1 0 -1; 1 0 -1]/3; >> imgC1 = conv2(img, msk); % caluclate the ’full’ convolution >> imgC2 = conv2(img, msk, ’same’); % calculate only the central part of the convolution % so that the output image has the same dimensions % as the input image >> imgC1 = conv2(img, msk, ’valid’); % calculate only the ’correct’ central part % of the convolution It is noticeable that, since we don’t have information on the pixel intensity values out of the image (surrounding the image) necessary for calculating the linear convolution, this approach introduces errors near the borders of the image. Therefore, only the middle part of the result, with the dimensions (Mx − Mh + 1) × (Nx − Nh + 1), is a fully reliable convolution result. 1.2.5 Problems 1. Select several images and calculate the linear convolution with the following masks: 21 0 −13 2 1 1 1 3 2−1 −1 −13 2 0 −1 0 3 1 1 42 0 −25; 4 0 0 0 5; 4−1 8 −15; 4−1 4 −15 4 3 1 0 −1 −1 −1 −1 −1 −1 −1 0 −1 0 2. Display the results and comment them. 3. Define the mask (with the dimensions 4 × 4) for averaging the images and apply it to one of the selected images from the Problem 1. using the linear convolution. 4. Which mask did you choose? 4 5. Did the chosen mask require the normalization of the image brightness? Why? 6. Is the convolution a unary or a binary image operation? 5.