Code Monkey home page Code Monkey logo

histogram-of-an-image's Introduction

Histogram and Histogram Equalization of an image

Aim

To obtain a histogram for finding the frequency of pixels in an Image with pixel values ranging from 0 to 255. Also write the code using OpenCV to perform histogram equalization.

Software Required

Anaconda - Python 3.7

Algorithm

Step1:

Import cv2, matplotlib.py libraries and display the saved images using cv2.imshow().

Step2:

Use cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) to find the histogram of the image.

Step3:

Plot the image and its stem plots using the plt.show() and plt.stem() functions.

Step4:

Equalize the grayscale image using the in-built function cv2.equalizeHist().

Step5:

Print the original and equalized image using cv2.imshow() and end the program.

Program

# Developed By: Y SHAVEDHA
# Register Number: 212221230095
import cv2
import matplotlib.pyplot as plt

# Write your code to find the histogram of gray scale image and color image channels.
color_image=cv2.imread("rcolorimg2.jpg")
gray_image=cv2.imread("gimg.jpg")
cv2.imshow("Gray Image",gray_image)
cv2.imshow("Color_image",color_image)
cv2.waitKey(0)
cv2.destroyAllWindows()


# Display the histogram of gray scale image and any one channel histogram from color image
hist_g = cv2.calcHist([gray_image],[0],None,[256],[0,256])
plt.figure()
plt.imshow(gray_image)
plt.show()
plt.title("Histogram")
plt.xlabel("Grayscale Value")
plt.ylabel("Pixel Count")
plt.stem(hist_g)
plt.show()

hist_c = cv2.calcHist([color_image],[2],None,[256],[0,256])
plt.figure()
plt.imshow(color_image)
plt.show()
plt.title("Histogram of Color Image - Red Channel")
plt.xlabel("Intensity Value")
plt.ylabel("Pixel Count")
plt.stem(hist_c)
plt.show()


# Write the code to perform histogram equalization of the image. 
gray_image = cv2.imread("gimg.jpg",0)
cv2.imshow("Gray Image",gray_image)
equ = cv2.equalizeHist(gray_image)
cv2.imshow("Equalized Image",equ)
cv2.waitKey(0)
cv2.destroyAllWindows

Output

Input Grayscale Image and Color Image

op1

Histogram of Grayscale Image and any channel of Color Image

GRAY SCALE IMAGE

op2

RED CHANNEL OF COLOR IMAGE

op3

Histogram Equalization of Grayscale Image

op4

Result:

Thus the histogram for finding the frequency of pixels in an image with pixel values ranging from 0 to 255 is obtained. Also,histogram equalization is done for the gray scale image using OpenCV.

histogram-of-an-image's People

Contributors

etjabajasphin avatar shavedha avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.