Add goodfeatures python test and cleanup in test directory

This commit is contained in:
Vladislav Sovrasov 2016-03-17 15:33:28 +03:00
parent 947307baae
commit fd61978795
13 changed files with 36 additions and 2406 deletions

View File

@ -1,53 +0,0 @@
#!/usr/bin/env python
# Calculating and displaying 2D Hue-Saturation histogram of a color image
import sys
import cv2.cv as cv
def hs_histogram(src):
# Convert to HSV
hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
# Extract the H and S planes
h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
cv.Split(hsv, h_plane, s_plane, None, None)
planes = [h_plane, s_plane]
h_bins = 30
s_bins = 32
hist_size = [h_bins, s_bins]
# hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
h_ranges = [0, 180]
# saturation varies from 0 (black-gray-white) to
# 255 (pure spectrum color)
s_ranges = [0, 255]
ranges = [h_ranges, s_ranges]
scale = 10
hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
cv.CalcHist([cv.GetImage(i) for i in planes], hist)
(_, max_value, _, _) = cv.GetMinMaxHistValue(hist)
hist_img = cv.CreateImage((h_bins*scale, s_bins*scale), 8, 3)
for h in range(h_bins):
for s in range(s_bins):
bin_val = cv.QueryHistValue_2D(hist, h, s)
intensity = cv.Round(bin_val * 255 / max_value)
cv.Rectangle(hist_img,
(h*scale, s*scale),
((h+1)*scale - 1, (s+1)*scale - 1),
cv.RGB(intensity, intensity, intensity),
cv.CV_FILLED)
return hist_img
if __name__ == '__main__':
src = cv.LoadImageM(sys.argv[1])
cv.NamedWindow("Source", 1)
cv.ShowImage("Source", src)
cv.NamedWindow("H-S Histogram", 1)
cv.ShowImage("H-S Histogram", hs_histogram(src))
cv.WaitKey(0)

View File

