210 likes | 232 Views
Image Arithmetic. Image arithmetic is the implementation of standard arithmetic operations, such as addition, subtraction, multiplication, and division, on images. Image arithmetic has many uses in image processing both as a preliminary step in more complex operations and by itself.
E N D
Image Arithmetic • Image arithmetic is the implementation of standard arithmetic operations, such as addition, subtraction, multiplication, and division, on images. • Image arithmetic has many uses in image processing both as a preliminary step in more complex operations and by itself.
Image Arithmetic • Adding Images • To add two images or add a constant value to an image, use the imadd function. • imadd adds the value of each pixel in one of the input images with the corresponding pixel in the other input image and returns the sum in the corresponding pixel of the output image • Image addition has many uses in image processing. For example, the following code fragment uses addition to superimpose one image on top of another. The images must be the same size and class. I = imread('rice.png'); J = imread('cameraman.tif'); K = imadd(I,J); imshow(K)
Image Arithmetic • Adding Images • You can also use addition to brighten an image by adding a constant value to each pixel. For example, the following code brightens an RGB image. RGB = imread('peppers.png'); imshow(RGB); RGB2 = imadd(RGB, 50); imshow(RGB2);
Image Arithmetic • Subtracting Images • To subtract one image from another, or subtract a constant value from an image, use the imsubtract function • imsubtract subtracts each pixel value in one of the input images from the corresponding pixel in the other input image and returns the result in the corresponding pixel in an output image. • you can use image subtraction to detect changes in a series of images of the same scene. This code fragment subtracts the background from an image of rice grains. The images must be the same size and class. rice= imread('rice.png'); background = imopen(rice, strel('disk',15)); % disk, radius 15 rice2 = imsubtract(rice,background); imshow(rice),figure,imshow(rice2)
Image Arithmetic • Multiplying Images • To multiply two images, use the immultiply function • immultiply does an element-by-element multiplication (.*) of corresponding pixels in a pair of input images and returns the product of these multiplications in the corresponding pixel in an output image. • Multiplying an image by a constant, referred to as scaling, is a common image processing operation. When used with a scaling factor greater than 1, scaling brightens an image; a factor less than 1 darkens an image I = imread('moon.tif'); J = immultiply(I,1.2); imshow(I); figure, imshow(J)
Image Arithmetic • Dividing Images • To divide two images, use the imdivide function. The imdivide function does an element-by-element division (./). • Image division, like image subtraction, can be used to detect changes in two images. However, instead of giving the absolute change for each pixel, division gives the fractional change or ratio between corresponding pixel values. Image division is also called ratioing. I = imread('rice.png'); background = imopen(I, strel('disk',15)); Ip = imdivide(I,background); imshow(Ip,[])
Image Resizing • Tochange the size of an image, use the imresize function • Using imresize, you can specify the size of the output image in two ways: • By specifying the magnification factor to be used on the image: • To enlarge an image, specify a magnification factor greater than 1. To reduce an image, specify a magnification factor between 0 and 1. For example, the command below increases the size of the image I by 1.25 times. 2.By specifying the dimensions of the output image I = imread('circuit.tif'); J = imresize(I,1.25); imshow(I) figure, imshow(J) Y = imresize(I,[100 150])
Image Rotation • To rotate an image, use the imrotate function. imrotate accepts two primary arguments: • The image to be rotated • The rotation angle I = imread('circuit.tif'); J = imrotate(I,35); imshow(I) figure, imshow(J)
Image Cropping • To extract a rectangular portion of an image, use the imcrop function. Imcrop accepts two primary arguments: • The image to be cropped • The coordinates of a rectangle that defines the crop area % rect is a four-element position vector[xminymin width height] that specifies the size and position of the crop rectangle Imshow ( ‘circuit.tif’) I = imcrop; imshow(I); z=imread('circuit.tif'); I = imcrop(z,[74 54 100 150]); imshow(I)
Histogram Processing • One of the most basic ways to enhance an image is to change its brightness and its contrast, and this can be done is by working with the image's histogram. • A histogram of an image shows the current level of contrast (the distribution of gray levels). I = imread('pout.tif'); imshow(I) imhist(I)
Histogram Equalization • I=imread('pout.tif'); • imhist(I); • I2=histeq(I); • imshow(I2) • imwrite(I2,’newone.jpg’) • imfinfo(’newone.jpg’)
Edge Detection • You can use the edge function to detect edges, which are those places in an image that correspond to object boundaries. • The most powerful edge-detection method that edge provides is the Canny method. The Canny method differs from the other edge-detection methods in that it uses two different thresholds (to detect strong and weak edges), and includes the weak edges in the output only if they are connected to strong edges. This method is therefore less likely than the others to be fooled by noise,andmore likely to detect true weak edges.
Edge Detection • The following example illustrates the power of the Canny edge detector by showing the results of applying the Sobel and Canny edge detectors to the same image: • Read image and display it. • I = imread('coins.png'); • imshow(I) • Apply the Sobel and Canny edge detectors to the image and display them. • x= edge(I,'sobel'); • y= edge(I,'canny'); • imshow(x) • figure, imshow(y)
Edge Detection • Finding the edges of an image using the Prewitt and Canny methods I = imread('circuit.tif'); BW1 = edge(I,'prewitt'); BW2 = edge(I,'canny'); imshow(BW1); figure, imshow(BW2)
Edge Detection • Finding the edges of an image using the Prewitt and Canny methods I = imread('circuit.tif'); BW1 = edge(I,'prewitt'); BW2 = edge(I,'canny'); imshow(BW1); figure, imshow(BW2)
Median Filter • Median filtering is accomplished using the medfilt2 function • Where B is the output image, A is the input image, and m x n is the size of the neighborhood to be used for median determination. B = medfilt2(A,[m n]); I = imread('eight.tif'); J = imnoise(I,'salt & pepper',0.02); K = medfilt2(J); imshow(J), figure, imshow(K)
Averaging filters >> c=imread('cameraman.tif'); >> f1=fspecial('average'); >> cf1=filter2(f1,c); • We now have a matrix of data type double.to display this,we can do any of the following • Transform it to a matrix type uint8 for use with imshow • Divide its values by 255 to obtain a matrix with values in the 0-1:1-0 ranges for use with imshow >> figure,imshow(c),figure,imshow(cf1/255)
Un sharp masking filter • I = imread('moon.tif'); • h = fspecial('unsharp'); • I2 = imfilter(I,h); • imshow(I), title('Original Image') • figure, imshow(I2), title('Filtered Image')