Patrick Zimmer |
Home | Projects | Music | Sitemap | Contact |
Image Processing Basic OperationsProjects / Academic / Image Processing Basic Operations Task Details1 Image AcquisitionAcquire and store suitable grey-level images of a hand (for example, your own) and of a face (more amusing if it's someone else's). By "suitable", I mean under appropriate lighting and imaging conditions such that the hand and face fill a reasonably large proportion of the field of view, that detail within each image can be seen, for example of the vein structure on the back of the hand and of facial features, and that the images are not too dark (ie, above the noise level) without being too bright (ie, reaching the maximum signal level of the camera). In view of the exercises to be carried out, you might grab several examples of each image so that you can choose the one best suited to each exercise. For the hand examples, for use in a later practical you may also wish, without moving the camera, to take some images of the background against which you imaged your hand.2 Grey-Level TransformationSubject one example of your hand and one example of your face images to a linear and to a non-linear grey-level transformation. In the former case, choose a suitable range of the original (source) image to be transformed, whilst in the latter case use the transformation: I' = (I) gwith g in the range 0.5-1.0. This kind of non-linear transformation (gamma correction) is used in photography and to correct for the characteristics of acquisition or display devices. Note that for a CCD camera, g is approximately one. A histogram of the image's grey-levels may be obtained by using the IDL 'histogram' command. You may spot that xv contains a facility for doing just this. If you wish, use it to check your result, but do not use it as a substitute for the exercise as that would destroy the point of the example which is to practise by coding it yourself using IDL. If your images don't work well, try the jpeg example image of me (bernard0.jpg) or Charlie (charliegrey.tif) in my public_html directory. The former is a colour image and will need converting to grey-level images first. Alternatively, try changing each colour channel separately or just have fun using the colour editor in xv. 3 Histogramming(i) Write an IDL program that computes the histogram h(I) of an MxN image I and demonstrate that it works by comparing results obtained with your code to the histograms generated by the IDL command for each of the two example images you chose above.(ii) Modify your code also to calculate the cumulative histogram, H(I), defined to be the number of pixels with grey-level or intensity less than or equal to I. Compute the cumulative histograms for the example images used in (i) above and compare to what may be obtained using IDL. Comment on the form of the cumulative histograms obtained. (iii) Adapt your code to produce the histogram and cumulative histograms from part of an image defined by a rectangular window specified by the pixel co-ordinates of its top left and bottom right hand corners. Again illustrate its use on your example images and compare your results with what can be obtained using IDL. Comment on the form of the cumulative histograms, in particular what happens to them near the maximum intensity, I U , or near I=255 as the case may be. 4 Histogram EqualisationThe histogram of an image's grey-level distribution, h(I), when normalised by the number of pixels, MN, may be regarded as an approximation to the probability distribution of the image intesity p(I) = h(I)/MN, and similary H(I) used to approximate the cumulative probability distribution P(I). In order to obtain a uniform histogram, the grey level intensities in image I must be mapped into a different set, I'. If the histogram of I' is to be uniform over the range, I' L </= u=" " /> I'=I' L +(I' U -I' L )H(I) .Comment on the nature of this mapping, in particular when, in terms of the quantisation levels, (i) it is one-to-one, (ii) many-to-one, (iii) one-to-many. Verify that the histogram obtained with this mapping is uniform. Comment on your results. 5 Producing an ImageUse the mapping above to produce the new image I'(x,y) from the original I(x,y), taking particular care when the mapping from I to I' is one-to-many and, in this case, assigning pixels from I at random to the appropriate quantisation levels in I'. Compare your results with what may be obtained by using a tool such as xv.6 Histogram ManipulationRepeat 3.4 and 3.5, but with the mapping I'=I' L +(I' U -I' L )[H(I)] 1/2 . Comment on the form of the histogram obtained for I' and on the appearance of the transformed image I'(x,y). Project WorkContents Page 1. Aim 1 2. Methods2 2.1 Grey-scale transformations 2 2.2 Histogram calculation 2 2.3 Area selection 3 2.4 Equalisation 3 3. Results 4 3.1 Grey-scale transformations 4 3.2 Histogram calculation 5 3.3 Area selection 7 3.4 Equalisation 8 4. Discussion and Conclusions 10 4.1 Grey-scale transformations10 4.2 Histogram calculation 10 4.3 Equalisation 10 5. Appendix12 5.1 Common Methods 12 5.2 Grey-scale transformations 13 5.3 Histogram calculation 13 5.4 Area selection 14 5.5 Equalisation 15 1. Aim To investigate various methods for manipulating the proportion of intensities in a greyscale image. Initial methods include linear and non-linear transformations on the intensity values to either lighten or darken the image. Code is written to produce normalised and normalised cumulative histograms for an image and adapted to analyse a user defined area. Further techniques involve using the cumulative histogram to equalise the greyscale intensities in an image. Furthermore the equalisation process is adapted to produce certain effects. Images The images of a hand and a face are shown below (Fig. 1.1 and 1.2) and were chosen for their differing range of intensities (Fig. 1.3 and 1.4). Fig. 1.1 Fig 1.2 2. Methods The image is read into a two dimensional array using the TIF_READ function. In older versions of IDL such as that on the Silicon Graphics workstations the function is named READ_TIF. It was necessary to rotate the original image array by 180 degrees and reverse it, since between reading the image file and displaying it, the horizontal orientation of the picture is reversed. This makes the image appear upsidedown, which may cause confusion when selecting an area later on. The vertical-axis REVERSE function and the ROTATE function, which rotates a matrix in multiples of 90 degrees, were used for this. 2.1 Grey-scale transformations The image array is subjected to liner and non-linear grey-level transformations, using a simple multiplier in the former case and raising the array to some power (<1) in the latter. The IDL HISTOGRAM function is used to observe the effect on the range of intensities. Linear multipliers of 0.5 an 1.5 were chosen. Powers of 0.8 and 0.9 were chosen for the non-linear transformation. The face image is chosen for grey-scale transformations since the wide and fairly even spread of intensities should show the effect of the transformations more fully. 2.2 Histogram calculation A normalised histogram gives the relative proportions of each pixel intensity in the image and hence approximates to the probability distribution of pixel intensities. A histogram is created by counting the number of pixels at each intensity between 0 and 255, so a one-dimensional array of 256 elements is required. Two 'for' loops are used to traverse the image array, incrementing the appropriate value in the histogram array each time the corresponding intensity is encountered. The histogram is normalised by dividing the frequency at each intensity by the total number of pixels in the image. A cumulative histogram is created by summing the frequencies up to and including the required value. A 'for' loop traverses the histogram array adding each element to the subsequent element to achieve this. The IDL HISTOGRAM function is used to compare with the histogram produced with the code. There is no cumulative histogram function in IDL so the summing 'for' loop would be required to convert the IDL histogram array. If the histograms compare well however it can be assumed that the cumulative histograms will also. Normalised and normalised cumulative histograms were plotted for both images. 2.3 Area selection The selected area is copied to a new array, mapping the original image elements at (x,y) to new positions at (x-x',y-y'), where (x',y') is the lower left corner of the area selected. The new array now contains the selected area of the original image and can be treated as a new image. 2.4 Equalisation The hand image was chosed for equalisation since the face image was observed to have a fairly even spread of intensities already. To produce a uniform histogram between I'L and I'U, the mapping: I'=I'L+(I'U-I'L)H(I) is required, where H(I) is the cumulative histogram of the original intensities, I. As with the histogram creation two 'for' loops traverse the image elements, performing the mapping on the current pixel intensity, and storing the value in an new array. Histograms and cumulative histograms were plotted using I'L=0 and I'U=255. For comparison the IDL HIST_EQUAL function was used to equalise the image array and the HISTOGRAM function used in plotting. The mapping code was adjusted to produce the mapping: I'=I'L+(I'U-I'L)[H(I)]1/2and plots produced (I'L=0 and I'U=255.) To observe the effect of changing the upper and lower intensity limits, plots were produced using: I'=I'L+(I'U-I'L)H(I) with I'L=100 and I'U=200. 3. Results3.1 Grey-scale transformations Below are shown the results of linear transformations with multipliers of 0.5 (Fig. 2.1 and 2.2) and 1.5 (Fig. 2.3 and 2.4). Also the effect of non-linear transformations using powers of 0.8 (Fig. 2.5 and 2.6) and 0.9 (Fig.2.7 and 2.8). Fig. 2.3 Normalised Histograms Below are the histograms generated by the histogram function in appendix 5.3 and those generated using the IDL HISTOGRAM function, for both face (Fig. 2.9 and 2.10) and hand images (Fig. 2.11 and 2.12). Cumulative HistogramsAs shown the IDL histograms match those produced well. The cumulative histograms would hence also not differ. Below, those produced with the code in appendix 5.3 are shown for both face (Fig. 2.14) and hand (Fig 2.15). 3.3 Area Selection Fig. 2.16
upper left co-ordinate of (100,200) and a lower right co-ordinate of (200,100). The normalised and normalised cumulative histograms are show in Fig. 2.17 and 2.18. 3.4 Equalisation The equalised hand image is shown in Fig. 2.19 below. Fig 2.20 and 2.21 show the normalised and normalised cumulative histograms. The histograms for the IDL equalised image are shown in Fig. 2.22 and 2.23 Fig. 2.19 Adapted Mapping Fig 2.24 and 2.25 show the effect of using the mapping I'=I'L+(I'U-I'L)[H(I)]1/2in equalising the hand image, with I'L = 0 and I'U = 255. Output Frequency RangeTo test the effect of changing the upper and lower frequency limits the mapping I'=I'L+(I'U-I'L)[H(I)]was used with I'L = 100 and I'U = 200, and histograms plotted (Fig. 2.26 and Fig. 2.27). 4. Discussion and Conclusions4.1 Grey level transformations Linear transformations with multipliers less than 1 lower all pixel values uniformly creating a darker and less defined image. Using multipliers over 1 in value produces grey scale values in excess of 255. The IDL TV procedure wrap's the histogram back around 0, meaning that the pixel proportions, h(I) at intensities above 255 are shifted to low intensities, h(I-255). This produces a colour distortion, which causes the loss of less defined details in the image, when viewed. If all intensity values above 255 were set to 255, the effect would be more the inverse of using multipliers below 1, in that the image would be made lighter with a loss of information in regions of high intensity. The non-linear transformation: I' = (I)g , with g in the range 0.5-1.0 will spread lower intensities more than higher. The effect is to increase the contrast in lighter regions while reducing the contrast in dark regions. Since all intensities are lowered the image becomes darker and the overall contrast is reduced. eg when g = 0.5 the highest intensity is 16. Using powers, g = 0.5 and g = 0.6 was observed to produce images which are virtually indistinguishable from a completely black image. 4.2 Histogram calculation The histogram for the face image (Fig. 2.10) shows a large spread of intensities with a fairly even distribution generally. The cumulative histogram therefore rises fairly steadily (Fig. 2.14). The hand histogram in contrast has no intensities at either extreme and a large peak in intensity proportion around 150. The cumulative histogram hence rises slowly at first, increasing to a maximum gradient at 150, and slowing to reach 1 before 255. There are no distinguishable differences between the IDL and self-generated histograms so the code fulfils its task. 4.3 Equalisation The mapping: I'=I'L+(I'U-I'L)H(I)in the simplest case when I'L=0 and I'U=255 reduces to I'=255.H(I). This shows that the new intensity is relative to the cumulative proportion of the original intensity. The old intensity has a corresponding cumulative proportion which is scaled this by the new range of intensities (eg 255) giving the new value. There are three possible combinations of mapping types depending on the shape of the cumulative histogram: One to One: In most cases each new intensity will be mapped to a fixed value determined by the corresponding value in the cumulative histogram. Many-to-One: When the gradient of the cumulative histogram is 0 ie. When there are intensities with zero occurrences of adjacent higher intensities, many intensities in the old image will be mapped to the same value new intensity. One-to-Many: For one intensity to be mapped to more than one, the slope of the cumulative histogram is required to be vertical, which is not possible. When the new range of intensities is larger than the original range it would appear as if one-to-many mapping would be required to spread the proportions of each intensity into the new range. The mapping used has the effect of spreading the range using one-to-one mapping, however. To fill a larger range, high intensity values are increased in value while low intensities are lowered. This has the effect of leaving no proportions of certain intensities, making the equalised histogram look very spiky. Fig. 2.26 and 2.27 show that the equalised frequency range can easily be controlled. The limit, I'L sets the minimum intensity while the scaling factor (I'U-I'L) limits the maximum intensity value. The effect is to reduce the overall contrast of the image and shift the range of intensities. The mapping: I'=I'L+(I'U-I'L)[H(I)]1/2 changes the shape of the cumulative histogram before mapping (all cumulative histograms mentioned are assumed to be normalised). The effect of square rooting the cumulative histogram function is to reduce the cumulative proportion at high intensities more than at low. Since the gradient of the cumulative histogram is then much lower at high intensities, more original pixels map to high values. This effect is shown in the equalised image (Fig 2.24 and 2.25) where there are larger proportions at high intensities so the cumulative histogram rises slowly at first then increasingly faster. Since the gradient of the original adapted cumulative histogram (H(I)1/2) is high at low intensities, a range of low intensities are spread more in mapping. This would enhance darker details of the image at the cost of reducing the contrast in lighter regions. This shows how the mapping can be used to bias the equalisation towards high intensities. Using I'=I'L+(I'U-I'L)[H(I)]2would produce the opposite effect and make the image darker overall while enhancing details of high intensity. The gradient of the cumulative histogram dictates how the new intensities are spread. The shape can therefore be tailored to spread any range of original intensities. This practical has emphasised how invaluable the histogram is as an analysis tool and how the cumulative histogram can be used to equalise the image. 5. Appendix - source code and explanations All code has been written for and tested on IDL Development Environment for Windows, Version 5.2.1. File paths variables are relative to the environment, so they are enclosed in '<>'. The specific file paths used are irrelevant. 5.1 Common methods Reading a file into an array, displaying the image and its histogram and saving the display to file. Any display window update will overwrite the current display so in saving to file the unwanted TV and PLOT commands are removed. Read tiff from file dest='' ;eg. C:face.tif imag = READ_TIFF(dest) Invert image horizontally image=REVERSE(ROTATE(imag,2)) Print image TV, image Plot histogram with IDL Histogram function PLOT, HISTOGRAM(image) Save contents of display window to bitmap image file WRITE_BMP, '', TVRD() ;eg. C:face.bmp END 5.2 Grey-scale transformations: The following code is used to produce linear and non-linear transformations on the image. Images and corresponding histogram plots are produced. The multiplier is chosen and filled in before the code is compiled to produce the different linear and non-linear plots. Read tiff from file dest='' ;eg. C:face.tif imag = READ_TIFF(dest) ;Invert image horizontally image=REVERSE(ROTATE(imag,2)) x= ;Linear:x=0.5 & x=1.5, Non-linear:x=0.8 & x=0.9 ;Linear transformations TV, image*x PLOT, HISTOGRAM(image*x) ;Non-linear transformations TV, image^x PLOT, HISTOGRAM(image^x) END 5.3 Histogram calculations To deal with any size images it is necessary to find the image dimentions. The QUERY_TIFF function returns a structure (here called inf) which contains the image dimentions in a two element array labelled DIMENTIONS. The following code calculates and produces plots of the histogram,normalised histogram and normalised cumulative histogram. ;Read tiff from file dest='' ;eg. C:face.tif imag = READ_TIFF(dest) image=REVERSE(ROTATE(imag,2)) info=QUERY_TIFF(dest,inf) ;Obtain image dimentions Wi=inf.DIMENSIONS[0] He=inf.DIMENSIONS[1] ;Create array for histogram hist=MAKE_ARRAY(256) ;Traverse image pixels, incrementing the appropriate element ;in the histogram array when the corresponding intensity is ;encountered FOR I = 0, Wi-1 DO BEGIN FOR J = 0, He-1 DO BEGIN a=image[[I],[J]] hist[a]=hist[a]+1 ENDFOR ENDFOR PLOT, hist ;Histogram ;Normalise by dividing each histogram value by the total ;number of pixels FOR X = 0, 255 DO hist[X]=hist[X]/(Wi*He) PLOT, hist ;Normalised histogram ;Convert the histogram to cumulative by adding each element ;sequentially to the next FOR K = 0, 254 DO hist[K+1]=hist[K+1]+hist[K] PLOT, hist ;Normalised Cumulative histogram END 5.4 Area Selection The following code promts a user to define an area of the image from which the normalised histogram and normalised cumulative histogram are plotted. ;Read tiff from file dest='' ;eg. C:face.tif imag = READ_TIFF(dest) image=REVERSE(ROTATE(imag,2)) Prompt user for co-ordinates of selected area READ, lox, PROMPT='Enter topleft X co-ordinate: ' READ, hiy, PROMPT='Enter topleft y co-ordinate: ' READ, hix, PROMPT='Enter bottomright X co-ordinate: ' READ, loy, PROMPT='Enter bottomright y co-ordinate: 'Wi=hix-lox He=hiy-loy Creating new array with selected area area=MAKE_ARRAY(hix-lox,hiy-loy) FOR A = lox, hix DO BEGIN FOR B = loy, hiy DO area[[A-lox],[B-loy]]=im[[A],[B]] ENDFOR ;Historam creation hist=MAKE_ARRAY(256) FOR I = 0, Wi DO BEGIN FOR J = 0,He DO BEGIN c=area[[I],[J]] hist[c]=hist[c]+1 ENDFOR ENDFOR ;Normalise FOR X = 0, 255 DO hist[X]=hist[X]/(Wi*He) PLOT, hist ;Normalised Histogram of selected area ;converting histogram to cumulative FOR K = 0, 254 DO hist[K+1]=hist[K+1]+hist[K] PLOT, hist END 5.5 Equalisation The following code calculates the normalised cumulative histogram of an image and uses it to produce an image, equalised between user defined intensity limits. The IDL HIST_EQUAL function produces an equalised image with which to compare that produced. Normalised histograms and normalised cumulative histograms are plotted for both IDL and code equalised images. Read tiff from file dest='c:/windows/desktop/ip/hand.tif' imag = READ_TIFF(dest) image=REVERSE(ROTATE(imag,2)) info=QUERY_TIFF(dest,inf) Prompt user for upper and lower intensities for equalisation READ, loI, PROMPT='Enter minimum new intensity value: ' READ, hiI, PROMPT='Enter maximum new intensity value: ' Obtain image dimentions Wi=inf.DIMENSIONS[0] He=inf.DIMENSIONS[1] Create Histogram from original image hist=MAKE_ARRAY(256) FOR I = 0, Wi-1 DO BEGIN FOR J = 0, He-1 DO BEGIN a=image[[I],[J]] hist[a]=hist[a]+1 ENDFOR ENDFOR Normalise FOR X = 0, 255 DO hist[X]=hist[X]/(Wi*He) Convert to cumulative FOR K = 0, 254 DO hist[K+1]=hist[K+1]+hist[K] Array for equalised image eqtif = MAKE_ARRAY(Wi,He) Intensity mapping FOR I = 0, Wi-1 DO BEGIN FOR J = 0, He-1 DO BEGIN b=image[[I],[J]] eqtif[[I],[J]]=(hist[b])*(hiI-loI)+loI For the adapted equalisation replace with eqtif[[I],[J]]=((hist[b])^0.5)*(hiI-loI)+loI ENDFOR ENDFOR Create histogram of equalised image his=MAKE_ARRAY(256) FOR I = 0, Wi-1 DO BEGIN FOR J = 0, He-1 DO BEGIN c=eqtif[[I],[J]] his[c]=his[c]+1 ENDFOR ENDFOR Normalise histogram FOR X = 0, 255 DO his[X]=his[X]/(Wi*He) PLOT, his Normalised Histogram of Equalised Image Convert histogram to cumulative FOR K = 0, 254 DO his[K+1]=his[K+1]+his[K] PLOT, his Normalised Cumulative Histogram of Equalised Image' Equalise image using IDL HIST_EQAL function idleq = HIST_EQUAL(image) Create histogram of IDL equalised image eqhis=MAKE_ARRAY(256) FOR I = 0, Wi-1 DO BEGIN FOR J = 0, He-1 DO BEGIN d=idleq[[I],[J]] eqhis[d]=eqhis[d]+1 ENDFOR ENDFOR FOR X = 0, 255 DO eqhis[X]=eqhis[X]/(Wi*He) Normalise PLOT, eqhis Normalised Histogram of IDL Equalised Image Convert to cumulative FOR K = 0, 254 DO eqhis[K+1]=eqhis[K+1]+eqhis[K] PLOT, eqhis Normalised Cumulative Histogram of IDL Equalised Image END |
|
Home | Projects | Music | Sitemap | Contact |