@ -1,360 +0,0 @@
#!/usr/bin/env python
import sys
import math
import time
import random
import numpy
import transformations
import cv2.cv as cv
def clamp(a, x, b):
return numpy.maximum(a, numpy.minimum(x, b))
def norm(v):
mag = numpy.sqrt(sum([e * e for e in v]))
return v / mag
class Vec3:
def __init__(self, x, y, z):
self.v = (x, y, z)
def x(self):
return self.v[0]
def y(self):
return self.v[1]
def z(self):
return self.v[2]
def __repr__(self):
return "<Vec3 (%s,%s,%s)>" % tuple([repr(c) for c in self.v])
def __add__(self, other):
return Vec3(*[self.v[i] + other.v[i] for i in range(3)])
def __sub__(self, other):
return Vec3(*[self.v[i] - other.v[i] for i in range(3)])
def __mul__(self, other):
if isinstance(other, Vec3):
return Vec3(*[self.v[i] * other.v[i] for i in range(3)])
else:
return Vec3(*[self.v[i] * other for i in range(3)])
def mag2(self):
return sum([e * e for e in self.v])
def __abs__(self):
return numpy.sqrt(sum([e * e for e in self.v]))
def norm(self):
return self * (1.0 / abs(self))
def dot(self, other):
return sum([self.v[i] * other.v[i] for i in range(3)])
def cross(self, other):
(ax, ay, az) = self.v
(bx, by, bz) = other.v
return Vec3(ay * bz - by * az, az * bx - bz * ax, ax * by - bx * ay)
class Ray:
def __init__(self, o, d):
self.o = o
self.d = d
def project(self, d):
return self.o + self.d * d
class Camera:
def __init__(self, F):
R = Vec3(1., 0., 0.)
U = Vec3(0, 1., 0)
self.center = Vec3(0, 0, 0)
self.pcenter = Vec3(0, 0, F)
self.up = U
self.right = R
def genray(self, x, y):
""" -1 <= y <= 1 """
r = numpy.sqrt(x * x + y * y)
if 0:
rprime = r + (0.17 * r**2)
else:
rprime = (10 * numpy.sqrt(17 * r + 25) - 50) / 17
print "scale", rprime / r
x *= rprime / r
y *= rprime / r
o = self.center
r = (self.pcenter + (self.right * x) + (self.up * y)) - o
return Ray(o, r.norm())
class Sphere:
def __init__(self, center, radius):
self.center = center
self.radius = radius
def hit(self, r):
# a = mag2(r.d)
a = 1.
v = r.o - self.center
b = 2 * r.d.dot(v)
c = self.center.mag2() + r.o.mag2() + -2 * self.center.dot(r.o) - (self.radius ** 2)
det = (b * b) - (4 * c)
pred = 0 < det
sq = numpy.sqrt(abs(det))
h0 = (-b - sq) / (2)
h1 = (-b + sq) / (2)
h = numpy.minimum(h0, h1)
pred = pred & (h > 0)
normal = (r.project(h) - self.center) * (1.0 / self.radius)
return (pred, numpy.where(pred, h, 999999.), normal)
def pt2plane(p, plane):
return p.dot(plane) * (1. / abs(plane))
class Plane:
def __init__(self, p, n, right):
self.D = -pt2plane(p, n)
self.Pn = n
self.right = right
self.rightD = -pt2plane(p, right)
self.up = n.cross(right)
self.upD = -pt2plane(p, self.up)
def hit(self, r):
Vd = self.Pn.dot(r.d)
V0 = -(self.Pn.dot(r.o) + self.D)
h = V0 / Vd
pred = (0 <= h)
return (pred, numpy.where(pred, h, 999999.), self.Pn)
def localxy(self, loc):
x = (loc.dot(self.right) + self.rightD)
y = (loc.dot(self.up) + self.upD)
return (x, y)
# lena = numpy.fromstring(cv.LoadImage("../samples/c/lena.jpg", 0).tostring(), numpy.uint8) / 255.0
def texture(xy):
x,y = xy
xa = numpy.floor(x * 512)
ya = numpy.floor(y * 512)
a = (512 * ya) + xa
safe = (0 <= x) & (0 <= y) & (x < 1) & (y < 1)
if 0:
a = numpy.where(safe, a, 0).astype(numpy.int)
return numpy.where(safe, numpy.take(lena, a), 0.0)
else:
xi = numpy.floor(x * 11).astype(numpy.int)
yi = numpy.floor(y * 11).astype(numpy.int)
inside = (1 <= xi) & (xi < 10) & (2 <= yi) & (yi < 9)
checker = (xi & 1) ^ (yi & 1)
final = numpy.where(inside, checker, 1.0)
return numpy.where(safe, final, 0.5)
def under(vv, m):
return Vec3(*(numpy.dot(m, vv.v + (1,))[:3]))
class Renderer:
def __init__(self, w, h, oversample):
self.w = w
self.h = h
random.seed(1)
x = numpy.arange(self.w*self.h) % self.w
y = numpy.floor(numpy.arange(self.w*self.h) / self.w)
h2 = h / 2.0
w2 = w / 2.0
self.r = [ None ] * oversample
for o in range(oversample):
stoch_x = numpy.random.rand(self.w * self.h)
stoch_y = numpy.random.rand(self.w * self.h)
nx = (x + stoch_x - 0.5 - w2) / h2
ny = (y + stoch_y - 0.5 - h2) / h2
self.r[o] = cam.genray(nx, ny)
self.rnds = [random.random() for i in range(10)]
def frame(self, i):
rnds = self.rnds
roll = math.sin(i * .01 * rnds[0] + rnds[1])
pitch = math.sin(i * .01 * rnds[2] + rnds[3])
yaw = math.pi * math.sin(i * .01 * rnds[4] + rnds[5])
x = math.sin(i * 0.01 * rnds[6])
y = math.sin(i * 0.01 * rnds[7])
x,y,z = -0.5,0.5,1
roll,pitch,yaw = (0,0,0)
z = 4 + 3 * math.sin(i * 0.1 * rnds[8])
print z
rz = transformations.euler_matrix(roll, pitch, yaw)
p = Plane(Vec3(x, y, z), under(Vec3(0,0,-1), rz), under(Vec3(1, 0, 0), rz))
acc = 0
for r in self.r:
(pred, h, norm) = p.hit(r)
l = numpy.where(pred, texture(p.localxy(r.project(h))), 0.0)
acc += l
acc *= (1.0 / len(self.r))
# print "took", time.time() - st
img = cv.CreateMat(self.h, self.w, cv.CV_8UC1)
cv.SetData(img, (clamp(0, acc, 1) * 255).astype(numpy.uint8).tostring(), self.w)
return img
#########################################################################
num_x_ints = 8
num_y_ints = 6
num_pts = num_x_ints * num_y_ints
def get_corners(mono, refine = False):
(ok, corners) = cv.FindChessboardCorners(mono, (num_x_ints, num_y_ints), cv.CV_CALIB_CB_ADAPTIVE_THRESH | cv.CV_CALIB_CB_NORMALIZE_IMAGE)
if refine and ok:
corners = cv.FindCornerSubPix(mono, corners, (5,5), (-1,-1), ( cv.CV_TERMCRIT_EPS+cv.CV_TERMCRIT_ITER, 30, 0.1 ))
return (ok, corners)
def mk_object_points(nimages, squaresize = 1):
opts = cv.CreateMat(nimages * num_pts, 3, cv.CV_32FC1)
for i in range(nimages):
for j in range(num_pts):
opts[i * num_pts + j, 0] = (j / num_x_ints) * squaresize
opts[i * num_pts + j, 1] = (j % num_x_ints) * squaresize
opts[i * num_pts + j, 2] = 0
return opts
def mk_image_points(goodcorners):
ipts = cv.CreateMat(len(goodcorners) * num_pts, 2, cv.CV_32FC1)
for (i, co) in enumerate(goodcorners):
for j in range(num_pts):
ipts[i * num_pts + j, 0] = co[j][0]
ipts[i * num_pts + j, 1] = co[j][1]
return ipts
def mk_point_counts(nimages):
npts = cv.CreateMat(nimages, 1, cv.CV_32SC1)
for i in range(nimages):
npts[i, 0] = num_pts
return npts
def cvmat_iterator(cvmat):
for i in range(cvmat.rows):
for j in range(cvmat.cols):
yield cvmat[i,j]
cam = Camera(3.0)
rend = Renderer(640, 480, 2)
cv.NamedWindow("snap")
#images = [rend.frame(i) for i in range(0, 2000, 400)]
images = [rend.frame(i) for i in [1200]]
if 0:
for i,img in enumerate(images):
cv.SaveImage("final/%06d.png" % i, img)
size = cv.GetSize(images[0])
corners = [get_corners(i) for i in images]
goodcorners = [co for (im, (ok, co)) in zip(images, corners) if ok]
def checkerboard_error(xformed):
def pt2line(a, b, c):
x0,y0 = a
x1,y1 = b
x2,y2 = c
return abs((x2 - x1) * (y1 - y0) - (x1 - x0) * (y2 - y1)) / math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
errorsum = 0.
for im in xformed:
for row in range(6):
l0 = im[8 * row]
l1 = im[8 * row + 7]
for col in range(1, 7):
e = pt2line(im[8 * row + col], l0, l1)
#print "row", row, "e", e
errorsum += e
return errorsum
if True:
from scipy.optimize import fmin
def xf(pt, poly):
x, y = pt
r = math.sqrt((x - 320) ** 2 + (y - 240) ** 2)
fr = poly(r) / r
return (320 + (x - 320) * fr, 240 + (y - 240) * fr)
def silly(p, goodcorners):
# print "eval", p
d = 1.0 # - sum(p)
poly = numpy.poly1d(list(p) + [d, 0.])
xformed = [[xf(pt, poly) for pt in co] for co in goodcorners]
return checkerboard_error(xformed)
x0 = [ 0. ]
#print silly(x0, goodcorners)
print "initial error", silly(x0, goodcorners)
xopt = fmin(silly, x0, args=(goodcorners,))
print "xopt", xopt
print "final error", silly(xopt, goodcorners)
d = 1.0 # - sum(xopt)
poly = numpy.poly1d(list(xopt) + [d, 0.])
print "final polynomial"
print poly
for co in goodcorners:
scrib = cv.CreateMat(480, 640, cv.CV_8UC3)
cv.SetZero(scrib)
cv.DrawChessboardCorners(scrib, (num_x_ints, num_y_ints), [xf(pt, poly) for pt in co], True)
cv.ShowImage("snap", scrib)
cv.WaitKey()
sys.exit(0)
for (i, (img, (ok, co))) in enumerate(zip(images, corners)):
scrib = cv.CreateMat(img.rows, img.cols, cv.CV_8UC3)
cv.CvtColor(img, scrib, cv.CV_GRAY2BGR)
if ok:
cv.DrawChessboardCorners(scrib, (num_x_ints, num_y_ints), co, True)
cv.ShowImage("snap", scrib)
cv.WaitKey()
print len(goodcorners)
ipts = mk_image_points(goodcorners)
opts = mk_object_points(len(goodcorners), .1)
npts = mk_point_counts(len(goodcorners))
intrinsics = cv.CreateMat(3, 3, cv.CV_64FC1)
distortion = cv.CreateMat(4, 1, cv.CV_64FC1)
cv.SetZero(intrinsics)
cv.SetZero(distortion)
# focal lengths have 1/1 ratio
intrinsics[0,0] = 1.0
intrinsics[1,1] = 1.0
cv.CalibrateCamera2(opts, ipts, npts,
cv.GetSize(images[0]),
intrinsics,
distortion,
cv.CreateMat(len(goodcorners), 3, cv.CV_32FC1),
cv.CreateMat(len(goodcorners), 3, cv.CV_32FC1),
flags = 0) # cv.CV_CALIB_ZERO_TANGENT_DIST)
print "D =", list(cvmat_iterator(distortion))
print "K =", list(cvmat_iterator(intrinsics))
mapx = cv.CreateImage((640, 480), cv.IPL_DEPTH_32F, 1)
mapy = cv.CreateImage((640, 480), cv.IPL_DEPTH_32F, 1)
cv.InitUndistortMap(intrinsics, distortion, mapx, mapy)
for img in images:
r = cv.CloneMat(img)
cv.Remap(img, r, mapx, mapy)
cv.ShowImage("snap", r)
cv.WaitKey()

