intro3.pptx
Document Details

Uploaded by OpulentCopper
Full Transcript
RGB to Grayscale Conversion There are two methods to convert RGB to Grayscale. Both have their own merits and demerits. The methods are: • Average method • Weighted method or Luminosity method Average method The average method is the simple one. You have to take the average of three colors. Since it...
RGB to Grayscale Conversion There are two methods to convert RGB to Grayscale. Both have their own merits and demerits. The methods are: • Average method • Weighted method or Luminosity method Average method The average method is the simple one. You have to take the average of three colors. Since it’s an RGB image, you have to add r with g with b and then divide it by 3 to get your desired grayscale image. It’s done in this way. Example Grayscale = (R + G + B / 3) : Explanation One thing to be sure is that something happens to the original works. It means that our average method works. But the results were not as expected. We wanted to convert the image into a grayscale, but this turned out to be a rather black image. Problem This problem arises because we take an average of the three colors. Since the three different colors have three different wavelengths and contribute to the image formation, we have to take the average according to their contribution, not do it averagely by using the average method. 33% of Red, 33% of Green, 33% of Blue We are taking 33% of each, which means, that each of the portions has the same contribution in the image. Weighted method or luminosity method You have seen the problem that occurs in the average method. The weighted method has a solution to that problem. Since red color has more wavelength than all three colors, and green is the color that has not only less wavelength than red color but also green is the color that gives a more soothing effect to the eyes. It means that we have to decrease the contribution of the red color, increase the contribution of the green color, and put the blue color contribution in between these two. So, the new equation that form is: New grayscale image = ( (0.3 * R) + (0.59 * G) + (0.11 * B) ). According to this equation, Red has contributed 30%, Green has contributed 59% which is greater in all three colors and Blue has contributed 11%. Applying this equation to the image, we get this Original Image: Explanation As you can see here, the image has now been properly converted to grayscale using the weighted method. As compared to the result of the average method, this image Pixel Resolution Resolution The resolution can be defined in many ways. Such as pixel resolution, spatial resolution, temporal resolution, and spectral resolution. Out of which we are going to discuss pixel resolution. You have probably seen that in your computer settings, you have monitor resolution of 800 x 600, 640 x 480, etc. In pixel resolution, the term resolution refers to the total number of count of pixels in a digital image. For example: If we define resolution as the total number of pixels, then pixel resolution can be defined with a set of two numbers. The first number is the width of the picture or the pixels across columns, and the second number is the height of the picture or the pixels across its width. We can say that the higher the pixel resolution, the Megapixels We can calculate the megapixels of a camera using pixel resolution. Column pixels (width ) X row pixels ( height ) / 1 Million. The size of an image can be defined by its pixel resolution. Size = pixel resolution X bpp ( bits per pixel ) Calculating the megapixels of the camera Let’s say we have an image of dimension: 2500 X 3192. Its pixel resolution = 2500 * 3192 = 7980000 bytes. Dividing it by 1 million = 7.9 = 8 megapixels (approximately). Aspect ratio Another important concept with the pixel resolution is the aspect ratio. Aspect ratio is the ratio between the width of an image and the height of an image. It is commonly explained as two numbers separated by a colon (8:9). This ratio differs in different images, and in different screens. The common aspect ratios are: 1.33:1, 1.37:1, 1.43:1, 1.50:1, 1.56:1, 1.66:1, 1.75:1, 1.78:1, 1.85:1, 2.00:1, etc. Advantage The aspect ratio maintains a balance between the appearance of an image on the screen, which means it maintains a ratio between horizontal and vertical pixels. It does not let the image get distorted when the aspect ratio is increased. for example This is a sample image, which has 100 rows and 100 columns. If we wish to make it smaller, and the condition is that the quality remains the same or in another way, the image does not get distorted, here is how it happens. Original image Changing the rows and columns by maintaining the aspect ratio in MS Paint. Result maller image, but with the same balance. You have probably seen aspect ratios in the video players, where you can adjust the video according to your screen resolution. Finding the dimensions of the image from the aspect ratio: The aspect ratio tells us many things. With the aspect ratio, you can calculate the dimensions of the image For example along the size of the image. If you with are given an image with an aspect ratio of 6:2 of an image of the pixel resolution of 480000 pixels the image is grayscale. And you are asked to calculate two things. Resolve pixel resolution to calculate the dimensions of the image Calculate the size of the image Solution: Given: Aspect ratio: c:r = 6:2 Pixel resolution: c * r = 480000 Find: Number of rows =? Number of cols =? Solving first part: Solving 2nd part: Size = rows * cols * bpp Size of image in bits = 400 * 1200 * 8 = 3840000 bits Size of image in bytes = 480000 bytes Concept of Zooming Zooming Zooming simply means enlarging a picture in the sense that the details in the image become visible and clear. Zooming an image has many wide applications ranging from zooming through a camera lens, to zooming an image on the internet, etc. You can zoom something at two different steps. The first step includes zooming before taking a particular image. This is known as pre-processing zoom. This zoom involves hardware and mechanical movement. The second step is to zoom in once an image has been captured. It is done through many different algorithms in which we manipulate pixels to zoom in on the required portion. Optical Zoom v/s Digital Zoom The cameras support these two types of zoom. Optical Zoom: The optical zoom is achieved using the movement of the lens of your camera. An optical zoom is a true zoom. The result of the optical zoom is far better than that of digital zoom. In optical zoom, an image is magnified by the lens in such a way that the objects in the image appear to be closer to the camera. In optical zoom, the lens is physically extended to zoom or magnify an object. Digital Zoom: Digital zoom is image processing within a camera. During a digital zoom, the center of the image is magnified and the edges of the picture are cropped out. Due to the magnified center, it looks like the object is closer to you. During a digital zoom, the pixels got to expand, due to which the quality of the image is compromised. The same effect of digital zoom can be seen after the image is taken through your computer by using an image processing toolbox/software, such as Photoshop. The following picture is the result of digital zoom done through one of the methods given below in the zooming methods. Now since we are learning digital image processing, we will not focus, on how an image can be zoomed optically using a lens or other stuff. Rather we will focus on the methods, that enable to zoom a digital image. Zooming methods: Although many methods do this job, we are going to Zooming Methods Methods Pixel replication or (Nearest neighbor interpolation) Zero-order hold method Zooming K times Each of the methods has its advantages and disadvantages. We will start by discussing pixel replication. Method 1: Pixel replication: It is also known as Nearest neighbor interpolation. As its name suggests, in this method, we just replicate the neighboring pixels. The zooming is nothing but the increased amount of samples or pixels. This algorithm works on the same principle. Working: In this method, we create new pixels from the already given pixels. Each pixel is replicated in this method n For example: if you have an image of 2 rows and 2 columns and you want to zoom it twice or 2 times using pixel replication, here is how it can be done. For a better understanding, the image has been taken in the form of a matrix with the pixel values 1 2 of the image. 3 4 The above image has two rows and two columns, we will first zoom it row-wise. Row wise zooming: When we zoom it row-wise, we will just simply copy the pixels of the row to its adjacent new cell. Here is 1 how 1 it 2would 2 be done. 3 3 4 4 As you can that in the above matrix, each pixel is replicated twice in the rows. Column size zooming: The next step is to replicate each of the pixel columns wise, that we will simply copy the column pixel to its adjacent new column or simply below it. Here is how it would be done. 1 1 2 1 2 3 4 3 4 2 1 2 3 4 3 4 New image size: As can be seen from the above example, an original image of 2 rows and 2 columns has been converted into 4 rows and 4 columns after zooming. That means the new image has dimensions of (Original image rows * zooming factor, Original Image cols * zooming factor) Advantages and disadvantages: One of the advantages of this zooming technique is, it is very simple. You just have to copy the pixels and nothing else. The disadvantage of this technique is that the image gets zoomed but the output is very blurry. And as the zooming factor increased, the image got more and more blurred. That would eventually result in a fully blurred image. Method 2: Zero order hold The zero-order hold method is another method of zooming. It is also known as Zoom twice. Because it can only zoom twice. We will see in the below example why it does that. Working In the zero order hold method, we pick two adjacent elements from the rows respectively and then we add them divide the result by two, and place their result in between those two elements. We first do this row and then we do this column-wise. For example Let’s take an image of the dimensions of 2 rows and 2 columns and zoom it twice using zero-order hold. 1 3 2 4 First, we will zoom it row-wise and then column-wise. Row wise zooming 1 3 1 3 2 4 As we take the first two numbers : (2 + 1) = 3 and then divide it by 2, we get 1.5 which is approximated to 1. The same method is applied in row 2. Column wise zooming 1 2 3 1 2 3 2 3 4 We take two adjacent column pixel values which are 1 and 3. We add them and get 4. 4 is then divided by 2 and we get 2 which is placed in between them. The same method is applied in all the columns. New image size As you can see the dimensions of the new image are 3 x 3 whereas the original image dimensions are 2 x 2. So, it means that the dimensions of the new image are based on the following formula Advantages and disadvantages. One of the advantages of this zooming technique is that it does not create a blurry picture as compared to the nearest neighbor interpolation method. But it also has a disadvantage that it can only run on the power of 2. It can be demonstrated here. The reason behind twice zooming: Consider the above image of 2 rows and 2 columns. If we have to zoom it 6 times, using the zero-order hold method, we can not do it. As the formula shows us this. Method 3: K-Times zooming K times is the third zooming method we are going to discuss. It is one of the most perfect zooming algorithms discussed so far. It caters to the challenges of both zooming and pixel replication. K in this zooming algorithm stands for zooming factor. Working: It works this way. First of all, you have to take two adjacent pixels as you did in the zooming twice. Then you have to subtract the smaller one from the greater one. We call this output (OP). Divide the output(OP) with the zooming factor(K). Now you have to add the result to the smaller value and put the result in between those two values. Add the value OP again to the value you just put and place it again next to the previously put value. You have to do it till you place k-1 values in it. Repeat the same step for all the rows and columns, and For example: Suppose you have an image of 2 rows and 3 columns, which is given below. And you have to zoom it thrice or three 15 30 times. 15 30 15 30 K in this case is 3. K = 3. The number of values that should be inserted is k-1 = 3-1 = 2. Row wise zooming Take the first two adjacent pixels. Which are 15 and 30. Subtract 15 from 30. 30-15 = 15. Divide 15 by k. 15/k = 15/3 = 5. We call it OP.(where op is just a name) Add OP to the lower number. 15 + OP = 15 + 5 = 20. Add OP to 20 again. 20 + OP = 20 + 5 = 25. We do that 2 times because we have to insert k-1 values. Now repeat this step for the next two adjacent pixels. It is shown in the first table. It is shown in the second table Table 1. 15 20 25 30 20 25 15 30 20 25 15 20 25 30 Table 2. Column wise zooming The same procedure has to be performed columnwise. The procedure includes taking the two adjacent pixel values and then subtracting the smaller one from the bigger one. Then after that, you have to divide it by k. Store the result as OP. Add OP to a smaller one, and then again add OP to the value that comes in the first edition of OP. Insert the Here what you got after all that. 15 20 25 30 20 25 21 21 22 25 20 30 25 22 15 25 21 21 20 22 20 25 20 15 20 22 25 30 New image size The best way to calculate the formula for the dimensions of a new image is to compare the dimensions of the original image and the final image. The dimensions of the original image were 2 X 3. The dimensions of the new image are 4 x 7. The formula thus is: (K (number of rows minus 1) + 1) X (K (number of cols minus 1) + 1) Advantages and disadvantages One of the clear advantages that the k-time zooming algorithm has is that it can compute the zoom of any factor which is the power of the pixel replication algorithm, it gives an improved result (less blurry) which is the power of the zero-order hold method. Hence It comprises the power of the two algorithms. The only difficulty this algorithm has is that it has to be sorted in the end, which is an additional step, and thus increases the cost of computation. Spatial Resolution Image resolution Image resolution can be defined in many ways. One type of it is pixel resolution which has been discussed in the slides of pixel resolution and aspect ratio. Spatial resolution Spatial resolution states that the clarity of an image cannot be determined by the pixel resolution. The number of pixels in an image does not matter. Spatial resolution can be defined as the smallest discernible detail in an image. In another way, we can define spatial resolution as the number of independent pixel values per inch. In short, what spatial resolution refers to is that we cannot compare two different types of images to see which one is clear or which one is not. If we have to compare the two images, to see which one is more clear or which has more spatial resolution, we have to compare two images of the same size. For example: You cannot compare these two images to see the clarity of the image. Although both images are of the same person, that is not the condition we are judging. The picture on the left is a zoomed-out picture of Einstein with dimensions of 227 x 222. The picture on the right side has the dimensions of 980 X 749 and also it is a zoomed image. We cannot compare them to see which one is more clear. Remember the factor of zoom does not matter in this condition, the only thing that matters is that these So, to measure spatial resolution, these pictures would serve the purpose. Now you can compare these two pictures. Both the pictures have the same dimensions which are 227 X 222. Now when you compare them, you will see that the above has more spatial resolution or is clearer than the below picture. That is because the below picture is a blurred image. Measuring spatial resolution Since spatial resolution refers to clarity, so for different devices, different measures have been made to measure it. For example Dots per inch or DPI is usually used in monitors Lines per inch or LPI is usually used in laser Pixels per inch Pixel density or Pixels per inch is a measure of spatial resolution for different devices including tablets, and mobile phones. The higher the PPI, the higher the quality. To better understand it, that is how it is calculated. Let’s calculate the PPI of a mobile phone. Calculating pixels per inch (PPI) of Samsung Galaxy The Samsung Galaxy S4 has a PPI or pixel S4: density of 441. But how does it is calculated? First of all, we will Pythagoras theorem to calculate the diagonal resolution in pixels. It can be given as: Where a and b are the height and width resolutions in pixels and c is the diagonal resolution in pixels. For Samsung Galaxy S4, it is 1080 x 1920 pixels. So, putting those values in the equation gives the result C = 2202.90717 Now we will calculate PPI PPI = c / diagonal size in inches The diagonal size in inches of Samsung Galaxy S4 is 5.0 inches, which can be confirmed from anywhere. PPI = 2202.90717/5.0 PPI = 440.58 PPI = 441 (approx) That means that the pixel density of the Samsung Dots per inch The dpi is often related to PPI, whereas there is a difference between these two. DPI or dots per inch is a measure of the spatial resolution of printers. In the case of printers, dpi means how many dots of ink are printed per inch when an image gets printed out from the printer. Remember, each Pixel per inch doesn’t need to be printed by one dot per inch. There may be many dots per inch used for printing one pixel. The reason behind this is that most of the color printers use the CMYK model. The colors are limited. The printer has to choose from these colors to make the color of the pixel whereas, within pc, you have hundreds of thousands of colors. The higher the dpi of the printer, the higher the quality of the printed document or image on paper. Lines per inch When dpi refers to dots per inch, liner per inch refers to lines of dots per inch. The resolution of the halftone screen is measured in lines per inch. The following table shows some of the lines per inch capacity of the printers. Concept of Quantization Sampling about digital images The concept of sampling is directly related to zooming. The more samples you take, the more pixels, you get. Oversampling can also be called zooming. But the story of digitizing a signal does not end at sampling too, there is another step involved which is known as Quantization. What is quantization Quantization is the opposite of sampling. It is done on the axis. When you are quantizing an image, you are dividing a signal into quanta (partitions). On the x-axis of the signal, are the Hereon how is done coordinate values, and theity-axis, You can see in this image, that the signal has been quantified into three different levels. That means that when we sample an image, we gather a lot of values, and in quantization, we set levels to these values. This can be more clear in the image below. In the figure shown in sampling, although the samples have been taken, they were still spanning vertically to a continuous range of gray level values. In the figure shown above, these vertically ranging values have been quantized into 5 different levels or partitions. Ranging from 0 black to 4 white. This level could vary according to the type of image you want. Relation of Quantization with gray level resolution: The quantized figure shown above has 5 different levels of gray. It means that the image formed from this signal would only have 5 different colors. It would be a black and white image more or less with some colors of gray. Now if you were to make the quality of the image better, there is one thing you can do here. Which is, to increase the levels, or gray level resolution up. If you increase this level to 256, it means you have a grayscale image. Which is far better than the simple black-and-white image. Now 256, or 5, or whatever level you choose is called gray level. Remember the formula that we discussed in the previous tutorial on gray-level resolution which is, We have discussed that the gray level can be defined in two ways. Which were these two? Gray level = number of bits per pixel (BPP).(k in the equation) Gray level = number of levels per pixel. In this case, we have a gray level equal to 256. If we have to calculate the number of bits, we would simply put the values in the equation. In the case of 256 levels, we have 256 different shades of gray and 8 bits per pixel, hence the image would be grayscale. Reducing the gray level Now we will reduce the gray levels of the image to see the effect on the image. For example Let’s say you have an image of 8bpp, that has 256 different levels. It is a grayscale image and the image looks something like this. Now we will start reducing the gray 256 Gray Levels levels. We will first reduce the gray levels from 256 to 128. 128 Gray Levels There is not much effect on an image after decreasing the gray levels to its half. Let’s decrease some more. 64 Gray Levels Still not much effect, then let’s reduce the levels more. 32 Gray Levels Surprised to see, that there is still some little effect. Maybe it’s due to reason, that it is the picture of Einstein, but let’s reduce the levels more. 16 Gray Levels Boom here, we go, the image finally reveals, that it is affected by the levels. 8 Gray Levels 4 Gray Levels Now before reducing it, further two 2 levels, you can easily see that the image has been distorted badly by reducing the gray levels. Now we will reduce it to 2 levels, which is nothing but a simple black-and-white level. It means the image would be a simple blackand-white image. 2 Gray Levels Contouring There is an interesting observation here, that as we reduce the number of gray levels, there is a special type of effect starts appearing in the image, which can be seen clearly in 16 gray-level pictures. This effect is known as Contouring. ISO preference curves The answer to this effect, that is why it appears, lies in ISO preference curves What is contouring? As we decrease the number of gray levels in an image, some false colors, or edges start appearing on an image. This has been shown in our last slides of Quantization. Let’s have a look at it. Consider we, have an image of 8bpp (a grayscale image) with 256 different shades of The above picture has 256 different shades of gray or gray levels. gray. Now when we reduce it to 128 and further reduce it to 64, the image is more or less the same. But when we reduce it further to 32 different levels, we got a picture like this If you look closely, you will find that the effects start appearing on the image. These effects are more visible when we reduce it further to 16 levels and we get an image like this. These lines, that start appearing in this image are known as contouring and are very much visible in the above image. Increase and decrease in contouring The effect of contouring increases as we reduce the number of gray levels and the effect decreases as we increase the number of gray levels. They are both vice versa V/S That means more quantization, will affect more contouring and vice versa. But is this always the case? The answer is No. That depends on something else that is discussed below. Isopreference curves A study conducted on this effect of gray level and contouring, and the results were shown in the graph in the form of curves, known as ISO preference curves. The phenomena of Isopreference curves show, that the effect of contouring not only depends on the decreasing of gray level resolution but also the image detail. The essence of the study is: If an image has more detail, the effect of contouring would start to appear on this image later, as compared to an image that has less detail, when the gray levels are quantized. According to the original research, the researchers took these three images and they vary the Gray level resolution, in all three images. The images were Level of detail The first image has only a face in it, and hence very little detail. The second image has some other objects in the image too, such as the cameraman, his camera, the camera stand, and background objects, etc. Whereas the third image has more details than all the other images. Experiment The gray level resolution was varied in all the images, and the audience was asked to rate these three images subjectively. After the rating, a graph was drawn according to the results. Result The result was drawn on the graph. Each curve on the graph represents one image. The values on the x-axis According to this graph, we can see that the first image which was of the face, was subject to contouring earlier than all of the other two images. The second image, which was of the cameraman was subject to contouring a bit after the first image when its gray levels are reduced. This is because it has more details than the first image. And the third image was subject to contouring a lot after the first two images i.e.: after 4 bpp. This is because this Concept of Dithering In the last two sections of quantization and contouring, we have seen that reducing the gray level of an image reduces the number of colors required to denote an image. If the gray levels are reduced to two 2, the image that appears does not have much spatial resolution or is not very much appealing. Dithering Dithering is the process by which we create illusions of the color that are not present actually. It is done by the random arrangement of pixels. For example. Consider this image. This is an image with only black and white pixels in it. Its pixels are arranged in an order to form another image that is shown below. Note that the arrangement of pixels has been changed, but not the quantity of pixels. Dithering with quantization When we perform quantization, to the last level, we see that the image that comes in the last level (level 2) looks like this. Now as we can see from the image here, the picture is not very clear, especially if you look at the left arm and back of the image of the Einstein. Also, this picture does not have much information or detail about Einstein. Now if we were to change this image into some image that gives more detail than this, we have to perform dithering. Performing dithering First of all, we will work on thresholding. Dithering is usually working to improve thresholding. During thresholding, the sharp edges appear where gradients are smooth in an image. We got this image after thresholding In thresholding, we simply choose a constant Since there is not much change in the image, as the values are already 0 and 1 or black and white in this image. Now we perform some random dithering to it. It’s some random arrangement of pixels. We got an image that gives a slighter of more details, but its contrast is very low. So, we do some more dithering that will increase the contrast. The image that Now weismix the concepts of random we got this: dithering, along with threshold and we got an image like this. Now you see, we got all these images by just re-arranging the pixels of an image. This re-arranging could be random or could be according to some measure. Histograms Introduction Before discussing the use of Histograms in image processing, we will first look at what histogram is, how it is used, and then an example of histograms to have more understanding of histogram. Histograms A histogram is a graph. A graph that shows the frequency of anything. Usually, histograms have bars that represent the frequency of occurrence of data in the whole data set. A Histogram has two axes the x-axis and the y-axis. The x-axis contains events whose frequency you have to count. The y-axis contains frequency. The different heights of the bar show different frequency of occurrence of data. Usually, a histogram looks like this. Example Consider a class of programming students and you are teaching Python to them. At the end of the semester, you got this result that is shown in the table. But it is very messy and does not show your overall result in class. So, you have to make a histogram of your result, showing the overall frequency of occurrence of grades in your class. Here is how you are going to do it. Now we will know how to use a histogram in an image. A histogram of an image, like other histograms, also shows frequency. However, an image histogram shows the frequency of pixels’ intensity values. In an image histogram, the x-axis shows the gray level intensities and the y-axis shows the frequency of these intensities. For example The histogram of the above picture of the Einstein would be something like this The x-axis of the histogram shows the range of pixel values. Since it’s an 8-bpp image, that means it has 256 levels of gray or shades of gray in it. That’s why the range of the xaxis starts from 0 and ends at 255 with a gap of 50. Whereas on the y axis, is the count of these intensities. As you can see from the graph, most of the bars that have high frequency lie in the first half portion which is the darker portion. That means that the image we have got is Applications of Histograms darker. This can be proved from the image too. Histograms have many uses in image processing. The first use as it has also been discussed above is the analysis of the image. We can predict about an image by just looking at its histogram. It’s like looking at an x-ray of a bone of a body. The second use of histogram is for brightness purposes. The histograms have wide applications in image brightness. Not only in brightness but histograms are also used in adjusting the contrast of an image. Another important use of histogram is to equalize an image. Last but not least, the histogram has wide use in thresholding.