Presentation is loading. Please wait.

Presentation is loading. Please wait.

Biomedical Signal processing 研究生生物医学图像处理实践课

Similar presentations


Presentation on theme: "Biomedical Signal processing 研究生生物医学图像处理实践课"— Presentation transcript:

1 Biomedical Signal processing 研究生生物医学图像处理实践课
Zhongguo Liu(刘忠国) Biomedical Engineering School of Control Science and Engineering, Shandong University 生物医学信号处理网站“教学资料”栏目下 2018/11/21 1 Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

2 Zhongguo Liu_Biomedical Engineering_Shandong Univ.
教材:John L. Semmlow编 Biosignal and Biomedical Image Processing MATLAB-Based Applications,2004年第一版 参考2008年第二版配书程序 2018/11/21 2 Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

3 第10章_Fundamentals of Image Processing: MATLAB Image Processing Toolbox
Image processing basics: matlab image formats Images can be treated as two-dimensional data, and many of the signal processing approaches presented in the previous chapters are equally applicable to images: directly or by modification. both PCA and ICA have been applied to image data treating the two-dimensional image as a single extended waveform., also Fourier transformation, convolution, and digital filtering using two-dimensional extensions. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

4 Image processing basics: matlab image formats
Two-dimensional images are usually represented by two-dimensional data arrays. Three-dimensional images can be constructed using multiple two-dimensional representations, as a volume image. MATLAB offers a variety of data formats. General Image Formats: Image Array Indexing an image: is represented in one, or more, two dimensional arrays, I(m,n). Each element of the variable, I, represents a single picture element, or pixel. (for a volume image, then an elemental volume, is termed a voxel.) Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

5 General Image Formats: Image Array Indexing
indexing protocol: MATLAB pixel coordinates I(m,n). The indexing protocol follows the matrix notation, with the horizontal pixel locations indexed left to right by n, and the vertical locations indexed top to bottom by m. Fig pixel coordinate system. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

6 the indexing protocol: spatial coordinates
spatial coordinates accepts non-integer indexes, the pixel is considered to be a square patch, the center of which has an integer value. the upper left-hand corner has coordinates of (0.5,0.5). the positions of the vertical and horizontal indexes are switched, as conventional x, y coordinate references. I(3,2) in pixel coordinates is the same pixel as I(2.0,3.0) in spatial coordinates. Fig 10.2 Indexing in the spatial coordinate system. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

7 the indexing protocol: spatial coordinates and pixel coordinates
Most routines expect a specific pixel coordinate system and produce outputs in that system. Examples of spatial coordinates are found primarily in the spatial transformation routines in the next chapter. It is possible to change the baseline reference in the spatial coordinate system as certain commands allow you to redefine the coordinates of the reference corner. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

8 Data Classes: Intensity Coding Schemes
There are four data classes, or encoding schemes : RGB images, indexed images (color), binary images, and intensity images for MATLAB. Each can store data in different formats. This reflects the variety in image types (color, grayscale, and black and white), and different efficiency in memory storage, as images require a large numbers of array locations. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

9 Data Classes: Intensity Coding Schemes
In indexed images, the pixel values are indexes to a table (colormap: N(64default)×3 matrix) that maps the index value to a color value. They are efficient in storing and displaying color images, but unfit for arithmetic operations (image processing operations), since the results do not produce meaningful images. They provide a convenient and flexible method for colorizing grayscale data to produce a pseudocolor image. (Example 10.3) Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

10 Data Classes: Intensity Coding Schemes
The other color coding scheme is the RGB coding scheme (truecolor) in which three different arrays indicate the intensity of the three color components of the image: red, green, or blue. Note that these variables are actually three-dimensional arrays having dimensions N×M×3. These arrays are indexed as RGB(n,m,i) where i=1,2,3. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

11 Data Classes: Intensity Coding Schemes
Grayscale images are stored as intensity class images where the pixel value represents the brightness or grayscale value of the image at that point. MATLAB convention suggests variable names beginning with I for variables in class intensity. If an image is only black or white (not intermediate grays), then the binary coding scheme can be used where the representative array is a logical array containing either 0’s or 1’s. MATLAB convention is to use BW for variable names in the binary class. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

12 Data Classes: Intensity Coding Schemes
Binary class variables can be specified as logical using the command BW = logical(A). A logical array can be converted to a standard array using the unary plusoperator: A=+BW. Check if a variable is logical using the routine: isa(I, ’logical’). Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