View File

@ -1,25 +0,0 @@
#!/usr/bin/env python
import sys
import cv2.cv as cv
def findstereocorrespondence(image_left, image_right):
# image_left and image_right are the input 8-bit single-channel images
# from the left and the right cameras, respectively
(r, c) = (image_left.rows, image_left.cols)
disparity_left = cv.CreateMat(r, c, cv.CV_16S)
disparity_right = cv.CreateMat(r, c, cv.CV_16S)
state = cv.CreateStereoGCState(16, 2)
cv.FindStereoCorrespondenceGC(image_left, image_right, disparity_left, disparity_right, state, 0)
return (disparity_left, disparity_right)
if __name__ == '__main__':
(l, r) = [cv.LoadImageM(f, cv.CV_LOAD_IMAGE_GRAYSCALE) for f in sys.argv[1:]]
(disparity_left, disparity_right) = findstereocorrespondence(l, r)
disparity_left_visual = cv.CreateMat(l.rows, l.cols, cv.CV_8U)
cv.ConvertScale(disparity_left, disparity_left_visual, -16)
cv.SaveImage("disparity.pgm", disparity_left_visual)

View File

@ -1,36 +0,0 @@
#!/usr/bin/env python
import cv2.cv as cv
import unittest
class TestGoodFeaturesToTrack(unittest.TestCase):
def test(self):
arr = cv.LoadImage("../samples/c/lena.jpg", 0)
original = cv.CloneImage(arr)
size = cv.GetSize(arr)
eig_image = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
temp_image = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
threshes = [ x / 100. for x in range(1,10) ]
results = dict([(t, cv.GoodFeaturesToTrack(arr, eig_image, temp_image, 20000, t, 2, useHarris = 1)) for t in threshes])
# Check that GoodFeaturesToTrack has not modified input image
self.assert_(arr.tostring() == original.tostring())
# Check for repeatability
for i in range(10):
results2 = dict([(t, cv.GoodFeaturesToTrack(arr, eig_image, temp_image, 20000, t, 2, useHarris = 1)) for t in threshes])
self.assert_(results == results2)
for t0,t1 in zip(threshes, threshes[1:]):
r0 = results[t0]
r1 = results[t1]
# Increasing thresh should make result list shorter
self.assert_(len(r0) > len(r1))
# Increasing thresh should monly truncate result list
self.assert_(r0[:len(r1)] == r1)
if __name__ == '__main__':
unittest.main()

