Created
April 20, 2014 09:47
-
-
Save satojkovic/11109963 to your computer and use it in GitHub Desktop.
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
#-*- coding: utf-8 -*- | |
import cv2 | |
from collections import defaultdict | |
class ImageFlip(object): | |
def __init__(self): | |
self._observers = [] | |
def attach(self, observer): | |
if not observer in self._observers: | |
self._observers.append(observer) | |
def detach(self, observer): | |
self._observers.remove(observer) | |
def notify(self, modifier=None): | |
for observer in self._observers: | |
if modifier != observer: | |
observer.update(self) | |
class CvImageFlip(ImageFlip): | |
def __init__(self, flip_code): | |
ImageFlip.__init__(self) | |
self._flip_code = flip_code | |
@property | |
def img(self): | |
return self._img | |
@img.setter | |
def img(self, image_file): | |
self._img = cv2.imread(image_file) | |
self.notify() | |
def execute(self): | |
self._img = cv2.flip(self._img, self._flip_code) | |
self.notify() | |
class CvViewer: | |
def __init__(self, window_name): | |
self._window_name = window_name | |
def update(self, subject): | |
cv2.imshow(self._window_name, subject._img) | |
def close(self, subject): | |
cv2.destroyWindow(self._window_name) | |
### Key Definition | |
UP_KEY = 63232 | |
DOWN_KEY = 63233 | |
LEFT_KEY = 63234 | |
RIGHT_KEY = 63235 | |
ESC_KEY = 27 | |
### Flip Definition | |
FLIP_Y = 1 | |
FLIP_X = 0 | |
def main(): | |
img1 = CvImageFlip(FLIP_Y) | |
view1 = CvViewer('TEST') | |
img1.attach(view1) | |
img1.img = 'test.jpg' | |
img2 = CvImageFlip(FLIP_X) | |
view2 = CvViewer('TEST2') | |
img2.attach(view2) | |
img2.img = 'test2.jpg' | |
while True: | |
key = cv2.waitKey(33) | |
if key == UP_KEY or key == DOWN_KEY: | |
img1.execute() | |
elif key == LEFT_KEY or key == RIGHT_KEY: | |
img2.execute() | |
elif key == ESC_KEY: | |
break | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment