From aaa43dc84f22a2653f9519a5dfce844c74f9318e Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Fri, 29 Jan 2016 13:07:08 +0300 Subject: [PATCH] Add morphology python test, fix python3 compabtibility in kmeans test --- modules/python/test/test.py | 1 + modules/python/test/test_kmeans.py | 11 ++---- modules/python/test/test_morphology.py | 51 ++++++++++++++++++++++++++ modules/python/test/tests_common.py | 2 +- 4 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 modules/python/test/test_morphology.py diff --git a/modules/python/test/test.py b/modules/python/test/test.py index d5fc533b7..f9213cffb 100755 --- a/modules/python/test/test.py +++ b/modules/python/test/test.py @@ -28,6 +28,7 @@ from test_houghlines import houghlines_test from test_gaussian_mix import gaussian_mix_test from test_facedetect import facedetect_test from test_kmeans import kmeans_test +from test_morphology import morphology_test # Python 3 moved urlopen to urllib.requests try: diff --git a/modules/python/test/test_kmeans.py b/modules/python/test/test_kmeans.py index 2420cce1a..4f886d9d8 100644 --- a/modules/python/test/test_kmeans.py +++ b/modules/python/test/test_kmeans.py @@ -10,10 +10,13 @@ from __future__ import print_function import numpy as np import cv2 from numpy import random +import sys +PY3 = sys.version_info[0] == 3 +if PY3: + xrange = range from tests_common import NewOpenCVTests - def make_gaussians(cluster_n, img_size): points = [] ref_distrs = [] @@ -53,12 +56,6 @@ class kmeans_test(NewOpenCVTests): cluster_n = 5 img_size = 512 - # generating bright palette - colors = np.zeros((1, cluster_n, 3), np.uint8) - colors[0,:] = 255 - colors[0,:,0] = np.arange(0, 180, 180.0/cluster_n) - colors = cv2.cvtColor(colors, cv2.COLOR_HSV2BGR)[0] - points, _, clusterSizes = make_gaussians(cluster_n, img_size) term_crit = (cv2.TERM_CRITERIA_EPS, 30, 0.1) diff --git a/modules/python/test/test_morphology.py b/modules/python/test/test_morphology.py new file mode 100644 index 000000000..d7abda4ed --- /dev/null +++ b/modules/python/test/test_morphology.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +''' +Morphology operations. +''' + +# Python 2/3 compatibility +from __future__ import print_function +import sys +PY3 = sys.version_info[0] == 3 + +import numpy as np +import cv2 + +from tests_common import NewOpenCVTests + +class morphology_test(NewOpenCVTests): + + def test_morphology(self): + + fn = 'samples/data/baboon.jpg' + img = self.get_sample(fn) + + modes = ['erode/dilate', 'open/close', 'blackhat/tophat', 'gradient'] + str_modes = ['ellipse', 'rect', 'cross'] + + referenceHashes = { modes[0]: '1bd14fc814e41b80ce7816bc04f60b65', modes[1] : '1bd14fc814e41b80ce7816bc04f60b65', + modes[2] : 'cb18a5d28e77522dfec6a6255bc3847e', modes[3] : '84909517e4866aa079f4b2e2906bf47b'} + + def update(cur_mode): + cur_str_mode = str_modes[0] + sz = 10 + iters = 1 + opers = cur_mode.split('/') + if len(opers) > 1: + sz = sz - 10 + op = opers[sz > 0] + sz = abs(sz) + else: + op = opers[0] + sz = sz*2+1 + + str_name = 'MORPH_' + cur_str_mode.upper() + oper_name = 'MORPH_' + op.upper() + + st = cv2.getStructuringElement(getattr(cv2, str_name), (sz, sz)) + return cv2.morphologyEx(img, getattr(cv2, oper_name), st, iterations=iters) + + for mode in modes: + res = update(mode) + self.assertEqual(self.hashimg(res), referenceHashes[mode]) \ No newline at end of file diff --git a/modules/python/test/tests_common.py b/modules/python/test/tests_common.py index 6ab26050b..c1cc12d6e 100644 --- a/modules/python/test/tests_common.py +++ b/modules/python/test/tests_common.py @@ -40,7 +40,7 @@ class NewOpenCVTests(unittest.TestCase): def hashimg(self, im): """ Compute a hash for an image, useful for image comparisons """ - return hashlib.md5(im.tostring()).digest() + return hashlib.md5(im.tostring()).hexdigest() if sys.version_info[:2] == (2, 6): def assertLess(self, a, b, msg=None):