View File

@ -1,9 +0,0 @@
#!/usr/bin/env python
import cv2.cv as cv
import numpy as np
cv.NamedWindow('Leak')
while 1:
leak = np.random.random((480, 640)) * 255
cv.ShowImage('Leak', leak.astype(np.uint8))
cv.WaitKey(10)

View File

@ -1,12 +0,0 @@
#!/usr/bin/env python
import cv2.cv as cv
import numpy as np
import time
while True:
for i in range(4000):
a = cv.CreateImage((1024,1024), cv.IPL_DEPTH_8U, 1)
b = cv.CreateMat(1024, 1024, cv.CV_8UC1)
c = cv.CreateMatND([1024,1024], cv.CV_8UC1)
print "pause..."

View File

@ -1,8 +0,0 @@
#!/usr/bin/env python
import cv2.cv as cv
import math
import time
while True:
h = cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1)

View File

@ -1,11 +0,0 @@
#!/usr/bin/env python
import cv2.cv as cv
import math
import time
N=50000
print "leak4"
while True:
seq=list((i*1., i*1.) for i in range(N))
cv.Moments(seq)

View File

@ -1,16 +0,0 @@
#!/usr/bin/env python
import cv2.cv as cv
def precornerdetect(image):
# assume that the image is floating-point
corners = cv.CloneMat(image)
cv.PreCornerDetect(image, corners, 3)
dilated_corners = cv.CloneMat(image)
cv.Dilate(corners, dilated_corners, None, 1)
corner_mask = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
cv.Sub(corners, dilated_corners, corners)
cv.CmpS(corners, 0, corner_mask, cv.CV_CMP_GE)
return (corners, corner_mask)