13 Data Formats MATLAB provides three different data formats for most classes mentioned above, to further reduce image storage requirements. The uint8 and uint16 data formats provide 1 or 2 bytes, respectively, for each array element (i.e., pixel) . Binary images do not support the uint16 format. The third data format, the double format, uses 8 bytes for each pixel. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

14 Data Formats A zero corresponds to the lowest intensity value,i.e., black. For uint8 and uint16 formats, the brightest intensity value (i.e.,white) is the largest number for that coding scheme: for uint8, 28-1; and for uint16, For the double format, the brightest value corresponds to 1.0. to test the format of an image: isa(I,’type’) will return a 1 if I is encoded in the format type: unit8, unit16, or double, and a zero otherwise. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

15 Data Formats Multiple images can be grouped together as one Multiple-image variable (termed multiframe variable) by adding another dimension to the variable array. Since image arrays are already considered three-dimensional, the additional images are added to the fourth dimension. This is one method of generating multiframe variable: incrementing along the fourth index as Example 10.2. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

16 Data Formats Multi-image variables are termed multiframe variables and each two-dimensional (or three-dimensional) image of a multiframe variable is termed a frame. Another method of generating multiframe variable: concatenating several images together using the cat function: IMF = cat(4, I1, I2, I3,...); 4: the fourth dimension, I1, I2, I3,.. images names. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

17 Data Conversions Certain operations (or MATLAB image processing functions) require a given data format and/or class. For example, standard MATLAB operations require the data be in double format, and will not work correctly with Indexed images. For converting format types, use im2xxx routines: I_uint8 = im2uint8(I); % Convert to uint8 format I_uint16 = im2uint16(I); % Convert to uint16 format I_double = im2double(I); % to double format Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

18 Data Conversions Converting between different image encoding schemes can be done by scaling (Ex. 10.3). an easier solution is simply to use MATLAB’s functions (like gray2ind): [x, map] = gray2ind(I, N); % Convert from grayscale % to indexed [x, map] = rgb2ind(RGB, N or map); % Convert from truecolor to indexed x = grayslice(I, N or V); % Convert grayscale to %indexed using thresholding (Ex. 10.4). Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

19 Data Conversions To convert indexed images to other encoding schemes:
I = ind2gray(x, map); % Convert to grayscale intensity RGB = ind2rgb(x, map); % Convert to RGB (“truecolor”) To convert an image to binary coding: BW = im2bw(I, Level); % Convert to binary logical encoding whereLevelspecifies the threshold that will be used to determine if a pixel is white (1) or black (0). Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

20 Data Conversions To convert general class double data to intensity data, scaled between 0 and 1: I = mat2gray(A, [Amin Amax]); % Scale matrix to intensity encoding, double format The various data classes, their conversion routines, and the data formats they support are summarized in Table 10.1 Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

21 Data Conversions (1) uint8 or unit 16 depending on the number of levels requested (N); (2) Double; (3) No format change (output format equals input format); and (4) Logical (size double).. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

22 Image Display The most useful and easiest options for displaying an image is to use the imshow function: imshow(I,arg) arg: optional argument, that depends on the data format. For indexed data, the variable name must be followed by the colormap, map. For intensity class ,arg can be a scalar, specifying the number of levels to use in rendering the image, or, if arg is a vector, [low high], arg specifies the values to readjust the range limits of a specific data format.* Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

23 Image Display To display multiframe variables. The function montage (MFW) displays the various images in a gird-like pattern as shown in Example 10.2. Alternatively, multiframe variables can be displayed as a movie using the immovie and movie commands: mov = imovie(MFW); % Generate movie variable movie(mov); % Display movie Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

24 Image Display Ex.10.1 Generate an image of a sinewave grating having a spatial frequency of 2 cycles/inch. A sine wave grating is a pattern that is constant in the vertical direction, but varies sinusoidally in the horizontal direction. It is used as a visual stimulus in experiments dealing with visual perception. Assume the figure will be 4 inches square; hence, the overall pattern should contain 4 cycles. Assume the image will be placed in a 400×400 pixel array (i.e., 100 pixels per inch) using a uint16 format. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

25 Image Display Ex.10.1 Solution: Since sinewave grating is a gray-scale image, we use intensity coding scheme, uint8 data format. we first generate a 400-by-1 format, then convert it to uint8 format using im2uint8. The uint8 image can then be extended vertically to 400×400 pixels. N = 400;Nu_cyc = 4; % 4 cycle grating x = (1:N)*Nu_cyc/N; % spatial vector I_sin(1,:) = .5 * sin(2*pi*x) + .5;%sine(01) I_8 = im2uint8(I_sin); % Convert to uint8 for i = 1:N % Extend to N (400) vertical lines I(i,:) = I_8; end imshow(I); title('Sinewave Grating'); % Display image Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

26 Image Display Ex Generate a multiframe variable consisting of a series of sinewave gratings having different phases. Display these images as a montage. Border the images with black for separation on the montage plot. Generate 12 frames, but reduce the image to 100×100 to save memory. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

27 Image Display montage(I, 'Size', [4 3]);
Ex Generate and display multiframe images as a montage. N = 100; Nu_cyc = 2; % 2 cycle grating M = 12; x = (1:N)*Nu_cyc/N; for j = 1:M % Generate M (12) images phase = 2*pi*(j-1)/M; I_sin = .5 * sin(2*pi*x + phase) + .5'; % Add some black space at left and right borders I_sin = [zeros(1,10) I_sin(1,:) zeros(1,10)]; I_8 = im2uint8(I_sin); for i = 1:N % Extend to N (100) vertical lines I(i,:,1,j) = I_8; if i < 10 | i > 90 % “I > 90 ”I 改成小写 I(i,:,1:j) = 0; % Insert black at top and bottom end montage(I); title('Sinewave Grating'); % Display image montage(I, 'Size', [4 3]); Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

28 Image Display Ex Construct a multiframe variable with 12 sinewave grating images. Display these data as a movie. Since the immovie function requires the multiframe image variable to be in either RGB or indexed format, convert the uint16 data to indexed format. This can be done by the gray2ind(I,N) function. This function simply scales the data to be between 0 and N, where N is the depth of the colormap. If N is unspecified, gray2ind defaults to 64 levels. MATLAB colormaps can also be specified to be of any depth, but as with gray2ind the default level is 64. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

29 Image Display Ex.10.3 Display multiframe images as a movie.
N = 100; Nu_cyc = 2; % Produce 4 cycle grating M = 100; % Produce 100 images x = (1:N)*Nu_cyc/N; % Generate spatial vector for j = 1:M % Generate M (100) images phase = 10*pi*j/M; % Shift phase through 180(pi) every 10 images I_sin(1,:) = .5 * sin(2*pi*x + phase) + .5'; % sine; scale (0 1) for i = 1:N % Extend to N (100) vertical lines Mf(i,:,1,j) = I_sin(1,:); end [Mf map] = gray2ind(Mf); % Use defalut map length 64 % The movie function requires either RGB or indexed format mov = immovie(Mf,jet); % Make into a movie movie(mov,3); % and display 3 times Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

30 Image Storage and Retrieval
Images may be stored on disk using imwrite command: imwrite(I, filename.ext, arg1, arg2, ...); where I is the array to be written into file filename. MATLAB supports the most popular formats for storing image data . The file format is indicated by the filename’s extension,ext, which may be: .bmp(Microsoft bitmap), .gif(graphic interchange format), .jpeg(Joint photographic experts group), .pcs(Paintbrush), .png(portable network graphics), and .tif(tagged image file format). The arguments are optional and may be used to specify image compression or resolution, or other format dependent information. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

31 Image Storage and Retrieval
imread function is used to retrieve images from disk: [I map] = imread(‘filename.ext’,fmt or frame); where filename is name of the image file and .ext is extensions. fmt, needed if the file format is not evident from the filename. frame specify which frame of a multiframe image is to be read in I(Ex.10.4). I will often be in uint8 format. .tif and.png support uint16 format, so imread may generate data in uint16. If the file contains a grayscale image data, then the output is encoded as an intensity image, if truecolor, then as RGB. map will be empty {checked with isempty(map)} . except the file contains indexed data, then both output, I and map will contain data. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

32 Image Storage and Retrieval
The type of data format used by a file can also be obtained by using the function infinfo. information = infinfo(‘filename.ext’) where information will contain text providing the essential information about the file including the ColorType, FileSize, and BitDepth. or using the function imread and whos command. Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

33 Basic Arithmetic Operations
MATLAB has supplied functions to carry out some basic mathematics on uint8- and uint16-format arrays. They actually carry out the operations in double precision on an element by element basis then convert back to the input format. This reduces roundoff and overflow errors. I_diff = imabssdiff(I, J); % Subtracts J from I on a pixel % by pixel basis and returns the absolute difference I_comp = imcomplement(I) % Compliments image I I_add = imadd(I, J); % Adds image I and J (images and/or % constants) to form image I_add I_sub = imsubtract(I, J); % Subtracts J from image I I_divide = imdivide(I, J) % Divides image I by J I_multiply = immultiply(I, J) % Multiply image I by J Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

34 Basic Arithmetic Operations
Several arithmetical operations can be combined using the imlincomb function, which calculates a weighted sum of images. For example to add 0.5 of imageI1 to 0.3 of imageI2, to 0.75 of ImageI3, use: I_combined = imlincomb (.5, I1, .3, I2, .75, I3); % Linear combination of images Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

35 Image Display Ex Demonstration of various image. Load all frames of the MRI image in mri.tif from the the MATLAB Image Processing Toolbox (in subdirectory imdemos). Select one frame based on a user input. Process that frame by: contrast enhancement of the image, inverting the image, slicing the image, windowing, and thresholding the image. mri = uint8(zeros(128,128,1,27)); for frame = 1: % Read all frames into mri [mri(:,:,:,frame), map ] = imread('mri.tif', frame); end montage(mri, map); % Display images as a montage % Include map in case Indexed image frame_select = input(‘Select frame for processing: ’); % 输入帧号 I = mri(:,:,:,frame_select); % Select specific frame for continued processing. Check if image is Indexed (in fact 'whos' shows it is). if isempty(map) == % Check to see if Indexed data I = ind2gray(I,map); % If so, convert to Intensity image I1= im2double(I); % Convert to Class double % I_bright = immultiply(I1,1.2); % Increase the contrast I_invert = imcomplement(I1); % Compliment image BW = im2bw(I1,.75); % Threshold image (.75) BW_invert = ~BW; % Inverted threshold [M N] = size(I1); for i = 1:M % Multiple horizontally by a Hamming window I_window(i,:) = I1(i,:) .* hamming(N)'; for i = 1:N % Multiply vertically by a Hamming window I_window(:,i) = I_window(:,i) .* hamming(M); I_window = mat2gray(I_window); % Scale windowed image appropriately % Convert to binary figure; subplot(3,2,1); % Display all images in a single plot imshow(I1); title('Original'); subplot(3,2,2); imshow(I_bright); title('Contrast'); subplot(3,2,3); imshow(I_invert); title('Inverted'); subplot(3,2,4); imshow(I_window); title('Windowed'); subplot(3,2,5); imshow(BW); title('Thresholded'); subplot(3,2,6); imshow(BW_invert) title('Inverted Threshold')' Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

36 Image Display Ex.10.3 Demonstration of various image.
if isempty(map) == % Check to see if Indexed data I = ind2gray(I,map); % If so, convert to Intensity image end I1= im2double(I); % Convert to Class double I_bright = immultiply(I1,1.2); % Increase the contrast I_invert = imcomplement(I1); % Compliment image BW = im2bw(I1,.75); % Convert to binary.Threshold image (.75) BW_invert = ~BW; % Inverted threshold [M N] = size(I1); for i = 1:M % Multiple horizontally by a Hamming window I_window(i,:) = I1(i,:) .* hamming(N)'; for i = 1:N % Multiply vertically by a Hamming window I_window(:,i) = I_window(:,i) .* hamming(M); I_window=mat2gray(I_window); %Scale windowed image appropriately figure; subplot(3,2,1); % Display all images in a single plot imshow(I1); title('Original'); subplot(3,2,2); imshow(I_bright); title('Contrast'); subplot(3,2,3); imshow(I_invert); title('Inverted'); subplot(3,2,4); imshow(I_window); title('Windowed'); subplot(3,2,5); imshow(BW); title('Thresholded'); subplot(3,2,6); imshow(BW_invert) title('Inverted Threshold')' Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.

37 Image Display Ex.10.3 Demonstration of various image. figure;
subplot(3,2,1); % Display all images in a single plot imshow(I1); title('Original'); subplot(3,2,2); imshow(I_bright); title('Contrast'); subplot(3,2,3); imshow(I_invert); title('Inverted'); subplot(3,2,4); imshow(I_window); title('Windowed'); subplot(3,2,5); imshow(BW); title('Thresholded'); subplot(3,2,6); imshow(BW_invert) title('Inverted Threshold')' Zhongguo Liu_Biomedical Engineering_Shandong Univ. Zhongguo Liu_Biomedical Engineering_Shandong Univ.


Download ppt "Biomedical Signal processing 研究生生物医学图像处理实践课"

Similar presentations


Ads by Google