-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomputingHistograms.py
More file actions
54 lines (40 loc) · 1.31 KB
/
computingHistograms.py
File metadata and controls
54 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
img = cv.imread('Photos/KenKaneki.jpg')
cv.imshow('Ken', img)
# 1. Computing Grayscale Histogram
# gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# cv.imshow('Gray Ken', gray)
blank = np.zeros(img.shape[:2], dtype='uint8')
cv.imshow('Blank', blank)
# circle = cv.circle(blank, (img.shape[1]//2, img.shape[0]//2), 100, 255, -1)
# cv.imshow('Cirlce', circle)
# mask = cv.bitwise_and(gray, gray, mask=circle)
# cv.imshow('Mask', mask)
# # gray_hist = cv.calcHist([gray], [0], None, [256], [0, 256])
# gray_hist = cv.calcHist([gray], [0], mask, [256], [0, 256])
# plt.figure()
# plt.title('Grayscale Histogram')
# plt.xlabel('Bins')
# plt.ylabel('No. Of Pixels')
# plt.plot(gray_hist)
# plt.xlim([0, 256])
# plt.show()
# 2. Computing Color Histogram
mask = cv.circle(blank, (img.shape[1]//2, img.shape[0]//2), 100, 255, -1)
cv.imshow('Circle', mask)
masked = cv.bitwise_and(img, img, mask=mask)
cv.imshow('Masked Ken', masked)
plt.figure()
plt.title('Colorful Histogram')
plt.xlabel('Bins')
plt.ylabel('No. Of Pixels')
colors = ('b', 'g', 'r')
for i, col in enumerate(colors):
# hist = cv.calcHist([img], [i], mask, [256], [0,256])
hist = cv.calcHist([img], [i], mask, [256], [0,256])
plt.plot(hist, color=col)
plt.xlim(0, 256)
plt.show()
cv.waitKey()