View File

@ -0,0 +1,36 @@
#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import cv2
import numpy as np
from tests_common import NewOpenCVTests
class TestGoodFeaturesToTrack_test(NewOpenCVTests):
def test_goodFeaturesToTrack(self):
arr = self.get_sample('samples/data/lena.jpg', 0)
original = arr.copy(True)
threshes = [ x / 100. for x in range(1,10) ]
numPoints = 20000
results = dict([(t, cv2.goodFeaturesToTrack(arr, numPoints, t, 2, useHarrisDetector=True)) for t in threshes])
# Check that GoodFeaturesToTrack has not modified input image
self.assertTrue(arr.tostring() == original.tostring())
# Check for repeatability
for i in range(1):
results2 = dict([(t, cv2.goodFeaturesToTrack(arr, numPoints, t, 2, useHarrisDetector=True)) for t in threshes])
for t in threshes:
self.assertTrue(len(results2[t]) == len(results[t]))
for i in range(len(results[t])):
self.assertTrue(cv2.norm(results[t][i][0] - results2[t][i][0]) == 0)
for t0,t1 in zip(threshes, threshes[1:]):
r0 = results[t0]
r1 = results[t1]
# Increasing thresh should make result list shorter
self.assertTrue(len(r0) > len(r1))
# Increasing thresh should monly truncate result list
for i in range(len(r1)):
self.assertTrue(cv2.norm(r1[i][0] - r0[i][0])==0)

