image processing. image processing once we have an image in a digital form we can process it in the...
TRANSCRIPT
Image processing
• Once we have an image in a digital form we can process it in the computer.
• We apply a “filter” and recalculate the pixel values of the processed image.
• We will look at the application of filters to sharpen and soften (make less sharp) the image.
Filters or Kernals
• Our filters take the form of a small matrix or vector which we place (in turn ) over the image at each pixel position.
• Typically our filters are symmetrical 3 x 3 matrices.
• We use a process called convolution to apply the filter to the image.
Convolution
• Convolution is the process of multiplying each element of our filter by the pixel values of the image that the filter covers and adding the products.
• We can describe the result as “the original image convolved with the filter”.
• conv2 is a Matlab function which can do this.
Strictly what is described above is not a convolution but a correlation, however if our filters are symmetric the result is the same.
Strictly what is described above is not a convolution but a correlation, however if our filters are symmetric the result is the same.
Convolution and filters
Filter
Image sample set
56
56
5656
565659 89 15
1 4 52 3 79 4 5
04
0
0 0-1-1
-1-1
04
0
0 0-1-1
-1-1
Convolution and filters
1 4 52 3 79 4 5
04
0
0 0-1-1
-1-1
34 5
59 47
5
904)1(90
7)1(342)1(
504)1(10
newvalue
So the value of 3 in the centre of the original image is replaced with –5 on the convolved image.
Image softening
• If we make all the elements of the filter equal to one ninth, then the convolution value will be the average of all the elements “covered” by the filter.
• It is the same as adding all the 9 values together and dividing by nine.
Image softening
• The following block of image samples represents a sharp vertical edge as pixel values suddenly change from 0 (black) to 255 (white).
• We will attempt to smooth it (make it less sharp).
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
Sample Block
Image softening
• Consider the operation of the averaging filter on the block of image samples.
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
1/9 1/9 1/9
1/9 1/9 1/9
1/9 1/9 1/9
Filter
Sample Block
Image softening
• As we “run” the filter over the centre of the sample block. The following changes will take place. See next slide.
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
0 0 0 0 255 255 255 255
Sample BlockFilter
Image softening
• The zeros in the 4th column are replaced with 85, since:
0 0 0 0 255 255 255 255
0 0 0 85 255 255 255 255
0 0 0 85 255 255 255 255
0 0 0 85 255 255 255 255
0 0 0 0 255 255 255 255
859
1255
9
10
9
10
9
1255
9
10
9
10
9
1255
9
10
9
10
Image softening
• Similarly as we perform the convolution of the filter with the rest of the centre of the sample block we get:
0 0 0 0 255 255 255 255
0 0 0 85 170 255 255 255
0 0 0 85 170 255 255 255
0 0 0 85 170 255 255 255
0 0 0 0 255 255 255 255
Image softening
• The resultant convolution (image) no longer has a sharp edge, but a more gradual one.
0 0 0 0 255 255 255 255
0 0 0 85 170 255 255 255
0 0 0 85 170 255 255 255
0 0 0 85 170 255 255 255
0 0 0 0 255 255 255 255
Image softening
• That’s the theory; lets try it.
for i=1:256grey(i, 1:3)=(i-1)/255endcolormap(grey)myedge=zeros(5, 8)myedge(:, 5:8)=255filt=ones(3,3)/9image(myedge)myconv=conv2(myedge,filt, 'valid')image(myconv)
Softening an image
im=imread(‘urban.bmp');
smooth=[ 1 1 1; 1 1 1; 1 1 1]/9
% convolve all colours
sharpim(:,:,1)=conv2(im(:,:,1),smooth);
sharpim(:,:,2)=conv2(im(:,:,2),smooth);
sharpim(:,:,3)=conv2(im(:,:,3),smooth);
image(im)
figure
image(uint8(smoothim))
Image sharpening
• It is easier to see this in one dimension initially.
• Consider some neighbouring pixels that are not sharp.
• If they are not sharp there will be no abrupt change in value between them.
• If we can cause an a more abrupt change in value between neighbouring pixels, we will sharpen the image.
Image sharpening
• It is easier to see this in one dimension initially.
• Consider some neighbouring pixels that are not sharp.
• If they are not sharp there will be no abrupt change in value between them.
• If we can cause an a more abrupt change in value between neighbouring pixels, we will sharpen the image.
Image sharpening
• The idea is to generate artificial sharp edges at the points of differing neighbouring pixels and then add these edges to the original image.
• The following diagram shows the variation in brightness values for:– (a) A perfect (sharp) edge.– (b) A blurred edge.– (c) The artificial correction to be
added.– (d) The corrected (enhaced) edge.
Image sharpening
• What kernal (filter) will (when convolved with the image) generate the correction waveform (c) ?
• In one dimension • Check it.
– It produces zero for flat fields, but a negative and positive edge at edges
• In two dimensions
04
0
0 0-1-1
-1-1
2-1 -12
Image sharpening
• We need to produce a kernal which will add the artificial edges and the original together.
• The original will be produced if we only have a one (1) in the centre of the a convolution kernal. Check it.
• Add this to the edge generator + =
01
0
0 000
00
04
0
0 0-1-1
-1-1
01
0
0 000
00
05
0
0 0-1-1
-1-1