How do you find the histogram of an image in Python?

How to Show Histogram of Image using OpenCV Python | OpenCV Tutorialby Indian AI Production / On April 18, 2021 / In OpenCV ProjectInPython OpenCV Tutorial, Explained How to show t

How do you find the histogram of an image in Python?

How to Show Histogram of Image using OpenCV Python | OpenCV Tutorialby Indian AI Production / On April 18, 2021 / In OpenCV Project

InPython OpenCV Tutorial, Explained How to show the Histogram of Image to analyse the image color format using OpenCV Python

Get the answers of below questions:

  1. How do you find the histogram of an image?
  2. How do you find the histogram of an image in Python?
  3. Green channel extraction in image processing python
  4. What is histogram in OpenCV?
  5. How do you plot a histogram of a picture?
  6. How do you calculate a histogram?
  7. What are the properties of histogram?

What is histogram ?

You can consider histogram as a graph or plot, which gives you an overall idea about the intensity distribution of an image. It is a plot with pixel values (ranging from 0 to 255, not always) in X-axis and corresponding number of pixels in the image on Y-axis.

Syntax: cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist1. Histogram Calculation in OpenCV So now we use cv.calcHist() function to find the histogram. Let's familiarize with the function and its parameters : cv.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) images : it is the source image of type uint8 or float32. it should be given in square brackets, ie, "[img]". channels : it is also given in square brackets. It is the index of channel for which we calculate histogram. For example, if input is grayscale image, its value is [0]. For color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel respectively. mask : mask image. To find histogram of full image, it is given as "None". But if you want to find histogram of particular region of image, you have to create a mask image for that and give it as mask. (I will show an example later.) histSize : this represents our BIN count. Need to be given in square brackets. For full scale, we pass [256]. ranges : this is our RANGE. Normally, it is [0,256]. So let's start with a sample image. Simply load an image in grayscale mode and find its full histogram. REF: https://docs.opencv.org/master/d1/db7/tutorial_py_histogram_begins.html

Code(Jupyter Notebook):## Get Prime Video free at ## OpenCV Tutorial Playlist: https://www.youtube.com/watch?v=-rm0P7A4Jbc&list=PLfP3JxW-T70G5FB9vcmT6T3xnmvFvqV7w # # How to Show Histogram of Image # In[1]: # Show Image import cv2 import matplotlib.pyplot as plt img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\banner-1235604.jpg" #img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\green.jpg" img = cv2.imread(img_path) img = cv2.resize(img, (1280, 720)) cv2.imshow("Image", img) cv2.waitKey(0) cv2.destroyAllWindows() # In[2]: img # In[3]: img.ravel() # In[4]: img.shape # In[5]: img.ravel().shape # In[6]: img.ravel().size # ## Histogram of Image # In[7]: plt.hist(img.ravel(), bins=256, range = [0,255]) plt.show() # ## Histogram of All channels # In[8]: colors = ('b', 'g', 'r') img_ravel = [img[:, :, 0].ravel(), img[:, :, 1].ravel(), img[:,:, 2].ravel()] plt.hist(img_ravel, color=colors, label=colors) plt.legend() plt.show() # In[9]: colors = ('b', 'g', 'r') img_ravel = [img[:, :, 0].ravel(), img[:, :, 1].ravel(), img[:,:, 2].ravel()] plt.hist(img_ravel, color=colors, label=colors, bins=256, range=[0,256]) plt.legend() plt.show() # ## Plot of all Channel using cv2.calcHist() Function # In[10]: colors = ('b', 'g', 'r') plt.figure(figsize=(16, 9)) for i, color in enumerate(colors): histogram = cv2.calcHist([img], [i], None, [256], [0, 256]) plt.plot(histogram, color=color) plt.show() # In[11]: histogram

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Telegram
  • WhatsApp
  • More
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Skype
  • Email

Video liên quan