View File

@ -1,78 +0,0 @@
#!/usr/bin/env python
import urllib
import cv2.cv as cv
import Image
import unittest
class TestLoadImage(unittest.TestCase):
def setUp(self):
open("large.jpg", "w").write(urllib.urlopen("http://www.cs.ubc.ca/labs/lci/curious_george/img/ROS_bug_imgs/IMG_3560.jpg").read())
def test_load(self):
pilim = Image.open("large.jpg")
cvim = cv.LoadImage("large.jpg")
self.assert_(len(pilim.tostring()) == len(cvim.tostring()))
class Creating(unittest.TestCase):
size=(640, 480)
repeat=100
def test_0_Create(self):
image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
cnt=cv.CountNonZero(image)
self.assertEqual(cnt, 0, msg="Created image is not black. CountNonZero=%i" % cnt)
def test_2_CreateRepeat(self):
cnt=0
for i in range(self.repeat):
image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
cnt+=cv.CountNonZero(image)
self.assertEqual(cnt, 0, msg="Created images are not black. Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
def test_2a_MemCreated(self):
cnt=0
v=[]
for i in range(self.repeat):
image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
cv.FillPoly(image, [[(0, 0), (0, 100), (100, 0)]], 0)
cnt+=cv.CountNonZero(image)
v.append(image)
self.assertEqual(cnt, 0, msg="Memorized images are not black. Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
def test_3_tostirng(self):
image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
image.tostring()
cnt=cv.CountNonZero(image)
self.assertEqual(cnt, 0, msg="After tostring(): CountNonZero=%i" % cnt)
def test_40_tostringRepeat(self):
cnt=0
image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
cv.Set(image, cv.Scalar(0,0,0,0))
for i in range(self.repeat*100):
image.tostring()
cnt=cv.CountNonZero(image)
self.assertEqual(cnt, 0, msg="Repeating tostring(): Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
def test_41_CreateToStringRepeat(self):
cnt=0
for i in range(self.repeat*100):
image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
cv.Set(image, cv.Scalar(0,0,0,0))
image.tostring()
cnt+=cv.CountNonZero(image)
self.assertEqual(cnt, 0, msg="Repeating create and tostring(): Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
def test_4a_MemCreatedToString(self):
cnt=0
v=[]
for i in range(self.repeat):
image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
cv.Set(image, cv.Scalar(0,0,0,0))
image.tostring()
cnt+=cv.CountNonZero(image)
v.append(image)
self.assertEqual(cnt, 0, msg="Repeating and memorizing after tostring(): Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
if __name__ == '__main__':
unittest.main()

View File

@ -1,91 +0,0 @@
#!/usr/bin/env python
import unittest
import random
import time
import math
import sys
import array
import os
import cv2.cv as cv
def find_sample(s):
for d in ["../samples/c/", "../doc/pics/"]:
path = os.path.join(d, s)
if os.access(path, os.R_OK):
return path
return s
class TestTickets(unittest.TestCase):
def test_2542670(self):
xys = [(94, 121), (94, 122), (93, 123), (92, 123), (91, 124), (91, 125), (91, 126), (92, 127), (92, 128), (92, 129), (92, 130), (92, 131), (91, 132), (90, 131), (90, 130), (90, 131), (91, 132), (92, 133), (92, 134), (93, 135), (94, 136), (94, 137), (94, 138), (95, 139), (96, 140), (96, 141), (96, 142), (96, 143), (97, 144), (97, 145), (98, 146), (99, 146), (100, 146), (101, 146), (102, 146), (103, 146), (104, 146), (105, 146), (106, 146), (107, 146), (108, 146), (109, 146), (110, 146), (111, 146), (112, 146), (113, 146), (114, 146), (115, 146), (116, 146), (117, 146), (118, 146), (119, 146), (120, 146), (121, 146), (122, 146), (123, 146), (124, 146), (125, 146), (126, 146), (126, 145), (126, 144), (126, 143), (126, 142), (126, 141), (126, 140), (127, 139), (127, 138), (127, 137), (127, 136), (127, 135), (127, 134), (127, 133), (128, 132), (129, 132), (130, 131), (131, 130), (131, 129), (131, 128), (132, 127), (133, 126), (134, 125), (134, 124), (135, 123), (136, 122), (136, 121), (135, 121), (134, 121), (133, 121), (132, 121), (131, 121), (130, 121), (129, 121), (128, 121), (127, 121), (126, 121), (125, 121), (124, 121), (123, 121), (122, 121), (121, 121), (120, 121), (119, 121), (118, 121), (117, 121), (116, 121), (115, 121), (114, 121), (113, 121), (112, 121), (111, 121), (110, 121), (109, 121), (108, 121), (107, 121), (106, 121), (105, 121), (104, 121), (103, 121), (102, 121), (101, 121), (100, 121), (99, 121), (98, 121), (97, 121), (96, 121), (95, 121)]
#xys = xys[:12] + xys[16:]
pts = cv.CreateMat(len(xys), 1, cv.CV_32SC2)
for i,(x,y) in enumerate(xys):
pts[i,0] = (x, y)
storage = cv.CreateMemStorage()
hull = cv.ConvexHull2(pts, storage)
hullp = cv.ConvexHull2(pts, storage, return_points = 1)
defects = cv.ConvexityDefects(pts, hull, storage)
vis = cv.CreateImage((1000,1000), 8, 3)
x0 = min([x for (x,y) in xys]) - 10
x1 = max([x for (x,y) in xys]) + 10
y0 = min([y for (y,y) in xys]) - 10
y1 = max([y for (y,y) in xys]) + 10
def xform(pt):
x,y = pt
return (1000 * (x - x0) / (x1 - x0),
1000 * (y - y0) / (y1 - y0))
for d in defects[:2]:
cv.Zero(vis)
# First draw the defect as a red triangle
cv.FillConvexPoly(vis, [xform(p) for p in d[:3]], cv.RGB(255,0,0))
# Draw the convex hull as a thick green line
for a,b in zip(hullp, hullp[1:]):
cv.Line(vis, xform(a), xform(b), cv.RGB(0,128,0), 3)
# Draw the original contour as a white line
for a,b in zip(xys, xys[1:]):
cv.Line(vis, xform(a), xform(b), (255,255,255))
self.snap(vis)
def test_2686307(self):
lena = cv.LoadImage(find_sample("lena.jpg"), 1)
dst = cv.CreateImage((512,512), 8, 3)
cv.Set(dst, (128,192,255))
mask = cv.CreateImage((512,512), 8, 1)
cv.Zero(mask)
cv.Rectangle(mask, (10,10), (300,100), 255, -1)
cv.Copy(lena, dst, mask)
self.snapL([lena, dst, mask])
m = cv.CreateMat(480, 640, cv.CV_8UC1)
print "ji", m
print m.rows, m.cols, m.type, m.step
def snap(self, img):
self.snapL([img])
def snapL(self, L):
for i,img in enumerate(L):
cv.NamedWindow("snap-%d" % i, 1)
cv.ShowImage("snap-%d" % i, img)
cv.WaitKey()
cv.DestroyAllWindows()
if __name__ == '__main__':
random.seed(0)
if len(sys.argv) == 1:
suite = unittest.TestLoader().loadTestsFromTestCase(TestTickets)
unittest.TextTestRunner(verbosity=2).run(suite)
else:
suite = unittest.TestSuite()
suite.addTest(TestTickets(sys.argv[1]))
unittest.TextTestRunner(verbosity=2).run(suite)

File diff suppressed because it is too large Load Diff