 Good morning and welcome back, yesterday by the way the attendees of the international conference were very happy to see that the technology is actually being used on a large scale in the country. However, individually I was slightly disappointed by the total number of clickers whose responses we had collected. I am conducting this quiz for two purposes, one of course is to get the current thinking of the participants on the matter of what they propose to submit as mandatory submission. The second purpose is to ensure that we are able to get responses from all the clickers which have been distributed across. So, let me read this question again to you. The question says, currently what activity do you propose to carry out within two weeks of the workshop conclusion for the mandatory submission. I remind you all of you have to make a mandatory submission. The optional activity will start later and continue further. So, it does not matter even if you are not decided yet you may have to still concern. So, it is possible that members of the same team may give different answers based on their understanding at this stage. As I said, I primarily want to capture your current thinking on the issue and also to test the clickers. So, there are four options that I have given. The first option says design questions and answers for examination. So, these will be long programming questions. So, it does not matter whether it is two questions, three questions, only one question with a proper model answer. The second is design quiz questions and answers because you also have to conduct quizzes. So, you may also submit quiz questions and answers. The third one could be add lecture slides with examples. So, you may prepare additional lectures to supplement whatever we have discussed here giving the specific topic name. There could be other choices. For example, if you are planning on the large projects and I saw I remember particularly the Nagpur center was very active immediately after my announcement they had actually discussed and I believe they are already way ahead in planning what kind of programming projects they will do. So, it is possible that you want to pursue the optional activity of writing or developing complete programs and you may if you so wish within the two weeks of mandatory submission you can submit the preliminary project proposals giving the problem definition and may be some aspect of design of the project you can submit within the two weeks. So, that could be one activity or there could be something else. All of those are clubbed under option D. So, these are the four options. Now, I will request all of you to start your clickers by pressing the S T button. After you press A B C or D you just leave the clicker on the system will automatically collect the answers and give the feedback to us. I promise that we will continue the interaction at the beginning of this session. I have a question maybe something apart from the usual programming question. My question is see we have different tools like our projectors, we have audio visual tools and internet and all those things. How would we use these some of these tools to make the teaching experience something interesting to the students that is my question over to you sir. Thank you very much. It is an important question, however it is extremely difficult to answer that question in short. So, I will note this question I will also request you to send a mail to workshop support again asking this question let us I forget. We are compiling a list of many questions which are similar to this important but not connected directly with the topic. Rest assured I will answer the question before the workshop is over. Thank you very much. NIT Suratkal has a question let me try to connect to NIT Suratkal now. Yes, I can see NIT Suratkal over to you. So, my question was when I was taking a class on arrays one student asked me we start the index of an array with 0. Is it possible to start the index of an array with a negative number? Yes, this is advisable to do so. Thank you. I think this question has come in multiple guises. I already explained how the authors of the book on numerical recipes attempted to change that from 0 to n minus 1 to 1 to n and they landed up into a lot of problems. The correct answer to the students is you can choose whatever index values that you want. So, you can artificially use a subset of the range which the C provides. However, the final limit remains once you declare an array of say 10 elements then the index value cannot be less than 0 and cannot be more than 9. Now that is a limit which is set by the C programming language standard that is sacrosan. There is absolutely nothing that you can do about it. You may of course decide artificially that you declare a 10 element array and you will use the indexes only 2, 3, 4, 5, 6, 7. That means you will use only 6 elements artificially restricting the index value from 2 to 7 that is your choice. However, it would be a bad choice because it would be a choice made by you for writing your own program. Neither other people will be able to clearly understand it nor they will be able to use any function that you write with such artificial choice along with any of their other programs. In a nutshell then the correct answer is while it might look funny to you please get used to it when you write your programs and in C programs please remember that your index range is absolutely, absolutely limited for a given size of the array. The minimum value cannot be lower than 0 and the maximum value of the index cannot be higher than size minus 1. That's the answer. Shouldn't there be a separate lecture stating that these are the standard libraries and should we, so my main question is should we discourage the use of turbo as a compiler because it creates a lot of confusion with the students. They don't know how to use console, how to use, what is a compiler, how to link it, the concept of object files. And one more question. Sir, what should we keep in mind while dividing the syllabus? Should it be, for example, we have in our course graphics, one of the topics that we are teaching. Sir, does it not create confusion in the minds of the students which I have even noticed with teachers also? Sir, basically what should be our strategy to divide syllabus for C or C plus so that our students may get maximum benefit and their ideas are clear over and out. Thank you, Shazia. First of all, I appreciate the clarity and the importance of the questions that you ask. Let me answer them in the sequence. Turbo C is a popular environment primarily because it is so easy to use. It does simple things very well. And yes, you are right. It does not communicate very many important concepts associated with the conventional process of compiling, linking with libraries, various compiler options, et cetera, et cetera. To my mind, it is important for us to convey to students the approach which is followed in GCC. My submission would be that even if you have Windows machines, please do download GCC and try to teach them those concepts. Let me hasten to add that I find nothing wrong with Turbo C because it is simple and people therefore can forget the nuances of the compilation process and concentrate on understanding program. So please understand that as far as C programs are concerned, particularly small C programs, they are written identically whether you compile them with GCC or whether you compile them with Turbo C. So while Turbo C could be a nice and cute environment for formal education, I would submit that a GCC kind of process would be better. Your first question and also the second question relates to graphics.h. Now graphics is a very peculiar thing. The authors of Turbo C correctly spotted that people would be interested in doing something concrete with graphical things and therefore graphics was part and parcel of turbo environment right from beginning. As I mentioned, GCC whether on Windows or on Linux has the underlying graphic libraries which are not part of GCC standard library. So that is the problem. However, there are graphic packages which are extensively being used in programming courses. In my own institute, for example, my colleague Professor Abhiram Nanade used the turtle graphics. The turtle graphics is an extremely simple, actually it is a Lego programming language and people can be exposed to do that. The dilemma that we face as teachers is that we have a limited time in a semester and in that semester our emphasis has to be on conveying the basic programming concepts. Doing that using any one language which is typically C itself is difficult to go over to other details. However, as I promised, I will give you all participants and will also put it on the subject portal later. The list of all open source graphic packages which are available and which can be easily linked using the GCC library. In my old course which I took last year, I had actually given as course projects for all students. I had a very ambitious plan. A group of 20 students was doing one project because I had more than 800 students so I had large groups, 40 groups and this project involved analysis of fingerprints for something equivalent to the National Unique ID project. Now to represent fingerprints, you require some graphic to actually show the fingerprints on the screen, you require some minimal graphics. So I had to use some graphic libraries for this purpose. As I said, I will explain all of that and you will be able to see some of these options. But it is difficult to lure people away from Turbosy, at least those who are used to it. My suggestion is when you start the lecture, you just say that as far as this course is concerned, this course will be conducted using GCC compiler and you should start getting used to the GCC compiler and its options right from day one. I think that should sort out the matter. Thank you very much. This question is in a curious way related to the question our colleague participant Shazia Askrod Srinagar. The question is that is it possible to get graphical representation when we solve our numerical problems? The answer is yes but for any graphical representation which you want to execute from within your program, you will have to include some graphical library, graphics library and we are back to square one because graphics.h in its form that people are familiar with in Turbosy is not available. Again you will have to wait. I will compile information on the various graphics libraries, comment on which ones are easy to use and include them. However please note that if you want to represent the numerical solutions in the graphical form, then there is a lot more work than just graphics. How will you convert for example the results of your numerical computation into showable or understandable graphics is not clear and straightforward. If you can do that there would be graphics libraries which you can use. Intrinsically however there is no library which will take the results of your numerical computation and sort of show them in terms of graph. The other utility package which many people use is called SciLab. Some of you would have heard of MATLAB. SciLab is an open source initiative started by the French government and the French group and it is now universally adapted as an adequate replacement for MATLAB which is actually a costed product. SciLab will permit you to do some computations and also have graphical representation. However you must note that that is not meaningful when you are teaching C programming. I come back to my earlier observation. I will work out which are the possible graphic libraries available and easily linkable with GCC on Ubuntu and Linux and I will get back to you. Thank you Ashwini. Her question is that why there is no bound checking in arrays in C program. Well good question. So the question is just like why do they say hash include scanF and printF? The standards were defined a very long time ago and once these standards are defined people write different compilers. So whether it is Darbossi or GCC or any other compiler. Once the compilers are written people write programs. Programs are written as per the standard specification. Now once these standards are defined and people have used it, please understand there are several hundred millions of lines of code in C already written for a variety of problems. Now at this stage you cannot change the C standards because the C standards if you modify them it will cause a lot of problems on large number of lines of code that exist today. You must have heard of backward compatibility issue in any engineering product. They apply also to programs. So software if you want to release a new version you want to ensure that it is backward compatible. It is worse with standards and that is why you will find that why standards may be augmented. They are augmented merely to include some new features which have come up later but the old features which are standardized are rarely changed. This is one of the reasons why C programming standards define that there will be no check. Therefore consequently people who write C programs assume that there will be no checks and they will not implement the checks. Let me elaborate this by telling you one more instance from a different field. That field is the field of databases. I do not know how many of you are computer science teachers but assuming that many of you are you will know SQL or the database management system SQL. Now you will remember that today in SQL you can define constraints such as primary key and foreign key and these constraints which once defined ensure that the data integrity pertaining to these constraints is implemented properly in database. However when the first ANSI SQL standard was released in 1986 the referential integrity constraints were not part of the standard. As a result much of the programs written in those days in databases had to use stored procedures where they had to write their own code to implement the standards and the new system which has come which implement these standards only ensures that if they have a facility to develop new applications they can use these standards but as far as SQL is concerned it still has to run with all the old style stored procedures where the standards did not say that referential integrity is implemented by SQL engine. This is just an example from a related field of what happens to the standard. So the correct answer to tell your students is C standards do not get modified they have been defined that there shall be no checks on the array bounds. Therefore you and me as mortals who write programs have to ensure that we check for the array bounds ourselves. Thank you very much. The question is are there any limitations in using iterative operations and in using recursive operations in the sense that there are places where I am permitted to use only recursion the answer is no. C programming language has absolutely no such limitation whether to use iteration or whether to use recursion is entirely your choice. The only care you have to take is that since recursion could explore exponentially into the number of stacks required for implementation you may run out of memory but that is a practical limitation in underlying hardware there is absolutely no limitation from compiler side whether you can do only this using iteration and that using recursion. Please understand that as far as programming is concerned the C programming language has implemented these features namely you can write iterations or you can write a recursive call to a function both facilities are available and there are no bounds. The restriction comes from the programming language or in the way in which we can write the program the restriction comes from the problem domain if the problem is not well formulated either as iteration or as recursion then we will find it difficult to implement so I repeat there are no restrictions as far as the programming language is concerned and you can specify either one of them. Let me now go over to Nirma Amdabad I heard most of your question but I did not hear the verb it related to single dimension and multi-dimensional arrays but what was the question how do we recent the value I did not get that will you please repeat that part of the question again yes I get the question thank you for asking a very important question his question is how do we get how do we return the value for single dimension as well as multi-dimensional array so actually this question is related to what you call the ability of our program to return values in our functions the answer is like this so far in the entire course we have considered the situation where any function returns a single value and that value is returned by using the return statement and therefore very clearly there is no facility to return more than one value so not only single dimension array or multi-dimensional array even if I want to return values of two different variables as of now we have no facility to do that that is the correct answer however I will assure you that there is a facility to return more than one value from a function it is returned in a different manner all together and when we discuss the pointers we shall be discussing this issue in fact briefly to answer in an anticipatory fashion when the arrays are passed as parameters actually the array values are not passed because it will involve so much work of copying let's say 500 elements of an array and sending them there and copying them back again so instead pointers to arrays are passed and therefore any operation on the arrays that you do inside the function you are actually not doing them on copied values but you are doing them in the original locations itself so there is no explicit copying of values and returning of values whenever you go to a function with arrays as parameters when you come back you will find your original arrays are modified if their values are changed in the function so that is what actually happens once of returning the value your question is valid the answer currently is yes a function can return only one value and that value could be integer float or whatever there is no other way of returning the value otherwise I will stop the video interaction now and very quickly look at the questions that I see on the chat GC Trishur wants to know can we return a structure from a function again we have not yet discussed the structure so I request you to wait for the structure there is a question from PHG Coimbatore do we in see does he support function overloading we had asked this question earlier answered this question earlier so I remind you of that answer the notion of function overloading or any overloading of operator is an object oriented concept intrinsically there is no such concept in C next is why is C C plus plus generally used to design and develop operating system why is not a not a question that can be answered it is a matter of choice of those who write operating systems incidentally not all operating systems are written in C or C plus plus the earlier operating systems are written in assembly language and there was a predecessor of C called BPCL which was language developed specifically to write operating system it just so happens that most people choose C to write their operating system there is a question of what is the application of bitwise operators from NIT Kurukshetra I will think of an application which is meaningful and show you that application in one of these stations GC Trishur says what is the maximum dimension possible for a multi-dimensional array I think person who asked this question did not recall what I had said while giving my lecture there is absolutely no limitation on the number of dimensions that the C standard imposes there is no limitation however the practical limitation will be the total amount of memory that you have more importantly suppose a student asks you this question the answer should be that for any application for which you naturally require a multi-dimensional array and for a particular application if you require 5 dimensions or 7 dimensions or 10 dimensions there will be absolutely no problem in C programming language but the question of unlimited dimensions does not occur because the very large number of dimensions are not really required to model a problem properly so it is related to the problem but C programming language has no such limitation GC GC Trishur also wants to know that Pascal was used earlier for discussing programming methodologies later why it turned to C in my opinion Pascal is more readable to discuss these things well I do not know whether you are an old timer like me but if you are a young teacher I admire your appreciation of Pascal programming language indeed Pascal was the language used to teach programming but different languages have been used to teach programming at different points in time in IIT Bombay we used to use FORTRAN to teach programming initially subsequently we started using Pascal and now we use CC plus plus so let me explain the objective the first course in programming in any institution is typically meant for students from all branches now look at the requirements of programming for other branches as I said typically mechanical engineers chemical engineers civil engineers aerospace engineers physics people chemistry people they would mostly use they are mostly required to write programs to solve scientific numerical computational problems now depending upon what is the most prevalent programming language in their field they would like their students to know that language while theoretically it is obvious that no matter which language you use the emphasis is on teaching programming concepts and we expect our students to learn other programming languages on their own in practice what happens is that the first programming language which is used to teach programming concepts is what is pretty strongly understood by them and they prefer to use that programming language later in later years when they have to write programs to solve actual problems that is the reason why in IIT Bombay also the choice of programming language used in the first course is not decided by the computer science department but it is decided by a group members from other disciplines who are the users of this course they have currently decided C C++ and this by the way is probably the reason why across the country you will find that this is the programming language that is chosen I will submit that while there are some idiosyncrasies in C which are very difficult to understand for beginning students overall this is not a bad choice and we can do our job explaining the concepts there is a question from PSG Coimbatore if C is not supporting function overloading means how it is possible to use different number of scanf with different arguments scanf is a very very special function scanf is a very very special function please remember that when the function scanf was defined in the C standard library the notion of overloading of operator the notion of object oriented programming was not understood as we understand it now yes in a limited sense since scanf does mean that there is a function overloading however no sane adherent of C programming will ever call it as a function overloading because that concept is foreign to C programming language C is essentially a procedural programming language you will notice that whatever scanf does it does it through a standard library the standard library functions you can write whatever functions you want to write as a matter of fact people have implemented operator overloading people have defined abstract data types people have defined a variety of objects and classes and implemented them using functions written in C so let us distinguish between the two things let us distinguish between C programming language itself supports and the other part is what all you can do using C programming language C programming language per se does not support operator overloading or function overloading but using C programming language you can always build anything including object libraries including classes whatever whatever the approach that you will have to follow will be slightly different function overloading and operator overloading is a natural concept in object oriented programming languages whereby you can do these things naturally as a part of the features the programming language itself so that is the fine distinction as far as C is concerned therefore we will say that there are no object oriented concepts available NIT Kurukshetra has a question when we declare float instruct and while scanning value it will display floating point normal termination error why this is so any abnormal termination error cannot have anything to do with the declaration of float in the structure when we discuss the structures please raise this question again we will answer that C in and C out return any value well C in and C out are some dumbo things that we are using for our convenience let us treat them as simple input-output statements and nothing else and let us not waste any time in discussing the features of C in and C out we can do that in another course on an object oriented programming where we properly define C in and C out as components of a class NIT Jalandhar wants to know what are macros and why they take lesser time during compilation okay I appreciate all these questions but I think I will stop now because I have hardly half an hour left and I still have some important example to suggest while I appreciate this question on macros but the macros in in C that you write how to be discussed separately I have not yet discussed them I will have a brief description of macros in one of the lectures in the next week and therefore we postpone this discussion to that point with this let me revert back now to the session proper where we are going to consider some examples of C program today I am going to discuss digital images and processing of digital images as one of the applications of multi-dimensional arrays specifically two-dimensional array for those of you are not familiar with digital images here is a brief explanation digital images are a collection of pixel values and these are arranged in an array which is so much wide and so it has so much height a pixel is a picture element a picture element like a dot or a spot or a rectangle on on a picture so if you have a photograph for example and if you draw a grid on that photograph every square in that grid could be called a picture element how many picture elements could be there it depends upon the resolution of either your camera or your display device such as a screen or a printed photograph each pixel value represents the intensity of light falling onto that picture picture element position the pixel value in a black and white color could be represented even by one bit that means every pixel is either black or white pure black and white pictures do not make much sense as pictures although generally if you see a document black ink on white paper that is the kind of black and white image we are talking about most images represent a pixel value by 8 bits which are gray scale images so what we call black and white photographs actually are gray scale images they represent white pure white by a value 255 pure black by a value 0 these are the kind of images that are most easy to represent because a pixel value fits into one byte and therefore let us say if you have a picture which has 300 rows and 100 columns that is 300 by 100 then the total number of pixels and therefore total number of bytes is 300 into 100 however when you come to representing colors then the color in a picture is actually made out of intensity of three different basic colors these basic colors are red, blue and green imagine the intensity of each of these colors is represented by a value between 0 to 255 that means I will require one byte to represent the intensity of red one byte to represent the intensity of blue and one byte to represent the intensity of green consequently every pixel will require three bytes to represent the composite values of these three colors and therefore it will require 24 bits per pixel the possible number of colors that we can have given one byte to represent red, blue and green is all possible combinations of 256 into 256 into 256 this leads to over 16 million colors the capacity of human eye is limited ordinarily to a small range from 200 to 2000 colors as a matter of fact since total color possible are 16 million and a human can distinguish only these many usually in any photograph you do some kind of a mapping from the 16 million possible colors to some decipherable colors which the human being will see or which exist in a particular picture please note that you do not have 16 million pixels in any photograph so obviously the total colors available in a photograph will be much less usually you use what is called a color palette to do the mapping from the 16 million colors to fewer colors which exist typically in photographs but that is not of great consequence important point is to understand that any picture can be digitized and can be represented as a digital image in particular modern photographic equipment or video equipment is all digital and therefore when you capture photographs or photo frames as part of the video you always capture them as so many pixels by so many pixel a color capture will involve representation of every pixel by 24 bits now when we want to process an image digitally then we have to first store the image somewhere ordinarily you get these images as files and these files will contain width by height pixels so we mainly need the values of width and height additionally we need the type of colors present and the corresponding color values for each pixel consider black and white finger prints if they are gray scale finger prints they typically have a small size 500 to 300 pixels for large images compression may be necessary you may have to compress the available information because the available information may be too much, consider this you might have heard of cameras and the resolution they have typically it started with 1 million pixel, 1 megapixel 2 megapixel, 5 megapixel you have now 12 megapixel cameras 12 megapixel cameras what it means is that if you take single photograph in the finest possible resolution it will have 12 million pixels in that single picture since each pixel is a full color pixel it can have it will require 3 bytes for storage and that means you will require 36 million bytes per image this is too much of a storage so you will typically compress this and store this, compression can either be lossy or lossless a lossy compression means you lose some information but overall the picture quality will still be acceptable on the other hand a lossless compression means that when you do a compression the size of the file will reduce however you are guaranteed that you will be able to reproduce the original full blown 12 million pixel image the lossless compressions are fewer on picture thing most of them are lossy compressions they reduce the size of the file drastically and still produce acceptable quality of pictures that is what people use here is the list of variety of formats which have been standardized a raw or PNG format or BMP format is a standard uncompressed kind of thing JPEG is on the other hand a well known compressed format JPEG is most popular by the way in the digital photograph that you will see or the images which are shared through images or whatever they are almost always as JPEG files incidentally you get a lot of information technical information on Wikipedia so just ask for image file format and you will find all these formats are explained in fact if you want the BMP format and want to write a C program from Wikipedia you will actually get to write describing the BMP format so there is lot of useful stuff there what we want to discuss is some application where we will show how we can use our ability to represent values in two dimensional array so how can we use that ability to do some processing on a picture what processing well there are lots of processing that can be done the processing that I am going to talk about is the processing involving histogram first some preliminaries I have already explained that digital images of pixel values they will ordinarily be stored in files which you will get from cameras or wherever but these can be read in a matrix for further processing so just as you have picture size in width and height you define a two dimensional array so many rows and so many columns and corresponding to every pixel you can read a value there the value may be a three byte value composite red, blue and green so you may actually have a third dimension saying red intensity, blue intensity, green intensity if it is a gray scale value it will be a one byte value between 0 to 255 so that is what I call the tonal value for gray scale images the value ranges between 0 to 255 and 0 represents black 255 represents white thus each element of an image matrix would contain a value which can be of the type or size of integer or character remember a care is one byte long thing so you can actually have a very small size array what is the histogram histogram indicates how many pixels in an image have the same value since the values can be 0 to 255 in any image which has large number of pixels some let's say pixel at this point has a value of 57 pixel at some other point has a value of 57 if you count the total number of pixels in an image whose intensity is 57 and let's say that comes to a 24, arbitrarily then we say that the histogram value of intensity 57 in this picture is 24 what is the advantage of computing such histogram which is a statistical summation of the number of times a particular value occurs that is by the way the definition of histogram we shall see it through some examples here is an example of a sample image which is simply 8 pixel by 8 pixel long 8 pixel by 8 pixel by the way in real life is a very small image and it will amount to a very small dot but I have expanded it to show the meaning of histogram and its processing so notice that here every pixel is actually is a square sort of area notice that there are 8 pixels in this horizontal direction 8 pixels in the vertical direction so 8 rows and 8 colors notice that some pixels are blackish some pixels are whitish and the other pixels are gray you can clearly see that there is no plain white pixel anywhere in this there is no complete dark black pixel here so I expect that the pixel value will neither be 0 nor 255 in fact this pixel value will be somewhere around 200 may be this pixel value may be somewhere around 30 or 40 but this is a sample image and I will now show you the matrix which contains the pixels values corresponding to this image incidentally this example is entirely and the other slides entirely from information in Wikipedia so these are the picture values in the sample image 52, 55, 61, 66, 70, 61 etc etc so these are the 8 pixels in first row 8 pixels in the second row etc whatever one notice something in this area itself the lowest value seems to be 52 in this corner and there are a few values like 154 somewhere here the highest value is 154 somewhere in the center and some neighboring pixels also have values like 154 and so on just let us go back to the previous slide remembering that 154 is somewhere here 52 is somewhere here if we go back to the previous slide you will notice that this probably is 154 so this is closest to white this is the lightest element or brightest element and this is the darkest element this is not 0 and this is not 255 but we note this fact the second thing we notice how many times a particular picture value is occurring in the elements so here we see 52 let us scan this is 52 occurring anywhere here well I do not see anywhere so in histogram if we were to compute we will say histogram of 52 is 1 because it is occurring only once 55 we will see 55 occurs here 55 occurs here this is count is 2 now 55 does not seem 55 occurs here the count is 3 now so if we count how many times 55 is occurring we will say the histogram at 55 is 3 what would be the histogram at pixel value 21 for example it will be 0 because 21 does not occur anywhere what is the histogram at value 190 it is 0 it is not occurring anywhere so essentially we take one value from the possible value of a pixel and we see how many pixels are that value that becomes the histogram here is the histogram values calculated here 52 occurs once 55 occurs as we saw there are 3, 2, 1, 4 occurrences like that 154 occurs once 145 146 147 occurs 0 time so these histogram values actually are listed only for those pixel values which are non-zero occurrence if a pixel does not occur at all it is not listed here just to keep the representation small in practice a histogram will have 256 entries there will be an entry for 0 entry for 1, entry for 2 etc etc what it means this particular chart is that for the histogram which will have 256 values 0, 1, 2, 3, 4, 5, 2, 56 the value at 0 is 0 the value at 1 is 0, value at 2 is 0 up to 51 the value of histogram is 0 at 52 the value is 1 at 55 the value is 3 what is the purpose of this histogram histogram represents the spread of colors inside a picture let us go back to this particular picture again it does appear slightly blurred because it does not have good enough contrast a high contrast will mean that a picture has values of pixels stretched from the lowest of the highest then you will see the most contrast for example if this was dark black and this was dark white and all other pixels were adjusted to reflect the relative ratio of their current value but in the larger range of pixels you will agree that you will get a better contrast that is the reason why we calculate the histogram and then there is a technique called histogram equalization which is applied on the histogram such that after histogram equalization you recalculate the values of the pixels and then you will suddenly get an image with a better contrast I will very briefly show you some of those processes so this is a sample image these are the pixel values these are the histogram values the cumulative distribution function is defined I have included it here for the sake of completeness I will not spend time in discussing but the cumulative distribution function is required to be calculated to do histogram equalization later all of this by the way is on Wikipedia and you can refer to more detailed explanation there but using the cumulative distribution function I can recompute the values of the original pixels in the histogram equalization process the histogram equalization formula is given and the equalization formula for the example image will work out to be this that is because you will see m into n minus cdfmin is the thing and cdfmin here so you put those values I will not go into the details of this conceptually let us understand that if I do histogram equalization I will get sharper image if I do recomputation for example the cumulative distribution function of 78 value is now 46 so I calculate h of 78 that means whatever pixel was earlier 78 what it should become in equalized image it should become 182 so you see it is being stretched now each pixel will get stretched with this stretching if I replot that particular image the pixel values after histogram equalization will look like this which was having a value 52 has now become 0 it has been stretched to the lowest value the pixel which was somewhere I think 100 and whatever the largest value has now become 255 so this has been stretched to be made pure 5 this has been stretched to be made pure 0 and please note that all other pixel values are appropriately adjusted within this larger range now in short histogram equalization means I have sort of stretched that histogram to cover all values between 0 to 255 and please note that it is not done arbitrarily the purpose of cumulative distribution function computation etc etc is that all pixels retain their relative values with respect to the other pixels if I plot this particular image again this will look like this notice that contrast has been enhanced very significantly here so this is now pure white this is now pure black and all other pixels are stretched roughly in the same ratio that they originally represented in the original so let's quickly look at the original image this was the original image you will agree that this contrast is lower this contrast is better consider another image this is the image of a scene a scenario you can see that the contrast is low I can identify some trees here some fields some flowing hilly area some trees in the distance and so on this is a more real image it's not a sample this is a much larger image it is not 8 by 8 it has very large number of rows and columns but the technique still remains the same so I can read the pixel values of this calculate the histogram once I calculate the histogram I can apply the histogram equalization formula and then we calculate the pixel values for this the histogram and the cumulative distribution function looks like this so this is the histogram notice the reason why the contrast is not very good is that all pixel values you see the histogram here there are no pixel values between 0 to 100 110 and there are no pixel values beyond 205 or something all pixels are concentrated here and therefore the cumulative distribution function looks like this now when I do histogram equalization or sort of stretching the histogram will look like this because it will span all between 0 to 255 and the cumulative distribution function will look like this that is what will result in a recomputed image which will look like this so you will appreciate this simple technique of histogram equalization which will give you an enhanced contrast compare it with the previous original image this is the original image and from this original image by this simple mechanism of contrast enhancement I have got this image so there is no black magic simple computations there in fact you will notice that if you take photoshop or any any popular software which edits photograph one of the easiest things and the most common feature that you find is you take a photograph and you say enhance contrast why even on some of the digital cameras today after you take a photograph before you finally save it or after saving it you want to in the camera itself you can say enhance contrast and save enhance contrast image what is happening is inside somebody is calculating the histogram doing the histogram computation or reapplying it to pixel values recomputing those pixel values and storing that digital image for us computationally what does it involve you can apply those formulae very easily what I am showing in the next 5 to 10 minutes is a c program which will actually compute histogram because histogram computation you will be I will have to be done first afterwards it straightforward application of those formulae which I have suggested just to illustrate how a two dimensional array can be used for some very useful and important purpose this is the original picture for comparison as I said let us go over to find out discuss the program to calculate histogram I am now considering that my images are smaller they are 500 to 500 500 by 500 at most in real life the images could be much larger particularly if you have high resolution images but as I told you there is no limitation of size on c and usually we have adequate computer memory these days so your arrays in image 500 by 500 what I have declared could be any large could be 5000 by 5000 for example there should be no problem you will get the size height and weight typically from the device where you originally capture the photograph say we have done a black and white capture from a digital camera then not only that resolution but the I will tell you that this picture is so much height and so much weight that means it has so many pictures we need to calculate histogram please note that independent of the size of the image large or small a histogram will have always 256 values because what histogram says is it is merely a count of the number of pixels at a particular intensity value so 0 intensity 1 intensity 2 intensity to 255 intensity notice that for once we find that the index structure of c is very much commensurate with the actual pixel values that you find in the real photograph because black is 0 white is 255 and you will find that the index of the array histogram histogram 0 histogram 1 histogram 2 histogram 3 histogram 255 the index value corresponds to the pixel value itself we shall use this fact to deploy this array computation in what we call in associative array fashion I will discuss that very briefly in a minute or so so once again we assume that the image data is contained in some text file called image dot txt in a suitable format what could be the suitable format we may give w we may give h as two values initially and then for every row we will give as many pixel values as they are there in that row one after another and we will use the command line redirection to read data from this file that will be the easiest so here is what we are doing we are saying c in npx the number of pixels read image pixel values in the matrix for i equal to 0 i less than npx so I am imagining that it is npx by npx image I am imagining a square image if the image is rectangular I will have two different values w and h and I will run these two loops differently for w times and h time I am reading image ij and as I am just showing it output of the sort of confirmatory value this is trivial so we have read all the image in the array image array it said it said two two dimensional array number of rows number of columns now we want to calculate the histogram this is the entire code required to calculate the histogram let us see what it is what I have to do I have to find out how many times a picture value occurs the logic that I follow is not to check one take one pixel value in mind say 0 and then see how many pixels are 0 value so I will have to go through the entire array saying ok 0 I found once sometime later 0 another 2 sometime later 0 another 3 and I will put the count and then whatever that count I will put in the 0th element of histogram then I will start all over again I will take one now and I will go over to all the pixels from first one how many times one occur I will total the count and put it there this will obviously be a very costly algorithm why because for every possible pixel value I am going through n by n pixels that is n square and I am doing this 256 times because there are possible 256 value 0 to 255 so I will be scanning every pixel every pixel 256 times can I do something in which I account I scan all pixels only once so here is the alternate strategy instead of looking at every pixel instead of looking at every pixel value and going through all pixels what I will do is I will set all my accounts to 0 now I will start getting looking at pixels one by one a pixel comes suppose the first pixel value is 52 what I will do in the array of my histogram I will go to the 52nd location and increment that count by one suppose next pixel is value 5 I will now go to the 5th location increment that count by one suppose I get another pixel which has a value 52 I will go back whatever is the current count 52 remember we said we already got one so the count would be one now if I find another value 52 I will add one more to count so that count will become one more it will become two this way if I keep examining pixels I will be looking at each pixel only once so my algorithm execution time it will have to execute some kind of counting operation only as many times as there are pixels so if there are n by n image n square pixels I do not have to look at these pixels 256 times that is the advantage let us see how this strategy is implemented first in order to increment counts I have to reset these counts to 0 initially so that is what is being done here say set histogram counts to 0 from 0 to less than 256 that means 0 to 25 set histogram I equal to 0 all 256 elements of the histogram array will be set to 0 now it is calculating histogram values remember what we said we will do we will look at every pixel one by one so for that we have set up this iteration for I equal to 0 to npx minus one and for every value of I for j equal to 0 to npx minus one what this means that inside this nested iteration I am looking at one particular pixel represented by I throw jth column what am I going to do with that pixel nothing I note what is the value of that pixel I have to increment the corresponding count in the histogram array how do I know the corresponding element in the histogram array simple the pixel value itself becomes the index inside why because if pixel value is 52 remember I have to increment 52nd element if the pixel value is 112 I have to increment 112th element if the pixel value is 2 I have to increment second element if pixel value is 0 I have to increment the 0th element this is the association that means the index expression inside the histogram array is nothing but the value of the pixel itself and that is how I neatly do the following histogram in bracket image I j plus plus the plus plus is for the element so what is the index expression here image I j is the index expression that means whatever a pixel I am looking at its value is the index if I am looking at Ith and I comma jth element its value is 94 then I am looking at 94th element what am I doing with the 94th element I am incrementing that because that holds the count now 94th element of histogram is the count so I am incrementing that count by one please note how elegantly this problem can be solved what we have used is the notion of associative array associative array means the index in this array is associated with a value in some other event in a single nested iteration going from i equal to 0 to npx and j equal to 0 to npx I would have taken every pixel from the image and incremented the corresponding count in the histogram so it is a very simple and elegant code I have finished the computation of histogram I will just print the histogram at non-zero values at non-zero values is so and so and what do I do i equal to 0 i less than 256 that means up to 255 if histogram i is not equal to 0 I will print i n histogram again I am saving some time because in real pictures generally a few pixels values at the lower end and few values at the higher end will be missing you will invariably get somewhere in between of course if you have a picture which has all the possible values this program will print the entire histogram this is just an additional code that I have written to find out the maximum value of the histogram in general it is good to find maximum and minimum value of a histogram and print it so that you know how much of stretching is required although you will implement the stretching blindly by using the algorithm by using those function definitions that you have but this is just an additional useful knowledge suppose someone asked you find out the maximum and minimum value of histogram here is the code to find out the maximum value this is just included for the sake of completeness all I am doing is I set up a max as 0 and I scan all the histogram elements now please note I have to find out the I have to find out what is the histogram which has the maximum count so I am not looking at the largest pixel value I am looking at that largest count in the histogram matrix and the pixel value at which the count is maximum so I just say max is equal to histogram and I will print that the maximum and it occurs at such and such you can print the gray levels at which the maximum value occurs through a separate iteration why it is required because maximum may not occur only for one pixel for example the largest count may be 10 and 5 different pixel values may be occurring 10 times that is why this becomes important anyway this is an additional exercise in calculating and printing maximum or minimum of histogram the main crux of the entire algorithm is the elegance I go back to the previous slide is a previous to previous slide is the elegance by which the histogram count is updated please remember any mundane strategy will require to look at a particular histogram value and find out all the pixels which actually match that value a far better strategy is to see the association between the index of the histogram array and the corresponding value of the pixel and use that concept to scan all pixels only once through an state iteration and whatever is the value of i, jth element use that as an index to go to the histogram array to the appropriate element and implement that count we will conclude with here in my example I have included only the histogram computation those of you who are interested can extend this program to do the histogram equalization using the formula that I had given I had given this kind of problem as a part of an exam and many people solved it correctly of extending this program but in your lab or in your spare time you can do that later on I will also try and find out how do you get a pixel values from a jpeg file because jpeg file has a different format what you require is a simple format in which number of rows number of columns and the picture values are there so some black and white image if I can get a BMP file I will try to include in the file some of you are interested can do the experiments we will close this session now thank you