mirror of https://github.com/opencv/opencv.git
parent
b2ba8b9969
commit
f886651cf0
55 changed files with 161 additions and 5434 deletions
File diff suppressed because it is too large
Load Diff
@ -1,133 +0,0 @@ |
||||
#!/usr/bin/env python |
||||
|
||||
import unittest |
||||
import random |
||||
import time |
||||
import math |
||||
import sys |
||||
import array |
||||
import urllib |
||||
import tarfile |
||||
import hashlib |
||||
import os |
||||
import getopt |
||||
import operator |
||||
import functools |
||||
import numpy as np |
||||
import cv2 |
||||
import cv2.cv as cv |
||||
|
||||
class NewOpenCVTests(unittest.TestCase): |
||||
|
||||
def get_sample(self, filename, iscolor = cv.CV_LOAD_IMAGE_COLOR): |
||||
if not filename in self.image_cache: |
||||
filedata = urllib.urlopen("https://raw.github.com/Itseez/opencv/master/" + filename).read() |
||||
self.image_cache[filename] = cv2.imdecode(np.fromstring(filedata, dtype=np.uint8), iscolor) |
||||
return self.image_cache[filename] |
||||
|
||||
def setUp(self): |
||||
self.image_cache = {} |
||||
|
||||
def hashimg(self, im): |
||||
""" Compute a hash for an image, useful for image comparisons """ |
||||
return hashlib.md5(im.tostring()).digest() |
||||
|
||||
if sys.version_info[:2] == (2, 6): |
||||
def assertLess(self, a, b, msg=None): |
||||
if not a < b: |
||||
self.fail('%s not less than %s' % (repr(a), repr(b))) |
||||
|
||||
def assertLessEqual(self, a, b, msg=None): |
||||
if not a <= b: |
||||
self.fail('%s not less than or equal to %s' % (repr(a), repr(b))) |
||||
|
||||
def assertGreater(self, a, b, msg=None): |
||||
if not a > b: |
||||
self.fail('%s not greater than %s' % (repr(a), repr(b))) |
||||
|
||||
# Tests to run first; check the handful of basic operations that the later tests rely on |
||||
|
||||
class Hackathon244Tests(NewOpenCVTests): |
||||
|
||||
def test_int_array(self): |
||||
a = np.array([-1, 2, -3, 4, -5]) |
||||
absa0 = np.abs(a) |
||||
self.assert_(cv2.norm(a, cv2.NORM_L1) == 15) |
||||
absa1 = cv2.absdiff(a, 0) |
||||
self.assertEqual(cv2.norm(absa1, absa0, cv2.NORM_INF), 0) |
||||
|
||||
def test_imencode(self): |
||||
a = np.zeros((480, 640), dtype=np.uint8) |
||||
flag, ajpg = cv2.imencode("img_q90.jpg", a, [cv2.IMWRITE_JPEG_QUALITY, 90]) |
||||
self.assertEqual(flag, True) |
||||
self.assertEqual(ajpg.dtype, np.uint8) |
||||
self.assertGreater(ajpg.shape[0], 1) |
||||
self.assertEqual(ajpg.shape[1], 1) |
||||
|
||||
def test_projectPoints(self): |
||||
objpt = np.float64([[1,2,3]]) |
||||
imgpt0, jac0 = cv2.projectPoints(objpt, np.zeros(3), np.zeros(3), np.eye(3), np.float64([])) |
||||
imgpt1, jac1 = cv2.projectPoints(objpt, np.zeros(3), np.zeros(3), np.eye(3), None) |
||||
self.assertEqual(imgpt0.shape, (objpt.shape[0], 1, 2)) |
||||
self.assertEqual(imgpt1.shape, imgpt0.shape) |
||||
self.assertEqual(jac0.shape, jac1.shape) |
||||
self.assertEqual(jac0.shape[0], 2*objpt.shape[0]) |
||||
|
||||
def test_estimateAffine3D(self): |
||||
pattern_size = (11, 8) |
||||
pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32) |
||||
pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2) |
||||
pattern_points *= 10 |
||||
(retval, out, inliers) = cv2.estimateAffine3D(pattern_points, pattern_points) |
||||
self.assertEqual(retval, 1) |
||||
if cv2.norm(out[2,:]) < 1e-3: |
||||
out[2,2]=1 |
||||
self.assertLess(cv2.norm(out, np.float64([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])), 1e-3) |
||||
self.assertEqual(cv2.countNonZero(inliers), pattern_size[0]*pattern_size[1]) |
||||
|
||||
def test_fast(self): |
||||
fd = cv2.FastFeatureDetector(30, True) |
||||
img = self.get_sample("samples/cpp/right02.jpg", 0) |
||||
img = cv2.medianBlur(img, 3) |
||||
imgc = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) |
||||
keypoints = fd.detect(img) |
||||
self.assert_(600 <= len(keypoints) <= 700) |
||||
for kpt in keypoints: |
||||
self.assertNotEqual(kpt.response, 0) |
||||
|
||||
def check_close_angles(self, a, b, angle_delta): |
||||
self.assert_(abs(a - b) <= angle_delta or |
||||
abs(360 - abs(a - b)) <= angle_delta) |
||||
|
||||
def check_close_pairs(self, a, b, delta): |
||||
self.assertLessEqual(abs(a[0] - b[0]), delta) |
||||
self.assertLessEqual(abs(a[1] - b[1]), delta) |
||||
|
||||
def check_close_boxes(self, a, b, delta, angle_delta): |
||||
self.check_close_pairs(a[0], b[0], delta) |
||||
self.check_close_pairs(a[1], b[1], delta) |
||||
self.check_close_angles(a[2], b[2], angle_delta) |
||||
|
||||
def test_geometry(self): |
||||
npt = 100 |
||||
np.random.seed(244) |
||||
a = np.random.randn(npt,2).astype('float32')*50 + 150 |
||||
|
||||
img = np.zeros((300, 300, 3), dtype='uint8') |
||||
be = cv2.fitEllipse(a) |
||||
br = cv2.minAreaRect(a) |
||||
mc, mr = cv2.minEnclosingCircle(a) |
||||
|
||||
be0 = ((150.2511749267578, 150.77322387695312), (158.024658203125, 197.57696533203125), 37.57804489135742) |
||||
br0 = ((161.2974090576172, 154.41793823242188), (199.2301483154297, 207.7177734375), -9.164555549621582) |
||||
mc0, mr0 = (160.41790771484375, 144.55152893066406), 136.713500977 |
||||
|
||||
self.check_close_boxes(be, be0, 5, 15) |
||||
self.check_close_boxes(br, br0, 5, 15) |
||||
self.check_close_pairs(mc, mc0, 5) |
||||
self.assertLessEqual(abs(mr - mr0), 5) |
||||
|
||||
if __name__ == '__main__': |
||||
print "testing", cv2.__version__ |
||||
random.seed(0) |
||||
unittest.main() |
@ -1,15 +0,0 @@ |
||||
#!/usr/bin/python |
||||
|
||||
import cv2.cv as cv |
||||
import time |
||||
|
||||
cv.NamedWindow("camera", 1) |
||||
|
||||
capture = cv.CaptureFromCAM(0) |
||||
|
||||
while True: |
||||
img = cv.QueryFrame(capture) |
||||
cv.ShowImage("camera", img) |
||||
if cv.WaitKey(10) == 27: |
||||
break |
||||
cv.DestroyAllWindows() |
@ -1,116 +0,0 @@ |
||||
#!/usr/bin/env python |
||||
|
||||
import cv2.cv as cv |
||||
|
||||
def is_rect_nonzero(r): |
||||
(_,_,w,h) = r |
||||
return (w > 0) and (h > 0) |
||||
|
||||
class CamShiftDemo: |
||||
|
||||
def __init__(self): |
||||
self.capture = cv.CaptureFromCAM(0) |
||||
cv.NamedWindow( "CamShiftDemo", 1 ) |
||||
cv.NamedWindow( "Histogram", 1 ) |
||||
cv.SetMouseCallback( "CamShiftDemo", self.on_mouse) |
||||
|
||||
self.drag_start = None # Set to (x,y) when mouse starts drag |
||||
self.track_window = None # Set to rect when the mouse drag finishes |
||||
|
||||
print( "Keys:\n" |
||||
" ESC - quit the program\n" |
||||
" b - switch to/from backprojection view\n" |
||||
"To initialize tracking, drag across the object with the mouse\n" ) |
||||
|
||||
def hue_histogram_as_image(self, hist): |
||||
""" Returns a nice representation of a hue histogram """ |
||||
|
||||
histimg_hsv = cv.CreateImage( (320,200), 8, 3) |
||||
|
||||
mybins = cv.CloneMatND(hist.bins) |
||||
cv.Log(mybins, mybins) |
||||
(_, hi, _, _) = cv.MinMaxLoc(mybins) |
||||
cv.ConvertScale(mybins, mybins, 255. / hi) |
||||
|
||||
w,h = cv.GetSize(histimg_hsv) |
||||
hdims = cv.GetDims(mybins)[0] |
||||
for x in range(w): |
||||
xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image |
||||
val = int(mybins[int(hdims * x / w)] * h / 255) |
||||
cv.Rectangle( histimg_hsv, (x, 0), (x, h-val), (xh,255,64), -1) |
||||
cv.Rectangle( histimg_hsv, (x, h-val), (x, h), (xh,255,255), -1) |
||||
|
||||
histimg = cv.CreateImage( (320,200), 8, 3) |
||||
cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR) |
||||
return histimg |
||||
|
||||
def on_mouse(self, event, x, y, flags, param): |
||||
if event == cv.CV_EVENT_LBUTTONDOWN: |
||||
self.drag_start = (x, y) |
||||
if event == cv.CV_EVENT_LBUTTONUP: |
||||
self.drag_start = None |
||||
self.track_window = self.selection |
||||
if self.drag_start: |
||||
xmin = min(x, self.drag_start[0]) |
||||
ymin = min(y, self.drag_start[1]) |
||||
xmax = max(x, self.drag_start[0]) |
||||
ymax = max(y, self.drag_start[1]) |
||||
self.selection = (xmin, ymin, xmax - xmin, ymax - ymin) |
||||
|
||||
def run(self): |
||||
hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0,180)], 1 ) |
||||
backproject_mode = False |
||||
while True: |
||||
frame = cv.QueryFrame( self.capture ) |
||||
|
||||
# Convert to HSV and keep the hue |
||||
hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) |
||||
cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) |
||||
self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1) |
||||
cv.Split(hsv, self.hue, None, None, None) |
||||
|
||||
# Compute back projection |
||||
backproject = cv.CreateImage(cv.GetSize(frame), 8, 1) |
||||
|
||||
# Run the cam-shift |
||||
cv.CalcArrBackProject( [self.hue], backproject, hist ) |
||||
if self.track_window and is_rect_nonzero(self.track_window): |
||||
crit = ( cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) |
||||
(iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) |
||||
self.track_window = rect |
||||
|
||||
# If mouse is pressed, highlight the current selected rectangle |
||||
# and recompute the histogram |
||||
|
||||
if self.drag_start and is_rect_nonzero(self.selection): |
||||
sub = cv.GetSubRect(frame, self.selection) |
||||
save = cv.CloneMat(sub) |
||||
cv.ConvertScale(frame, frame, 0.5) |
||||
cv.Copy(save, sub) |
||||
x,y,w,h = self.selection |
||||
cv.Rectangle(frame, (x,y), (x+w,y+h), (255,255,255)) |
||||
|
||||
sel = cv.GetSubRect(self.hue, self.selection ) |
||||
cv.CalcArrHist( [sel], hist, 0) |
||||
(_, max_val, _, _) = cv.GetMinMaxHistValue( hist) |
||||
if max_val != 0: |
||||
cv.ConvertScale(hist.bins, hist.bins, 255. / max_val) |
||||
elif self.track_window and is_rect_nonzero(self.track_window): |
||||
cv.EllipseBox( frame, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 ) |
||||
|
||||
if not backproject_mode: |
||||
cv.ShowImage( "CamShiftDemo", frame ) |
||||
else: |
||||
cv.ShowImage( "CamShiftDemo", backproject) |
||||
cv.ShowImage( "Histogram", self.hue_histogram_as_image(hist)) |
||||
|
||||
c = cv.WaitKey(7) % 0x100 |
||||
if c == 27: |
||||
break |
||||
elif c == ord("b"): |
||||
backproject_mode = not backproject_mode |
||||
|
||||
if __name__=="__main__": |
||||
demo = CamShiftDemo() |
||||
demo.run() |
||||
cv.DestroyAllWindows() |
@ -1,34 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import cv2.cv as cv |
||||
import sys |
||||
import urllib2 |
||||
|
||||
if __name__ == "__main__": |
||||
cv.NamedWindow("win") |
||||
if len(sys.argv) > 1: |
||||
filename = sys.argv[1] |
||||
im = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
im3 = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_COLOR) |
||||
else: |
||||
try: # try opening local copy of image |
||||
fileName = '../cpp/left01.jpg' |
||||
im = cv.LoadImageM(fileName, False) |
||||
im3 = cv.LoadImageM(fileName, True) |
||||
except: # if local copy cannot be opened, try downloading it |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/cpp/left01.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
im = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
im3 = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
|
||||
chessboard_dim = ( 9, 6 ) |
||||
|
||||
found_all, corners = cv.FindChessboardCorners( im, chessboard_dim ) |
||||
print found_all, len(corners) |
||||
|
||||
cv.DrawChessboardCorners( im3, chessboard_dim, corners, found_all ) |
||||
|
||||
cv.ShowImage("win", im3); |
||||
cv.WaitKey() |
||||
cv.DestroyAllWindows() |
@ -1,136 +0,0 @@ |
||||
#! /usr/bin/env python |
||||
|
||||
print "OpenCV Python version of contours" |
||||
|
||||
# import the necessary things for OpenCV |
||||
import cv2.cv as cv |
||||
|
||||
# some default constants |
||||
_SIZE = 500 |
||||
_DEFAULT_LEVEL = 3 |
||||
|
||||
# definition of some colors |
||||
_red = (0, 0, 255, 0); |
||||
_green = (0, 255, 0, 0); |
||||
_white = cv.RealScalar (255) |
||||
_black = cv.RealScalar (0) |
||||
|
||||
# the callback on the trackbar, to set the level of contours we want |
||||
# to display |
||||
def on_trackbar (position): |
||||
|
||||
# create the image for putting in it the founded contours |
||||
contours_image = cv.CreateImage ( (_SIZE, _SIZE), 8, 3) |
||||
|
||||
# compute the real level of display, given the current position |
||||
levels = position - 3 |
||||
|
||||
# initialisation |
||||
_contours = contours |
||||
|
||||
if levels <= 0: |
||||
# zero or negative value |
||||
# => get to the nearest face to make it look more funny |
||||
_contours = contours.h_next().h_next().h_next() |
||||
|
||||
# first, clear the image where we will draw contours |
||||
cv.SetZero (contours_image) |
||||
|
||||
# draw contours in red and green |
||||
cv.DrawContours (contours_image, _contours, |
||||
_red, _green, |
||||
levels, 3, cv.CV_AA, |
||||
(0, 0)) |
||||
|
||||
# finally, show the image |
||||
cv.ShowImage ("contours", contours_image) |
||||
|
||||
if __name__ == '__main__': |
||||
|
||||
# create the image where we want to display results |
||||
image = cv.CreateImage ( (_SIZE, _SIZE), 8, 1) |
||||
|
||||
# start with an empty image |
||||
cv.SetZero (image) |
||||
|
||||
# draw the original picture |
||||
for i in range (6): |
||||
dx = (i % 2) * 250 - 30 |
||||
dy = (i / 2) * 150 |
||||
|
||||
cv.Ellipse (image, |
||||
(dx + 150, dy + 100), |
||||
(100, 70), |
||||
0, 0, 360, _white, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 115, dy + 70), |
||||
(30, 20), |
||||
0, 0, 360, _black, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 185, dy + 70), |
||||
(30, 20), |
||||
0, 0, 360, _black, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 115, dy + 70), |
||||
(15, 15), |
||||
0, 0, 360, _white, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 185, dy + 70), |
||||
(15, 15), |
||||
0, 0, 360, _white, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 115, dy + 70), |
||||
(5, 5), |
||||
0, 0, 360, _black, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 185, dy + 70), |
||||
(5, 5), |
||||
0, 0, 360, _black, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 150, dy + 100), |
||||
(10, 5), |
||||
0, 0, 360, _black, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 150, dy + 150), |
||||
(40, 10), |
||||
0, 0, 360, _black, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 27, dy + 100), |
||||
(20, 35), |
||||
0, 0, 360, _white, -1, 8, 0) |
||||
cv.Ellipse (image, |
||||
(dx + 273, dy + 100), |
||||
(20, 35), |
||||
0, 0, 360, _white, -1, 8, 0) |
||||
|
||||
# create window and display the original picture in it |
||||
cv.NamedWindow ("image", 1) |
||||
cv.ShowImage ("image", image) |
||||
|
||||
# create the storage area |
||||
storage = cv.CreateMemStorage (0) |
||||
|
||||
# find the contours |
||||
contours = cv.FindContours(image, |
||||
storage, |
||||
cv.CV_RETR_TREE, |
||||
cv.CV_CHAIN_APPROX_SIMPLE, |
||||
(0,0)) |
||||
|
||||
# comment this out if you do not want approximation |
||||
contours = cv.ApproxPoly (contours, |
||||
storage, |
||||
cv.CV_POLY_APPROX_DP, 3, 1) |
||||
|
||||
# create the window for the contours |
||||
cv.NamedWindow ("contours", 1) |
||||
|
||||
# create the trackbar, to enable the change of the displayed level |
||||
cv.CreateTrackbar ("levels+3", "contours", 3, 7, on_trackbar) |
||||
|
||||
# call one time the callback, so we will have the 1st display done |
||||
on_trackbar (_DEFAULT_LEVEL) |
||||
|
||||
# wait a key pressed to end |
||||
cv.WaitKey (0) |
||||
cv.DestroyAllWindows() |
@ -1,65 +0,0 @@ |
||||
#! /usr/bin/env python |
||||
|
||||
print "OpenCV Python version of convexhull" |
||||
|
||||
# import the necessary things for OpenCV |
||||
import cv2.cv as cv |
||||
|
||||
# to generate random values |
||||
import random |
||||
|
||||
# how many points we want at max |
||||
_MAX_POINTS = 100 |
||||
|
||||
if __name__ == '__main__': |
||||
|
||||
# main object to get random values from |
||||
my_random = random.Random () |
||||
|
||||
# create the image where we want to display results |
||||
image = cv.CreateImage ( (500, 500), 8, 3) |
||||
|
||||
# create the window to put the image in |
||||
cv.NamedWindow ('hull', cv.CV_WINDOW_AUTOSIZE) |
||||
|
||||
while True: |
||||
# do forever |
||||
|
||||
# get a random number of points |
||||
count = my_random.randrange (0, _MAX_POINTS) + 1 |
||||
|
||||
# initialisations |
||||
points = [] |
||||
|
||||
for i in range (count): |
||||
# generate a random point |
||||
points.append ( ( |
||||
my_random.randrange (0, image.width / 2) + image.width / 4, |
||||
my_random.randrange (0, image.width / 2) + image.width / 4 |
||||
)) |
||||
|
||||
# compute the convex hull |
||||
storage = cv.CreateMemStorage(0) |
||||
hull = cv.ConvexHull2 (points, storage, cv.CV_CLOCKWISE, 1) |
||||
|
||||
# start with an empty image |
||||
cv.SetZero (image) |
||||
|
||||
# draw all the points as circles in red |
||||
for i in range (count): |
||||
cv.Circle (image, points [i], 2, |
||||
(0, 0, 255, 0), |
||||
cv.CV_FILLED, cv.CV_AA, 0) |
||||
|
||||
# Draw the convex hull as a closed polyline in green |
||||
cv.PolyLine(image, [hull], 1, cv.RGB(0,255,0), 1, cv.CV_AA) |
||||
|
||||
# display the final image |
||||
cv.ShowImage ('hull', image) |
||||
|
||||
# handle events, and wait a key pressed |
||||
k = cv.WaitKey (0) % 0x100 |
||||
if k == 27: |
||||
# user has press the ESC key, so exit |
||||
break |
||||
cv.DestroyAllWindows() |
@ -1,169 +0,0 @@ |
||||
#!/usr/bin/python |
||||
|
||||
""" |
||||
Find Squares in image by finding countours and filtering |
||||
""" |
||||
#Results slightly different from C version on same images, but is |
||||
#otherwise ok |
||||
|
||||
import math |
||||
import cv2.cv as cv |
||||
|
||||
def angle(pt1, pt2, pt0): |
||||
"calculate angle contained by 3 points(x, y)" |
||||
dx1 = pt1[0] - pt0[0] |
||||
dy1 = pt1[1] - pt0[1] |
||||
dx2 = pt2[0] - pt0[0] |
||||
dy2 = pt2[1] - pt0[1] |
||||
|
||||
nom = dx1*dx2 + dy1*dy2 |
||||
denom = math.sqrt( (dx1*dx1 + dy1*dy1) * (dx2*dx2 + dy2*dy2) + 1e-10 ) |
||||
ang = nom / denom |
||||
return ang |
||||
|
||||
def is_square(contour): |
||||
""" |
||||
Squareness checker |
||||
|
||||
Square contours should: |
||||
-have 4 vertices after approximation, |
||||
-have relatively large area (to filter out noisy contours) |
||||
-be convex. |
||||
-have angles between sides close to 90deg (cos(ang) ~0 ) |
||||
Note: absolute value of an area is used because area may be |
||||
positive or negative - in accordance with the contour orientation |
||||
""" |
||||
|
||||
area = math.fabs( cv.ContourArea(contour) ) |
||||
isconvex = cv.CheckContourConvexity(contour) |
||||
s = 0 |
||||
if len(contour) == 4 and area > 1000 and isconvex: |
||||
for i in range(1, 4): |
||||
# find minimum angle between joint edges (maximum of cosine) |
||||
pt1 = contour[i] |
||||
pt2 = contour[i-1] |
||||
pt0 = contour[i-2] |
||||
|
||||
t = math.fabs(angle(pt0, pt1, pt2)) |
||||
if s <= t:s = t |
||||
|
||||
# if cosines of all angles are small (all angles are ~90 degree) |
||||
# then its a square |
||||
if s < 0.3:return True |
||||
|
||||
return False |
||||
|
||||
def find_squares_from_binary( gray ): |
||||
""" |
||||
use contour search to find squares in binary image |
||||
returns list of numpy arrays containing 4 points |
||||
""" |
||||
squares = [] |
||||
storage = cv.CreateMemStorage(0) |
||||
contours = cv.FindContours(gray, storage, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, (0,0)) |
||||
storage = cv.CreateMemStorage(0) |
||||
while contours: |
||||
#approximate contour with accuracy proportional to the contour perimeter |
||||
arclength = cv.ArcLength(contours) |
||||
polygon = cv.ApproxPoly( contours, storage, cv.CV_POLY_APPROX_DP, arclength * 0.02, 0) |
||||
if is_square(polygon): |
||||
squares.append(polygon[0:4]) |
||||
contours = contours.h_next() |
||||
|
||||
return squares |
||||
|
||||
def find_squares4(color_img): |
||||
""" |
||||
Finds multiple squares in image |
||||
|
||||
Steps: |
||||
-Use Canny edge to highlight contours, and dilation to connect |
||||
the edge segments. |
||||
-Threshold the result to binary edge tokens |
||||
-Use cv.FindContours: returns a cv.CvSequence of cv.CvContours |
||||
-Filter each candidate: use Approx poly, keep only contours with 4 vertices, |
||||
enough area, and ~90deg angles. |
||||
|
||||
Return all squares contours in one flat list of arrays, 4 x,y points each. |
||||
""" |
||||
#select even sizes only |
||||
width, height = (color_img.width & -2, color_img.height & -2 ) |
||||
timg = cv.CloneImage( color_img ) # make a copy of input image |
||||
gray = cv.CreateImage( (width,height), 8, 1 ) |
||||
|
||||
# select the maximum ROI in the image |
||||
cv.SetImageROI( timg, (0, 0, width, height) ) |
||||
|
||||
# down-scale and upscale the image to filter out the noise |
||||
pyr = cv.CreateImage( (width/2, height/2), 8, 3 ) |
||||
cv.PyrDown( timg, pyr, 7 ) |
||||
cv.PyrUp( pyr, timg, 7 ) |
||||
|
||||
tgray = cv.CreateImage( (width,height), 8, 1 ) |
||||
squares = [] |
||||
|
||||
# Find squares in every color plane of the image |
||||
# Two methods, we use both: |
||||
# 1. Canny to catch squares with gradient shading. Use upper threshold |
||||
# from slider, set the lower to 0 (which forces edges merging). Then |
||||
# dilate canny output to remove potential holes between edge segments. |
||||
# 2. Binary thresholding at multiple levels |
||||
N = 11 |
||||
for c in [0, 1, 2]: |
||||
#extract the c-th color plane |
||||
cv.SetImageCOI( timg, c+1 ); |
||||
cv.Copy( timg, tgray, None ); |
||||
cv.Canny( tgray, gray, 0, 50, 5 ) |
||||
cv.Dilate( gray, gray) |
||||
squares = squares + find_squares_from_binary( gray ) |
||||
|
||||
# Look for more squares at several threshold levels |
||||
for l in range(1, N): |
||||
cv.Threshold( tgray, gray, (l+1)*255/N, 255, cv.CV_THRESH_BINARY ) |
||||
squares = squares + find_squares_from_binary( gray ) |
||||
|
||||
return squares |
||||
|
||||
|
||||
RED = (0,0,255) |
||||
GREEN = (0,255,0) |
||||
def draw_squares( color_img, squares ): |
||||
""" |
||||
Squares is py list containing 4-pt numpy arrays. Step through the list |
||||
and draw a polygon for each 4-group |
||||
""" |
||||
color, othercolor = RED, GREEN |
||||
for square in squares: |
||||
cv.PolyLine(color_img, [square], True, color, 3, cv.CV_AA, 0) |
||||
color, othercolor = othercolor, color |
||||
|
||||
cv.ShowImage(WNDNAME, color_img) |
||||
|
||||
|
||||
WNDNAME = "Squares Demo" |
||||
def main(): |
||||
"""Open test color images, create display window, start the search""" |
||||
cv.NamedWindow(WNDNAME, 1) |
||||
for name in [ "../c/pic%d.png" % i for i in [1, 2, 3, 4, 5, 6] ]: |
||||
img0 = cv.LoadImage(name, 1) |
||||
try: |
||||
img0 |
||||
except ValueError: |
||||
print "Couldn't load %s\n" % name |
||||
continue |
||||
|
||||
# slider deleted from C version, same here and use fixed Canny param=50 |
||||
img = cv.CloneImage(img0) |
||||
|
||||
cv.ShowImage(WNDNAME, img) |
||||
|
||||
# force the image processing |
||||
draw_squares( img, find_squares4( img ) ) |
||||
|
||||
# wait for key. |
||||
if cv.WaitKey(-1) % 0x100 == 27: |
||||
break |
||||
|
||||
if __name__ == "__main__": |
||||
main() |
||||
cv.DestroyAllWindows() |
@ -1,20 +0,0 @@ |
||||
#!/usr/bin/python |
||||
|
||||
import cv2.cv as cv |
||||
import urllib2 |
||||
from sys import argv |
||||
|
||||
def load_sample(name=None): |
||||
if len(argv) > 1: |
||||
img0 = cv.LoadImage(argv[1], cv.CV_LOAD_IMAGE_COLOR) |
||||
elif name is not None: |
||||
try: |
||||
img0 = cv.LoadImage(name, cv.CV_LOAD_IMAGE_COLOR) |
||||
except IOError: |
||||
urlbase = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/' |
||||
file = name.split('/')[-1] |
||||
filedata = urllib2.urlopen(urlbase+file).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
img0 = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
return img0 |
@ -1,139 +0,0 @@ |
||||
#!/usr/bin/python |
||||
""" |
||||
the script demostrates iterative construction of |
||||
delaunay triangulation and voronoi tesselation |
||||
|
||||
Original Author (C version): ? |
||||
Converted to Python by: Roman Stanchak |
||||
""" |
||||
import cv2.cv as cv |
||||
import random |
||||
|
||||
def draw_subdiv_point( img, fp, color ): |
||||
cv.Circle( img, (cv.Round(fp[0]), cv.Round(fp[1])), 3, color, cv.CV_FILLED, 8, 0 ); |
||||
|
||||
def draw_subdiv_edge( img, edge, color ): |
||||
org_pt = cv.Subdiv2DEdgeOrg(edge); |
||||
dst_pt = cv.Subdiv2DEdgeDst(edge); |
||||
|
||||
if org_pt and dst_pt : |
||||
|
||||
org = org_pt.pt; |
||||
dst = dst_pt.pt; |
||||
|
||||
iorg = ( cv.Round( org[0] ), cv.Round( org[1] )); |
||||
idst = ( cv.Round( dst[0] ), cv.Round( dst[1] )); |
||||
|
||||
cv.Line( img, iorg, idst, color, 1, cv.CV_AA, 0 ); |
||||
|
||||
|
||||
def draw_subdiv( img, subdiv, delaunay_color, voronoi_color ): |
||||
|
||||
for edge in subdiv.edges: |
||||
edge_rot = cv.Subdiv2DRotateEdge( edge, 1 ) |
||||
|
||||
draw_subdiv_edge( img, edge_rot, voronoi_color ); |
||||
draw_subdiv_edge( img, edge, delaunay_color ); |
||||
|
||||
|
||||
def locate_point( subdiv, fp, img, active_color ): |
||||
|
||||
(res, e0) = cv.Subdiv2DLocate( subdiv, fp ); |
||||
|
||||
if res in [ cv.CV_PTLOC_INSIDE, cv.CV_PTLOC_ON_EDGE ]: |
||||
e = e0 |
||||
while True: |
||||
draw_subdiv_edge( img, e, active_color ); |
||||
e = cv.Subdiv2DGetEdge(e, cv.CV_NEXT_AROUND_LEFT); |
||||
if e == e0: |
||||
break |
||||
|
||||
draw_subdiv_point( img, fp, active_color ); |
||||
|
||||
|
||||
def draw_subdiv_facet( img, edge ): |
||||
|
||||
t = edge; |
||||
count = 0; |
||||
|
||||
# count number of edges in facet |
||||
while count == 0 or t != edge: |
||||
count+=1 |
||||
t = cv.Subdiv2DGetEdge( t, cv.CV_NEXT_AROUND_LEFT ); |
||||
|
||||
buf = [] |
||||
|
||||
# gather points |
||||
t = edge; |
||||
for i in range(count): |
||||
assert t>4 |
||||
pt = cv.Subdiv2DEdgeOrg( t ); |
||||
if not pt: |
||||
break; |
||||
buf.append( ( cv.Round(pt.pt[0]), cv.Round(pt.pt[1]) ) ); |
||||
t = cv.Subdiv2DGetEdge( t, cv.CV_NEXT_AROUND_LEFT ); |
||||
|
||||
if( len(buf)==count ): |
||||
pt = cv.Subdiv2DEdgeDst( cv.Subdiv2DRotateEdge( edge, 1 )); |
||||
cv.FillConvexPoly( img, buf, cv.RGB(random.randrange(256),random.randrange(256),random.randrange(256)), cv.CV_AA, 0 ); |
||||
cv.PolyLine( img, [buf], 1, cv.RGB(0,0,0), 1, cv.CV_AA, 0); |
||||
draw_subdiv_point( img, pt.pt, cv.RGB(0,0,0)); |
||||
|
||||
def paint_voronoi( subdiv, img ): |
||||
|
||||
cv.CalcSubdivVoronoi2D( subdiv ); |
||||
|
||||
for edge in subdiv.edges: |
||||
|
||||
# left |
||||
draw_subdiv_facet( img, cv.Subdiv2DRotateEdge( edge, 1 )); |
||||
|
||||
# right |
||||
draw_subdiv_facet( img, cv.Subdiv2DRotateEdge( edge, 3 )); |
||||
|
||||
if __name__ == '__main__': |
||||
win = "source"; |
||||
rect = ( 0, 0, 600, 600 ); |
||||
|
||||
active_facet_color = cv.RGB( 255, 0, 0 ); |
||||
delaunay_color = cv.RGB( 0,0,0); |
||||
voronoi_color = cv.RGB(0, 180, 0); |
||||
bkgnd_color = cv.RGB(255,255,255); |
||||
|
||||
img = cv.CreateImage( (rect[2],rect[3]), 8, 3 ); |
||||
cv.Set( img, bkgnd_color ); |
||||
|
||||
cv.NamedWindow( win, 1 ); |
||||
|
||||
storage = cv.CreateMemStorage(0); |
||||
subdiv = cv.CreateSubdivDelaunay2D( rect, storage ); |
||||
|
||||
print "Delaunay triangulation will be build now interactively." |
||||
print "To stop the process, press any key\n"; |
||||
|
||||
for i in range(200): |
||||
fp = ( random.random()*(rect[2]-10)+5, random.random()*(rect[3]-10)+5 ) |
||||
|
||||
locate_point( subdiv, fp, img, active_facet_color ); |
||||
cv.ShowImage( win, img ); |
||||
|
||||
if( cv.WaitKey( 100 ) >= 0 ): |
||||
break; |
||||
|
||||
cv.SubdivDelaunay2DInsert( subdiv, fp ); |
||||
cv.CalcSubdivVoronoi2D( subdiv ); |
||||
cv.Set( img, bkgnd_color ); |
||||
draw_subdiv( img, subdiv, delaunay_color, voronoi_color ); |
||||
cv.ShowImage( win, img ); |
||||
|
||||
if( cv.WaitKey( 100 ) >= 0 ): |
||||
break; |
||||
|
||||
|
||||
cv.Set( img, bkgnd_color ); |
||||
paint_voronoi( subdiv, img ); |
||||
cv.ShowImage( win, img ); |
||||
|
||||
cv.WaitKey(0); |
||||
|
||||
cv.DestroyWindow( win ); |
@ -1,80 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import cv2.cv as cv |
||||
import sys |
||||
import urllib2 |
||||
|
||||
hist_size = 64 |
||||
range_0 = [0, 256] |
||||
ranges = [ range_0 ] |
||||
|
||||
class DemHist: |
||||
|
||||
def __init__(self, src_image): |
||||
self.src_image = src_image |
||||
self.dst_image = cv.CloneMat(src_image) |
||||
self.hist_image = cv.CreateImage((320, 200), 8, 1) |
||||
self.hist = cv.CreateHist([hist_size], cv.CV_HIST_ARRAY, ranges, 1) |
||||
|
||||
self.brightness = 0 |
||||
self.contrast = 0 |
||||
|
||||
cv.NamedWindow("image", 0) |
||||
cv.NamedWindow("histogram", 0) |
||||
cv.CreateTrackbar("brightness", "image", 100, 200, self.update_brightness) |
||||
cv.CreateTrackbar("contrast", "image", 100, 200, self.update_contrast) |
||||
|
||||
self.update_brightcont() |
||||
|
||||
def update_brightness(self, val): |
||||
self.brightness = val - 100 |
||||
self.update_brightcont() |
||||
|
||||
def update_contrast(self, val): |
||||
self.contrast = val - 100 |
||||
self.update_brightcont() |
||||
|
||||
def update_brightcont(self): |
||||
# The algorithm is by Werner D. Streidt |
||||
# (http://visca.com/ffactory/archives/5-99/msg00021.html) |
||||
|
||||
if self.contrast > 0: |
||||
delta = 127. * self.contrast / 100 |
||||
a = 255. / (255. - delta * 2) |
||||
b = a * (self.brightness - delta) |
||||
else: |
||||
delta = -128. * self.contrast / 100 |
||||
a = (256. - delta * 2) / 255. |
||||
b = a * self.brightness + delta |
||||
|
||||
cv.ConvertScale(self.src_image, self.dst_image, a, b) |
||||
cv.ShowImage("image", self.dst_image) |
||||
|
||||
cv.CalcArrHist([self.dst_image], self.hist) |
||||
(min_value, max_value, _, _) = cv.GetMinMaxHistValue(self.hist) |
||||
cv.Scale(self.hist.bins, self.hist.bins, float(self.hist_image.height) / max_value, 0) |
||||
|
||||
cv.Set(self.hist_image, cv.ScalarAll(255)) |
||||
bin_w = round(float(self.hist_image.width) / hist_size) |
||||
|
||||
for i in range(hist_size): |
||||
cv.Rectangle(self.hist_image, (int(i * bin_w), self.hist_image.height), |
||||
(int((i + 1) * bin_w), self.hist_image.height - cv.Round(self.hist.bins[i])), |
||||
cv.ScalarAll(0), -1, 8, 0) |
||||
|
||||
cv.ShowImage("histogram", self.hist_image) |
||||
|
||||
if __name__ == "__main__": |
||||
# Load the source image. |
||||
if len(sys.argv) > 1: |
||||
src_image = cv.GetMat(cv.LoadImage(sys.argv[1], 0)) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/baboon.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
src_image = cv.DecodeImageM(imagefiledata, 0) |
||||
|
||||
dh = DemHist(src_image) |
||||
|
||||
cv.WaitKey(0) |
||||
cv.DestroyAllWindows() |
@ -1,114 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import cv2.cv as cv |
||||
import sys |
||||
import urllib2 |
||||
|
||||
# Rearrange the quadrants of Fourier image so that the origin is at |
||||
# the image center |
||||
# src & dst arrays of equal size & type |
||||
def cvShiftDFT(src_arr, dst_arr ): |
||||
|
||||
size = cv.GetSize(src_arr) |
||||
dst_size = cv.GetSize(dst_arr) |
||||
|
||||
if dst_size != size: |
||||
cv.Error( cv.CV_StsUnmatchedSizes, "cv.ShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ ) |
||||
|
||||
if(src_arr is dst_arr): |
||||
tmp = cv.CreateMat(size[1]/2, size[0]/2, cv.GetElemType(src_arr)) |
||||
|
||||
cx = size[0] / 2 |
||||
cy = size[1] / 2 # image center |
||||
|
||||
q1 = cv.GetSubRect( src_arr, (0,0,cx, cy) ) |
||||
q2 = cv.GetSubRect( src_arr, (cx,0,cx,cy) ) |
||||
q3 = cv.GetSubRect( src_arr, (cx,cy,cx,cy) ) |
||||
q4 = cv.GetSubRect( src_arr, (0,cy,cx,cy) ) |
||||
d1 = cv.GetSubRect( src_arr, (0,0,cx,cy) ) |
||||
d2 = cv.GetSubRect( src_arr, (cx,0,cx,cy) ) |
||||
d3 = cv.GetSubRect( src_arr, (cx,cy,cx,cy) ) |
||||
d4 = cv.GetSubRect( src_arr, (0,cy,cx,cy) ) |
||||
|
||||
if(src_arr is not dst_arr): |
||||
if( not cv.CV_ARE_TYPES_EQ( q1, d1 )): |
||||
cv.Error( cv.CV_StsUnmatchedFormats, "cv.ShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ ) |
||||
|
||||
cv.Copy(q3, d1) |
||||
cv.Copy(q4, d2) |
||||
cv.Copy(q1, d3) |
||||
cv.Copy(q2, d4) |
||||
|
||||
else: |
||||
cv.Copy(q3, tmp) |
||||
cv.Copy(q1, q3) |
||||
cv.Copy(tmp, q1) |
||||
cv.Copy(q4, tmp) |
||||
cv.Copy(q2, q4) |
||||
cv.Copy(tmp, q2) |
||||
|
||||
if __name__ == "__main__": |
||||
|
||||
if len(sys.argv) > 1: |
||||
im = cv.LoadImage( sys.argv[1], cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/baboon.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
im = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
|
||||
realInput = cv.CreateImage( cv.GetSize(im), cv.IPL_DEPTH_64F, 1) |
||||
imaginaryInput = cv.CreateImage( cv.GetSize(im), cv.IPL_DEPTH_64F, 1) |
||||
complexInput = cv.CreateImage( cv.GetSize(im), cv.IPL_DEPTH_64F, 2) |
||||
|
||||
cv.Scale(im, realInput, 1.0, 0.0) |
||||
cv.Zero(imaginaryInput) |
||||
cv.Merge(realInput, imaginaryInput, None, None, complexInput) |
||||
|
||||
dft_M = cv.GetOptimalDFTSize( im.height - 1 ) |
||||
dft_N = cv.GetOptimalDFTSize( im.width - 1 ) |
||||
|
||||
dft_A = cv.CreateMat( dft_M, dft_N, cv.CV_64FC2 ) |
||||
image_Re = cv.CreateImage( (dft_N, dft_M), cv.IPL_DEPTH_64F, 1) |
||||
image_Im = cv.CreateImage( (dft_N, dft_M), cv.IPL_DEPTH_64F, 1) |
||||
|
||||
# copy A to dft_A and pad dft_A with zeros |
||||
tmp = cv.GetSubRect( dft_A, (0,0, im.width, im.height)) |
||||
cv.Copy( complexInput, tmp, None ) |
||||
if(dft_A.width > im.width): |
||||
tmp = cv.GetSubRect( dft_A, (im.width,0, dft_N - im.width, im.height)) |
||||
cv.Zero( tmp ) |
||||
|
||||
# no need to pad bottom part of dft_A with zeros because of |
||||
# use nonzero_rows parameter in cv.FT() call below |
||||
|
||||
cv.DFT( dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height ) |
||||
|
||||
cv.NamedWindow("win", 0) |
||||
cv.NamedWindow("magnitude", 0) |
||||
cv.ShowImage("win", im) |
||||
|
||||
# Split Fourier in real and imaginary parts |
||||
cv.Split( dft_A, image_Re, image_Im, None, None ) |
||||
|
||||
# Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2) |
||||
cv.Pow( image_Re, image_Re, 2.0) |
||||
cv.Pow( image_Im, image_Im, 2.0) |
||||
cv.Add( image_Re, image_Im, image_Re, None) |
||||
cv.Pow( image_Re, image_Re, 0.5 ) |
||||
|
||||
# Compute log(1 + Mag) |
||||
cv.AddS( image_Re, cv.ScalarAll(1.0), image_Re, None ) # 1 + Mag |
||||
cv.Log( image_Re, image_Re ) # log(1 + Mag) |
||||
|
||||
|
||||
# Rearrange the quadrants of Fourier image so that the origin is at |
||||
# the image center |
||||
cvShiftDFT( image_Re, image_Re ) |
||||
|
||||
min, max, pt1, pt2 = cv.MinMaxLoc(image_Re) |
||||
cv.Scale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min)) |
||||
cv.ShowImage("magnitude", image_Re) |
||||
|
||||
cv.WaitKey(0) |
||||
cv.DestroyAllWindows() |
@ -1,72 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import sys |
||||
import cv2.cv as cv |
||||
import urllib2 |
||||
|
||||
wndname = "Distance transform" |
||||
tbarname = "Threshold" |
||||
|
||||
# The output images |
||||
dist = 0 |
||||
dist8u1 = 0 |
||||
dist8u2 = 0 |
||||
dist8u = 0 |
||||
dist32s = 0 |
||||
|
||||
gray = 0 |
||||
edge = 0 |
||||
|
||||
# define a trackbar callback |
||||
def on_trackbar(edge_thresh): |
||||
|
||||
cv.Threshold(gray, edge, float(edge_thresh), float(edge_thresh), cv.CV_THRESH_BINARY) |
||||
#Distance transform |
||||
cv.DistTransform(edge, dist, cv.CV_DIST_L2, cv.CV_DIST_MASK_5) |
||||
|
||||
cv.ConvertScale(dist, dist, 5000.0, 0) |
||||
cv.Pow(dist, dist, 0.5) |
||||
|
||||
cv.ConvertScale(dist, dist32s, 1.0, 0.5) |
||||
cv.AndS(dist32s, cv.ScalarAll(255), dist32s, None) |
||||
cv.ConvertScale(dist32s, dist8u1, 1, 0) |
||||
cv.ConvertScale(dist32s, dist32s, -1, 0) |
||||
cv.AddS(dist32s, cv.ScalarAll(255), dist32s, None) |
||||
cv.ConvertScale(dist32s, dist8u2, 1, 0) |
||||
cv.Merge(dist8u1, dist8u2, dist8u2, None, dist8u) |
||||
cv.ShowImage(wndname, dist8u) |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
edge_thresh = 100 |
||||
|
||||
if len(sys.argv) > 1: |
||||
gray = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/stuff.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
gray = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
|
||||
# Create the output image |
||||
dist = cv.CreateImage((gray.width, gray.height), cv.IPL_DEPTH_32F, 1) |
||||
dist8u1 = cv.CloneImage(gray) |
||||
dist8u2 = cv.CloneImage(gray) |
||||
dist8u = cv.CreateImage((gray.width, gray.height), cv.IPL_DEPTH_8U, 3) |
||||
dist32s = cv.CreateImage((gray.width, gray.height), cv.IPL_DEPTH_32S, 1) |
||||
|
||||
# Convert to grayscale |
||||
edge = cv.CloneImage(gray) |
||||
|
||||
# Create a window |
||||
cv.NamedWindow(wndname, 1) |
||||
|
||||
# create a toolbar |
||||
cv.CreateTrackbar(tbarname, wndname, edge_thresh, 255, on_trackbar) |
||||
|
||||
# Show the image |
||||
on_trackbar(edge_thresh) |
||||
|
||||
# Wait for a key stroke; the same function arranges events processing |
||||
cv.WaitKey(0) |
||||
cv.DestroyAllWindows() |
@ -1,184 +0,0 @@ |
||||
#!/usr/bin/python |
||||
|
||||
import cv2.cv as cv |
||||
import time |
||||
from pydmtx import DataMatrix |
||||
import numpy |
||||
import sys |
||||
import math |
||||
|
||||
''' |
||||
Find 2 D barcode based on up to 3 channel datamatrix |
||||
''' |
||||
|
||||
def absnorm8(im, im8): |
||||
""" im may be any single-channel image type. Return an 8-bit version, absolute value, normalized so that max is 255 """ |
||||
(minVal, maxVal, _, _) = cv.MinMaxLoc(im) |
||||
cv.ConvertScaleAbs(im, im8, 255 / max(abs(minVal), abs(maxVal)), 0) |
||||
return im8 |
||||
|
||||
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, thickness = 2, lineType = cv.CV_AA) |
||||
if 0: |
||||
started = time.time() |
||||
print dm_write.decode(bg.width, bg.height, buffer(bg.tostring()), max_count = 1, min_edge = 12, max_edge = 13, shape = DataMatrix.DmtxSymbol10x10) # , timeout = 10) |
||||
print "took", time.time() - started |
||||
|
||||
class DmtxFinder: |
||||
def __init__(self): |
||||
self.cache = {} |
||||
self.dm = DataMatrix() |
||||
|
||||
def Cached(self, name, rows, cols, type): |
||||
key = (name, rows, cols) |
||||
if not key in self.cache: |
||||
self.cache[key] = cv.CreateMat(rows, cols, type) |
||||
return self.cache[key] |
||||
|
||||
def find0(self, img): |
||||
started = time.time() |
||||
self.dm.decode(img.width, |
||||
img.height, |
||||
buffer(img.tostring()), |
||||
max_count = 4, |
||||
#min_edge = 6, |
||||
#max_edge = 19 # Units of 2 pixels |
||||
) |
||||
print "brute", time.time() - started |
||||
found = {} |
||||
for i in range(self.dm.count()): |
||||
stats = dm_read.stats(i + 1) |
||||
print stats |
||||
found[stats[0]] = stats[1] |
||||
return found |
||||
|
||||
def find(self, img): |
||||
started = time.time() |
||||
gray = self.Cached('gray', img.height, img.width, cv.CV_8UC1) |
||||
cv.CvtColor(img, gray, cv.CV_BGR2GRAY) |
||||
|
||||
sobel = self.Cached('sobel', img.height, img.width, cv.CV_16SC1) |
||||
sobely = self.Cached('sobely', img.height, img.width, cv.CV_16SC1) |
||||
|
||||
cv.Sobel(gray, sobel, 1, 0) |
||||
cv.Sobel(gray, sobely, 0, 1) |
||||
cv.Add(sobel, sobely, sobel) |
||||
|
||||
sobel8 = self.Cached('sobel8', sobel.height, sobel.width, cv.CV_8UC1) |
||||
absnorm8(sobel, sobel8) |
||||
cv.Threshold(sobel8, sobel8, 128.0, 255.0, cv.CV_THRESH_BINARY) |
||||
|
||||
sobel_integral = self.Cached('sobel_integral', img.height + 1, img.width + 1, cv.CV_32SC1) |
||||
cv.Integral(sobel8, sobel_integral) |
||||
|
||||
d = 16 |
||||
_x1y1 = cv.GetSubRect(sobel_integral, (0, 0, sobel_integral.cols - d, sobel_integral.rows - d)) |
||||
_x1y2 = cv.GetSubRect(sobel_integral, (0, d, sobel_integral.cols - d, sobel_integral.rows - d)) |
||||
_x2y1 = cv.GetSubRect(sobel_integral, (d, 0, sobel_integral.cols - d, sobel_integral.rows - d)) |
||||
_x2y2 = cv.GetSubRect(sobel_integral, (d, d, sobel_integral.cols - d, sobel_integral.rows - d)) |
||||
|
||||
summation = cv.CloneMat(_x2y2) |
||||
cv.Sub(summation, _x1y2, summation) |
||||
cv.Sub(summation, _x2y1, summation) |
||||
cv.Add(summation, _x1y1, summation) |
||||
sum8 = self.Cached('sum8', summation.height, summation.width, cv.CV_8UC1) |
||||
absnorm8(summation, sum8) |
||||
cv.Threshold(sum8, sum8, 32.0, 255.0, cv.CV_THRESH_BINARY) |
||||
|
||||
cv.ShowImage("sum8", sum8) |
||||
seq = cv.FindContours(sum8, cv.CreateMemStorage(), cv.CV_RETR_EXTERNAL) |
||||
subimg = cv.GetSubRect(img, (d / 2, d / 2, sum8.cols, sum8.rows)) |
||||
t_cull = time.time() - started |
||||
|
||||
seqs = [] |
||||
while seq: |
||||
seqs.append(seq) |
||||
seq = seq.h_next() |
||||
|
||||
started = time.time() |
||||
found = {} |
||||
print 'seqs', len(seqs) |
||||
for seq in seqs: |
||||
area = cv.ContourArea(seq) |
||||
if area > 1000: |
||||
rect = cv.BoundingRect(seq) |
||||
edge = int((14 / 14.) * math.sqrt(area) / 2 + 0.5) |
||||
candidate = cv.GetSubRect(subimg, rect) |
||||
sym = self.dm.decode(candidate.width, |
||||
candidate.height, |
||||
buffer(candidate.tostring()), |
||||
max_count = 1, |
||||
#min_edge = 6, |
||||
#max_edge = int(edge) # Units of 2 pixels |
||||
) |
||||
if sym: |
||||
onscreen = [(d / 2 + rect[0] + x, d / 2 + rect[1] + y) for (x, y) in self.dm.stats(1)[1]] |
||||
found[sym] = onscreen |
||||
else: |
||||
print "FAILED" |
||||
t_brute = time.time() - started |
||||
print "cull took", t_cull, "brute", t_brute |
||||
return found |
||||
|
||||
bg = cv.CreateMat(1024, 1024, cv.CV_8UC3) |
||||
cv.Set(bg, cv.RGB(0, 0, 0)) |
||||
df = DmtxFinder() |
||||
|
||||
cv.NamedWindow("camera", 1) |
||||
|
||||
def mkdmtx(msg): |
||||
dm_write = DataMatrix() |
||||
dm_write.encode(msg) |
||||
pi = dm_write.image # .resize((14, 14)) |
||||
cv_im = cv.CreateImageHeader(pi.size, cv.IPL_DEPTH_8U, 3) |
||||
cv.SetData(cv_im, pi.tostring()) |
||||
return cv_im |
||||
|
||||
# test = [('WIL', (100,100))]: # , ('LOW', (250,100)), ('GAR', (300, 300)), ('AGE', (500, 300))]: |
||||
|
||||
test = [] |
||||
y = 10 |
||||
for j in range(7): |
||||
r = 28 + j * 4 |
||||
mr = r * math.sqrt(2) |
||||
y += mr * 1.8 |
||||
test += [(str(deg) + "abcdefgh"[j], (50 + deg * 11, y), math.pi * deg / 180, r) for deg in range(0, 90, 10)] |
||||
|
||||
for (msg, (x, y), angle, r) in test: |
||||
map = cv.CreateMat(2, 3, cv.CV_32FC1) |
||||
corners = [(x + r * math.cos(angle + th), y + r * math.sin(angle + th)) for th in [0, math.pi / 2, math.pi, 3 * math.pi / 4]] |
||||
src = mkdmtx(msg) |
||||
(sx, sy) = cv.GetSize(src) |
||||
cv.GetAffineTransform([(0,0), (sx, 0), (sx, sy)], corners[:3], map) |
||||
temp = cv.CreateMat(bg.rows, bg.cols, cv.CV_8UC3) |
||||
cv.Set(temp, cv.RGB(0, 0, 0)) |
||||
cv.WarpAffine(src, temp, map) |
||||
cv.Or(temp, bg, bg) |
||||
|
||||
|
||||
cv.ShowImage("comp", bg) |
||||
scribble = cv.CloneMat(bg) |
||||
|
||||
if 0: |
||||
for i in range(10): |
||||
df.find(bg) |
||||
|
||||
for (sym, coords) in df.find(bg).items(): |
||||
print sym |
||||
cv.PolyLine(scribble, [coords], 1, cv.CV_RGB(255, 0,0), 1, lineType = cv.CV_AA) |
||||
Xs = [x for (x, y) in coords] |
||||
Ys = [y for (x, y) in coords] |
||||
where = ((min(Xs) + max(Xs)) / 2, max(Ys) - 50) |
||||
cv.PutText(scribble, sym, where, font, cv.RGB(0,255, 0)) |
||||
|
||||
cv.ShowImage("results", scribble) |
||||
cv.WaitKey() |
||||
cv.DestroyAllWindows() |
||||
|
||||
sys.exit(0) |
||||
|
||||
capture = cv.CaptureFromCAM(0) |
||||
while True: |
||||
img = cv.QueryFrame(capture) |
||||
cv.ShowImage("capture", img) |
||||
print df.find(img) |
||||
cv.WaitKey(6) |
@ -1,162 +0,0 @@ |
||||
#! /usr/bin/env python |
||||
from random import Random |
||||
import colorsys |
||||
|
||||
print "OpenCV Python version of drawing" |
||||
|
||||
import cv2.cv as cv |
||||
|
||||
def random_color(random): |
||||
""" |
||||
Return a random color |
||||
""" |
||||
icolor = random.randint(0, 0xFFFFFF) |
||||
return cv.Scalar(icolor & 0xff, (icolor >> 8) & 0xff, (icolor >> 16) & 0xff) |
||||
|
||||
if __name__ == '__main__': |
||||
|
||||
# some "constants" |
||||
width = 1000 |
||||
height = 700 |
||||
window_name = "Drawing Demo" |
||||
number = 100 |
||||
delay = 5 |
||||
line_type = cv.CV_AA # change it to 8 to see non-antialiased graphics |
||||
|
||||
# create the source image |
||||
image = cv.CreateImage( (width, height), 8, 3) |
||||
|
||||
# create window and display the original picture in it |
||||
cv.NamedWindow(window_name, 1) |
||||
cv.SetZero(image) |
||||
cv.ShowImage(window_name, image) |
||||
|
||||
# create the random number |
||||
random = Random() |
||||
|
||||
# draw some lines |
||||
for i in range(number): |
||||
pt1 = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
pt2 = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
cv.Line(image, pt1, pt2, |
||||
random_color(random), |
||||
random.randrange(0, 10), |
||||
line_type, 0) |
||||
|
||||
cv.ShowImage(window_name, image) |
||||
cv.WaitKey(delay) |
||||
|
||||
# draw some rectangles |
||||
for i in range(number): |
||||
pt1 = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
pt2 = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
cv.Rectangle(image, pt1, pt2, |
||||
random_color(random), |
||||
random.randrange(-1, 9), |
||||
line_type, 0) |
||||
|
||||
cv.ShowImage(window_name, image) |
||||
cv.WaitKey(delay) |
||||
|
||||
# draw some ellipes |
||||
for i in range(number): |
||||
pt1 = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
sz = (random.randrange(0, 200), |
||||
random.randrange(0, 200)) |
||||
angle = random.randrange(0, 1000) * 0.180 |
||||
cv.Ellipse(image, pt1, sz, angle, angle - 100, angle + 200, |
||||
random_color(random), |
||||
random.randrange(-1, 9), |
||||
line_type, 0) |
||||
|
||||
cv.ShowImage(window_name, image) |
||||
cv.WaitKey(delay) |
||||
|
||||
# init the list of polylines |
||||
nb_polylines = 2 |
||||
polylines_size = 3 |
||||
pt = [0,] * nb_polylines |
||||
for a in range(nb_polylines): |
||||
pt [a] = [0,] * polylines_size |
||||
|
||||
# draw some polylines |
||||
for i in range(number): |
||||
for a in range(nb_polylines): |
||||
for b in range(polylines_size): |
||||
pt [a][b] = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
cv.PolyLine(image, pt, 1, |
||||
random_color(random), |
||||
random.randrange(1, 9), |
||||
line_type, 0) |
||||
|
||||
cv.ShowImage(window_name, image) |
||||
cv.WaitKey(delay) |
||||
|
||||
# draw some filled polylines |
||||
for i in range(number): |
||||
for a in range(nb_polylines): |
||||
for b in range(polylines_size): |
||||
pt [a][b] = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
cv.FillPoly(image, pt, |
||||
random_color(random), |
||||
line_type, 0) |
||||
|
||||
cv.ShowImage(window_name, image) |
||||
cv.WaitKey(delay) |
||||
|
||||
# draw some circles |
||||
for i in range(number): |
||||
pt1 = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
cv.Circle(image, pt1, random.randrange(0, 300), |
||||
random_color(random), |
||||
random.randrange(-1, 9), |
||||
line_type, 0) |
||||
|
||||
cv.ShowImage(window_name, image) |
||||
cv.WaitKey(delay) |
||||
|
||||
# draw some text |
||||
for i in range(number): |
||||
pt1 = (random.randrange(-width, 2 * width), |
||||
random.randrange(-height, 2 * height)) |
||||
font = cv.InitFont(random.randrange(0, 8), |
||||
random.randrange(0, 100) * 0.05 + 0.01, |
||||
random.randrange(0, 100) * 0.05 + 0.01, |
||||
random.randrange(0, 5) * 0.1, |
||||
random.randrange(0, 10), |
||||
line_type) |
||||
|
||||
cv.PutText(image, "Testing text rendering!", |
||||
pt1, font, |
||||
random_color(random)) |
||||
|
||||
cv.ShowImage(window_name, image) |
||||
cv.WaitKey(delay) |
||||
|
||||
# prepare a text, and get it's properties |
||||
font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, |
||||
3, 3, 0.0, 5, line_type) |
||||
text_size, ymin = cv.GetTextSize("OpenCV forever!", font) |
||||
pt1 = ((width - text_size[0]) / 2, (height + text_size[1]) / 2) |
||||
image2 = cv.CloneImage(image) |
||||
|
||||
# now, draw some OpenCV pub ;-) |
||||
for i in range(0, 512, 2): |
||||
cv.SubS(image2, cv.ScalarAll(i), image) |
||||
(r, g, b) = colorsys.hsv_to_rgb((i % 100) / 100., 1, 1) |
||||
cv.PutText(image, "OpenCV forever!", |
||||
pt1, font, cv.RGB(255 * r, 255 * g, 255 * b)) |
||||
cv.ShowImage(window_name, image) |
||||
cv.WaitKey(delay) |
||||
|
||||
# wait some key to end |
||||
cv.WaitKey(0) |
||||
cv.DestroyAllWindows() |
@ -1,60 +0,0 @@ |
||||
#! /usr/bin/env python |
||||
|
||||
print "OpenCV Python version of edge" |
||||
|
||||
import sys |
||||
import urllib2 |
||||
import cv2.cv as cv |
||||
|
||||
# some definitions |
||||
win_name = "Edge" |
||||
trackbar_name = "Threshold" |
||||
|
||||
# the callback on the trackbar |
||||
def on_trackbar(position): |
||||
|
||||
cv.Smooth(gray, edge, cv.CV_BLUR, 3, 3, 0) |
||||
cv.Not(gray, edge) |
||||
|
||||
# run the edge dector on gray scale |
||||
cv.Canny(gray, edge, position, position * 3, 3) |
||||
|
||||
# reset |
||||
cv.SetZero(col_edge) |
||||
|
||||
# copy edge points |
||||
cv.Copy(im, col_edge, edge) |
||||
|
||||
# show the im |
||||
cv.ShowImage(win_name, col_edge) |
||||
|
||||
if __name__ == '__main__': |
||||
if len(sys.argv) > 1: |
||||
im = cv.LoadImage( sys.argv[1], cv.CV_LOAD_IMAGE_COLOR) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/fruits.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
im = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
|
||||
# create the output im |
||||
col_edge = cv.CreateImage((im.width, im.height), 8, 3) |
||||
|
||||
# convert to grayscale |
||||
gray = cv.CreateImage((im.width, im.height), 8, 1) |
||||
edge = cv.CreateImage((im.width, im.height), 8, 1) |
||||
cv.CvtColor(im, gray, cv.CV_BGR2GRAY) |
||||
|
||||
# create the window |
||||
cv.NamedWindow(win_name, cv.CV_WINDOW_AUTOSIZE) |
||||
|
||||
# create the trackbar |
||||
cv.CreateTrackbar(trackbar_name, win_name, 1, 100, on_trackbar) |
||||
|
||||
# show the im |
||||
on_trackbar(0) |
||||
|
||||
# wait a key pressed to end |
||||
cv.WaitKey(0) |
||||
cv.DestroyAllWindows() |
@ -1,101 +0,0 @@ |
||||
#!/usr/bin/python |
||||
""" |
||||
This program is demonstration for face and object detection using haar-like features. |
||||
The program finds faces in a camera image or video stream and displays a red box around them. |
||||
|
||||
Original C implementation by: ? |
||||
Python implementation by: Roman Stanchak, James Bowman |
||||
""" |
||||
import sys |
||||
import cv2.cv as cv |
||||
from optparse import OptionParser |
||||
|
||||
# Parameters for haar detection |
||||
# From the API: |
||||
# The default parameters (scale_factor=2, min_neighbors=3, flags=0) are tuned |
||||
# for accurate yet slow object detection. For a faster operation on real video |
||||
# images the settings are: |
||||
# scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING, |
||||
# min_size=<minimum possible face size |
||||
|
||||
min_size = (20, 20) |
||||
image_scale = 2 |
||||
haar_scale = 1.2 |
||||
min_neighbors = 2 |
||||
haar_flags = 0 |
||||
|
||||
def detect_and_draw(img, cascade): |
||||
# allocate temporary images |
||||
gray = cv.CreateImage((img.width,img.height), 8, 1) |
||||
small_img = cv.CreateImage((cv.Round(img.width / image_scale), |
||||
cv.Round (img.height / image_scale)), 8, 1) |
||||
|
||||
# convert color input image to grayscale |
||||
cv.CvtColor(img, gray, cv.CV_BGR2GRAY) |
||||
|
||||
# scale input image for faster processing |
||||
cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) |
||||
|
||||
cv.EqualizeHist(small_img, small_img) |
||||
|
||||
if(cascade): |
||||
t = cv.GetTickCount() |
||||
faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), |
||||
haar_scale, min_neighbors, haar_flags, min_size) |
||||
t = cv.GetTickCount() - t |
||||
print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.)) |
||||
if faces: |
||||
for ((x, y, w, h), n) in faces: |
||||
# the input to cv.HaarDetectObjects was resized, so scale the |
||||
# bounding box of each face and convert it to two CvPoints |
||||
pt1 = (int(x * image_scale), int(y * image_scale)) |
||||
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) |
||||
cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) |
||||
|
||||
cv.ShowImage("result", img) |
||||
|
||||
if __name__ == '__main__': |
||||
|
||||
parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]") |
||||
parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml") |
||||
(options, args) = parser.parse_args() |
||||
|
||||
cascade = cv.Load(options.cascade) |
||||
|
||||
if len(args) != 1: |
||||
parser.print_help() |
||||
sys.exit(1) |
||||
|
||||
input_name = args[0] |
||||
if input_name.isdigit(): |
||||
capture = cv.CreateCameraCapture(int(input_name)) |
||||
else: |
||||
capture = None |
||||
|
||||
cv.NamedWindow("result", 1) |
||||
|
||||
if capture: |
||||
frame_copy = None |
||||
while True: |
||||
frame = cv.QueryFrame(capture) |
||||
if not frame: |
||||
cv.WaitKey(0) |
||||
break |
||||
if not frame_copy: |
||||
frame_copy = cv.CreateImage((frame.width,frame.height), |
||||
cv.IPL_DEPTH_8U, frame.nChannels) |
||||
if frame.origin == cv.IPL_ORIGIN_TL: |
||||
cv.Copy(frame, frame_copy) |
||||
else: |
||||
cv.Flip(frame, frame_copy, 0) |
||||
|
||||
detect_and_draw(frame_copy, cascade) |
||||
|
||||
if cv.WaitKey(10) >= 0: |
||||
break |
||||
else: |
||||
image = cv.LoadImage(input_name, 1) |
||||
detect_and_draw(image, cascade) |
||||
cv.WaitKey(0) |
||||
|
||||
cv.DestroyWindow("result") |
@ -1,56 +0,0 @@ |
||||
#!/usr/bin/env python |
||||
|
||||
from cv import * |
||||
|
||||
class FBackDemo: |
||||
def __init__(self): |
||||
self.capture = CaptureFromCAM(0) |
||||
self.mv_step = 16 |
||||
self.mv_scale = 1.5 |
||||
self.mv_color = (0, 255, 0) |
||||
self.cflow = None |
||||
self.flow = None |
||||
|
||||
NamedWindow( "Optical Flow", 1 ) |
||||
|
||||
print( "Press ESC - quit the program\n" ) |
||||
|
||||
def draw_flow(self, flow, prevgray): |
||||
""" Returns a nice representation of a hue histogram """ |
||||
|
||||
CvtColor(prevgray, self.cflow, CV_GRAY2BGR) |
||||
for y in range(0, flow.height, self.mv_step): |
||||
for x in range(0, flow.width, self.mv_step): |
||||
fx, fy = flow[y, x] |
||||
Line(self.cflow, (x,y), (x+fx,y+fy), self.mv_color) |
||||
Circle(self.cflow, (x,y), 2, self.mv_color, -1) |
||||
ShowImage("Optical Flow", self.cflow) |
||||
|
||||
def run(self): |
||||
first_frame = True |
||||
|
||||
while True: |
||||
frame = QueryFrame( self.capture ) |
||||
|
||||
if first_frame: |
||||
gray = CreateImage(GetSize(frame), 8, 1) |
||||
prev_gray = CreateImage(GetSize(frame), 8, 1) |
||||
flow = CreateImage(GetSize(frame), 32, 2) |
||||
self.cflow = CreateImage(GetSize(frame), 8, 3) |
||||
|
||||
CvtColor(frame, gray, CV_BGR2GRAY) |
||||
if not first_frame: |
||||
CalcOpticalFlowFarneback(prev_gray, gray, flow, |
||||
pyr_scale=0.5, levels=3, winsize=15, |
||||
iterations=3, poly_n=5, poly_sigma=1.2, flags=0) |
||||
self.draw_flow(flow, prev_gray) |
||||
c = WaitKey(7) |
||||
if c in [27, ord('q'), ord('Q')]: |
||||
break |
||||
prev_gray, gray = gray, prev_gray |
||||
first_frame = False |
||||
|
||||
if __name__=="__main__": |
||||
demo = FBackDemo() |
||||
demo.run() |
||||
cv.DestroyAllWindows() |
@ -1,160 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import sys |
||||
import random |
||||
import urllib2 |
||||
import cv2.cv as cv |
||||
|
||||
im=None; |
||||
mask=None; |
||||
color_img=None; |
||||
gray_img0 = None; |
||||
gray_img = None; |
||||
ffill_case = 1; |
||||
lo_diff = 20 |
||||
up_diff = 20; |
||||
connectivity = 4; |
||||
is_color = 1; |
||||
is_mask = 0; |
||||
new_mask_val = 255; |
||||
|
||||
def update_lo( pos ): |
||||
lo_diff = pos |
||||
def update_up( pos ): |
||||
up_diff = pos |
||||
|
||||
def on_mouse( event, x, y, flags, param ): |
||||
|
||||
if( not color_img ): |
||||
return; |
||||
|
||||
if event == cv.CV_EVENT_LBUTTONDOWN: |
||||
my_mask = None |
||||
seed = (x,y); |
||||
if ffill_case==0: |
||||
lo = up = 0 |
||||
flags = connectivity + (new_mask_val << 8) |
||||
else: |
||||
lo = lo_diff; |
||||
up = up_diff; |
||||
flags = connectivity + (new_mask_val << 8) + cv.CV_FLOODFILL_FIXED_RANGE |
||||
b = random.randint(0,255) |
||||
g = random.randint(0,255) |
||||
r = random.randint(0,255) |
||||
|
||||
if( is_mask ): |
||||
my_mask = mask |
||||
cv.Threshold( mask, mask, 1, 128, cv.CV_THRESH_BINARY ); |
||||
|
||||
if( is_color ): |
||||
|
||||
color = cv.CV_RGB( r, g, b ); |
||||
comp = cv.FloodFill( color_img, seed, color, cv.CV_RGB( lo, lo, lo ), |
||||
cv.CV_RGB( up, up, up ), flags, my_mask ); |
||||
cv.ShowImage( "image", color_img ); |
||||
|
||||
else: |
||||
|
||||
brightness = cv.RealScalar((r*2 + g*7 + b + 5)/10); |
||||
comp = cv.FloodFill( gray_img, seed, brightness, cv.RealScalar(lo), |
||||
cv.RealScalar(up), flags, my_mask ); |
||||
cv.ShowImage( "image", gray_img ); |
||||
|
||||
|
||||
print "%g pixels were repainted" % comp[0] |
||||
|
||||
if( is_mask ): |
||||
cv.ShowImage( "mask", mask ); |
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
|
||||
if len(sys.argv) > 1: |
||||
im = cv.LoadImage( sys.argv[1], cv.CV_LOAD_IMAGE_COLOR) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/fruits.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
im = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
|
||||
print "Hot keys:" |
||||
print "\tESC - quit the program" |
||||
print "\tc - switch color/grayscale mode" |
||||
print "\tm - switch mask mode" |
||||
print "\tr - restore the original image" |
||||
print "\ts - use null-range floodfill" |
||||
print "\tf - use gradient floodfill with fixed(absolute) range" |
||||
print "\tg - use gradient floodfill with floating(relative) range" |
||||
print "\t4 - use 4-connectivity mode" |
||||
print "\t8 - use 8-connectivity mode" |
||||
|
||||
color_img = cv.CloneImage( im ); |
||||
gray_img0 = cv.CreateImage( (color_img.width, color_img.height), 8, 1 ); |
||||
cv.CvtColor( color_img, gray_img0, cv.CV_BGR2GRAY ); |
||||
gray_img = cv.CloneImage( gray_img0 ); |
||||
mask = cv.CreateImage( (color_img.width + 2, color_img.height + 2), 8, 1 ); |
||||
|
||||
cv.NamedWindow( "image", 1 ); |
||||
cv.CreateTrackbar( "lo_diff", "image", lo_diff, 255, update_lo); |
||||
cv.CreateTrackbar( "up_diff", "image", up_diff, 255, update_up); |
||||
|
||||
cv.SetMouseCallback( "image", on_mouse ); |
||||
|
||||
while True: |
||||
if( is_color ): |
||||
cv.ShowImage( "image", color_img ); |
||||
else: |
||||
cv.ShowImage( "image", gray_img ); |
||||
|
||||
c = cv.WaitKey(0) % 0x100 |
||||
if c == 27: |
||||
print("Exiting ..."); |
||||
sys.exit(0) |
||||
elif c == ord('c'): |
||||
if( is_color ): |
||||
|
||||
print("Grayscale mode is set"); |
||||
cv.CvtColor( color_img, gray_img, cv.CV_BGR2GRAY ); |
||||
is_color = 0; |
||||
|
||||
else: |
||||
|
||||
print("Color mode is set"); |
||||
cv.Copy( im, color_img, None ); |
||||
cv.Zero( mask ); |
||||
is_color = 1; |
||||
|
||||
elif c == ord('m'): |
||||
if( is_mask ): |
||||
cv.DestroyWindow( "mask" ); |
||||
is_mask = 0; |
||||
|
||||
else: |
||||
cv.NamedWindow( "mask", 0 ); |
||||
cv.Zero( mask ); |
||||
cv.ShowImage( "mask", mask ); |
||||
is_mask = 1; |
||||
|
||||
elif c == ord('r'): |
||||
print("Original image is restored"); |
||||
cv.Copy( im, color_img, None ); |
||||
cv.Copy( gray_img0, gray_img, None ); |
||||
cv.Zero( mask ); |
||||
elif c == ord('s'): |
||||
print("Simple floodfill mode is set"); |
||||
ffill_case = 0; |
||||
elif c == ord('f'): |
||||
print("Fixed Range floodfill mode is set"); |
||||
ffill_case = 1; |
||||
elif c == ord('g'): |
||||
print("Gradient (floating range) floodfill mode is set"); |
||||
ffill_case = 2; |
||||
elif c == ord('4'): |
||||
print("4-connectivity mode is set"); |
||||
connectivity = 4; |
||||
elif c == ord('8'): |
||||
print("8-connectivity mode is set"); |
||||
connectivity = 8; |
||||
cv.DestroyAllWindows() |
@ -1,104 +0,0 @@ |
||||
#!/usr/bin/python |
||||
""" |
||||
This program is a demonstration of ellipse fitting. |
||||
|
||||
Trackbar controls threshold parameter. |
||||
|
||||
Gray lines are contours. Colored lines are fit ellipses. |
||||
|
||||
Original C implementation by: Denis Burenkov. |
||||
Python implementation by: Roman Stanchak, James Bowman |
||||
""" |
||||
|
||||
import sys |
||||
import urllib2 |
||||
import random |
||||
import cv2.cv as cv |
||||
|
||||
def contour_iterator(contour): |
||||
while contour: |
||||
yield contour |
||||
contour = contour.h_next() |
||||
|
||||
class FitEllipse: |
||||
|
||||
def __init__(self, source_image, slider_pos): |
||||
self.source_image = source_image |
||||
cv.CreateTrackbar("Threshold", "Result", slider_pos, 255, self.process_image) |
||||
self.process_image(slider_pos) |
||||
|
||||
def process_image(self, slider_pos): |
||||
""" |
||||
This function finds contours, draws them and their approximation by ellipses. |
||||
""" |
||||
stor = cv.CreateMemStorage() |
||||
|
||||
# Create the destination images |
||||
image02 = cv.CloneImage(self.source_image) |
||||
cv.Zero(image02) |
||||
image04 = cv.CreateImage(cv.GetSize(self.source_image), cv.IPL_DEPTH_8U, 3) |
||||
cv.Zero(image04) |
||||
|
||||
# Threshold the source image. This needful for cv.FindContours(). |
||||
cv.Threshold(self.source_image, image02, slider_pos, 255, cv.CV_THRESH_BINARY) |
||||
|
||||
# Find all contours. |
||||
cont = cv.FindContours(image02, |
||||
stor, |
||||
cv.CV_RETR_LIST, |
||||
cv.CV_CHAIN_APPROX_NONE, |
||||
(0, 0)) |
||||
|
||||
for c in contour_iterator(cont): |
||||
# Number of points must be more than or equal to 6 for cv.FitEllipse2 |
||||
if len(c) >= 6: |
||||
# Copy the contour into an array of (x,y)s |
||||
PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2) |
||||
for (i, (x, y)) in enumerate(c): |
||||
PointArray2D32f[0, i] = (x, y) |
||||
|
||||
# Draw the current contour in gray |
||||
gray = cv.CV_RGB(100, 100, 100) |
||||
cv.DrawContours(image04, c, gray, gray,0,1,8,(0,0)) |
||||
|
||||
# Fits ellipse to current contour. |
||||
(center, size, angle) = cv.FitEllipse2(PointArray2D32f) |
||||
|
||||
# Convert ellipse data from float to integer representation. |
||||
center = (cv.Round(center[0]), cv.Round(center[1])) |
||||
size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5)) |
||||
|
||||
# Draw ellipse in random color |
||||
color = cv.CV_RGB(random.randrange(256),random.randrange(256),random.randrange(256)) |
||||
cv.Ellipse(image04, center, size, |
||||
angle, 0, 360, |
||||
color, 2, cv.CV_AA, 0) |
||||
|
||||
# Show image. HighGUI use. |
||||
cv.ShowImage( "Result", image04 ) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
if len(sys.argv) > 1: |
||||
source_image = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/stuff.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
source_image = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
|
||||
# Create windows. |
||||
cv.NamedWindow("Source", 1) |
||||
cv.NamedWindow("Result", 1) |
||||
|
||||
# Show the image. |
||||
cv.ShowImage("Source", source_image) |
||||
|
||||
fe = FitEllipse(source_image, 70) |
||||
|
||||
print "Press any key to exit" |
||||
cv.WaitKey(0) |
||||
|
||||
cv.DestroyWindow("Source") |
||||
cv.DestroyWindow("Result") |
@ -1,58 +0,0 @@ |
||||
#!/usr/bin/python |
||||
# This is a standalone program. Pass an image name as a first parameter of the program. |
||||
|
||||
import sys |
||||
from math import sin, cos, sqrt, pi |
||||
import cv2.cv as cv |
||||
import urllib2 |
||||
|
||||
# toggle between CV_HOUGH_STANDARD and CV_HOUGH_PROBILISTIC |
||||
USE_STANDARD = True |
||||
|
||||
if __name__ == "__main__": |
||||
if len(sys.argv) > 1: |
||||
filename = sys.argv[1] |
||||
src = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/doc/pics/building.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
src = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE) |
||||
|
||||
|
||||
cv.NamedWindow("Source", 1) |
||||
cv.NamedWindow("Hough", 1) |
||||
|
||||
while True: |
||||
dst = cv.CreateImage(cv.GetSize(src), 8, 1) |
||||
color_dst = cv.CreateImage(cv.GetSize(src), 8, 3) |
||||
storage = cv.CreateMemStorage(0) |
||||
lines = 0 |
||||
cv.Canny(src, dst, 50, 200, 3) |
||||
cv.CvtColor(dst, color_dst, cv.CV_GRAY2BGR) |
||||
|
||||
if USE_STANDARD: |
||||
lines = cv.HoughLines2(dst, storage, cv.CV_HOUGH_STANDARD, 1, pi / 180, 100, 0, 0) |
||||
for (rho, theta) in lines[:100]: |
||||
a = cos(theta) |
||||
b = sin(theta) |
||||
x0 = a * rho |
||||
y0 = b * rho |
||||
pt1 = (cv.Round(x0 + 1000*(-b)), cv.Round(y0 + 1000*(a))) |
||||
pt2 = (cv.Round(x0 - 1000*(-b)), cv.Round(y0 - 1000*(a))) |
||||
cv.Line(color_dst, pt1, pt2, cv.RGB(255, 0, 0), 3, 8) |
||||
else: |
||||
lines = cv.HoughLines2(dst, storage, cv.CV_HOUGH_PROBABILISTIC, 1, pi / 180, 50, 50, 10) |
||||
for line in lines: |
||||
cv.Line(color_dst, line[0], line[1], cv.CV_RGB(255, 0, 0), 3, 8) |
||||
|
||||
cv.ShowImage("Source", src) |
||||
cv.ShowImage("Hough", color_dst) |
||||
|
||||
k = cv.WaitKey(0) % 0x100 |
||||
if k == ord(' '): |
||||
USE_STANDARD = not USE_STANDARD |
||||
if k == 27: |
||||
break |
||||
cv.DestroyAllWindows() |
@ -1,69 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import urllib2 |
||||
import sys |
||||
import cv2.cv as cv |
||||
|
||||
class Sketcher: |
||||
def __init__(self, windowname, dests): |
||||
self.prev_pt = None |
||||
self.windowname = windowname |
||||
self.dests = dests |
||||
cv.SetMouseCallback(self.windowname, self.on_mouse) |
||||
|
||||
def on_mouse(self, event, x, y, flags, param): |
||||
pt = (x, y) |
||||
if event == cv.CV_EVENT_LBUTTONUP or not (flags & cv.CV_EVENT_FLAG_LBUTTON): |
||||
self.prev_pt = None |
||||
elif event == cv.CV_EVENT_LBUTTONDOWN: |
||||
self.prev_pt = pt |
||||
elif event == cv.CV_EVENT_MOUSEMOVE and (flags & cv.CV_EVENT_FLAG_LBUTTON) : |
||||
if self.prev_pt: |
||||
for dst in self.dests: |
||||
cv.Line(dst, self.prev_pt, pt, cv.ScalarAll(255), 5, 8, 0) |
||||
self.prev_pt = pt |
||||
cv.ShowImage(self.windowname, img) |
||||
|
||||
if __name__=="__main__": |
||||
if len(sys.argv) > 1: |
||||
img0 = cv.LoadImage( sys.argv[1], cv.CV_LOAD_IMAGE_COLOR) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/fruits.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
img0 = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
|
||||
print "Hot keys:" |
||||
print "\tESC - quit the program" |
||||
print "\tr - restore the original image" |
||||
print "\ti or ENTER - run inpainting algorithm" |
||||
print "\t\t(before running it, paint something on the image)" |
||||
|
||||
cv.NamedWindow("image", 1) |
||||
cv.NamedWindow("inpainted image", 1) |
||||
|
||||
img = cv.CloneImage(img0) |
||||
inpainted = cv.CloneImage(img0) |
||||
inpaint_mask = cv.CreateImage(cv.GetSize(img), 8, 1) |
||||
|
||||
cv.Zero(inpaint_mask) |
||||
cv.Zero(inpainted) |
||||
cv.ShowImage("image", img) |
||||
cv.ShowImage("inpainted image", inpainted) |
||||
|
||||
sk = Sketcher("image", [img, inpaint_mask]) |
||||
while True: |
||||
c = cv.WaitKey(0) % 0x100 |
||||
|
||||
if c == 27 or c == ord('q'): |
||||
break |
||||
|
||||
if c == ord('r'): |
||||
cv.Zero(inpaint_mask) |
||||
cv.Copy(img0, img) |
||||
cv.ShowImage("image", img) |
||||
|
||||
if c == ord('i') or c == ord('\n'): |
||||
cv.Inpaint(img, inpaint_mask, inpainted, 3, cv.CV_INPAINT_TELEA) |
||||
cv.ShowImage("inpainted image", inpainted) |
||||
cv.DestroyAllWindows() |
@ -1,98 +0,0 @@ |
||||
#!/usr/bin/python |
||||
""" |
||||
Tracking of rotating point. |
||||
Rotation speed is constant. |
||||
Both state and measurements vectors are 1D (a point angle), |
||||
Measurement is the real point angle + gaussian noise. |
||||
The real and the estimated points are connected with yellow line segment, |
||||
the real and the measured points are connected with red line segment. |
||||
(if Kalman filter works correctly, |
||||
the yellow segment should be shorter than the red one). |
||||
Pressing any key (except ESC) will reset the tracking with a different speed. |
||||
Pressing ESC will stop the program. |
||||
""" |
||||
import urllib2 |
||||
import cv2.cv as cv |
||||
from math import cos, sin, sqrt |
||||
import sys |
||||
|
||||
if __name__ == "__main__": |
||||
A = [ [1, 1], [0, 1] ] |
||||
|
||||
img = cv.CreateImage((500, 500), 8, 3) |
||||
kalman = cv.CreateKalman(2, 1, 0) |
||||
state = cv.CreateMat(2, 1, cv.CV_32FC1) # (phi, delta_phi) |
||||
process_noise = cv.CreateMat(2, 1, cv.CV_32FC1) |
||||
measurement = cv.CreateMat(1, 1, cv.CV_32FC1) |
||||
rng = cv.RNG(-1) |
||||
code = -1L |
||||
|
||||
cv.Zero(measurement) |
||||
cv.NamedWindow("Kalman", 1) |
||||
|
||||
while True: |
||||
cv.RandArr(rng, state, cv.CV_RAND_NORMAL, cv.RealScalar(0), cv.RealScalar(0.1)) |
||||
|
||||
kalman.transition_matrix[0,0] = 1 |
||||
kalman.transition_matrix[0,1] = 1 |
||||
kalman.transition_matrix[1,0] = 0 |
||||
kalman.transition_matrix[1,1] = 1 |
||||
|
||||
cv.SetIdentity(kalman.measurement_matrix, cv.RealScalar(1)) |
||||
cv.SetIdentity(kalman.process_noise_cov, cv.RealScalar(1e-5)) |
||||
cv.SetIdentity(kalman.measurement_noise_cov, cv.RealScalar(1e-1)) |
||||
cv.SetIdentity(kalman.error_cov_post, cv.RealScalar(1)) |
||||
cv.RandArr(rng, kalman.state_post, cv.CV_RAND_NORMAL, cv.RealScalar(0), cv.RealScalar(0.1)) |
||||
|
||||
|
||||
while True: |
||||
def calc_point(angle): |
||||
return (cv.Round(img.width/2 + img.width/3*cos(angle)), |
||||
cv.Round(img.height/2 - img.width/3*sin(angle))) |
||||
|
||||
state_angle = state[0,0] |
||||
state_pt = calc_point(state_angle) |
||||
|
||||
prediction = cv.KalmanPredict(kalman) |
||||
predict_angle = prediction[0, 0] |
||||
predict_pt = calc_point(predict_angle) |
||||
|
||||
cv.RandArr(rng, measurement, cv.CV_RAND_NORMAL, cv.RealScalar(0), |
||||
cv.RealScalar(sqrt(kalman.measurement_noise_cov[0, 0]))) |
||||
|
||||
# generate measurement |
||||
cv.MatMulAdd(kalman.measurement_matrix, state, measurement, measurement) |
||||
|
||||
measurement_angle = measurement[0, 0] |
||||
measurement_pt = calc_point(measurement_angle) |
||||
|
||||
# plot points |
||||
def draw_cross(center, color, d): |
||||
cv.Line(img, (center[0] - d, center[1] - d), |
||||
(center[0] + d, center[1] + d), color, 1, cv.CV_AA, 0) |
||||
cv.Line(img, (center[0] + d, center[1] - d), |
||||
(center[0] - d, center[1] + d), color, 1, cv.CV_AA, 0) |
||||
|
||||
cv.Zero(img) |
||||
draw_cross(state_pt, cv.CV_RGB(255, 255, 255), 3) |
||||
draw_cross(measurement_pt, cv.CV_RGB(255, 0,0), 3) |
||||
draw_cross(predict_pt, cv.CV_RGB(0, 255, 0), 3) |
||||
cv.Line(img, state_pt, measurement_pt, cv.CV_RGB(255, 0,0), 3, cv. CV_AA, 0) |
||||
cv.Line(img, state_pt, predict_pt, cv.CV_RGB(255, 255, 0), 3, cv. CV_AA, 0) |
||||
|
||||
cv.KalmanCorrect(kalman, measurement) |
||||
|
||||
cv.RandArr(rng, process_noise, cv.CV_RAND_NORMAL, cv.RealScalar(0), |
||||
cv.RealScalar(sqrt(kalman.process_noise_cov[0, 0]))) |
||||
cv.MatMulAdd(kalman.transition_matrix, state, process_noise, state) |
||||
|
||||
cv.ShowImage("Kalman", img) |
||||
|
||||
code = cv.WaitKey(100) % 0x100 |
||||
if code != -1: |
||||
break |
||||
|
||||
if code in [27, ord('q'), ord('Q')]: |
||||
break |
||||
|
||||
cv.DestroyWindow("Kalman") |
@ -1,60 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import urllib2 |
||||
import cv2.cv as cv |
||||
from random import randint |
||||
MAX_CLUSTERS = 5 |
||||
|
||||
if __name__ == "__main__": |
||||
|
||||
color_tab = [ |
||||
cv.CV_RGB(255, 0,0), |
||||
cv.CV_RGB(0, 255, 0), |
||||
cv.CV_RGB(100, 100, 255), |
||||
cv.CV_RGB(255, 0,255), |
||||
cv.CV_RGB(255, 255, 0)] |
||||
img = cv.CreateImage((500, 500), 8, 3) |
||||
rng = cv.RNG(-1) |
||||
|
||||
cv.NamedWindow("clusters", 1) |
||||
|
||||
while True: |
||||
cluster_count = randint(2, MAX_CLUSTERS) |
||||
sample_count = randint(1, 1000) |
||||
points = cv.CreateMat(sample_count, 1, cv.CV_32FC2) |
||||
clusters = cv.CreateMat(sample_count, 1, cv.CV_32SC1) |
||||
|
||||
# generate random sample from multigaussian distribution |
||||
for k in range(cluster_count): |
||||
center = (cv.RandInt(rng)%img.width, cv.RandInt(rng)%img.height) |
||||
first = k*sample_count/cluster_count |
||||
last = sample_count |
||||
if k != cluster_count: |
||||
last = (k+1)*sample_count/cluster_count |
||||
|
||||
point_chunk = cv.GetRows(points, first, last) |
||||
|
||||
cv.RandArr(rng, point_chunk, cv.CV_RAND_NORMAL, |
||||
cv.Scalar(center[0], center[1], 0, 0), |
||||
cv.Scalar(img.width*0.1, img.height*0.1, 0, 0)) |
||||
|
||||
|
||||
# shuffle samples |
||||
cv.RandShuffle(points, rng) |
||||
|
||||
cv.KMeans2(points, cluster_count, clusters, |
||||
(cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 10, 1.0)) |
||||
|
||||
cv.Zero(img) |
||||
|
||||
for i in range(sample_count): |
||||
cluster_idx = int(clusters[i, 0]) |
||||
pt = (cv.Round(points[i, 0][0]), cv.Round(points[i, 0][1])) |
||||
cv.Circle(img, pt, 2, color_tab[cluster_idx], cv.CV_FILLED, cv.CV_AA, 0) |
||||
|
||||
cv.ShowImage("clusters", img) |
||||
|
||||
key = cv.WaitKey(0) % 0x100 |
||||
if key in [27, ord('q'), ord('Q')]: |
||||
break |
||||
|
||||
cv.DestroyWindow("clusters") |
@ -1,45 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import urllib2 |
||||
import cv2.cv as cv |
||||
import sys |
||||
|
||||
if __name__ == "__main__": |
||||
laplace = None |
||||
colorlaplace = None |
||||
planes = [ None, None, None ] |
||||
capture = None |
||||
|
||||
if len(sys.argv) == 1: |
||||
capture = cv.CreateCameraCapture(0) |
||||
elif len(sys.argv) == 2 and sys.argv[1].isdigit(): |
||||
capture = cv.CreateCameraCapture(int(sys.argv[1])) |
||||
elif len(sys.argv) == 2: |
||||
capture = cv.CreateFileCapture(sys.argv[1]) |
||||
|
||||
if not capture: |
||||
print "Could not initialize capturing..." |
||||
sys.exit(-1) |
||||
|
||||
cv.NamedWindow("Laplacian", 1) |
||||
|
||||
while True: |
||||
frame = cv.QueryFrame(capture) |
||||
if frame: |
||||
if not laplace: |
||||
planes = [cv.CreateImage((frame.width, frame.height), 8, 1) for i in range(3)] |
||||
laplace = cv.CreateImage((frame.width, frame.height), cv.IPL_DEPTH_16S, 1) |
||||
colorlaplace = cv.CreateImage((frame.width, frame.height), 8, 3) |
||||
|
||||
cv.Split(frame, planes[0], planes[1], planes[2], None) |
||||
for plane in planes: |
||||
cv.Laplace(plane, laplace, 3) |
||||
cv.ConvertScaleAbs(laplace, plane, 1, 0) |
||||
|
||||
cv.Merge(planes[0], planes[1], planes[2], None, colorlaplace) |
||||
|
||||
cv.ShowImage("Laplacian", colorlaplace) |
||||
|
||||
if cv.WaitKey(10) != -1: |
||||
break |
||||
|
||||
cv.DestroyWindow("Laplacian") |
@ -1,192 +0,0 @@ |
||||
#! /usr/bin/env python |
||||
|
||||
print "OpenCV Python version of lkdemo" |
||||
|
||||
import sys |
||||
|
||||
# import the necessary things for OpenCV |
||||
import cv2.cv as cv |
||||
|
||||
############################################################################# |
||||
# some "constants" |
||||
|
||||
win_size = 10 |
||||
MAX_COUNT = 500 |
||||
|
||||
############################################################################# |
||||
# some "global" variables |
||||
|
||||
image = None |
||||
pt = None |
||||
add_remove_pt = False |
||||
flags = 0 |
||||
night_mode = False |
||||
need_to_init = False |
||||
|
||||
############################################################################# |
||||
# the mouse callback |
||||
|
||||
# the callback on the trackbar |
||||
def on_mouse (event, x, y, flags, param): |
||||
|
||||
# we will use the global pt and add_remove_pt |
||||
global pt |
||||
global add_remove_pt |
||||
|
||||
if image is None: |
||||
# not initialized, so skip |
||||
return |
||||
|
||||
if image.origin != 0: |
||||
# different origin |
||||
y = image.height - y |
||||
|
||||
if event == cv.CV_EVENT_LBUTTONDOWN: |
||||
# user has click, so memorize it |
||||
pt = (x, y) |
||||
add_remove_pt = True |
||||
|
||||
############################################################################# |
||||
# so, here is the main part of the program |
||||
|
||||
if __name__ == '__main__': |
||||
|
||||
frames = sys.argv[1:] |
||||
if frames == []: |
||||
print "usage lkdemo.py <image files>" |
||||
sys.exit(1) |
||||
|
||||
# display a small howto use it |
||||
print "Hot keys: \n" \ |
||||
"\tESC - quit the program\n" \ |
||||
"\tr - auto-initialize tracking\n" \ |
||||
"\tc - delete all the points\n" \ |
||||
"\tn - switch the \"night\" mode on/off\n" \ |
||||
"\tSPACE - next frame\n" \ |
||||
"To add/remove a feature point click it\n" |
||||
|
||||
# first, create the necessary windows |
||||
cv.NamedWindow ('LkDemo', cv.CV_WINDOW_AUTOSIZE) |
||||
|
||||
# register the mouse callback |
||||
cv.SetMouseCallback ('LkDemo', on_mouse, None) |
||||
|
||||
fc = 0 |
||||
while 1: |
||||
# do forever |
||||
|
||||
frame = cv.LoadImage(frames[fc]) |
||||
|
||||
if image is None: |
||||
# create the images we need |
||||
image = cv.CreateImage (cv.GetSize (frame), 8, 3) |
||||
image.origin = frame.origin |
||||
grey = cv.CreateImage (cv.GetSize (frame), 8, 1) |
||||
prev_grey = cv.CreateImage (cv.GetSize (frame), 8, 1) |
||||
pyramid = cv.CreateImage (cv.GetSize (frame), 8, 1) |
||||
prev_pyramid = cv.CreateImage (cv.GetSize (frame), 8, 1) |
||||
features = [] |
||||
|
||||
# copy the frame, so we can draw on it |
||||
cv.Copy (frame, image) |
||||
|
||||
# create a grey version of the image |
||||
cv.CvtColor (image, grey, cv.CV_BGR2GRAY) |
||||
|
||||
if night_mode: |
||||
# night mode: only display the points |
||||
cv.SetZero (image) |
||||
|
||||
if need_to_init: |
||||
# we want to search all the good points |
||||
|
||||
# create the wanted images |
||||
eig = cv.CreateImage (cv.GetSize (grey), 32, 1) |
||||
temp = cv.CreateImage (cv.GetSize (grey), 32, 1) |
||||
|
||||
# the default parameters |
||||
quality = 0.01 |
||||
min_distance = 10 |
||||
|
||||
# search the good points |
||||
features = cv.GoodFeaturesToTrack ( |
||||
grey, eig, temp, |
||||
MAX_COUNT, |
||||
quality, min_distance, None, 3, 0, 0.04) |
||||
|
||||
# refine the corner locations |
||||
features = cv.FindCornerSubPix ( |
||||
grey, |
||||
features, |
||||
(win_size, win_size), (-1, -1), |
||||
(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) |
||||
|
||||
elif features != []: |
||||
# we have points, so display them |
||||
|
||||
# calculate the optical flow |
||||
features, status, track_error = cv.CalcOpticalFlowPyrLK ( |
||||
prev_grey, grey, prev_pyramid, pyramid, |
||||
features, |
||||
(win_size, win_size), 3, |
||||
(cv.CV_TERMCRIT_ITER|cv.CV_TERMCRIT_EPS, 20, 0.03), |
||||
flags) |
||||
|
||||
# set back the points we keep |
||||
features = [ p for (st,p) in zip(status, features) if st] |
||||
|
||||
if add_remove_pt: |
||||
# we have a point to add, so see if it is close to |
||||
# another one. If yes, don't use it |
||||
def ptptdist(p0, p1): |
||||
dx = p0[0] - p1[0] |
||||
dy = p0[1] - p1[1] |
||||
return dx**2 + dy**2 |
||||
if min([ ptptdist(pt, p) for p in features ]) < 25: |
||||
# too close |
||||
add_remove_pt = 0 |
||||
|
||||
# draw the points as green circles |
||||
for the_point in features: |
||||
cv.Circle (image, (int(the_point[0]), int(the_point[1])), 3, (0, 255, 0, 0), -1, 8, 0) |
||||
|
||||
if add_remove_pt: |
||||
# we want to add a point |
||||
# refine this corner location and append it to 'features' |
||||
|
||||
features += cv.FindCornerSubPix ( |
||||
grey, |
||||
[pt], |
||||
(win_size, win_size), (-1, -1), |
||||
(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, |
||||
20, 0.03)) |
||||
# we are no longer in "add_remove_pt" mode |
||||
add_remove_pt = False |
||||
|
||||
# swapping |
||||
prev_grey, grey = grey, prev_grey |
||||
prev_pyramid, pyramid = pyramid, prev_pyramid |
||||
need_to_init = False |
||||
|
||||
# we can now display the image |
||||
cv.ShowImage ('LkDemo', image) |
||||
|
||||
# handle events |
||||
c = cv.WaitKey(10) % 0x100 |
||||
|
||||
if c == 27: |
||||
# user has press the ESC key, so exit |
||||
break |
||||
|
||||
# processing depending on the character |
||||
if 32 <= c and c < 128: |
||||
cc = chr(c).lower() |
||||
if cc == 'r': |
||||
need_to_init = True |
||||
elif cc == 'c': |
||||
features = [] |
||||
elif cc == 'n': |
||||
night_mode = not night_mode |
||||
elif cc == ' ': |
||||
fc = (fc + 1) % len(frames) |
||||
cv.DestroyAllWindows() |
@ -1,45 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import sys |
||||
import urllib2 |
||||
import cv2.cv as cv |
||||
|
||||
src=None |
||||
dst=None |
||||
src2=None |
||||
|
||||
def on_mouse(event, x, y, flags, param): |
||||
|
||||
if not src: |
||||
return |
||||
|
||||
if event==cv.CV_EVENT_LBUTTONDOWN: |
||||
cv.LogPolar(src, dst, (x, y), 40, cv.CV_INTER_LINEAR + cv.CV_WARP_FILL_OUTLIERS) |
||||
cv.LogPolar(dst, src2, (x, y), 40, cv.CV_INTER_LINEAR + cv.CV_WARP_FILL_OUTLIERS + cv.CV_WARP_INVERSE_MAP) |
||||
cv.ShowImage("log-polar", dst) |
||||
cv.ShowImage("inverse log-polar", src2) |
||||
|
||||
if __name__ == "__main__": |
||||
|
||||
if len(sys.argv) > 1: |
||||
src = cv.LoadImage( sys.argv[1], cv.CV_LOAD_IMAGE_COLOR) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/fruits.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
src = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
|
||||
cv.NamedWindow("original", 1) |
||||
cv.NamedWindow("log-polar", 1) |
||||
cv.NamedWindow("inverse log-polar", 1) |
||||
|
||||
|
||||
dst = cv.CreateImage((256, 256), 8, 3) |
||||
src2 = cv.CreateImage(cv.GetSize(src), 8, 3) |
||||
|
||||
cv.SetMouseCallback("original", on_mouse) |
||||
on_mouse(cv.CV_EVENT_LBUTTONDOWN, src.width/2, src.height/2, None, None) |
||||
|
||||
cv.ShowImage("original", src) |
||||
cv.WaitKey() |
||||
cv.DestroyAllWindows() |
@ -1,62 +0,0 @@ |
||||
#!/usr/bin/python |
||||
|
||||
import urllib2 |
||||
import cv2.cv as cv |
||||
from random import randint |
||||
|
||||
def roundxy(pt): |
||||
return (cv.Round(pt[0]), cv.Round(pt[1])) |
||||
|
||||
def draw_common(points): |
||||
success, center, radius = cv.MinEnclosingCircle(points) |
||||
if success: |
||||
cv.Circle(img, roundxy(center), cv.Round(radius), cv.CV_RGB(255, 255, 0), 1, cv. CV_AA, 0) |
||||
|
||||
box = cv.MinAreaRect2(points) |
||||
box_vtx = [roundxy(p) for p in cv.BoxPoints(box)] |
||||
cv.PolyLine(img, [box_vtx], 1, cv.CV_RGB(0, 255, 255), 1, cv. CV_AA) |
||||
|
||||
def minarea_array(img, count): |
||||
pointMat = cv.CreateMat(count, 1, cv.CV_32SC2) |
||||
for i in range(count): |
||||
pointMat[i, 0] = (randint(img.width/4, img.width*3/4), |
||||
randint(img.height/4, img.height*3/4)) |
||||
|
||||
cv.Zero(img) |
||||
|
||||
for i in range(count): |
||||
cv.Circle(img, roundxy(pointMat[i, 0]), 2, cv.CV_RGB(255, 0, 0), cv.CV_FILLED, cv. CV_AA, 0) |
||||
|
||||
draw_common(pointMat) |
||||
|
||||
def minarea_seq(img, count, storage): |
||||
points = [(randint(img.width/4, img.width*3/4), randint(img.height/4, img.height*3/4)) for i in range(count)] |
||||
cv.Zero(img) |
||||
|
||||
for p in points: |
||||
cv.Circle(img, roundxy(p), 2, cv.CV_RGB(255, 0, 0), cv.CV_FILLED, cv. CV_AA, 0) |
||||
|
||||
draw_common(points) |
||||
|
||||
if __name__ == "__main__": |
||||
img = cv.CreateImage((500, 500), 8, 3) |
||||
storage = cv.CreateMemStorage() |
||||
|
||||
cv.NamedWindow("rect & circle", 1) |
||||
|
||||
use_seq = True |
||||
|
||||
while True: |
||||
count = randint(1, 100) |
||||
if use_seq: |
||||
minarea_seq(img, count, storage) |
||||
else: |
||||
minarea_array(img, count) |
||||
|
||||
cv.ShowImage("rect & circle", img) |
||||
key = cv.WaitKey() % 0x100 |
||||
if key in [27, ord('q'), ord('Q')]: |
||||
break |
||||
|
||||
use_seq = not use_seq |
||||
cv.DestroyAllWindows() |
@ -1,13 +0,0 @@ |
||||
#! /usr/bin/env python |
||||
|
||||
import cv2.cv as cv |
||||
|
||||
cap = cv.CreateFileCapture("../c/tree.avi") |
||||
img = cv.QueryFrame(cap) |
||||
print "Got frame of dimensions (", img.width, " x ", img.height, ")" |
||||
|
||||
cv.NamedWindow("win", cv.CV_WINDOW_AUTOSIZE) |
||||
cv.ShowImage("win", img) |
||||
cv.MoveWindow("win", 200, 200) |
||||
cv.WaitKey(0) |
||||
cv.DestroyAllWindows() |
@ -1,52 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import sys |
||||
import urllib2 |
||||
import cv2.cv as cv |
||||
|
||||
src = 0 |
||||
image = 0 |
||||
dest = 0 |
||||
element_shape = cv.CV_SHAPE_RECT |
||||
|
||||
def Opening(pos): |
||||
element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape) |
||||
cv.Erode(src, image, element, 1) |
||||
cv.Dilate(image, dest, element, 1) |
||||
cv.ShowImage("Opening & Closing", dest) |
||||
def Closing(pos): |
||||
element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape) |
||||
cv.Dilate(src, image, element, 1) |
||||
cv.Erode(image, dest, element, 1) |
||||
cv.ShowImage("Opening & Closing", dest) |
||||
def Erosion(pos): |
||||
element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape) |
||||
cv.Erode(src, dest, element, 1) |
||||
cv.ShowImage("Erosion & Dilation", dest) |
||||
def Dilation(pos): |
||||
element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape) |
||||
cv.Dilate(src, dest, element, 1) |
||||
cv.ShowImage("Erosion & Dilation", dest) |
||||
|
||||
if __name__ == "__main__": |
||||
if len(sys.argv) > 1: |
||||
src = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_COLOR) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/fruits.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
src = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
|
||||
image = cv.CloneImage(src) |
||||
dest = cv.CloneImage(src) |
||||
cv.NamedWindow("Opening & Closing", 1) |
||||
cv.NamedWindow("Erosion & Dilation", 1) |
||||
cv.ShowImage("Opening & Closing", src) |
||||
cv.ShowImage("Erosion & Dilation", src) |
||||
cv.CreateTrackbar("Open", "Opening & Closing", 0, 10, Opening) |
||||
cv.CreateTrackbar("Close", "Opening & Closing", 0, 10, Closing) |
||||
cv.CreateTrackbar("Dilate", "Erosion & Dilation", 0, 10, Dilation) |
||||
cv.CreateTrackbar("Erode", "Erosion & Dilation", 0, 10, Erosion) |
||||
cv.WaitKey(0) |
||||
cv.DestroyWindow("Opening & Closing") |
||||
cv.DestroyWindow("Erosion & Dilation") |
@ -1,110 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import urllib2 |
||||
import sys |
||||
import time |
||||
from math import cos, sin |
||||
import cv2.cv as cv |
||||
|
||||
CLOCKS_PER_SEC = 1.0 |
||||
MHI_DURATION = 1 |
||||
MAX_TIME_DELTA = 0.5 |
||||
MIN_TIME_DELTA = 0.05 |
||||
N = 4 |
||||
buf = range(10) |
||||
last = 0 |
||||
mhi = None # MHI |
||||
orient = None # orientation |
||||
mask = None # valid orientation mask |
||||
segmask = None # motion segmentation map |
||||
storage = None # temporary storage |
||||
|
||||
def update_mhi(img, dst, diff_threshold): |
||||
global last |
||||
global mhi |
||||
global storage |
||||
global mask |
||||
global orient |
||||
global segmask |
||||
timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds |
||||
size = cv.GetSize(img) # get current frame size |
||||
idx1 = last |
||||
if not mhi or cv.GetSize(mhi) != size: |
||||
for i in range(N): |
||||
buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) |
||||
cv.Zero(buf[i]) |
||||
mhi = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) |
||||
cv.Zero(mhi) # clear MHI at the beginning |
||||
orient = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) |
||||
segmask = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) |
||||
mask = cv.CreateImage(size,cv. IPL_DEPTH_8U, 1) |
||||
|
||||
cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale |
||||
idx2 = (last + 1) % N # index of (last - (N-1))th frame |
||||
last = idx2 |
||||
silh = buf[idx2] |
||||
cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames |
||||
cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it |
||||
cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI |
||||
cv.CvtScale(mhi, mask, 255./MHI_DURATION, |
||||
(MHI_DURATION - timestamp)*255./MHI_DURATION) |
||||
cv.Zero(dst) |
||||
cv.Merge(mask, None, None, None, dst) |
||||
cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3) |
||||
if not storage: |
||||
storage = cv.CreateMemStorage(0) |
||||
seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA) |
||||
for (area, value, comp_rect) in seq: |
||||
if comp_rect[2] + comp_rect[3] > 100: # reject very small components |
||||
color = cv.CV_RGB(255, 0,0) |
||||
silh_roi = cv.GetSubRect(silh, comp_rect) |
||||
mhi_roi = cv.GetSubRect(mhi, comp_rect) |
||||
orient_roi = cv.GetSubRect(orient, comp_rect) |
||||
mask_roi = cv.GetSubRect(mask, comp_rect) |
||||
angle = 360 - cv.CalcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION) |
||||
|
||||
count = cv.Norm(silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI |
||||
if count < (comp_rect[2] * comp_rect[3] * 0.05): |
||||
continue |
||||
|
||||
magnitude = 30. |
||||
center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2)) |
||||
cv.Circle(dst, center, cv.Round(magnitude*1.2), color, 3, cv.CV_AA, 0) |
||||
cv.Line(dst, |
||||
center, |
||||
(cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)), |
||||
cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))), |
||||
color, |
||||
3, |
||||
cv.CV_AA, |
||||
0) |
||||
|
||||
if __name__ == "__main__": |
||||
motion = 0 |
||||
capture = 0 |
||||
|
||||
if len(sys.argv)==1: |
||||
capture = cv.CreateCameraCapture(0) |
||||
elif len(sys.argv)==2 and sys.argv[1].isdigit(): |
||||
capture = cv.CreateCameraCapture(int(sys.argv[1])) |
||||
elif len(sys.argv)==2: |
||||
capture = cv.CreateFileCapture(sys.argv[1]) |
||||
|
||||
if not capture: |
||||
print "Could not initialize capturing..." |
||||
sys.exit(-1) |
||||
|
||||
cv.NamedWindow("Motion", 1) |
||||
while True: |
||||
image = cv.QueryFrame(capture) |
||||
if(image): |
||||
if(not motion): |
||||
motion = cv.CreateImage((image.width, image.height), 8, 3) |
||||
cv.Zero(motion) |
||||
#motion.origin = image.origin |
||||
update_mhi(image, motion, 30) |
||||
cv.ShowImage("Motion", motion) |
||||
if(cv.WaitKey(10) != -1): |
||||
break |
||||
else: |
||||
break |
||||
cv.DestroyWindow("Motion") |
@ -1,66 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import urllib2 |
||||
import sys |
||||
import cv2.cv as cv |
||||
import numpy |
||||
|
||||
# SRGB-linear conversions using NumPy - see http://en.wikipedia.org/wiki/SRGB |
||||
|
||||
def srgb2lin(x): |
||||
a = 0.055 |
||||
return numpy.where(x <= 0.04045, |
||||
x * (1.0 / 12.92), |
||||
numpy.power((x + a) * (1.0 / (1 + a)), 2.4)) |
||||
|
||||
def lin2srgb(x): |
||||
a = 0.055 |
||||
return numpy.where(x <= 0.0031308, |
||||
x * 12.92, |
||||
(1 + a) * numpy.power(x, 1 / 2.4) - a) |
||||
|
||||
if __name__ == "__main__": |
||||
if len(sys.argv) > 1: |
||||
img0 = cv.LoadImageM( sys.argv[1], cv.CV_LOAD_IMAGE_COLOR) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/lena.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
img0 = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
|
||||
cv.NamedWindow("original", 1) |
||||
cv.ShowImage("original", img0) |
||||
|
||||
# Image was originally bytes in range 0-255. Turn it into an array of floats in range 0.0 - 1.0 |
||||
n = numpy.asarray(img0) / 255.0 |
||||
|
||||
# Use NumPy to do some transformations on the image |
||||
|
||||
# Negate the image by subtracting it from 1.0 |
||||
cv.NamedWindow("negative") |
||||
cv.ShowImage("negative", cv.fromarray(1.0 - n)) |
||||
|
||||
# Assume the image was sRGB, and compute the linear version. |
||||
cv.NamedWindow("linear") |
||||
cv.ShowImage("linear", cv.fromarray(srgb2lin(n))) |
||||
|
||||
# Look at a subwindow |
||||
cv.NamedWindow("subwindow") |
||||
cv.ShowImage("subwindow", cv.fromarray(n[200:300,200:400])) |
||||
|
||||
# Compute the grayscale image |
||||
cv.NamedWindow("monochrome") |
||||
ln = srgb2lin(n) |
||||
red = ln[:,:,0] |
||||
grn = ln[:,:,1] |
||||
blu = ln[:,:,2] |
||||
linear_mono = 0.3 * red + 0.59 * grn + 0.11 * blu |
||||
cv.ShowImage("monochrome", cv.fromarray(lin2srgb(linear_mono))) |
||||
|
||||
# Apply a blur to the NumPy array using OpenCV |
||||
cv.NamedWindow("gaussian") |
||||
cv.Smooth(n, n, cv.CV_GAUSSIAN, 15, 15) |
||||
cv.ShowImage("gaussian", cv.fromarray(n)) |
||||
|
||||
cv.WaitKey(0) |
||||
cv.DestroyAllWindows() |
@ -1,48 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import urllib2 |
||||
import sys |
||||
import cv2.cv as cv |
||||
import numpy |
||||
|
||||
if __name__ == "__main__": |
||||
cv.NamedWindow("camera", 1) |
||||
|
||||
capture = cv.CaptureFromCAM(0) |
||||
|
||||
paste = cv.CreateMat(960, 1280, cv.CV_8UC3) |
||||
topleft = numpy.asarray(cv.GetSubRect(paste, (0, 0, 640, 480))) |
||||
topright = numpy.asarray(cv.GetSubRect(paste, (640, 0, 640, 480))) |
||||
bottomleft = numpy.asarray(cv.GetSubRect(paste, (0, 480, 640, 480))) |
||||
bottomright = numpy.asarray(cv.GetSubRect(paste, (640, 480, 640, 480))) |
||||
|
||||
while True: |
||||
img = cv.GetMat(cv.QueryFrame(capture)) |
||||
|
||||
n = (numpy.asarray(img)).astype(numpy.uint8) |
||||
|
||||
red = n[:,:,0] |
||||
grn = n[:,:,1] |
||||
blu = n[:,:,2] |
||||
|
||||
topleft[:,:,0] = 255 - grn |
||||
topleft[:,:,1] = red |
||||
topleft[:,:,2] = blu |
||||
|
||||
topright[:,:,0] = blu |
||||
topright[:,:,1] = 255 - red |
||||
topright[:,:,2] = grn |
||||
|
||||
bottomright[:,:,0] = red |
||||
bottomright[:,:,1] = grn |
||||
bottomright[:,:,2] = 255 - blu |
||||
|
||||
fgrn = grn.astype(numpy.float32) |
||||
fred = red.astype(numpy.float32) |
||||
bottomleft[:,:,0] = blu |
||||
bottomleft[:,:,1] = (abs(fgrn - fred)).astype(numpy.uint8) |
||||
bottomleft[:,:,2] = red |
||||
|
||||
cv.ShowImage("camera", paste) |
||||
if cv.WaitKey(6) == 27: |
||||
break |
||||
cv.DestroyAllWindows() |
@ -1,56 +0,0 @@ |
||||
#!/usr/bin/python |
||||
|
||||
import sys |
||||
from cv import * |
||||
|
||||
def inside(r, q): |
||||
(rx, ry), (rw, rh) = r |
||||
(qx, qy), (qw, qh) = q |
||||
return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh |
||||
|
||||
try: |
||||
img = LoadImage(sys.argv[1]) |
||||
except: |
||||
try: |
||||
f = open(sys.argv[1], "rt") |
||||
except: |
||||
print "cannot read " + sys.argv[1] |
||||
sys.exit(-1) |
||||
imglist = list(f.readlines()) |
||||
else: |
||||
imglist = [sys.argv[1]] |
||||
|
||||
NamedWindow("people detection demo", 1) |
||||
storage = CreateMemStorage(0) |
||||
|
||||
for name in imglist: |
||||
n = name.strip() |
||||
print n |
||||
try: |
||||
img = LoadImage(n) |
||||
except: |
||||
continue |
||||
|
||||
#ClearMemStorage(storage) |
||||
found = list(HOGDetectMultiScale(img, storage, win_stride=(8,8), |
||||
padding=(32,32), scale=1.05, group_threshold=2)) |
||||
found_filtered = [] |
||||
for r in found: |
||||
insidef = False |
||||
for q in found: |
||||
if inside(r, q): |
||||
insidef = True |
||||
break |
||||
if not insidef: |
||||
found_filtered.append(r) |
||||
for r in found_filtered: |
||||
(rx, ry), (rw, rh) = r |
||||
tl = (rx + int(rw*0.1), ry + int(rh*0.07)) |
||||
br = (rx + int(rw*0.9), ry + int(rh*0.87)) |
||||
Rectangle(img, tl, br, (0, 255, 0), 3) |
||||
|
||||
ShowImage("people detection demo", img) |
||||
c = WaitKey(0) |
||||
if c == ord('q'): |
||||
break |
||||
cv.DestroyAllWindows() |
@ -1,41 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import cv2.cv as cv |
||||
|
||||
class PyrSegmentation: |
||||
def __init__(self, img0): |
||||
self.thresh1 = 255 |
||||
self.thresh2 = 30 |
||||
self.level =4 |
||||
self.storage = cv.CreateMemStorage() |
||||
cv.NamedWindow("Source", 0) |
||||
cv.ShowImage("Source", img0) |
||||
cv.NamedWindow("Segmentation", 0) |
||||
cv.CreateTrackbar("Thresh1", "Segmentation", self.thresh1, 255, self.set_thresh1) |
||||
cv.CreateTrackbar("Thresh2", "Segmentation", self.thresh2, 255, self.set_thresh2) |
||||
self.image0 = cv.CloneImage(img0) |
||||
self.image1 = cv.CloneImage(img0) |
||||
cv.ShowImage("Segmentation", self.image1) |
||||
|
||||
def set_thresh1(self, val): |
||||
self.thresh1 = val |
||||
self.on_segment() |
||||
|
||||
def set_thresh2(self, val): |
||||
self.thresh2 = val |
||||
self.on_segment() |
||||
|
||||
def on_segment(self): |
||||
comp = cv.PyrSegmentation(self.image0, self.image1, self.storage, \ |
||||
self.level, self.thresh1+1, self.thresh2+1) |
||||
cv.ShowImage("Segmentation", self.image1) |
||||
|
||||
def run(self): |
||||
self.on_segment() |
||||
cv.WaitKey(0) |
||||
|
||||
if __name__ == "__main__": |
||||
img0 = cv.LoadImage("../c/fruits.jpg", 1) |
||||
|
||||
# segmentation of the color image |
||||
PyrSegmentation(img0).run() |
||||
cv.DestroyAllWindows() |
@ -1,153 +0,0 @@ |
||||
#!/usr/bin/python |
||||
# |
||||
# The full "Square Detector" program. |
||||
# It loads several images subsequentally and tries to find squares in |
||||
# each image |
||||
# |
||||
|
||||
import urllib2 |
||||
from math import sqrt |
||||
import cv2.cv as cv |
||||
|
||||
thresh = 50 |
||||
img = None |
||||
img0 = None |
||||
storage = None |
||||
wndname = "Square Detection Demo" |
||||
|
||||
def angle(pt1, pt2, pt0): |
||||
dx1 = pt1.x - pt0.x |
||||
dy1 = pt1.y - pt0.y |
||||
dx2 = pt2.x - pt0.x |
||||
dy2 = pt2.y - pt0.y |
||||
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10) |
||||
|
||||
def findSquares4(img, storage): |
||||
N = 11 |
||||
sz = (img.width & -2, img.height & -2) |
||||
timg = cv.CloneImage(img); # make a copy of input image |
||||
gray = cv.CreateImage(sz, 8, 1) |
||||
pyr = cv.CreateImage((sz.width/2, sz.height/2), 8, 3) |
||||
# create empty sequence that will contain points - |
||||
# 4 points per square (the square's vertices) |
||||
squares = cv.CreateSeq(0, sizeof_CvSeq, sizeof_CvPoint, storage) |
||||
squares = CvSeq_CvPoint.cast(squares) |
||||
|
||||
# select the maximum ROI in the image |
||||
# with the width and height divisible by 2 |
||||
subimage = cv.GetSubRect(timg, cv.Rect(0, 0, sz.width, sz.height)) |
||||
|
||||
# down-scale and upscale the image to filter out the noise |
||||
cv.PyrDown(subimage, pyr, 7) |
||||
cv.PyrUp(pyr, subimage, 7) |
||||
tgray = cv.CreateImage(sz, 8, 1) |
||||
# find squares in every color plane of the image |
||||
for c in range(3): |
||||
# extract the c-th color plane |
||||
channels = [None, None, None] |
||||
channels[c] = tgray |
||||
cv.Split(subimage, channels[0], channels[1], channels[2], None) |
||||
for l in range(N): |
||||
# hack: use Canny instead of zero threshold level. |
||||
# Canny helps to catch squares with gradient shading |
||||
if(l == 0): |
||||
# apply Canny. Take the upper threshold from slider |
||||
# and set the lower to 0 (which forces edges merging) |
||||
cv.Canny(tgray, gray, 0, thresh, 5) |
||||
# dilate canny output to remove potential |
||||
# holes between edge segments |
||||
cv.Dilate(gray, gray, None, 1) |
||||
else: |
||||
# apply threshold if l!=0: |
||||
# tgray(x, y) = gray(x, y) < (l+1)*255/N ? 255 : 0 |
||||
cv.Threshold(tgray, gray, (l+1)*255/N, 255, cv.CV_THRESH_BINARY) |
||||
|
||||
# find contours and store them all as a list |
||||
count, contours = cv.FindContours(gray, storage, sizeof_CvContour, |
||||
cv.CV_RETR_LIST, cv. CV_CHAIN_APPROX_SIMPLE, (0, 0)) |
||||
|
||||
if not contours: |
||||
continue |
||||
|
||||
# test each contour |
||||
for contour in contours.hrange(): |
||||
# approximate contour with accuracy proportional |
||||
# to the contour perimeter |
||||
result = cv.ApproxPoly(contour, sizeof_CvContour, storage, |
||||
cv.CV_POLY_APPROX_DP, cv.ContourPerimeter(contours)*0.02, 0) |
||||
# square contours should have 4 vertices after approximation |
||||
# relatively large area (to filter out noisy contours) |
||||
# and be convex. |
||||
# Note: absolute value of an area is used because |
||||
# area may be positive or negative - in accordance with the |
||||
# contour orientation |
||||
if(result.total == 4 and |
||||
abs(cv.ContourArea(result)) > 1000 and |
||||
cv.CheckContourConvexity(result)): |
||||
s = 0 |
||||
for i in range(5): |
||||
# find minimum angle between joint |
||||
# edges (maximum of cosine) |
||||
if(i >= 2): |
||||
t = abs(angle(result[i], result[i-2], result[i-1])) |
||||
if s<t: |
||||
s=t |
||||
# if cosines of all angles are small |
||||
# (all angles are ~90 degree) then write quandrange |
||||
# vertices to resultant sequence |
||||
if(s < 0.3): |
||||
for i in range(4): |
||||
squares.append(result[i]) |
||||
|
||||
return squares |
||||
|
||||
# the function draws all the squares in the image |
||||
def drawSquares(img, squares): |
||||
cpy = cv.CloneImage(img) |
||||
# read 4 sequence elements at a time (all vertices of a square) |
||||
i=0 |
||||
while i<squares.total: |
||||
pt = [] |
||||
# read 4 vertices |
||||
pt.append(squares[i]) |
||||
pt.append(squares[i+1]) |
||||
pt.append(squares[i+2]) |
||||
pt.append(squares[i+3]) |
||||
|
||||
# draw the square as a closed polyline |
||||
cv.PolyLine(cpy, [pt], 1, cv.CV_RGB(0, 255, 0), 3, cv. CV_AA, 0) |
||||
i+=4 |
||||
|
||||
# show the resultant image |
||||
cv.ShowImage(wndname, cpy) |
||||
|
||||
def on_trackbar(a): |
||||
if(img): |
||||
drawSquares(img, findSquares4(img, storage)) |
||||
|
||||
names = ["../c/pic1.png", "../c/pic2.png", "../c/pic3.png", |
||||
"../c/pic4.png", "../c/pic5.png", "../c/pic6.png" ] |
||||
|
||||
if __name__ == "__main__": |
||||
# create memory storage that will contain all the dynamic data |
||||
storage = cv.CreateMemStorage(0) |
||||
for name in names: |
||||
img0 = cv.LoadImage(name, 1) |
||||
if not img0: |
||||
print "Couldn't load %s" % name |
||||
continue |
||||
img = cv.CloneImage(img0) |
||||
# create window and a trackbar (slider) with parent "image" and set callback |
||||
# (the slider regulates upper threshold, passed to Canny edge detector) |
||||
cv.NamedWindow(wndname, 1) |
||||
cv.CreateTrackbar("canny thresh", wndname, thresh, 1000, on_trackbar) |
||||
# force the image processing |
||||
on_trackbar(0) |
||||
# wait for key. |
||||
# Also the function cv.WaitKey takes care of event processing |
||||
c = cv.WaitKey(0) % 0x100 |
||||
# clear memory storage - reset free space position |
||||
cv.ClearMemStorage(storage) |
||||
if(c == '\x1b'): |
||||
break |
||||
cv.DestroyWindow(wndname) |
@ -1,109 +0,0 @@ |
||||
#!/usr/bin/python |
||||
import urllib2 |
||||
import sys |
||||
import cv2.cv as cv |
||||
|
||||
class Sketcher: |
||||
def __init__(self, windowname, dests): |
||||
self.prev_pt = None |
||||
self.windowname = windowname |
||||
self.dests = dests |
||||
cv.SetMouseCallback(self.windowname, self.on_mouse) |
||||
|
||||
def on_mouse(self, event, x, y, flags, param): |
||||
pt = (x, y) |
||||
if event == cv.CV_EVENT_LBUTTONUP or not (flags & cv.CV_EVENT_FLAG_LBUTTON): |
||||
self.prev_pt = None |
||||
elif event == cv.CV_EVENT_LBUTTONDOWN: |
||||
self.prev_pt = pt |
||||
elif event == cv.CV_EVENT_MOUSEMOVE and (flags & cv.CV_EVENT_FLAG_LBUTTON) : |
||||
if self.prev_pt: |
||||
for dst in self.dests: |
||||
cv.Line(dst, self.prev_pt, pt, cv.ScalarAll(255), 5, 8, 0) |
||||
self.prev_pt = pt |
||||
cv.ShowImage(self.windowname, img) |
||||
|
||||
if __name__ == "__main__": |
||||
if len(sys.argv) > 1: |
||||
img0 = cv.LoadImage( sys.argv[1], cv.CV_LOAD_IMAGE_COLOR) |
||||
else: |
||||
url = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/fruits.jpg' |
||||
filedata = urllib2.urlopen(url).read() |
||||
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) |
||||
cv.SetData(imagefiledata, filedata, len(filedata)) |
||||
img0 = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
||||
|
||||
rng = cv.RNG(-1) |
||||
|
||||
print "Hot keys:" |
||||
print "\tESC - quit the program" |
||||
print "\tr - restore the original image" |
||||
print "\tw - run watershed algorithm" |
||||
print "\t (before that, roughly outline several markers on the image)" |
||||
|
||||
cv.NamedWindow("image", 1) |
||||
cv.NamedWindow("watershed transform", 1) |
||||
|
||||
img = cv.CloneImage(img0) |
||||
img_gray = cv.CloneImage(img0) |
||||
wshed = cv.CloneImage(img0) |
||||
marker_mask = cv.CreateImage(cv.GetSize(img), 8, 1) |
||||
markers = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_32S, 1) |
||||
|
||||
cv.CvtColor(img, marker_mask, cv.CV_BGR2GRAY) |
||||
cv.CvtColor(marker_mask, img_gray, cv.CV_GRAY2BGR) |
||||
|
||||
cv.Zero(marker_mask) |
||||
cv.Zero(wshed) |
||||
|
||||
cv.ShowImage("image", img) |
||||
cv.ShowImage("watershed transform", wshed) |
||||
|
||||
sk = Sketcher("image", [img, marker_mask]) |
||||
|
||||
while True: |
||||
c = cv.WaitKey(0) % 0x100 |
||||
if c == 27 or c == ord('q'): |
||||
break |
||||
if c == ord('r'): |
||||
cv.Zero(marker_mask) |
||||
cv.Copy(img0, img) |
||||
cv.ShowImage("image", img) |
||||
if c == ord('w'): |
||||
storage = cv.CreateMemStorage(0) |
||||
#cv.SaveImage("wshed_mask.png", marker_mask) |
||||
#marker_mask = cv.LoadImage("wshed_mask.png", 0) |
||||
contours = cv.FindContours(marker_mask, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) |
||||
def contour_iterator(contour): |
||||
while contour: |
||||
yield contour |
||||
contour = contour.h_next() |
||||
|
||||
cv.Zero(markers) |
||||
comp_count = 0 |
||||
for c in contour_iterator(contours): |
||||
cv.DrawContours(markers, |
||||
c, |
||||
cv.ScalarAll(comp_count + 1), |
||||
cv.ScalarAll(comp_count + 1), |
||||
-1, |
||||
-1, |
||||
8) |
||||
comp_count += 1 |
||||
|
||||
cv.Watershed(img0, markers) |
||||
|
||||
cv.Set(wshed, cv.ScalarAll(255)) |
||||
|
||||
# paint the watershed image |
||||
color_tab = [(cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50) for i in range(comp_count)] |
||||
for j in range(markers.height): |
||||
for i in range(markers.width): |
||||
idx = markers[j, i] |
||||
if idx != -1: |
||||
wshed[j, i] = color_tab[int(idx - 1)] |
||||
|
||||
cv.AddWeighted(wshed, 0.5, img_gray, 0.5, 0, wshed) |
||||
cv.ShowImage("watershed transform", wshed) |
||||
cv.DestroyAllWindows() |
||||
|
Loading…
Reference in new issue