Meta tags:
description=
Remember when you saw a low-quality image and felt a bit disappointed? It wasn t clear enough, and the details were a bit fuzzy. What if you could enhance that image to a better version? Wouldn t...;
Headings (most frequently used words):
histogram, equalization, in, python, what, does, an, image, represent, is, putting, it, all, together, conclusion,
Text of the page (most frequently used words):
the (169), #histogram (52), image (49), and (44), that (31), for (29), will (28), equalization (27), all (25), python (22), our (19), you (19), have (19), this (19), cv2 (19), development (19), intensity (18), pixel (17), values (16), can (16), method (14), learning (13), what (12), intensities (12), channel (12), envato (10), get (10), one (10), start (10), result (10), using (10), img (10), code (10), channels (10), different (10), are (9), design (9), templates (9), from (9), how (9), opencv (9), frequency (9), each (9), 113 (9), red (9), value (9), view (9), now (8), img_to_yuv (8), web (7), use (7), contrast (7), range (7), with (7), let (7), jpg (7), import (7), three (7), pixels (7), row (7), width (7), img_shape (7), axis (7), hosting (7), ruby (7), php (7), original (6), output (6), notice (6), above (6), rgb (6), color (6), height (6), 106 (6), react (6), tuts (5), video (5), creative (5), over (5), see (5), better (5), tutorial (5), enhance (5), looks (5), cvtcolor (5), monkey (5), follows (5), show (5), bgr (5), here (5), which (5), column (5), cloud (5), coding (5), fundamentals (5), javascript (5), mobile (5), wordpress (5), about (4), music (4), like (4), need (4), assets (4), finally (4), than (4), implement (4), together (4), 102 (4), unique (4), script (4), following (4), images (4), other (4), equalizehist (4), imread (4), look (4), convert (4), apply (4), grayscale (4), actually (4), 131 (4), 124 (4), print (4), some (4), way (4), green (4), blue (4), same (4), histograms (4), particular (4), represent (4), ionic (4), native (4), android (4), ios (4), author (3), help (3), discover (3), policy (3), terms (3), access (3), unlimited (3), scripts (3), was (3), flatter (3), resulting (3), put (3), hist_equalization_result (3), numpy (3), applying (3), since (3), first (3), machine (3), through (3), but (3), read (3), more (3), when (3), most_frequent (3), shape (3), bit (3), make (3), order (3), 132 (3), 114 (3), 138 (3), 110 (3), small (3), sample (3), thus (3), has (3), where (3), number (3), regular (3), details (3), end (3), version (3), tools (3), products (2), market (2), center (2), become (2), blog (2), join (2), not (2), share (2), license (2), stock (2), everything (2), your (2), next (2), project (2), million (2), asset (2), abder (2), rahman (2), ali (2), don (2), much (2), clearer (2), showing (2), distribution (2), across (2), saw (2), whole (2), does (2), before (2), difference (2), right (2), almost (2), systems (2), color_yuv2bgr (2), color_bgr2yuv (2), pout (2), section (2), after (2), instead (2), loads (2), thing (2), space (2), yuv (2), good (2), function (2), case (2), process (2), step (2), enough (2), implementation (2), however (2), these (2), into (2), there (2), only (2), 129 (2), most (2), common (2), intensity_values (2), elem (2), count (2), give (2), words (2), take (2), shows (2), always (2), drawn (2), top (2), draw (2), many (2), times (2), were (2), results (2), then (2), means (2), columns (2), rows (2), parameter (2), out (2), 850 (2), 1126 (2), random (2), great (2), such (2), its (2), bars (2), black (2), white (2), because (2), spread (2), part (2), while (2), 255 (2), used (2), english (2), scaling (2), servers (2), aws (2), game (2), rails (2), django (2), terminal (2), cli (2), expressions (2), ajax (2), xml (2), performance (2), control (2), git (2), authentication (2), rest (2), api (2), patterns (2), workflow (2), testing (2), security (2), databases (2), sql (2), functional (2), programming (2), oop (2), yii (2), codeigniter (2), laravel (2), apis (2), angular (2), jquery (2), node (2), vue (2), kotlin (2), firebase (2), corona (2), theme (2), plugin (2), photo (2), business (2), youtube (2), type (2), 2025, trademarks, brands, property, their, respective, owners, sitemap, mixkit, placeit, sign, authors, company, team, purpose, who, affiliate, explore, marketing, resources, sell, personal, information, cookie, settings, cookies, fair, acceptable, privacy, conditions, user, popular, searches, fonts, photos, royalty, free, pricing, plans, ideas, host, meetups, lead, discussions, collaborate, community, professional, without, photoshop, create, beautiful, logos, designs, mockups, seconds, digital, downloads, month, toolkit, hesitate, add, ons, available, sale, study, very, interesting, called, easy, conclusion, distributed, they, less, clumped, 10814, 222, 10980, 159, 11383, 208, 11863, 160, 13577, 256, running, calculator, earlier, gives, compare, exactly, wrap, did, wonder, imaging, perform, two, beside, left, imwrite, learned, putting, congratulations, applied, mentioned, format, done, converts, another, nice, solution, came, book, equalize, real, world, news, provides, namely, straightforward, equalizes, cannot, separately, simply, experiments, libraries, think, discussion, want, too, mathematical, especially, check, notes, formulas, involved, dive, found, 000, 118, 37276, 128, 37606, 116, 41339, 42114, 52816, execute, keys, true, reverse, lambda, key, items, sorted, dict, len, str, calculate, five, concentrated, towards, middle, figure, distribute, further, flattened, transform, adjusts, would, previous, shown, below, seem, lines, appearing, line, article, instance, had, saying, showed, repeated, appeared, among, reason, introduction, sake, improving, main, work, level, modification, yes, know, expecting, please, aware, contains, respectively, doing, reading, investigating, size, return, second, loop, pair, randint, referred, wrote, just, library, pixabay, converted, reduced, might, produce, photography, scientific, applications, satellite, thermal, stretching, reach, either, spectrum, stretched, manner, well, parts, higher, confined, set, reduce, clarity, difficult, balanced, say, having, single, leftmost, rightmost, plotting, ranges, represents, similarly, consider, made, own, colors, usually, represented, separate, full, numerical, data, graph, divided, bins, intervals, plot, bin, encountered, being, meant, happens, ready, methods, enhances, camera, pictures, why, remember, low, quality, felt, disappointed, wasn, clear, fuzzy, could, wouldn, fortunately, copied, clipboard, español, português, min, jul, 2023, scroll, home, every, any, skip, content,
Text of the page (random words):
histogram equalization in python envato tuts skip to content get every type of asset for any type of project and access to ai tools from 16 50 m design business photo video web design code code view all code start learning wordpress wordpress view all wordpress plugin development theme development mobile development mobile development view all mobile development ios development ios templates android development android templates react native development react native templates ionic development ionic templates corona firebase kotlin javascript javascript view all javascript react vue js node jquery angular web apis php php view all php laravel php scripts codeigniter yii coding fundamentals coding fundamentals view all coding fundamentals oop functional programming databases sql security testing workflow design patterns rest api machine learning authentication version control git performance xml ajax regular expressions tools terminal and cli python python view all python django ruby ruby view all ruby ruby on rails game development cloud hosting cloud hosting view all cloud hosting aws web servers hosting scaling music tuts youtube envato get unlimited creative assets go to envato tuts youtube design business photo video web design code wordpress start learning plugin development theme development all wordpress mobile development start learning ios development ios templates android development android templates react native development react native templates ionic development ionic templates corona firebase kotlin all mobile development javascript start learning react vue js node jquery angular web apis all javascript php start learning laravel php scripts codeigniter yii all php coding fundamentals start learning oop functional programming databases sql security testing workflow design patterns rest api machine learning authentication version control git performance xml ajax regular expressions tools terminal and cli all coding fundamentals python start learning django all python ruby start learning ruby on rails all ruby game development cloud hosting start learning aws web servers hosting scaling all cloud hosting music get unlimited creative assets home code python histogram equalization in python this tutorial shows how we can enhance the contrast of an image using histogram equalization and implement the method using python and opencv scroll to top abder rahman ali jul 28 2023 9 min read english english português español copied to clipboard python remember when you saw a low quality image and felt a bit disappointed it wasn t clear enough and the details were a bit fuzzy what if you could enhance that image to a better version wouldn t that be great fortunately there s a way to do that using python one of the methods you can use to enhance an image is histogram equalization which in particular enhances the contrast of the image almost all camera systems use histogram equalization to make our pictures look better and at the end of the tutorial you will discover why this is so in this tutorial we will being by learning what is meant by histogram and histogram equalization and what happens to the image when applying the method and then we ll see how we can implement the method in python ready what does an image histogram represent histograms are used to represent the distribution of numerical data on a graph the x axis is divided into bins or intervals while the y axis is used to plot the frequency with which the values in a particular bin are encountered now consider an image which is made up of pixels each pixel will have its own color and intensity colors are usually represented with separate values for red green and blue channels each of these channel values can range from 0 no color to 255 full color plotting the number of different pixels with the same value of the red channel that ranges from 0 to 255 will give you a histogram that represents that red channel of the image similarly you can have different histograms for different channels let s say you have a grayscale image in this case we will have the same values for the red green and blue channels for a particular pixel this means that instead of having three different histograms because of three different channels we can have a single histogram the leftmost part on the x axis will now represent black while the rightmost part will represent white for some images all the histogram bars will be confined to a small set of intensities this can reduce the clarity of that image and make it difficult to see details a good image will have a balanced histogram where the color intensity is more spread out what is histogram equalization histogram equalization is the process of stretching the histogram of an image so that its bars reach either end of the spectrum in other words the histogram is stretched in such a manner that it will have some black as well as some white parts the end result will have a higher contrast because the intensity value of the pixels is now spread over the whole range histogram equalization might not always produce great results with regular photography but it has scientific applications where you need better details such as satellite or thermal images we will be using this monkey image from pixabay that i have converted to grayscale with reduced contrast the image looks as follows let s take a look at how we can access the pixel values of the image referred to as intensities i wrote this small python script that we can use to do just that notice that i m using the opencv library 1 import cv2 random 2 3 img cv2 imread monkey jpg 4 5 img_shape img shape 6 7 height img_shape 0 8 width img_shape 1 9 10 for row in range width 11 for column in range height 12 if random randint 0 width row and row 10 13 print img column row 14 what i m doing here is reading our image and then investigating the shape size of the image img_shape will return 1126 850 3 this means that our image is of height number of columns 1126 and of width number of rows 850 and has 3 channels rgb notice that the first parameter in the result is the height and the second parameter is the width finally we loop through the rows and columns and print out the different pixel values intensities at each row column pair one sample of the output is 113 113 113 yes i know you were expecting one value as a result for the pixel intensity we actually have the value of the pixel intensity here but what the output is showing us are the results of the red green and blue rgb channels please be aware however that in opencv the order is bgr as this is how opencv loads the image thus the above sample result contains the value 113 for each channel in the order of b g and r respectively the reason for the introduction is that histogram equalization is actually about the modification of pixel intensities for the sake of improving the image s contrast thus our main work here will be at the pixel intensity level since our pixels have three values one for each of the bgr channels one way to draw the histogram is to have three histograms one for each channel where the x axis will have the different pixel values intensities and the y axis will show how many times frequency that particular pixel value appeared among the different pixel values for instance the red channel histogram can have a pixel value of 113 on the x axis and the y axis can show how many pixels had this value for the red channel e g 86 so the way we read that is by saying that the pixel value for the red channel of 113 showed up in 86 pixels or has repeated 86 times in our image using the code from this image histogram article to draw the histogram for our image we get the following the histogram is actually for the red green and blue channels let s take a small sample of the output you would get from the previous code as shown below this shows that the channel values seem to always be the same and the different three lines drawn will thus have the same values and will be drawn on top of each other appearing as only one line 1 113 113 113 2 110 110 110 3 106 106 106 4 102 102 102 5 138 138 138 6 106 106 106 7 114 114 114 8 131 131 131 9 124 124 124 10 132 132 132 what the histogram equalization method will do for the above histogram is transform the intensity values in a way that will make the histogram look flatter in the resulting image in other words histogram equalization is a method that adjusts image intensities in order to enhance the contrast of the image the above histogram looks a bit concentrated towards the middle of the figure and histogram equalization will distribute the pixel intensity values further to get a more flattened histogram here is some python code that will calculate the frequency of all pixel intensities and give us the values for the five most common intensities 1 import cv2 2 3 img cv2 imread monkey jpg 4 img_shape img shape 5 6 height img_shape 0 7 width img_shape 1 8 9 frequency 10 11 for row in range width 12 for column in range height 13 intensity img column row 0 14 count frequency get intensity 0 15 frequency intensity count 1 16 17 print unique intensities str len frequency 18 19 most_frequent dict sorted frequency items key lambda elem elem 1 reverse true 20 21 intensity_values most_frequent keys 22 23 i 0 24 for intensity in intensity_values 25 i 1 26 if i 5 27 print intensity most_frequent intensity when i execute the above code i get the following output 1 unique intensities 129 2 124 52816 3 131 42114 4 116 41339 5 128 37606 6 118 37276 as you can see there are only 129 unique intensities and the most common intensity is found in over 52 000 pixels i think that s enough discussion of histogram equalization as we don t want to get too mathematical in this tutorial especially since it is more about the implementation of the method in python however you can check these histogram equalization notes that show the different formulas involved in the method now let s dive into the implementation histogram equalization in python in this section i will show you how to implement the histogram equalization method in python we will use the above image pout jpg in our experiments let s go through the process step by step the first thing we need to do is import the opencv and numpy libraries as follows 1 import cv2 2 import numpy after that we simply need to read our image 1 img cv2 imread monkey jpg the good news is that opencv provides us with a function through which we can apply histogram equalization to an image namely equalizehist it is straightforward to apply this function to a grayscale image as the method actually equalizes the histogram of a grayscale image but in our case we have three channels rgb for each pixel and we cannot apply histogram equalization on the three channels separately a nice solution i came across in the book python real world machine learning is to convert our image to the yuv color space equalize the y channel and finally convert the result to rgb so the first thing we do is convert our image to yuv this can be done using the cvtcolor method which converts the image from one space color to another as follows 1 img_to_yuv cv2 cvtcolor img cv2 color_bgr2yuv notice that we use bgr instead of rgb here since opencv as mentioned before loads the images in bgr format we now apply the histogram equalization method on the y channel using the equalizehist method 1 img_to_yuv 0 cv2 equalizehist img_to_yuv 0 finally we convert the y channel to rgb bgr in opencv as follows 1 hist_equalization_result cv2 cvtcolor img_to_yuv cv2 color_yuv2bgr congratulations you have now applied histogram equalization to the image in the next section i will put all the code together and show you what our image will look like after applying histogram equalization putting it all together let s put everything we have learned together the python script for applying histogram equalization on pout jpg looks as follows 1 import cv2 2 import numpy 3 img cv2 imread monkey jpg 4 img_to_yuv cv2 cvtcolor img cv2 color_bgr2yuv 5 img_to_yuv 0 cv2 equalizehist img_to_yuv 0 6 hist_equalization_result cv2 cvtcolor img_to_yuv cv2 color_yuv2bgr 7 cv2 imwrite result jpg hist_equalization_result the output of the above script is the following image to notice the difference better i will put the two images beside each other left original image right result of histogram equalization did you notice the difference the right image looks much clearer than the original image no wonder almost all imaging systems perform histogram equalization before we wrap up let s see what the histogram of our result looks like if you compare the histogram of the resulting image with the histogram of the original image you will notice that the histogram of the resulting image is flatter than the histogram of the original image and this is exactly what the histogram equalization method does running our intensity frequency calculator script from earlier gives me the following output 1 unique intensities 256 2 160 13577 3 208 11863 4 159 11383 5 222 10980 6 102 10814 the intensity values are now distributed over the whole range and they are less clumped together conclusion in this tutorial we saw how we can enhance the contrast of an image using a method called histogram equalization and how it is easy to implement using python and opencv the result was very interesting as it was much clearer than the original image and the histogram of the result was flatter than the histogram of the original image showing a better distribution of pixel intensity values across the image finally don t hesitate to see what python scripts and add ons we have available for sale and for study on envato market python by abder rahman ali like your all in one creative asset toolkit start now unlimited downloads from 16 50 month get access to over one million creative assets on envato over 9 million digital assets everything you need for your next creative project create beautiful logos designs mockups in seconds design like a professional without photoshop join the community share ideas host meetups lead discussions collaborate discover about envato our pricing plans stock video video templates royalty free music stock photos fonts popular searches license user terms license terms terms conditions privacy policy acceptable use policy fair use policy cookies cookie settings do not sell or share my personal information resources discover tuts video music design marketing web design explore blog help help center become an affiliate about us who we are our products our purpose join our team company blog authors become an author author sign in author help center envato market envato tuts placeit by envato mixkit all products sitemap 2025 envato trademarks and brands are the property of their respective owners
|