Last active
September 10, 2019 07:51
-
-
Save JeonghunLee/8d55ef0b722c53cc0632a3ea0c180b47 to your computer and use it in GitHub Desktop.
jpeg_histogram_view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
import cv2 | |
import numpy as np | |
import random | |
import matplotlib.pyplot as plt | |
from matplotlib.backend_bases import NavigationToolbar2, Event | |
parser = argparse.ArgumentParser( | |
description='Run OpenCV ') | |
parser.add_argument( | |
'-i', | |
'--imageDir', | |
help='Directory to the images', | |
default='./data/alpr/test/temp') | |
home = NavigationToolbar2.home | |
back = NavigationToolbar2.back | |
forward = NavigationToolbar2.forward | |
def new_home(self, *args, **kwargs): | |
s = 'home_event' | |
event = Event(s, self) | |
event.foo = 100 | |
self.canvas.callbacks.process(s, event) | |
home(self, *args, **kwargs) | |
def new_back(self, *args, **kwargs): | |
s = 'back_event' | |
event = Event(s, self) | |
event.foo = 100 | |
self.canvas.callbacks.process(s, event) | |
back(self, *args, **kwargs) | |
def new_forward(self, *args, **kwargs): | |
s = 'forward_event' | |
event = Event(s, self) | |
event.foo = 100 | |
self.canvas.callbacks.process(s, event) | |
forward(self, *args, **kwargs) | |
NavigationToolbar2.home = new_home | |
NavigationToolbar2.back = new_back | |
NavigationToolbar2.forward = new_forward | |
global JPEG_CNT | |
global JPEG_CNT_MAX | |
global jpeglist | |
global jpegdir | |
def handle_home(evt): | |
if JPEG_CNT_MAX > 0 : | |
plt.clf() | |
show_images(jpeglist[0]) | |
plt.draw() | |
print "home max=%d" %(JPEG_CNT_MAX) | |
def handle_back(evt): | |
global JPEG_CNT | |
if JPEG_CNT > 0: | |
JPEG_CNT = JPEG_CNT - 1 | |
if JPEG_CNT >= 0 and JPEG_CNT < JPEG_CNT_MAX : | |
plt.clf() | |
show_images(jpeglist[JPEG_CNT]) | |
plt.draw() | |
#evt.canvas.draw() | |
else : | |
JPEG_CNT = 0 | |
print "back (%d/%d)" %(JPEG_CNT,JPEG_CNT_MAX) | |
def handle_forward(evt): | |
global JPEG_CNT | |
if JPEG_CNT >= 0: | |
JPEG_CNT = JPEG_CNT + 1 | |
if JPEG_CNT >= 0 and JPEG_CNT < JPEG_CNT_MAX : | |
plt.clf() | |
show_images(jpeglist[JPEG_CNT]) | |
plt.draw() | |
#evt.canvas.draw() | |
else : | |
JPEG_CNT = 0 | |
print "forward (%d/%d)" %(JPEG_CNT,JPEG_CNT_MAX) | |
NavigationToolbar2.home = new_home | |
NavigationToolbar2.back = new_back | |
NavigationToolbar2.forward = new_forward | |
def search(dirname): | |
cnt = 0 | |
jpeglist = list() | |
for (path, dir, files) in os.walk(dirname): | |
for filename in files: | |
ext = os.path.splitext(filename)[-1] | |
if ext == '.jpg': | |
fullpath = "%s/%s" %(path,filename) | |
#print("%s" % (fullpath)) | |
if cnt == 0: | |
jpeglist.append(filename) | |
print("%s cnt=%d" % (fullpath,cnt)) | |
else : | |
jpeglist.append(filename) | |
print("%s cnt=%d" % (fullpath,cnt)) | |
cnt = cnt +1 | |
return jpeglist,cnt | |
def show_images(name): | |
jpeg = "%s/%s" %(jpegdir,name) | |
img1 = cv2.imread(jpeg,0) | |
hist1 = cv2.calcHist([img1],[0],None,[256],[0,256]) | |
hist2 = cv2.calcHist([img1],[0],None,[128],[0,128]) | |
hist3 = cv2.calcHist([img1],[0],None,[128],[128,256]) | |
#hist4,bins = np.histogram(img1.ravel(),256,[0,256]) | |
print name | |
#print "hist4-128 %d" %(hist4[128]) | |
#print "hist1-128 %d" %(hist1[128]) | |
#print "hist2-128 %d" %(hist2[127]) | |
#print "hist3-128 %d" %(hist3[127]) | |
#print "hist2-129 %d" %(hist2[129]) | |
#print "hist2-140 %d" %(hist2[140]) | |
plt.subplot(221),plt.imshow(img1,'gray'),plt.title(name) | |
plt.subplot(222),plt.plot(hist1,color='r'),plt.title('Histogram'),plt.xlim([0,256]) | |
plt.subplot(223),plt.plot(hist2,color='g'),plt.title('Histo(0~128)'),plt.xlim([0,128]) | |
plt.subplot(224),plt.plot(hist3,color='b'),plt.title('Histo(128~256)'),plt.xlim([0,128]) | |
#plt.xlim([0,256]) | |
#plt.show() | |
if __name__ == "__main__": | |
fig = plt.figure() | |
fig.suptitle('mouse hover over figure or axes to trigger events') | |
fig.canvas.mpl_connect('home_event', handle_home) | |
fig.canvas.mpl_connect('back_event', handle_back) | |
fig.canvas.mpl_connect('forward_event', handle_forward) | |
args = parser.parse_args() | |
print("args:",args.imageDir) | |
jpegdir = args.imageDir | |
JPEG_CNT = 0 | |
jpeglist,JPEG_CNT_MAX = search(args.imageDir) | |
print("JPEG %s cnt=%d " %(jpeglist[1],JPEG_CNT_MAX)) | |
if len(jpeglist[5]) != 0: | |
show_images(jpeglist[0]) | |
#plt.draw() | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment