"atomic bomb" commit. Reorganized OpenCV directory structure
This commit is contained in:
12
samples/octave/CMakeLists.txt
Normal file
12
samples/octave/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
# -------------------------------------------------------------------------
|
||||
# CMake file for Octave samples. See root CMakeLists.txt
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
file(GLOB OCTAVE_SAMPLES *.m)
|
||||
|
||||
if(NOT WIN32)
|
||||
install(FILES ${OCTAVE_SAMPLES}
|
||||
DESTINATION share/opencv/samples/octave
|
||||
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)
|
||||
endif()
|
||||
|
||||
184
samples/octave/cam-histo.m
Normal file
184
samples/octave/cam-histo.m
Normal file
@@ -0,0 +1,184 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
## import the necessary things for OpenCV
|
||||
addpath("/home/x/opencv2/interfaces/swig/octave");
|
||||
source("/home/x/opencv2/interfaces/swig/octave/PKG_ADD_template");
|
||||
debug_on_error(true);
|
||||
debug_on_warning(true);
|
||||
crash_dumps_octave_core (0)
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
#############################################################################
|
||||
## definition of some constants
|
||||
|
||||
## how many bins we want for the histogram, and their ranges
|
||||
hdims = 16;
|
||||
hranges = {0, 180};
|
||||
|
||||
## ranges for the limitation of the histogram
|
||||
vmin = 10;
|
||||
vmax = 256;
|
||||
smin = 30;
|
||||
|
||||
## the range we want to monitor
|
||||
hsv_min = cv.cvScalar (0, smin, vmin, 0);
|
||||
hsv_max = cv.cvScalar (180, 256, vmax, 0);
|
||||
|
||||
#############################################################################
|
||||
## some useful functions
|
||||
|
||||
function rgb = hsv2rgb (hue)
|
||||
global cv;
|
||||
## convert the hue value to the corresponding rgb value
|
||||
|
||||
sector_data = [0, 2, 1; 1, 2, 0; 1, 0, 2; 2, 0, 1; 2, 1, 0; 0, 1, 2]+1;
|
||||
hue *= 0.1 / 3;
|
||||
sector = cv.cvFloor (hue);
|
||||
p = cv.cvRound (255 * (hue - sector));
|
||||
if (bitand(sector,1))
|
||||
p = bitxor(p,255);
|
||||
endif
|
||||
|
||||
rgb = zeros(1,3);
|
||||
rgb (sector_data (sector+1, 1)) = 255;
|
||||
rgb (sector_data (sector+1, 2)) = 0;
|
||||
rgb (sector_data (sector+1, 3)) = p;
|
||||
|
||||
rgb = cv.cvScalar (rgb (3), rgb (2), rgb (1), 0);
|
||||
endfunction
|
||||
|
||||
#############################################################################
|
||||
## so, here is the main part of the program
|
||||
|
||||
## a small welcome
|
||||
printf("OpenCV Octave wrapper test\n");
|
||||
printf("OpenCV version: %s (%d, %d, %d)\n",
|
||||
cv.CV_VERSION,cv.CV_MAJOR_VERSION,
|
||||
cv.CV_MINOR_VERSION,cv.CV_SUBMINOR_VERSION);
|
||||
|
||||
## first, create the necessary windows
|
||||
highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE);
|
||||
highgui.cvNamedWindow ('Histogram', highgui.CV_WINDOW_AUTOSIZE);
|
||||
|
||||
## move the new window to a better place
|
||||
#highgui.cvMoveWindow ('Camera', 10, 40);
|
||||
#highgui.cvMoveWindow ('Histogram', 10, 270);
|
||||
|
||||
try
|
||||
## try to get the device number from the command line
|
||||
device = int32 (argv(){1});
|
||||
have_device = true;
|
||||
catch
|
||||
## no device number on the command line, assume we want the 1st device
|
||||
device = -1;
|
||||
end_try_catch
|
||||
|
||||
## no argument on the command line, try to use the camera
|
||||
capture = highgui.cvCreateCameraCapture (device);
|
||||
|
||||
## set the wanted image size from the camera
|
||||
highgui.cvSetCaptureProperty (capture, \
|
||||
highgui.CV_CAP_PROP_FRAME_WIDTH, 320);
|
||||
highgui.cvSetCaptureProperty (capture, \
|
||||
highgui.CV_CAP_PROP_FRAME_HEIGHT, 240);
|
||||
|
||||
## create an image to put in the histogram
|
||||
histimg = cv.cvCreateImage (cv.cvSize (320,240), 8, 3);
|
||||
|
||||
## init the image of the histogram to black
|
||||
cv.cvSetZero (histimg);
|
||||
|
||||
## capture the 1st frame to get some propertie on it
|
||||
frame = highgui.cvQueryFrame (capture);
|
||||
|
||||
## get some properties of the frame
|
||||
frame_size = cv.cvGetSize (frame);
|
||||
|
||||
## compute which selection of the frame we want to monitor
|
||||
selection = cv.cvRect (0, 0, frame.width, frame.height);
|
||||
|
||||
## create some images usefull later
|
||||
hue = cv.cvCreateImage (frame_size, 8, 1);
|
||||
mask = cv.cvCreateImage (frame_size, 8, 1);
|
||||
hsv = cv.cvCreateImage (frame_size, 8, 3 );
|
||||
|
||||
## create the histogram
|
||||
hist = cv.cvCreateHist ({hdims}, cv.CV_HIST_ARRAY, {hranges}, 1);
|
||||
|
||||
while (1) ## do forever
|
||||
|
||||
## 1. capture the current image
|
||||
frame = highgui.cvQueryFrame (capture);
|
||||
if (swig_this(frame)==0);
|
||||
## no image captured... end the processing
|
||||
break
|
||||
endif
|
||||
|
||||
## mirror the captured image
|
||||
cv.cvFlip (frame, [], 1);
|
||||
|
||||
## compute the hsv version of the image
|
||||
cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV);
|
||||
|
||||
## compute which pixels are in the wanted range
|
||||
cv.cvInRangeS (hsv, hsv_min, hsv_max, mask);
|
||||
|
||||
## extract the hue from the hsv array
|
||||
cv.cvSplit (hsv, hue, [], [], []);
|
||||
|
||||
## select the rectangle of interest in the hue/mask arrays
|
||||
hue_roi = cv.cvGetSubRect (hue, selection);
|
||||
mask_roi = cv.cvGetSubRect (mask, selection);
|
||||
|
||||
## it's time to compute the histogram
|
||||
cv.cvCalcHist (hue_roi, hist, 0, mask_roi);
|
||||
|
||||
## extract the min and max value of the histogram
|
||||
[min_val, max_val, min_idx, max_idx] = cv.cvGetMinMaxHistValue (hist);
|
||||
|
||||
## compute the scale factor
|
||||
if (max_val > 0)
|
||||
scale = 255. / max_val;
|
||||
else
|
||||
scale = 0.;
|
||||
endif
|
||||
|
||||
## scale the histograms
|
||||
cv.cvConvertScale (hist.bins, hist.bins, scale, 0);
|
||||
|
||||
## clear the histogram image
|
||||
cv.cvSetZero (histimg);
|
||||
|
||||
## compute the width for each bin do display
|
||||
bin_w = histimg.width / hdims;
|
||||
|
||||
for (i=0:hdims-1)
|
||||
## for all the bins
|
||||
|
||||
## get the value, and scale to the size of the hist image
|
||||
val = cv.cvRound (cv.cvGetReal1D (hist.bins, i)
|
||||
* histimg.height / 255);
|
||||
|
||||
## compute the color
|
||||
color = hsv2rgb (i * 180. / hdims);
|
||||
|
||||
## draw the rectangle in the wanted color
|
||||
cv.cvRectangle (histimg,
|
||||
cv.cvPoint (i * bin_w, histimg.height),
|
||||
cv.cvPoint ((i + 1) * bin_w, histimg.height - val),
|
||||
color, -1, 8, 0);
|
||||
|
||||
## we can now display the images
|
||||
highgui.cvShowImage ('Camera', frame);
|
||||
highgui.cvShowImage ('Histogram', histimg);
|
||||
endfor
|
||||
|
||||
## handle events
|
||||
k = highgui.cvWaitKey (5);
|
||||
|
||||
if (k == 27)
|
||||
## user has press the ESC key, so exit
|
||||
break;
|
||||
endif
|
||||
endwhile
|
||||
107
samples/octave/capture-cam.m
Normal file
107
samples/octave/capture-cam.m
Normal file
@@ -0,0 +1,107 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
## import the necessary things for OpenCV
|
||||
addpath("/home/x/opencv2/interfaces/swig/octave");
|
||||
source("/home/x/opencv2/interfaces/swig/octave/PKG_ADD_template");
|
||||
debug_on_error(true);
|
||||
debug_on_warning(true);
|
||||
crash_dumps_octave_core (0)
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
## the codec existing in cvcapp.cpp,
|
||||
## need to have a better way to specify them in the future
|
||||
## WARNING: I have see only MPEG1VIDEO working on my computer
|
||||
H263 = 0x33363255;
|
||||
H263I = 0x33363249;
|
||||
MSMPEG4V3 = 0x33564944;
|
||||
MPEG4 = 0x58564944;
|
||||
MSMPEG4V2 = 0x3234504D;
|
||||
MJPEG = 0x47504A4D;
|
||||
MPEG1VIDEO = 0x314D4950;
|
||||
AC3 = 0x2000;
|
||||
MP2 = 0x50;
|
||||
FLV1 = 0x31564C46;
|
||||
|
||||
#############################################################################
|
||||
## so, here is the main part of the program
|
||||
|
||||
## a small welcome
|
||||
printf("OpenCV Octave capture video\n");
|
||||
|
||||
## first, create the necessary window
|
||||
highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE);
|
||||
|
||||
## move the new window to a better place
|
||||
#highgui.cvMoveWindow ('Camera', 10, 10);
|
||||
|
||||
try
|
||||
## try to get the device number from the command line
|
||||
device = int32 (argv(){1});
|
||||
catch
|
||||
## no device number on the command line, assume we want the 1st device
|
||||
device = -1;
|
||||
end_try_catch
|
||||
|
||||
## no argument on the command line, try to use the camera
|
||||
capture = highgui.cvCreateCameraCapture (device);
|
||||
|
||||
## check that capture device is OK
|
||||
if (!swig_this(capture))
|
||||
printf("Error opening capture device\n");
|
||||
exit (1);
|
||||
endif
|
||||
|
||||
## capture the 1st frame to get some propertie on it
|
||||
frame = highgui.cvQueryFrame (capture);
|
||||
|
||||
## get size of the frame
|
||||
frame_size = cv.cvGetSize (frame);
|
||||
|
||||
## get the frame rate of the capture device
|
||||
fps = highgui.cvGetCaptureProperty (capture, highgui.CV_CAP_PROP_FPS);
|
||||
if (fps == 0)
|
||||
## no fps getted, so set it to 30 by default
|
||||
fps = 30;
|
||||
endif
|
||||
|
||||
## create the writer
|
||||
writer = highgui.cvCreateVideoWriter ("captured.mpg", MPEG1VIDEO,
|
||||
fps, frame_size, true);
|
||||
|
||||
## check the writer is OK
|
||||
if (!swig_this(writer))
|
||||
printf("Error opening writer\n");
|
||||
exit(1);
|
||||
endif
|
||||
|
||||
while (1)
|
||||
## do forever
|
||||
|
||||
## 1. capture the current image
|
||||
frame = highgui.cvQueryFrame (capture);
|
||||
if (swig_this(frame) == 0)
|
||||
## no image captured... end the processing
|
||||
break
|
||||
endif
|
||||
|
||||
## write the frame to the output file
|
||||
highgui.cvWriteFrame (writer, frame);
|
||||
|
||||
## display the frames to have a visual output
|
||||
highgui.cvShowImage ('Camera', frame);
|
||||
|
||||
## handle events
|
||||
k = highgui.cvWaitKey (5);
|
||||
|
||||
if (k & 0x100 == 27)
|
||||
## user has press the ESC key, so exit
|
||||
break
|
||||
endif
|
||||
endwhile
|
||||
|
||||
|
||||
## end working with the writer
|
||||
## not working at this time... Need to implement some typemaps...
|
||||
## but exiting without calling it is OK in this simple application
|
||||
##highgui.cvReleaseVideoWriter (writer)
|
||||
23
samples/octave/chessboard.m
Normal file
23
samples/octave/chessboard.m
Normal file
@@ -0,0 +1,23 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
arg_list=argv();
|
||||
|
||||
cvNamedWindow("win");
|
||||
if (!size(arg_list,1))
|
||||
error("must specify filename");
|
||||
exit
|
||||
endif
|
||||
filename = arg_list{1};
|
||||
im = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
|
||||
im3 = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
|
||||
chessboard_dim = cvSize( 5, 6 );
|
||||
|
||||
[found_all, corners] = cvFindChessboardCorners( im, chessboard_dim );
|
||||
|
||||
cvDrawChessboardCorners( im3, chessboard_dim, corners, found_all );
|
||||
|
||||
cvShowImage("win", im3);
|
||||
cvWaitKey();
|
||||
|
||||
140
samples/octave/contours.m
Normal file
140
samples/octave/contours.m
Normal file
@@ -0,0 +1,140 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
printf("OpenCV Octave version of contours\n");
|
||||
|
||||
## import the necessary things for OpenCV
|
||||
cv;
|
||||
|
||||
|
||||
global _SIZE;
|
||||
global _red;
|
||||
global _green;
|
||||
global _blue;
|
||||
global contours;
|
||||
|
||||
## some default constants
|
||||
_SIZE = 500;
|
||||
_DEFAULT_LEVEL = 3;
|
||||
|
||||
## definition of some colors
|
||||
_red = cvScalar (0, 0, 255, 0);
|
||||
_green = cvScalar (0, 255, 0, 0);
|
||||
_white = cvRealScalar (255);
|
||||
_black = cvRealScalar (0);
|
||||
|
||||
## the callback on the trackbar, to set the level of contours we want
|
||||
## to display
|
||||
function on_trackbar (position)
|
||||
global cv;
|
||||
global _SIZE;
|
||||
global _red;
|
||||
global _green;
|
||||
global _blue;
|
||||
global contours;
|
||||
|
||||
## create the image for putting in it the founded contours
|
||||
contours_image = cvCreateImage (cvSize (_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;
|
||||
endif
|
||||
|
||||
## first, clear the image where we will draw contours
|
||||
cvSetZero (contours_image);
|
||||
|
||||
## draw contours in red and green
|
||||
cvDrawContours (contours_image, _contours, _red, _green, levels, 3, cv.CV_AA, cvPoint (0, 0));
|
||||
|
||||
## finally, show the image
|
||||
cvShowImage ("contours", contours_image);
|
||||
endfunction
|
||||
|
||||
## create the image where we want to display results
|
||||
image = cvCreateImage (cvSize (_SIZE, _SIZE), 8, 1);
|
||||
|
||||
## start with an empty image
|
||||
cvSetZero (image);
|
||||
|
||||
## draw the original picture
|
||||
for i=0:6-1,
|
||||
dx = mod(i,2) * 250 - 30;
|
||||
dy = (i / 2) * 150;
|
||||
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 150, dy + 100),
|
||||
cvSize (100, 70),
|
||||
0, 0, 360, _white, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 115, dy + 70),
|
||||
cvSize (30, 20),
|
||||
0, 0, 360, _black, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 185, dy + 70),
|
||||
cvSize (30, 20),
|
||||
0, 0, 360, _black, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 115, dy + 70),
|
||||
cvSize (15, 15),
|
||||
0, 0, 360, _white, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 185, dy + 70),
|
||||
cvSize (15, 15),
|
||||
0, 0, 360, _white, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 115, dy + 70),
|
||||
cvSize (5, 5),
|
||||
0, 0, 360, _black, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 185, dy + 70),
|
||||
cvSize (5, 5),
|
||||
0, 0, 360, _black, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 150, dy + 100),
|
||||
cvSize (10, 5),
|
||||
0, 0, 360, _black, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 150, dy + 150),
|
||||
cvSize (40, 10),
|
||||
0, 0, 360, _black, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 27, dy + 100),
|
||||
cvSize (20, 35),
|
||||
0, 0, 360, _white, -1, 8, 0);
|
||||
cvEllipse (image,
|
||||
cvPoint (dx + 273, dy + 100),
|
||||
cvSize (20, 35),
|
||||
0, 0, 360, _white, -1, 8, 0);
|
||||
endfor
|
||||
|
||||
## create window and display the original picture in it
|
||||
cvNamedWindow ("image", 1);
|
||||
cvShowImage ("image", image);
|
||||
|
||||
## create the storage area
|
||||
storage = cvCreateMemStorage (0);
|
||||
|
||||
## find the contours
|
||||
[nb_contours, contours] = cvFindContours (image, storage, sizeof_CvContour, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint (0,0));
|
||||
|
||||
## comment this out if you do not want approximation
|
||||
contours = cvApproxPoly (contours, sizeof_CvContour, storage, CV_POLY_APPROX_DP, 3, 1);
|
||||
|
||||
## create the window for the contours
|
||||
cvNamedWindow ("contours", 1);
|
||||
|
||||
## create the trackbar, to enable the change of the displayed level
|
||||
cvCreateTrackbar ("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
|
||||
cvWaitKey (0);
|
||||
74
samples/octave/convexhull.m
Normal file
74
samples/octave/convexhull.m
Normal file
@@ -0,0 +1,74 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
printf("OpenCV Octave version of convexhull\n");
|
||||
|
||||
## import the necessary things for OpenCV
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
## how many points we want at max
|
||||
_MAX_POINTS = 100;
|
||||
|
||||
## create the image where we want to display results
|
||||
image = cv.cvCreateImage (cv.cvSize (500, 500), 8, 3);
|
||||
|
||||
## create the window to put the image in
|
||||
highgui.cvNamedWindow ('hull', highgui.CV_WINDOW_AUTOSIZE);
|
||||
|
||||
while (true)
|
||||
## do forever
|
||||
|
||||
## get a random number of points
|
||||
count = int32(rand()*_MAX_POINTS)+1
|
||||
|
||||
## initialisations
|
||||
points = {};
|
||||
|
||||
for i=1:count,
|
||||
## generate a random point
|
||||
points{i} = cv.cvPoint \
|
||||
(int32(rand() * (image.width / 2) + image.width / 4), \
|
||||
int32(rand() * (image.height / 2) + image.height / 4)); \
|
||||
endfor
|
||||
|
||||
## compute the convex hull
|
||||
hull = cv.cvConvexHull2 (points, cv.CV_CLOCKWISE, 0);
|
||||
|
||||
## start with an empty image
|
||||
cv.cvSetZero (image);
|
||||
|
||||
for i=1:count,
|
||||
## draw all the points
|
||||
cv.cvCircle (image, points {i}, 2, \
|
||||
cv.cvScalar (0, 0, 255, 0), \
|
||||
cv.CV_FILLED, cv.CV_AA, 0);
|
||||
endfor
|
||||
|
||||
## start the line from the last point
|
||||
pt0 = points {hull [-1]};
|
||||
|
||||
for point_index = 1:hull.rows,
|
||||
## connect the previous point to the current one
|
||||
|
||||
## get the current one
|
||||
pt1 = points {point_index};
|
||||
|
||||
## draw
|
||||
cv.cvLine (image, pt0, pt1, \
|
||||
cv.cvScalar (0, 255, 0, 0), \
|
||||
1, cv.CV_AA, 0);
|
||||
|
||||
## now, current one will be the previous one for the next iteration
|
||||
pt0 = pt1;
|
||||
endfor
|
||||
|
||||
## display the final image
|
||||
highgui.cvShowImage ('hull', image);
|
||||
|
||||
## handle events, and wait a key pressed
|
||||
k = highgui.cvWaitKey (0);
|
||||
if (k == '\x1b')
|
||||
## user has press the ESC key, so exit
|
||||
break
|
||||
endif
|
||||
endwhile
|
||||
170
samples/octave/delaunay.m
Normal file
170
samples/octave/delaunay.m
Normal file
@@ -0,0 +1,170 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
## the script demostrates iterative construction of
|
||||
## delaunay triangulation and voronoi tesselation
|
||||
|
||||
## Original Author (C version): ?
|
||||
## Converted to Python by: Roman Stanchak
|
||||
## Converted to Octave by: Xavier Delacour
|
||||
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
function draw_subdiv_point( img, fp, color )
|
||||
global CV_FILLED;
|
||||
cvCircle( img, cvPoint(cvRound(fp.x), cvRound(fp.y)), 3, color, \
|
||||
CV_FILLED, 8, 0 );
|
||||
endfunction
|
||||
|
||||
function draw_subdiv_edge( img, edge, color )
|
||||
global CV_AA;
|
||||
|
||||
org_pt = cvSubdiv2DEdgeOrg(edge);
|
||||
dst_pt = cvSubdiv2DEdgeDst(edge);
|
||||
|
||||
if (org_pt && dst_pt )
|
||||
|
||||
org = org_pt.pt;
|
||||
dst = dst_pt.pt;
|
||||
|
||||
iorg = cvPoint( cvRound( org.x ), cvRound( org.y ));
|
||||
idst = cvPoint( cvRound( dst.x ), cvRound( dst.y ));
|
||||
|
||||
cvLine( img, iorg, idst, color, 1, CV_AA, 0 );
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function draw_subdiv( img, subdiv, delaunay_color, voronoi_color )
|
||||
|
||||
total = subdiv.edges.total;
|
||||
elem_size = subdiv.edges.elem_size;
|
||||
|
||||
for edge in subdiv.edges,
|
||||
edge_rot = cvSubdiv2DRotateEdge( edge, 1 );
|
||||
|
||||
if( CV_IS_SET_ELEM( edge ))
|
||||
draw_subdiv_edge( img, edge_rot, voronoi_color );
|
||||
draw_subdiv_edge( img, edge, delaunay_color );
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
|
||||
function locate_point( subdiv, fp, img, active_color )
|
||||
|
||||
[res, e0, p] = cvSubdiv2DLocate( subdiv, fp );
|
||||
|
||||
if (e0)
|
||||
e = e0
|
||||
while (true)
|
||||
draw_subdiv_edge( img, e, active_color );
|
||||
e = cvSubdiv2DGetEdge(e,CV_NEXT_AROUND_LEFT);
|
||||
if (e == e0)
|
||||
break
|
||||
endif
|
||||
endwhile
|
||||
endif
|
||||
|
||||
draw_subdiv_point( img, fp, active_color );
|
||||
endfunction
|
||||
|
||||
function draw_subdiv_facet( img, edge )
|
||||
|
||||
t = edge;
|
||||
count = 0;
|
||||
|
||||
## count number of edges in facet
|
||||
while (count == 0 || t != edge)
|
||||
count+=1;
|
||||
t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
|
||||
endwhile
|
||||
|
||||
buf = []
|
||||
|
||||
## gather points
|
||||
t = edge;
|
||||
for i=0:count-1,
|
||||
assert t>4
|
||||
pt = cvSubdiv2DEdgeOrg( t );
|
||||
if (! pt)
|
||||
break;
|
||||
endif
|
||||
buf.append( cvPoint( cvRound(pt.pt.x), cvRound(pt.pt.y) ) );
|
||||
t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
|
||||
endfor
|
||||
|
||||
if( len(buf)==count )
|
||||
pt = cvSubdiv2DEdgeDst( cvSubdiv2DRotateEdge( edge, 1 ));
|
||||
cvFillConvexPoly( img, buf, CV_RGB(randint(0,255),randint(0,255),randint(0,255)), CV_AA, 0 );
|
||||
cvPolyLine( img, [buf], 1, CV_RGB(0,0,0), 1, CV_AA, 0);
|
||||
draw_subdiv_point( img, pt.pt, CV_RGB(0,0,0));
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function paint_voronoi( subdiv, img )
|
||||
total = subdiv.edges.total;
|
||||
elem_size = subdiv.edges.elem_size;
|
||||
|
||||
cvCalcSubdivVoronoi2D( subdiv );
|
||||
|
||||
for edge in subdiv.edges,
|
||||
|
||||
if( CV_IS_SET_ELEM( edge ))
|
||||
## left
|
||||
draw_subdiv_facet( img, cvSubdiv2DRotateEdge( edge, 1 ));
|
||||
|
||||
## right
|
||||
draw_subdiv_facet( img, cvSubdiv2DRotateEdge( edge, 3 ));
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
win = "source";
|
||||
rect = cvRect( 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 = cvCreateImage( cvSize(rect.width,rect.height), 8, 3 );
|
||||
cvSet( img, bkgnd_color );
|
||||
|
||||
cvNamedWindow( win, 1 );
|
||||
|
||||
storage = cvCreateMemStorage(0);
|
||||
subdiv = cvCreateSubdivDelaunay2D( rect, storage );
|
||||
|
||||
printf("Delaunay triangulation will be build now interactively.\n");
|
||||
printf("To stop the process, press any key\n");
|
||||
|
||||
for i=0:200-1,
|
||||
fp = cvPoint2D32f( int32(rand()*(rect.width-10)+5), int32(rand()*(rect.height-10)+5) )
|
||||
|
||||
locate_point( subdiv, fp, img, active_facet_color );
|
||||
cvShowImage( win, img );
|
||||
|
||||
if( cvWaitKey( 100 ) >= 0 )
|
||||
break;
|
||||
endif
|
||||
|
||||
cvSubdivDelaunay2DInsert( subdiv, fp );
|
||||
cvCalcSubdivVoronoi2D( subdiv );
|
||||
cvSet( img, bkgnd_color );
|
||||
draw_subdiv( img, subdiv, delaunay_color, voronoi_color );
|
||||
cvShowImage( win, img );
|
||||
|
||||
if( cvWaitKey( 100 ) >= 0 )
|
||||
break;
|
||||
endif
|
||||
endfor
|
||||
|
||||
|
||||
cvSet( img, bkgnd_color );
|
||||
paint_voronoi( subdiv, img );
|
||||
cvShowImage( win, img );
|
||||
|
||||
cvWaitKey(0);
|
||||
|
||||
cvDestroyWindow( win );
|
||||
129
samples/octave/demhist.m
Normal file
129
samples/octave/demhist.m
Normal file
@@ -0,0 +1,129 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
file_name = "../c/baboon.jpg";
|
||||
|
||||
global Gbrightness;
|
||||
global Gcontrast;
|
||||
global hist_size;
|
||||
global ranges;
|
||||
global src_image;
|
||||
global dst_image;
|
||||
global hist_image;
|
||||
global hist;
|
||||
global lut;
|
||||
|
||||
_brightness = 100;
|
||||
_contrast = 100;
|
||||
Gbrightness = 100;
|
||||
Gcontrast = 100;
|
||||
|
||||
hist_size = 64;
|
||||
range_0={0,256};
|
||||
ranges = { range_0 };
|
||||
src_image=[];
|
||||
dst_image=[];
|
||||
hist_image=[];
|
||||
hist=[];
|
||||
lut=cvCreateMat(256,1,CV_8U);
|
||||
|
||||
## brightness/contrast callback function
|
||||
function update_brightness( val )
|
||||
global Gbrightness # global tag is required, or we get UnboundLocalError
|
||||
Gbrightness = val;
|
||||
update_brightcont( );
|
||||
endfunction
|
||||
|
||||
function update_contrast( val )
|
||||
global Gcontrast; # global tag is required, or we get UnboundLocalError
|
||||
Gcontrast = val;
|
||||
update_brightcont( );
|
||||
endfunction
|
||||
|
||||
function update_brightcont()
|
||||
global Gbrightness;
|
||||
global Gcontrast;
|
||||
global hist_size;
|
||||
global ranges;
|
||||
global src_image;
|
||||
global dst_image;
|
||||
global hist_image;
|
||||
global hist;
|
||||
global lut;
|
||||
global cvCalcHist; # use cv namespace for these instead
|
||||
global cvZero;
|
||||
global cvScale;
|
||||
|
||||
brightness = Gbrightness - 100;
|
||||
contrast = Gcontrast - 100;
|
||||
max_value = 0;
|
||||
|
||||
## The algorithm is by Werner D. Streidt
|
||||
## (http://visca.com/ffactory/archives/5-99/msg00021.html)
|
||||
if( contrast > 0 )
|
||||
delta = 127.*contrast/100;
|
||||
a = 255./(255. - delta*2);
|
||||
b = a*(brightness - delta);
|
||||
else
|
||||
delta = -128.*contrast/100;
|
||||
a = (256.-delta*2)/255.;
|
||||
b = a*brightness + delta;
|
||||
endif
|
||||
|
||||
for i=0:256-1,
|
||||
v = cvRound(a*i + b);
|
||||
if( v < 0 )
|
||||
v = 0;
|
||||
endif
|
||||
if( v > 255 )
|
||||
v = 255;
|
||||
endif
|
||||
lut(i) = v;
|
||||
endfor
|
||||
|
||||
cvLUT( src_image, dst_image, lut );
|
||||
cvShowImage( "image", dst_image );
|
||||
|
||||
cvCalcHist( dst_image, hist, 0, [] );
|
||||
cvZero( dst_image );
|
||||
[min_value, max_value] = cvGetMinMaxHistValue( hist );
|
||||
cvScale( hist.bins, hist.bins, double(hist_image.height)/max_value, 0 );
|
||||
##cvNormalizeHist( hist, 1000 );
|
||||
|
||||
cvSet( hist_image, cvScalarAll(255));
|
||||
bin_w = cvRound(double(hist_image.width)/hist_size);
|
||||
|
||||
for i=0:hist_size-1,
|
||||
cvRectangle( hist_image, cvPoint(i*bin_w, hist_image.height), cvPoint((i+1)*bin_w, hist_image.height - cvRound(cvGetReal1D(hist.bins,i))), cvScalarAll(0), -1, 8, 0 );
|
||||
endfor
|
||||
|
||||
cvShowImage( "histogram", hist_image );
|
||||
endfunction
|
||||
|
||||
|
||||
## Load the source image. HighGUI use.
|
||||
if size(argv, 1)>1
|
||||
file_name = argv(){1}
|
||||
endif
|
||||
|
||||
src_image = cvLoadImage( file_name, 0 );
|
||||
|
||||
if (!swig_this(src_image))
|
||||
printf("Image was not loaded.\n");
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
|
||||
dst_image = cvCloneImage(src_image);
|
||||
hist_image = cvCreateImage(cvSize(320,200), 8, 1);
|
||||
hist = cvCreateHist({hist_size}, CV_HIST_ARRAY, ranges, 1);
|
||||
|
||||
cvNamedWindow("image", 0);
|
||||
cvNamedWindow("histogram", 0);
|
||||
|
||||
cvCreateTrackbar("brightness", "image", _brightness, 200, @update_brightness);
|
||||
cvCreateTrackbar("contrast", "image", _contrast, 200, @update_contrast);
|
||||
|
||||
update_brightcont();
|
||||
cvWaitKey(0);
|
||||
115
samples/octave/dft.m
Normal file
115
samples/octave/dft.m
Normal file
@@ -0,0 +1,115 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
## Rearrange the quadrants of Fourier image so that the origin is at
|
||||
## the image center
|
||||
## src & dst arrays of equal size & type
|
||||
function cvShiftDFT(src_arr, dst_arr )
|
||||
|
||||
size = cvGetSize(src_arr);
|
||||
dst_size = cvGetSize(dst_arr);
|
||||
|
||||
if(dst_size.width != size.width || \
|
||||
dst_size.height != size.height)
|
||||
cvError( CV_StsUnmatchedSizes, "cvShiftDFT", \
|
||||
"Source and Destination arrays must have equal sizes", \
|
||||
__FILE__, __LINE__ );
|
||||
endif
|
||||
|
||||
if(swig_this(src_arr) == swig_this(dst_arr))
|
||||
tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
|
||||
endif
|
||||
|
||||
cx = size.width/2;
|
||||
cy = size.height/2; # image center
|
||||
|
||||
q1 = cvGetSubRect( src_arr, cvRect(0,0,cx, cy) );
|
||||
q2 = cvGetSubRect( src_arr, cvRect(cx,0,cx,cy) );
|
||||
q3 = cvGetSubRect( src_arr, cvRect(cx,cy,cx,cy) );
|
||||
q4 = cvGetSubRect( src_arr, cvRect(0,cy,cx,cy) );
|
||||
d1 = cvGetSubRect( src_arr, cvRect(0,0,cx,cy) );
|
||||
d2 = cvGetSubRect( src_arr, cvRect(cx,0,cx,cy) );
|
||||
d3 = cvGetSubRect( src_arr, cvRect(cx,cy,cx,cy) );
|
||||
d4 = cvGetSubRect( src_arr, cvRect(0,cy,cx,cy) );
|
||||
|
||||
if(swig_this(src_arr) != swig_this(dst_arr))
|
||||
if( !CV_ARE_TYPES_EQ( q1, d1 ))
|
||||
cvError( CV_StsUnmatchedFormats, \
|
||||
"cvShiftDFT", "Source and Destination arrays must have the same format", \
|
||||
__FILE__, __LINE__ );
|
||||
endif
|
||||
|
||||
cvCopy(q3, d1);
|
||||
cvCopy(q4, d2);
|
||||
cvCopy(q1, d3);
|
||||
cvCopy(q2, d4);
|
||||
|
||||
else
|
||||
cvCopy(q3, tmp);
|
||||
cvCopy(q1, q3);
|
||||
cvCopy(tmp, q1);
|
||||
cvCopy(q4, tmp);
|
||||
cvCopy(q2, q4);
|
||||
cvCopy(tmp, q2);
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
im = cvLoadImage( argv(){1}, CV_LOAD_IMAGE_GRAYSCALE);
|
||||
|
||||
realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
|
||||
imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
|
||||
complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);
|
||||
|
||||
cvScale(im, realInput, 1.0, 0.0);
|
||||
cvZero(imaginaryInput);
|
||||
cvMerge(realInput, imaginaryInput, [], [], complexInput);
|
||||
|
||||
dft_M = cvGetOptimalDFTSize( im.height - 1 );
|
||||
dft_N = cvGetOptimalDFTSize( im.width - 1 );
|
||||
|
||||
dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
|
||||
image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
|
||||
image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
|
||||
|
||||
## copy A to dft_A and pad dft_A with zeros
|
||||
tmp = cvGetSubRect( dft_A, cvRect(0,0, im.width, im.height));
|
||||
cvCopy( complexInput, tmp, [] );
|
||||
if(dft_A.width > im.width)
|
||||
tmp = cvGetSubRect( dft_A, cvRect(im.width,0, dft_N - im.width, im.height));
|
||||
cvZero( tmp );
|
||||
endif
|
||||
|
||||
## no need to pad bottom part of dft_A with zeros because of
|
||||
## use nonzero_rows parameter in cvDFT() call below
|
||||
|
||||
cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput.height );
|
||||
|
||||
cvNamedWindow("win", 0);
|
||||
cvNamedWindow("magnitude", 0);
|
||||
cvShowImage("win", im);
|
||||
|
||||
## Split Fourier in real and imaginary parts
|
||||
cvSplit( dft_A, image_Re, image_Im, [], [] );
|
||||
|
||||
## Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
|
||||
cvPow( image_Re, image_Re, 2.0);
|
||||
cvPow( image_Im, image_Im, 2.0);
|
||||
cvAdd( image_Re, image_Im, image_Re, []);
|
||||
cvPow( image_Re, image_Re, 0.5 );
|
||||
|
||||
## Compute log(1 + Mag)
|
||||
cvAddS( image_Re, cvScalarAll(1.0), image_Re, [] ); # 1 + Mag
|
||||
cvLog( 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] = cvMinMaxLoc(image_Re);
|
||||
cvScale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min));
|
||||
cvShowImage("magnitude", image_Re);
|
||||
|
||||
cvWaitKey(-1);
|
||||
76
samples/octave/distrans.m
Normal file
76
samples/octave/distrans.m
Normal file
@@ -0,0 +1,76 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global g;
|
||||
|
||||
g.wndname = "Distance transform";
|
||||
g.tbarname = "Threshold";
|
||||
|
||||
## The output images
|
||||
g.dist = 0;
|
||||
g.dist8u1 = 0;
|
||||
g.dist8u2 = 0;
|
||||
g.dist8u = 0;
|
||||
g.dist32s = 0;
|
||||
|
||||
g.gray = 0;
|
||||
g.edge = 0;
|
||||
|
||||
## define a trackbar callback
|
||||
function on_trackbar( edge_thresh )
|
||||
global g;
|
||||
global cv;
|
||||
|
||||
cvThreshold( g.gray, g.edge, double(edge_thresh), double(edge_thresh), cv.CV_THRESH_BINARY );
|
||||
## Distance transform
|
||||
cvDistTransform( g.edge, g.dist, cv.CV_DIST_L2, cv.CV_DIST_MASK_5, [], [] );
|
||||
|
||||
cvConvertScale( g.dist, g.dist, 5000.0, 0 );
|
||||
cvPow( g.dist, g.dist, 0.5 );
|
||||
|
||||
cvConvertScale( g.dist, g.dist32s, 1.0, 0.5 );
|
||||
cvAndS( g.dist32s, cvScalarAll(255), g.dist32s, [] );
|
||||
cvConvertScale( g.dist32s, g.dist8u1, 1, 0 );
|
||||
cvConvertScale( g.dist32s, g.dist32s, -1, 0 );
|
||||
cvAddS( g.dist32s, cvScalarAll(255), g.dist32s, [] );
|
||||
cvConvertScale( g.dist32s, g.dist8u2, 1, 0 );
|
||||
cvMerge( g.dist8u1, g.dist8u2, g.dist8u2, [], g.dist8u );
|
||||
cvShowImage( g.wndname, g.dist8u );
|
||||
endfunction
|
||||
|
||||
|
||||
edge_thresh = 100;
|
||||
|
||||
filename = "../c/stuff.jpg";
|
||||
if (size(argv, 1) > 1)
|
||||
filename = argv(){1};
|
||||
endif
|
||||
|
||||
g.gray = cvLoadImage( filename, 0 );
|
||||
if (!swig_this(g.gray))
|
||||
printf("Failed to load %s\n",filename);
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
## Create the output image
|
||||
g.dist = cvCreateImage( cvSize(g.gray.width,g.gray.height), IPL_DEPTH_32F, 1 );
|
||||
g.dist8u1 = cvCloneImage( g.gray );
|
||||
g.dist8u2 = cvCloneImage( g.gray );
|
||||
g.dist8u = cvCreateImage( cvSize(g.gray.width,g.gray.height), IPL_DEPTH_8U, 3 );
|
||||
g.dist32s = cvCreateImage( cvSize(g.gray.width,g.gray.height), IPL_DEPTH_32S, 1 );
|
||||
|
||||
## Convert to grayscale
|
||||
g.edge = cvCloneImage( g.gray );
|
||||
|
||||
## Create a window
|
||||
cvNamedWindow( g.wndname, 1 );
|
||||
|
||||
## create a toolbar
|
||||
cvCreateTrackbar( g.tbarname, g.wndname, edge_thresh, 255, @on_trackbar );
|
||||
|
||||
## Show the image
|
||||
on_trackbar(edge_thresh);
|
||||
|
||||
## Wait for a key stroke; the same function arranges events processing
|
||||
cvWaitKey(0);
|
||||
161
samples/octave/drawing.m
Normal file
161
samples/octave/drawing.m
Normal file
@@ -0,0 +1,161 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
printf("OpenCV Octave version of drawing\n");
|
||||
|
||||
## import the necessary things for OpenCV
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
function ret=random_color ()
|
||||
ret = CV_RGB(int32(rand()*255), int32(rand()*255), int32(rand()*255));
|
||||
endfunction
|
||||
|
||||
|
||||
## 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.cvCreateImage (cv.cvSize (width, height), 8, 3);
|
||||
|
||||
## create window and display the original picture in it
|
||||
highgui.cvNamedWindow (window_name, 1);
|
||||
cv.cvSetZero (image);
|
||||
highgui.cvShowImage (window_name, image);
|
||||
|
||||
## draw some lines
|
||||
for i=0:number-1,
|
||||
pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
|
||||
int32(rand() * 2 * height - height));
|
||||
pt2 = cv.cvPoint (int32(rand() * 2 * width - width),
|
||||
int32(rand() * 2 * height - height));
|
||||
cv.cvLine (image, pt1, pt2,
|
||||
random_color (),
|
||||
int32(rand() * 10),
|
||||
line_type, 0);
|
||||
|
||||
highgui.cvShowImage (window_name, image);
|
||||
highgui.cvWaitKey (delay);
|
||||
endfor
|
||||
|
||||
## draw some rectangles
|
||||
for i=0:number-1,
|
||||
pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
|
||||
int32(rand() * 2 * height - height));
|
||||
pt2 = cv.cvPoint (int32(rand() * 2 * width - width),
|
||||
int32(rand() * 2 * height - height));
|
||||
cv.cvRectangle (image, pt1, pt2,
|
||||
random_color (),
|
||||
int32(rand() * 10 - 1),
|
||||
line_type, 0);
|
||||
|
||||
highgui.cvShowImage (window_name, image);
|
||||
highgui.cvWaitKey (delay);
|
||||
endfor
|
||||
|
||||
## draw some ellipes
|
||||
for i=0:number-1,
|
||||
pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
|
||||
int32(rand() * 2 * height - height));
|
||||
sz = cv.cvSize (int32(rand() * 200),
|
||||
int32(rand() * 200));
|
||||
angle = rand() * 1000 * 0.180;
|
||||
cv.cvEllipse (image, pt1, sz, angle, angle - 100, angle + 200,
|
||||
random_color (),
|
||||
int32(rand() * 10 - 1),
|
||||
line_type, 0);
|
||||
|
||||
highgui.cvShowImage (window_name, image);
|
||||
highgui.cvWaitKey (delay);
|
||||
endfor
|
||||
|
||||
## init the list of polylines
|
||||
nb_polylines = 2;
|
||||
polylines_size = 3;
|
||||
pt = cell(1, nb_polylines);
|
||||
for a=1:nb_polylines,
|
||||
pt{a} = cell(1,polylines_size);
|
||||
endfor
|
||||
|
||||
## draw some polylines
|
||||
for i=0:number-1,
|
||||
for a=1:nb_polylines,
|
||||
for b=1:polylines_size,
|
||||
pt {a}{b} = cv.cvPoint (int32(rand() * 2 * width - width), \
|
||||
int32(rand() * 2 * height - height));
|
||||
endfor
|
||||
endfor
|
||||
cv.cvPolyLine (image, pt, 1, random_color(), int32(rand() * 8 + 1), line_type, 0);
|
||||
|
||||
highgui.cvShowImage (window_name, image);
|
||||
highgui.cvWaitKey (delay);
|
||||
endfor
|
||||
|
||||
## draw some filled polylines
|
||||
for i=0:number-1,
|
||||
for a=1:nb_polylines,
|
||||
for b=1:polylines_size,
|
||||
pt {a}{b} = cv.cvPoint (int32(rand() * 2 * width - width),
|
||||
int32(rand() * 2 * height - height));
|
||||
endfor
|
||||
endfor
|
||||
cv.cvFillPoly (image, pt, random_color (), line_type, 0);
|
||||
|
||||
highgui.cvShowImage (window_name, image);
|
||||
highgui.cvWaitKey (delay);
|
||||
endfor
|
||||
|
||||
## draw some circles
|
||||
for i=0:number-1,
|
||||
pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
|
||||
int32(rand() * 2 * height - height));
|
||||
cv.cvCircle (image, pt1, int32(rand() * 300), random_color (), \
|
||||
int32(rand() * 10 - 1), line_type, 0);
|
||||
|
||||
highgui.cvShowImage (window_name, image);
|
||||
highgui.cvWaitKey (delay);
|
||||
endfor
|
||||
|
||||
## draw some text
|
||||
for i=0:number-1,
|
||||
pt1 = cv.cvPoint (int32(rand() * 2 * width - width), \
|
||||
int32(rand() * 2 * height - height));
|
||||
font = cv.cvInitFont (int32(rand() * 8), \
|
||||
rand() * 100 * 0.05 + 0.01, \
|
||||
rand() * 100 * 0.05 + 0.01, \
|
||||
rand() * 5 * 0.1, \
|
||||
int32(rand() * 10), \
|
||||
line_type);
|
||||
|
||||
cv.cvPutText (image, "Testing text rendering!", \
|
||||
pt1, font, \
|
||||
random_color ());
|
||||
|
||||
highgui.cvShowImage (window_name, image);
|
||||
highgui.cvWaitKey (delay);
|
||||
endfor
|
||||
|
||||
## prepare a text, and get it's properties
|
||||
font = cv.cvInitFont (cv.CV_FONT_HERSHEY_COMPLEX, \
|
||||
3, 3, 0.0, 5, line_type);
|
||||
[text_size, ymin] = cv.cvGetTextSize ("OpenCV forever!", font);
|
||||
pt1.x = int32((width - text_size.width) / 2);
|
||||
pt1.y = int32((height + text_size.height) / 2);
|
||||
image2 = cv.cvCloneImage(image);
|
||||
|
||||
## now, draw some OpenCV pub ;-)
|
||||
for i=0:255-1,
|
||||
cv.cvSubS (image2, cv.cvScalarAll (i), image, []);
|
||||
cv.cvPutText (image, "OpenCV forever!",
|
||||
pt1, font, cv.cvScalar (255, i, i));
|
||||
highgui.cvShowImage (window_name, image);
|
||||
highgui.cvWaitKey (delay);
|
||||
endfor
|
||||
|
||||
## wait some key to end
|
||||
highgui.cvWaitKey (0);
|
||||
|
||||
69
samples/octave/edge.m
Normal file
69
samples/octave/edge.m
Normal file
@@ -0,0 +1,69 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
printf("OpenCV Octave version of edge\n");
|
||||
|
||||
global g;
|
||||
|
||||
## import the necessary things for OpenCV
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
## some definitions
|
||||
g.win_name = "Edge";
|
||||
g.trackbar_name = "Threshold";
|
||||
|
||||
## the callback on the trackbar
|
||||
function on_trackbar (position)
|
||||
global g;
|
||||
global cv;
|
||||
global highgui;
|
||||
|
||||
cv.cvSmooth (g.gray, g.edge, cv.CV_BLUR, 3, 3, 0);
|
||||
cv.cvNot (g.gray, g.edge);
|
||||
|
||||
## run the edge dector on gray scale
|
||||
cv.cvCanny (g.gray, g.edge, position, position * 3, 3);
|
||||
|
||||
## reset
|
||||
cv.cvSetZero (g.col_edge);
|
||||
|
||||
## copy edge points
|
||||
cv.cvCopy (g.image, g.col_edge, g.edge);
|
||||
|
||||
## show the image
|
||||
highgui.cvShowImage (g.win_name, g.col_edge);
|
||||
endfunction
|
||||
|
||||
filename = "../c/fruits.jpg";
|
||||
|
||||
if (size(argv, 1)>1)
|
||||
filename = argv(){1};
|
||||
endif
|
||||
|
||||
## load the image gived on the command line
|
||||
g.image = highgui.cvLoadImage (filename);
|
||||
|
||||
if (!swig_this(g.image))
|
||||
printf("Error loading image '%s'",filename);
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
## create the output image
|
||||
g.col_edge = cv.cvCreateImage (cv.cvSize (g.image.width, g.image.height), 8, 3);
|
||||
|
||||
## convert to grayscale
|
||||
g.gray = cv.cvCreateImage (cv.cvSize (g.image.width, g.image.height), 8, 1);
|
||||
g.edge = cv.cvCreateImage (cv.cvSize (g.image.width, g.image.height), 8, 1);
|
||||
cv.cvCvtColor (g.image, g.gray, cv.CV_BGR2GRAY);
|
||||
|
||||
## create the window
|
||||
highgui.cvNamedWindow (g.win_name, highgui.CV_WINDOW_AUTOSIZE);
|
||||
|
||||
## create the trackbar
|
||||
highgui.cvCreateTrackbar (g.trackbar_name, g.win_name, 1, 100, @on_trackbar);
|
||||
|
||||
## show the image
|
||||
on_trackbar (0);
|
||||
|
||||
## wait a key pressed to end
|
||||
highgui.cvWaitKey (0);
|
||||
143
samples/octave/facedetect.m
Normal file
143
samples/octave/facedetect.m
Normal file
@@ -0,0 +1,143 @@
|
||||
#! /usr/bin/env octave
|
||||
## 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
|
||||
## Octave implementation by: Xavier Delacour
|
||||
addpath("/home/x/opencv2/interfaces/swig/octave");
|
||||
source("/home/x/opencv2/interfaces/swig/octave/PKG_ADD_template");
|
||||
debug_on_error(true);
|
||||
debug_on_warning(true);
|
||||
crash_dumps_octave_core (0)
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
|
||||
## Global Variables
|
||||
global g;
|
||||
g.cascade = [];
|
||||
g.storage = cvCreateMemStorage(0);
|
||||
g.cascade_name = "../../data/haarcascades/haarcascade_frontalface_alt.xml";
|
||||
g.input_name = "../c/lena.jpg";
|
||||
|
||||
## Parameters for haar detection
|
||||
## From the API:
|
||||
## The default parameters (scale_factor=1.1, 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
|
||||
g.min_size = cvSize(20,20);
|
||||
g.image_scale = 1.3;
|
||||
g.haar_scale = 1.2;
|
||||
g.min_neighbors = 2;
|
||||
g.haar_flags = 0;
|
||||
|
||||
|
||||
function detect_and_draw( img )
|
||||
global g;
|
||||
global cv;
|
||||
|
||||
gray = cvCreateImage( cvSize(img.width,img.height), 8, 1 );
|
||||
small_img = cvCreateImage( cvSize( cvRound (img.width/g.image_scale),
|
||||
cvRound (img.height/g.image_scale)), 8, 1 );
|
||||
cvCvtColor( img, gray, cv.CV_BGR2GRAY );
|
||||
cvResize( gray, small_img, cv.CV_INTER_LINEAR );
|
||||
|
||||
cvEqualizeHist( small_img, small_img );
|
||||
|
||||
cvClearMemStorage( g.storage );
|
||||
|
||||
if( swig_this(g.cascade) )
|
||||
tic
|
||||
faces = cvHaarDetectObjects( small_img, g.cascade, g.storage,
|
||||
g.haar_scale, g.min_neighbors, g.haar_flags, g.min_size );
|
||||
toc
|
||||
if (swig_this(faces))
|
||||
for r = CvSeq_map(faces),
|
||||
r = r{1};
|
||||
pt1 = cvPoint( int32(r.x*g.image_scale), int32(r.y*g.image_scale));
|
||||
pt2 = cvPoint( int32((r.x+r.width)*g.image_scale), int32((r.y+r.height)*g.image_scale) );
|
||||
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
|
||||
endfor
|
||||
endif
|
||||
endif
|
||||
|
||||
cvShowImage( "result", img );
|
||||
endfunction
|
||||
|
||||
|
||||
if (size(argv, 2) > 0 && (strcmp(argv(){1}, "--help") || strcmp(argv(){1}, "-h")))
|
||||
printf("Usage: facedetect --cascade \"<cascade_path>\" [filename|camera_index]\n");
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
if (size(argv, 2) >= 2)
|
||||
if (strcmp(argv(){1},"--cascade"))
|
||||
g.cascade_name = argv(){2};
|
||||
if (size(argv, 2) >= 3)
|
||||
g.input_name = argv(){3};
|
||||
endif
|
||||
endif
|
||||
elseif (size(argv, 2) == 1)
|
||||
g.input_name = argv(){1};
|
||||
endif
|
||||
|
||||
## the OpenCV API says this function is obsolete, but we can't
|
||||
## cast the output of cvLoad to a HaarClassifierCascade, so use this anyways
|
||||
## the size parameter is ignored
|
||||
g.cascade = cvLoadHaarClassifierCascade( g.cascade_name, cvSize(1,1) );
|
||||
|
||||
if (!swig_this(g.cascade))
|
||||
printf("ERROR: Could not load classifier cascade\n");
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
g.input_name
|
||||
|
||||
if (all(isdigit(g.input_name)))
|
||||
capture = cvCreateCameraCapture( sscanf(g.input_name, "%i") );
|
||||
else
|
||||
capture = cvCreateFileCapture( g.input_name );
|
||||
endif
|
||||
|
||||
capture
|
||||
|
||||
cvNamedWindow( "result", 1 );
|
||||
|
||||
if( swig_this(capture) )
|
||||
frame_copy = [];
|
||||
while (true)
|
||||
frame = cvQueryFrame( capture );
|
||||
if( ! swig_this(frame) )
|
||||
cvWaitKey(0);
|
||||
endif
|
||||
if( !swig_this(frame_copy) )
|
||||
frame_copy = cvCreateImage( cvSize(frame.width,frame.height),
|
||||
IPL_DEPTH_8U, frame.nChannels );
|
||||
endif
|
||||
if( frame.origin == IPL_ORIGIN_TL )
|
||||
cvCopy( frame, frame_copy );
|
||||
else
|
||||
cvFlip( frame, frame_copy, 0 );
|
||||
endif
|
||||
|
||||
detect_and_draw( frame_copy );
|
||||
|
||||
if( cvWaitKey( 10 ) == 27 )
|
||||
break;
|
||||
endif
|
||||
endwhile
|
||||
|
||||
else
|
||||
image = cvLoadImage( g.input_name, 1 );
|
||||
|
||||
if( swig_this(image) )
|
||||
|
||||
detect_and_draw( image );
|
||||
cvWaitKey(0);
|
||||
endif
|
||||
endif
|
||||
|
||||
cvDestroyWindow("result");
|
||||
179
samples/octave/ffilldemo.m
Normal file
179
samples/octave/ffilldemo.m
Normal file
@@ -0,0 +1,179 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global g;
|
||||
g.color_img0 = [];
|
||||
g.mask = [];
|
||||
g.color_img = [];
|
||||
g.gray_img0 = [];
|
||||
g.gray_img = [];
|
||||
g.ffill_case = 1;
|
||||
g.lo_diff = 20
|
||||
g.up_diff = 20;
|
||||
g.connectivity = 4;
|
||||
g.is_color = 1;
|
||||
g.is_mask = 0;
|
||||
g.new_mask_val = 255;
|
||||
|
||||
function ret = randint(v1, v2)
|
||||
ret = int32(rand() * (v2 - v1) + v1);
|
||||
end
|
||||
|
||||
function update_lo( pos )
|
||||
g.lo_diff = pos;
|
||||
endfunction
|
||||
function update_up( pos )
|
||||
g.up_diff = pos;
|
||||
endfunction
|
||||
|
||||
function on_mouse( event, x, y, flags, param )
|
||||
global g;
|
||||
global cv;
|
||||
global highgui;
|
||||
|
||||
if( !swig_this(g.color_img) )
|
||||
return;
|
||||
endif
|
||||
|
||||
if (event == highgui.CV_EVENT_LBUTTONDOWN)
|
||||
comp = cv.CvConnectedComp();
|
||||
my_mask = [];
|
||||
seed = cvPoint(x,y);
|
||||
if (g.ffill_case==0)
|
||||
lo = 0;
|
||||
up = 0;
|
||||
flags = g.connectivity + bitshift(g.new_mask_val,8);
|
||||
else
|
||||
lo = g.lo_diff;
|
||||
up = g.up_diff;
|
||||
flags = g.connectivity + bitshift(g.new_mask_val,8) + \
|
||||
cv.CV_FLOODFILL_FIXED_RANGE;
|
||||
endif
|
||||
color = CV_RGB( randint(0,255), randint(0,255), randint(0,255) );
|
||||
|
||||
if( g.is_mask )
|
||||
my_mask = g.mask;
|
||||
cvThreshold( g.mask, g.mask, 1, 128, cv.CV_THRESH_BINARY );
|
||||
endif
|
||||
|
||||
if( g.is_color )
|
||||
cv.cvFloodFill( g.color_img, seed, color, cv.CV_RGB( lo, lo, lo ),
|
||||
CV_RGB( up, up, up ), comp, flags, my_mask );
|
||||
cvShowImage( "image", g.color_img );
|
||||
|
||||
else
|
||||
|
||||
brightness = cvRealScalar((r*2 + g*7 + b + 5)/10);
|
||||
cvFloodFill( g.gray_img, seed, brightness, cvRealScalar(lo),
|
||||
cvRealScalar(up), comp, flags, my_mask );
|
||||
cvShowImage( "image", g.gray_img );
|
||||
endif
|
||||
|
||||
|
||||
printf("%i pixels were repainted\n", comp.area);
|
||||
|
||||
if( g.is_mask )
|
||||
cvShowImage( "mask", g.mask );
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
filename = "../c/fruits.jpg";
|
||||
if (size(argv, 1)>0)
|
||||
filename=argv(){1};
|
||||
endif
|
||||
|
||||
g.color_img0 = cvLoadImage(filename,1);
|
||||
if (!swig_this(g.color_img0))
|
||||
printf("Could not open %s\n",filename);
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
printf("Hot keys:\n");
|
||||
printf("\tESC - quit the program\n");
|
||||
printf("\tc - switch color/grayscale mode\n");
|
||||
printf("\tm - switch mask mode\n");
|
||||
printf("\tr - restore the original image\n");
|
||||
printf("\ts - use null-range floodfill\n");
|
||||
printf("\tf - use gradient floodfill with fixed(absolute) range\n");
|
||||
printf("\tg - use gradient floodfill with floating(relative) range\n");
|
||||
printf("\t4 - use 4-g.connectivity mode\n");
|
||||
printf("\t8 - use 8-g.connectivity mode\n");
|
||||
|
||||
g.color_img = cvCloneImage( g.color_img0 );
|
||||
g.gray_img0 = cvCreateImage( cvSize(g.color_img.width, g.color_img.height), 8, 1 );
|
||||
cvCvtColor( g.color_img, g.gray_img0, CV_BGR2GRAY );
|
||||
g.gray_img = cvCloneImage( g.gray_img0 );
|
||||
g.mask = cvCreateImage( cvSize(g.color_img.width + 2, g.color_img.height + 2), 8, 1 );
|
||||
|
||||
cvNamedWindow( "image", 1 );
|
||||
cvCreateTrackbar( "g.lo_diff", "image", g.lo_diff, 255, @update_lo);
|
||||
cvCreateTrackbar( "g.up_diff", "image", g.up_diff, 255, @update_up);
|
||||
|
||||
cvSetMouseCallback( "image", @on_mouse );
|
||||
|
||||
while (true)
|
||||
if( g.is_color )
|
||||
cvShowImage( "image", g.color_img );
|
||||
else
|
||||
cvShowImage( "image", g.gray_img );
|
||||
endif
|
||||
|
||||
c = cvWaitKey(0);
|
||||
if (c==27)
|
||||
printf("Exiting ...\n");
|
||||
exit(0)
|
||||
elseif (c=='c')
|
||||
if( g.is_color )
|
||||
|
||||
print("Grayscale mode is set");
|
||||
cvCvtColor( g.color_img, g.gray_img, CV_BGR2GRAY );
|
||||
g.is_color = 0;
|
||||
|
||||
else
|
||||
|
||||
print("Color mode is set");
|
||||
cvCopy( g.color_img0, g.color_img, [] );
|
||||
cvZero( g.mask );
|
||||
g.is_color = 1;
|
||||
endif
|
||||
|
||||
elseif (c=='m')
|
||||
if( g.is_mask )
|
||||
cvDestroyWindow( "mask" );
|
||||
g.is_mask = 0;
|
||||
|
||||
else
|
||||
cvNamedWindow( "mask", 0 );
|
||||
cvZero( g.mask );
|
||||
cvShowImage( "mask", g.mask );
|
||||
g.is_mask = 1;
|
||||
endif
|
||||
|
||||
elseif (c=='r')
|
||||
printf("Original image is restored");
|
||||
cvCopy( g.color_img0, g.color_img, [] );
|
||||
cvCopy( g.gray_img0, g.gray_img, [] );
|
||||
cvZero( g.mask );
|
||||
elseif (c=='s')
|
||||
printf("Simple floodfill mode is set");
|
||||
g.ffill_case = 0;
|
||||
elseif (c=='f')
|
||||
printf("Fixed Range floodfill mode is set");
|
||||
g.ffill_case = 1;
|
||||
elseif (c=='g')
|
||||
printf("Gradient (floating range) floodfill mode is set");
|
||||
g.ffill_case = 2;
|
||||
elseif (c=='4')
|
||||
printf("4-g.connectivity mode is set");
|
||||
g.connectivity = 4;
|
||||
elseif (c=='8')
|
||||
printf("8-g.connectivity mode is set");
|
||||
g.connectivity = 8;
|
||||
endif
|
||||
|
||||
endwhile
|
||||
123
samples/octave/fitellipse.m
Normal file
123
samples/octave/fitellipse.m
Normal file
@@ -0,0 +1,123 @@
|
||||
#! /usr/bin/env octave
|
||||
## This program is demonstration for ellipse fitting. Program finds
|
||||
## contours and approximate it by ellipses.
|
||||
|
||||
## Trackbar specify threshold parametr.
|
||||
|
||||
## White lines is contours. Red lines is fitting ellipses.
|
||||
|
||||
## Original C implementation by: Denis Burenkov.
|
||||
## Python implementation by: Roman Stanchak
|
||||
## Octave implementation by: Xavier Delacour
|
||||
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global g;
|
||||
g.image02 = [];
|
||||
g.image03 = [];
|
||||
g.image04 = [];
|
||||
|
||||
function process_image( slider_pos )
|
||||
global g;
|
||||
global cv;
|
||||
global highgui;
|
||||
|
||||
## Define trackbar callback functon. This function find contours,
|
||||
## draw it and approximate it by ellipses.
|
||||
stor = cv.cvCreateMemStorage(0);
|
||||
|
||||
## Threshold the source image. This needful for cv.cvFindContours().
|
||||
cv.cvThreshold( g.image03, g.image02, slider_pos, 255, cv.CV_THRESH_BINARY );
|
||||
|
||||
## Find all contours.
|
||||
[nb_contours, cont] = cv.cvFindContours (g.image02,stor,cv.sizeof_CvContour,cv.CV_RETR_LIST,cv.CV_CHAIN_APPROX_NONE,cv.cvPoint (0,0));
|
||||
|
||||
## Clear images. IPL use.
|
||||
cv.cvZero(g.image02);
|
||||
cv.cvZero(g.image04);
|
||||
|
||||
## This cycle draw all contours and approximate it by ellipses.
|
||||
for c = cv.CvSeq_hrange(cont),
|
||||
c = c{1};
|
||||
count = c.total; # This is number point in contour
|
||||
|
||||
## Number point must be more than or equal to 6 (for cv.cvFitEllipse_32f).
|
||||
if( count < 6 )
|
||||
continue;
|
||||
endif
|
||||
|
||||
## Alloc memory for contour point set.
|
||||
PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2);
|
||||
PointArray2D32f= cv.cvCreateMat( 1, count, cv.CV_32FC2);
|
||||
|
||||
## Get contour point set.
|
||||
cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX));
|
||||
|
||||
## Convert CvPoint set to CvBox2D32f set.
|
||||
cv.cvConvert( PointArray, PointArray2D32f );
|
||||
|
||||
box = cv.CvBox2D();
|
||||
|
||||
## Fits ellipse to current contour.
|
||||
box = cv.cvFitEllipse2(PointArray2D32f);
|
||||
|
||||
## Draw current contour.
|
||||
cv.cvDrawContours(g.image04, c, cv.CV_RGB(255,255,255), cv.CV_RGB(255,255,255),0,1,8,cv.cvPoint(0,0));
|
||||
|
||||
## Convert ellipse data from float to integer representation.
|
||||
center = cv.CvPoint();
|
||||
size = cv.CvSize();
|
||||
center.x = cv.cvRound(box.center.x);
|
||||
center.y = cv.cvRound(box.center.y);
|
||||
size.width = cv.cvRound(box.size.width*0.5);
|
||||
size.height = cv.cvRound(box.size.height*0.5);
|
||||
box.angle = -box.angle;
|
||||
|
||||
## Draw ellipse.
|
||||
cv.cvEllipse(g.image04, center, size,box.angle, 0, 360,cv.CV_RGB(0,0,255), 1, cv.CV_AA, 0);
|
||||
endfor
|
||||
|
||||
## Show image. HighGUI use.
|
||||
highgui.cvShowImage( "Result", g.image04 );
|
||||
endfunction
|
||||
|
||||
argc = size(argv, 1);
|
||||
filename = "../c/stuff.jpg";
|
||||
if(argc == 2)
|
||||
filename = argv(){1};
|
||||
endif
|
||||
|
||||
slider_pos = 70;
|
||||
|
||||
## load image and force it to be grayscale
|
||||
g.image03 = highgui.cvLoadImage(filename, 0);
|
||||
if (!swig_this( g.image03))
|
||||
printf("Could not load image %s\n", filename);
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
## Create the destination images
|
||||
g.image02 = cv.cvCloneImage( g.image03 );
|
||||
g.image04 = cv.cvCloneImage( g.image03 );
|
||||
|
||||
## Create windows.
|
||||
highgui.cvNamedWindow("Source", 1);
|
||||
highgui.cvNamedWindow("Result", 1);
|
||||
|
||||
## Show the image.
|
||||
highgui.cvShowImage("Source", g.image03);
|
||||
|
||||
## Create toolbars. HighGUI use.
|
||||
highgui.cvCreateTrackbar( "Threshold", "Result", slider_pos, 255, @process_image );
|
||||
|
||||
|
||||
process_image( 1 );
|
||||
|
||||
## Wait for a key stroke; the same function arranges events processing
|
||||
printf("Press any key to exit\n");
|
||||
highgui.cvWaitKey(0);
|
||||
|
||||
highgui.cvDestroyWindow("Source");
|
||||
highgui.cvDestroyWindow("Result");
|
||||
|
||||
62
samples/octave/houghlines.m
Normal file
62
samples/octave/houghlines.m
Normal file
@@ -0,0 +1,62 @@
|
||||
#! /usr/bin/env octave
|
||||
## This is a standalone program. Pass an image name as a first parameter of the program.
|
||||
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
## toggle between CV_HOUGH_STANDARD and CV_HOUGH_PROBILISTIC
|
||||
USE_STANDARD=0;
|
||||
|
||||
filename = "../../docs/ref/pics/building.jpg"
|
||||
if (size(argv, 1)>=1)
|
||||
filename = argv(){1};
|
||||
endif
|
||||
|
||||
src=cvLoadImage(filename, 0);
|
||||
if (!swig_this(src))
|
||||
printf("Error opening image %s\n",filename);
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
dst = cvCreateImage( cvGetSize(src), 8, 1 );
|
||||
color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
|
||||
storage = cvCreateMemStorage(0);
|
||||
lines = 0;
|
||||
cvCanny( src, dst, 50, 200, 3 );
|
||||
cvCvtColor( dst, color_dst, CV_GRAY2BGR );
|
||||
|
||||
if (USE_STANDARD)
|
||||
lines = cvHoughLines2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 );
|
||||
|
||||
for i=0:min(lines.total, 100)-1,
|
||||
line = lines{i};
|
||||
rho = line{0};
|
||||
theta = line{1};
|
||||
pt1 = CvPoint();
|
||||
pt2 = CvPoint();
|
||||
a = cos(theta);
|
||||
b = sin(theta);
|
||||
x0 = a*rho;
|
||||
y0 = b*rho;
|
||||
pt1.x = cvRound(x0 + 1000*(-b));
|
||||
pt1.y = cvRound(y0 + 1000*(a));
|
||||
pt2.x = cvRound(x0 - 1000*(-b));
|
||||
pt2.y = cvRound(y0 - 1000*(a));
|
||||
cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
|
||||
endfor
|
||||
|
||||
else
|
||||
lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );
|
||||
for line = CvSeq_map(lines),
|
||||
line = line{1};
|
||||
cvLine( color_dst, line{0}, line{1}, CV_RGB(255,0,0), 3, 8 );
|
||||
endfor
|
||||
endif
|
||||
|
||||
cvNamedWindow( "Source", 1 );
|
||||
cvShowImage( "Source", src );
|
||||
|
||||
cvNamedWindow( "Hough", 1 );
|
||||
cvShowImage( "Hough", color_dst );
|
||||
|
||||
cvWaitKey(0);
|
||||
84
samples/octave/inpaint.m
Normal file
84
samples/octave/inpaint.m
Normal file
@@ -0,0 +1,84 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global g;
|
||||
inpaint_mask = [];
|
||||
g.img0 = [];
|
||||
g.img = [];
|
||||
g.inpainted = [];
|
||||
g.prev_pt = cvPoint(-1,-1);
|
||||
|
||||
function on_mouse( event, x, y, flags, param )
|
||||
global g;
|
||||
global cv;
|
||||
global highgui;
|
||||
if (!swig_this(g.img))
|
||||
return;
|
||||
endif
|
||||
|
||||
if (event == highgui.CV_EVENT_LBUTTONUP || ! (bitand(flags,highgui.CV_EVENT_FLAG_LBUTTON)))
|
||||
g.prev_pt = cvPoint(-1,-1);
|
||||
elseif (event == highgui.CV_EVENT_LBUTTONDOWN)
|
||||
g.prev_pt = cvPoint(x,y);
|
||||
elseif (event == highgui.CV_EVENT_MOUSEMOVE && bitand(flags,highgui.CV_EVENT_FLAG_LBUTTON))
|
||||
pt = cvPoint(x,y);
|
||||
if (g.prev_pt.x < 0)
|
||||
g.prev_pt = pt;
|
||||
endif
|
||||
cvLine( g.inpaint_mask, g.prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
|
||||
cvLine( g.img, g.prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
|
||||
g.prev_pt = pt;
|
||||
cvShowImage( "image", g.img );
|
||||
endif
|
||||
endfunction
|
||||
|
||||
filename = "../c/fruits.jpg";
|
||||
if (size(argv, 1)>=1)
|
||||
filename = argv(){1};
|
||||
endif
|
||||
|
||||
g.img0 = cvLoadImage(filename,-1);
|
||||
if (!swig_this(g.img0))
|
||||
printf("Can't open image '%s'\n", filename);
|
||||
exit(1);
|
||||
endif
|
||||
|
||||
printf("Hot keys:\n");
|
||||
printf("\tESC - quit the program\n");
|
||||
printf("\tr - restore the original image\n");
|
||||
printf("\ti or ENTER - run inpainting algorithm\n");
|
||||
printf("\t\t(before running it, paint something on the image)\n");
|
||||
|
||||
cvNamedWindow( "image", 1 );
|
||||
|
||||
g.img = cvCloneImage( g.img0 );
|
||||
g.inpainted = cvCloneImage( g.img0 );
|
||||
g.inpaint_mask = cvCreateImage( cvGetSize(g.img), 8, 1 );
|
||||
|
||||
cvZero( g.inpaint_mask );
|
||||
cvZero( g.inpainted );
|
||||
cvShowImage( "image", g.img );
|
||||
cvShowImage( "watershed transform", g.inpainted );
|
||||
cvSetMouseCallback( "image", @on_mouse, [] );
|
||||
|
||||
while (true)
|
||||
c = cvWaitKey(0);
|
||||
|
||||
if( c == 27 || c=='q')
|
||||
break;
|
||||
endif
|
||||
|
||||
if( c == 'r' )
|
||||
cvZero( g.inpaint_mask );
|
||||
cvCopy( g.img0, g.img );
|
||||
cvShowImage( "image", g.img );
|
||||
endif
|
||||
|
||||
if( c == 'i' || c == '\012' )
|
||||
cvNamedWindow( "g.inpainted image", 1 );
|
||||
cvInpaint( g.img, g.inpaint_mask, g.inpainted, 3, CV_INPAINT_TELEA );
|
||||
cvShowImage( "g.inpainted image", g.inpainted );
|
||||
endif
|
||||
endwhile
|
||||
|
||||
102
samples/octave/kalman.m
Normal file
102
samples/octave/kalman.m
Normal file
@@ -0,0 +1,102 @@
|
||||
#! /usr/bin/env octave
|
||||
## 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.
|
||||
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global img;
|
||||
|
||||
function ret=calc_point(angle)
|
||||
global img;
|
||||
ret=cvPoint( cvRound(img.width/2 + img.width/3*cos(angle)), \
|
||||
cvRound(img.height/2 - img.width/3*sin(angle)));
|
||||
endfunction
|
||||
|
||||
function draw_cross( center, color, d )
|
||||
global img;
|
||||
global CV_AA;
|
||||
cvLine( img, cvPoint( center.x - d, center.y - d ),
|
||||
cvPoint( center.x + d, center.y + d ), color, 1, CV_AA, 0);
|
||||
cvLine( img, cvPoint( center.x + d, center.y - d ),
|
||||
cvPoint( center.x - d, center.y + d ), \
|
||||
color, 1, CV_AA, 0 );
|
||||
endfunction
|
||||
|
||||
A = [ 1, 1; 0, 1 ];
|
||||
|
||||
img = cvCreateImage( cvSize(500,500), 8, 3 );
|
||||
kalman = cvCreateKalman( 2, 1, 0 );
|
||||
state = cvCreateMat( 2, 1, CV_32FC1 ); # (phi, delta_phi)
|
||||
process_noise = cvCreateMat( 2, 1, CV_32FC1 );
|
||||
measurement = cvCreateMat( 1, 1, CV_32FC1 );
|
||||
rng = cvRNG(-1);
|
||||
code = -1;
|
||||
|
||||
cvZero( measurement );
|
||||
cvNamedWindow( "Kalman", 1 );
|
||||
|
||||
while (true),
|
||||
cvRandArr( rng, state, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
|
||||
|
||||
kalman.transition_matrix = mat2cv(A, CV_32FC1);
|
||||
cvSetIdentity( kalman.measurement_matrix, cvRealScalar(1) );
|
||||
cvSetIdentity( kalman.process_noise_cov, cvRealScalar(1e-5) );
|
||||
cvSetIdentity( kalman.measurement_noise_cov, cvRealScalar(1e-1) );
|
||||
cvSetIdentity( kalman.error_cov_post, cvRealScalar(1));
|
||||
cvRandArr( rng, kalman.state_post, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
|
||||
|
||||
while (true),
|
||||
|
||||
state_angle = state(0);
|
||||
state_pt = calc_point(state_angle);
|
||||
|
||||
prediction = cvKalmanPredict( kalman );
|
||||
predict_angle = prediction(0);
|
||||
predict_pt = calc_point(predict_angle);
|
||||
|
||||
cvRandArr( rng, measurement, CV_RAND_NORMAL, cvRealScalar(0), \
|
||||
cvRealScalar(sqrt(kalman.measurement_noise_cov(0))) );
|
||||
|
||||
## generate measurement
|
||||
cvMatMulAdd( kalman.measurement_matrix, state, measurement, measurement );
|
||||
|
||||
measurement_angle = measurement(0);
|
||||
measurement_pt = calc_point(measurement_angle);
|
||||
|
||||
## plot points
|
||||
cvZero( img );
|
||||
draw_cross( state_pt, CV_RGB(255,255,255), 3 );
|
||||
draw_cross( measurement_pt, CV_RGB(255,0,0), 3 );
|
||||
draw_cross( predict_pt, CV_RGB(0,255,0), 3 );
|
||||
cvLine( img, state_pt, measurement_pt, CV_RGB(255,0,0), 3, CV_AA, 0 );
|
||||
cvLine( img, state_pt, predict_pt, CV_RGB(255,255,0), 3, CV_AA, 0 );
|
||||
|
||||
cvKalmanCorrect( kalman, measurement );
|
||||
|
||||
cvRandArr( rng, process_noise, CV_RAND_NORMAL, cvRealScalar(0), \
|
||||
cvRealScalar(sqrt(kalman.process_noise_cov(0)(0))));
|
||||
cvMatMulAdd( kalman.transition_matrix, state, process_noise, state );
|
||||
|
||||
cvShowImage( "Kalman", img );
|
||||
code = cvWaitKey( 100 );
|
||||
|
||||
if( code > 0 )
|
||||
break;
|
||||
endif
|
||||
endwhile
|
||||
|
||||
if( code == '\x1b' || code == 'q' || code == 'Q' )
|
||||
break;
|
||||
endif
|
||||
endwhile
|
||||
|
||||
cvDestroyWindow("Kalman");
|
||||
72
samples/octave/kmeans.m
Normal file
72
samples/octave/kmeans.m
Normal file
@@ -0,0 +1,72 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
MAX_CLUSTERS=5;
|
||||
|
||||
function ret = randint(v1, v2)
|
||||
ret = int32(rand() * (v2 - v1) + v1);
|
||||
end
|
||||
|
||||
color_tab = { \
|
||||
CV_RGB(255,0,0), \
|
||||
CV_RGB(0,255,0), \
|
||||
CV_RGB(100,100,255), \
|
||||
CV_RGB(255,0,255), \
|
||||
CV_RGB(255,255,0)};
|
||||
img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
|
||||
rng = cvRNG(-1);
|
||||
|
||||
cvNamedWindow( "clusters", 1 );
|
||||
|
||||
while (true),
|
||||
cluster_count = randint(2, MAX_CLUSTERS);
|
||||
sample_count = randint(1, 1000);
|
||||
points = cvCreateMat( sample_count, 1, CV_32FC2 );
|
||||
clusters = cvCreateMat( sample_count, 1, CV_32SC1 );
|
||||
|
||||
## generate random sample from multigaussian distribution
|
||||
for k=0:cluster_count-1,
|
||||
center = CvPoint();
|
||||
center.x = mod(cvRandInt(rng), img.width);
|
||||
center.y = mod(cvRandInt(rng), img.height);
|
||||
first = k*sample_count/cluster_count;
|
||||
last = sample_count;
|
||||
if (k != cluster_count)
|
||||
last = (k+1)*sample_count/cluster_count;
|
||||
endif
|
||||
|
||||
point_chunk = cvGetRows(points, first, last);
|
||||
|
||||
cvRandArr( rng, point_chunk, CV_RAND_NORMAL, \
|
||||
cvScalar(center.x,center.y,0,0), \
|
||||
cvScalar(img.width*0.1,img.height*0.1,0,0));
|
||||
endfor
|
||||
|
||||
|
||||
## shuffle samples
|
||||
cvRandShuffle( points, rng );
|
||||
|
||||
cvKMeans2( points, cluster_count, clusters, \
|
||||
cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ));
|
||||
|
||||
cvZero( img );
|
||||
|
||||
for i=0:sample_count-1,
|
||||
cluster_idx = clusters(i);
|
||||
pt = points(i);
|
||||
cvCircle( img, pt, 2, color_tab{cluster_idx + 1}, CV_FILLED, CV_AA, 0 );
|
||||
|
||||
cvCircle( img, pt, 2, color_tab{cluster_idx + 1}, CV_FILLED, CV_AA, 0 );
|
||||
endfor
|
||||
|
||||
|
||||
cvShowImage( "clusters", img );
|
||||
|
||||
key = cvWaitKey(0);
|
||||
if( key == 27 || key == 'q' || key == 'Q' )
|
||||
break;
|
||||
endif
|
||||
endwhile
|
||||
|
||||
|
||||
cvDestroyWindow( "clusters" );
|
||||
65
samples/octave/laplace.m
Normal file
65
samples/octave/laplace.m
Normal file
@@ -0,0 +1,65 @@
|
||||
#! /usr/bin/env octave
|
||||
addpath("/home/x/opencv2/interfaces/swig/octave");
|
||||
source("/home/x/opencv2/interfaces/swig/octave/PKG_ADD_template");
|
||||
debug_on_error(true);
|
||||
debug_on_warning(true);
|
||||
crash_dumps_octave_core (0)
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
laplace = [];
|
||||
colorlaplace = [];
|
||||
planes = { [], [], [] };
|
||||
capture = [];
|
||||
|
||||
if (size(argv, 2)==0)
|
||||
capture = cvCreateCameraCapture( -1 );
|
||||
elseif (size(argv, 2)==1 && all(isdigit(argv(){1})))
|
||||
capture = cvCreateCameraCapture( int32(argv(){1}) );
|
||||
elseif (size(argv, 2)==1)
|
||||
capture = cvCreateFileCapture( argv(){1} );
|
||||
endif
|
||||
|
||||
if (!swig_this(capture))
|
||||
printf("Could not initialize capturing...\n");
|
||||
exit(-1)
|
||||
endif
|
||||
|
||||
cvNamedWindow( "Laplacian", 1 );
|
||||
|
||||
while (true),
|
||||
frame = cvQueryFrame( capture );
|
||||
if (!swig_this(frame))
|
||||
break
|
||||
endif
|
||||
|
||||
if (!swig_this(laplace))
|
||||
for i=1:size(planes,2),
|
||||
planes{i} = cvCreateImage( \
|
||||
cvSize(frame.width,frame.height), \
|
||||
8, 1 );
|
||||
endfor
|
||||
laplace = cvCreateImage( cvSize(frame.width,frame.height), IPL_DEPTH_16S, 1 );
|
||||
colorlaplace = cvCreateImage( \
|
||||
cvSize(frame.width,frame.height), \
|
||||
8, 3 );
|
||||
endif
|
||||
|
||||
cvSplit( frame, planes{1}, planes{2}, planes{3}, [] );
|
||||
for plane = planes,
|
||||
plane = plane{1};
|
||||
cvLaplace( plane, laplace, 3 );
|
||||
cvConvertScaleAbs( laplace, plane, 1, 0 );
|
||||
endfor
|
||||
|
||||
cvMerge( planes{1}, planes{2}, planes{3}, [], colorlaplace );
|
||||
# colorlaplace.origin = frame.origin;
|
||||
|
||||
cvShowImage("Laplacian", colorlaplace );
|
||||
|
||||
if (cvWaitKey(10) == 27)
|
||||
break;
|
||||
endif
|
||||
endwhile
|
||||
|
||||
cvDestroyWindow("Laplacian");
|
||||
219
samples/octave/lkdemo.m
Normal file
219
samples/octave/lkdemo.m
Normal file
@@ -0,0 +1,219 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
printf("OpenCV Octave version of lkdemo\n");
|
||||
|
||||
## import the necessary things for OpenCV
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
#############################################################################
|
||||
## some "constants"
|
||||
|
||||
win_size = 10;
|
||||
MAX_COUNT = 500;
|
||||
|
||||
#############################################################################
|
||||
## some "global" variables
|
||||
|
||||
global g;
|
||||
|
||||
g.image = [];
|
||||
g.pt = [];
|
||||
g.add_remove_pt = false;
|
||||
g.flags = 0;
|
||||
g.night_mode = false;
|
||||
g.need_to_init = true;
|
||||
|
||||
g
|
||||
|
||||
#############################################################################
|
||||
## the mouse callback
|
||||
|
||||
## the callback on the trackbar
|
||||
function on_mouse (event, x, y, flags, param)
|
||||
global g;
|
||||
global cv;
|
||||
global highgui;
|
||||
|
||||
if (swig_this(g.image) == 0)
|
||||
## not initialized, so skip
|
||||
return;
|
||||
endif
|
||||
|
||||
if (g.image.origin != 0)
|
||||
## different origin
|
||||
y = g.image.height - y;
|
||||
endif
|
||||
|
||||
if (event == highgui.CV_EVENT_LBUTTONDOWN)
|
||||
## user has click, so memorize it
|
||||
pt = cv.cvPoint (x, y);
|
||||
g.add_remove_pt = true;
|
||||
endif
|
||||
endfunction
|
||||
|
||||
#############################################################################
|
||||
## so, here is the main part of the program
|
||||
|
||||
|
||||
filename = "/home/x/work/sneaker/dvgrab-001.avi";
|
||||
if (size(argv, 1)>1)
|
||||
filename=argv(){1};
|
||||
endif
|
||||
|
||||
capture = highgui.cvCreateFileCapture (filename);
|
||||
|
||||
## check that capture device is OK
|
||||
if (!swig_this(capture))
|
||||
printf("Error opening capture device\n");
|
||||
exit(1)
|
||||
endif
|
||||
|
||||
## display a small howto use it
|
||||
printf("Hot keys: \n");
|
||||
printf("\tESC - quit the program\n");
|
||||
printf("\tr - auto-initialize tracking\n");
|
||||
printf("\tc - delete all the points\n");
|
||||
printf("\tn - switch the \"night\" mode on/off\n");
|
||||
printf("To add/remove a feature point click it\n");
|
||||
|
||||
## first, create the necessary windows
|
||||
highgui.cvNamedWindow ('LkDemo', 1);
|
||||
|
||||
## register the mouse callback
|
||||
highgui.cvSetMouseCallback ('LkDemo', @on_mouse, []);
|
||||
|
||||
while (1)
|
||||
## do forever
|
||||
|
||||
## 1. capture the current image
|
||||
frame = highgui.cvQueryFrame (capture);
|
||||
if (swig_this(frame) == 0)
|
||||
## no image captured... end the processing
|
||||
break
|
||||
endif
|
||||
|
||||
if (swig_this(g.image) == 0),
|
||||
## create the images we need
|
||||
g.image = cv.cvCreateImage (cv.cvGetSize (frame), 8, 3);
|
||||
# g.image.origin = frame.origin;
|
||||
g.grey = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1);
|
||||
g.prev_grey = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1);
|
||||
g.pyramid = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1);
|
||||
g.prev_pyramid = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1);
|
||||
g.points = {[], []};
|
||||
endif
|
||||
|
||||
## copy the frame, so we can draw on it
|
||||
cv.cvCopy (frame, g.image)
|
||||
|
||||
## create a grey version of the image
|
||||
cv.cvCvtColor (g.image, g.grey, cv.CV_BGR2GRAY)
|
||||
|
||||
if (g.night_mode)
|
||||
## night mode: only display the points
|
||||
cv.cvSetZero (g.image);
|
||||
endif
|
||||
|
||||
if (g.need_to_init)
|
||||
## we want to search all the good points
|
||||
|
||||
## create the wanted images
|
||||
eig = cv.cvCreateImage (cv.cvGetSize (g.grey), 32, 1);
|
||||
temp = cv.cvCreateImage (cv.cvGetSize (g.grey), 32, 1);
|
||||
|
||||
## the default parameters
|
||||
quality = 0.01;
|
||||
min_distance = 10;
|
||||
|
||||
## search the good points
|
||||
g.points {1} = cv.cvGoodFeaturesToTrack (g.grey, eig, temp,MAX_COUNT,quality, min_distance, [], 3, 0, 0.04);
|
||||
|
||||
## refine the corner locations
|
||||
cv.cvFindCornerSubPix (g.grey,g.points {1},cv.cvSize (win_size, win_size), cv.cvSize (-1, -1),cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS,20, 0.03));
|
||||
|
||||
elseif (size (g.points {1}, 2) > 0)
|
||||
## we have points, so display them
|
||||
|
||||
## calculate the optical flow
|
||||
[tmp, status] = cv.cvCalcOpticalFlowPyrLK (g.prev_grey, g.grey, g.prev_pyramid, g.pyramid,g.points {1}, size (g.points {1},2),cv.cvSize (win_size, win_size), 3,size (g.points {1}, 2),[],cv.cvTermCriteria (bitor(cv.CV_TERMCRIT_ITER,cv.CV_TERMCRIT_EPS),20, 0.03),g.flags);
|
||||
g.points {2} = tmp;
|
||||
|
||||
## initializations
|
||||
point_counter = -1;
|
||||
new_points = {};
|
||||
|
||||
for the_point = g.points {2},
|
||||
the_point = the_point{1};
|
||||
## go trough all the points
|
||||
|
||||
## increment the counter
|
||||
point_counter += 1;
|
||||
|
||||
if (g.add_remove_pt)
|
||||
## we have a point to add, so see if it is close to
|
||||
## another one. If yes, don't use it
|
||||
dx = pt.x - the_point.x;
|
||||
dy = pt.y - the_point.y;
|
||||
if (dx * dx + dy * dy <= 25)
|
||||
## too close
|
||||
g.add_remove_pt = 0;
|
||||
continue;
|
||||
endif
|
||||
endif
|
||||
|
||||
if (!status {point_counter+1})
|
||||
## we will disable this point
|
||||
continue;
|
||||
endif
|
||||
|
||||
## this point is a correct point
|
||||
new_points{end+1} = the_point;
|
||||
|
||||
## draw the current point
|
||||
cv.cvCircle (g.image, {the_point.x, the_point.y},3, cv.cvScalar (0, 255, 0, 0),-1, 8, 0);
|
||||
endfor
|
||||
|
||||
## set back the points we keep;
|
||||
points {1} = new_points;
|
||||
endif
|
||||
|
||||
if (g.add_remove_pt)
|
||||
## we want to add a point
|
||||
points {1} = append (points {1}, cv.cvPointTo32f (pt));
|
||||
|
||||
## refine the corner locations
|
||||
g.points {1} = cv.cvFindCornerSubPix \
|
||||
(g.grey, {points {1}}, cv.cvSize (win_size, win_size), cv.cvSize \
|
||||
(-1, -1), cv.cvTermCriteria (bitor(cv.CV_TERMCRIT_ITER, cv.CV_TERMCRIT_EPS),20, 0.03));
|
||||
|
||||
## we are no more in "add_remove_pt" mode
|
||||
g.add_remove_pt = false
|
||||
endif
|
||||
|
||||
## swapping
|
||||
tmp = g.prev_grey; g.prev_grey = g.grey; g.grey = tmp;
|
||||
tmp = g.prev_pyramid; g.prev_pyramid = g.pyramid; g.pyramid = tmp;
|
||||
tmp = g.points{1}; g.points{1} = g.points{2}; g.points{2} = tmp;
|
||||
g.need_to_init = false;
|
||||
|
||||
## we can now display the image
|
||||
highgui.cvShowImage ('LkDemo', g.image)
|
||||
|
||||
## handle events
|
||||
c = highgui.cvWaitKey (10);
|
||||
|
||||
if (c == 27)
|
||||
## user has press the ESC key, so exit
|
||||
break
|
||||
endif
|
||||
|
||||
## processing depending on the character
|
||||
if (c == int32('r'))
|
||||
g.need_to_init = true;
|
||||
elseif (c == int32('c'))
|
||||
g.points = {[], []};
|
||||
elseif (c == int32('n'))
|
||||
g.night_mode = !g.night_mode;
|
||||
endif
|
||||
endwhile
|
||||
50
samples/octave/logpolar.m
Normal file
50
samples/octave/logpolar.m
Normal file
@@ -0,0 +1,50 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global g;
|
||||
g.src=[];
|
||||
g.dst=[];
|
||||
g.src2=[];
|
||||
|
||||
function on_mouse( event, x, y, flags, param )
|
||||
global g;
|
||||
global cv;
|
||||
global highgui;
|
||||
|
||||
if(!swig_this(g.src) )
|
||||
return;
|
||||
endif
|
||||
|
||||
if (event==highgui.CV_EVENT_LBUTTONDOWN)
|
||||
cvLogPolar( g.src, g.dst, cvPoint2D32f(x,y), 40, cv.CV_INTER_LINEAR+cv.CV_WARP_FILL_OUTLIERS );
|
||||
cvLogPolar( g.dst, g.src2, cvPoint2D32f(x,y), 40, cv.CV_INTER_LINEAR+cv.CV_WARP_FILL_OUTLIERS+cv.CV_WARP_INVERSE_MAP );
|
||||
cvShowImage( "log-polar", g.dst );
|
||||
cvShowImage( "inverse log-polar", g.src2 );
|
||||
endif
|
||||
endfunction
|
||||
|
||||
filename = "../c/fruits.jpg"
|
||||
if (size(argv, 1)>1)
|
||||
filename=argv(){1};
|
||||
endif
|
||||
|
||||
g.src = cvLoadImage(filename,1);
|
||||
if (!swig_this(g.src))
|
||||
printf("Could not open %s",filename);
|
||||
exit(-1)
|
||||
endif
|
||||
|
||||
cvNamedWindow( "original",1 );
|
||||
cvNamedWindow( "log-polar", 1 );
|
||||
cvNamedWindow( "inverse log-polar", 1 );
|
||||
|
||||
|
||||
g.dst = cvCreateImage( cvSize(256,256), 8, 3 );
|
||||
g.src2 = cvCreateImage( cvGetSize(g.src), 8, 3 );
|
||||
|
||||
cvSetMouseCallback( "original", @on_mouse );
|
||||
on_mouse( CV_EVENT_LBUTTONDOWN, g.src.width/2, g.src.height/2, [], []);
|
||||
|
||||
cvShowImage( "original", g.src );
|
||||
cvWaitKey();
|
||||
85
samples/octave/minarea.m
Normal file
85
samples/octave/minarea.m
Normal file
@@ -0,0 +1,85 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
function ret = randint(a, b)
|
||||
ret = int32(rand() * (b - a) + a);
|
||||
endfunction
|
||||
|
||||
function minarea_array(img, count)
|
||||
global cv;
|
||||
global highgui;
|
||||
pointMat = cvCreateMat( count, 1, cv.CV_32SC2 );
|
||||
for i=0:count-1,
|
||||
pointMat(i) = cvPoint( randint(img.width/4, img.width*3/4), randint(img.height/4, img.height*3/4) );
|
||||
endfor
|
||||
|
||||
box = cvMinAreaRect2( pointMat );
|
||||
box_vtx = cvBoxPoints( box );
|
||||
[success, center, radius] = cvMinEnclosingCircle( pointMat );
|
||||
cv.cvZero( img );
|
||||
for i=0:count-1,
|
||||
cvCircle( img, cvGet1D(pointMat,i), 2, CV_RGB( 255, 0, 0 ), \
|
||||
cv.CV_FILLED, cv.CV_AA, 0 );
|
||||
endfor
|
||||
|
||||
box_vtx = {cvPointFrom32f(box_vtx{1}), \
|
||||
cvPointFrom32f(box_vtx{2}), \
|
||||
cvPointFrom32f(box_vtx{3}), \
|
||||
cvPointFrom32f(box_vtx{4})};
|
||||
cvCircle( img, cvPointFrom32f(center), cvRound(radius), CV_RGB(255, 255, 0), 1, cv.CV_AA, 0 );
|
||||
cvPolyLine( img, {box_vtx}, 1, CV_RGB(0,255,255), 1, cv.CV_AA ) ;
|
||||
endfunction
|
||||
|
||||
|
||||
function minarea_seq(img, count, storage)
|
||||
global cv;
|
||||
global highgui;
|
||||
ptseq = cvCreateSeq( bitor(cv.CV_SEQ_KIND_GENERIC, cv.CV_32SC2), cv.sizeof_CvContour, cv.sizeof_CvPoint, storage );
|
||||
ptseq = cv.CvSeq_CvPoint.cast( ptseq );
|
||||
for i=0:count-1,
|
||||
pt0 = cvPoint( randint(img.width/4, img.width*3/4), randint(img.height/4, img.height*3/4) );
|
||||
cvSeqPush( ptseq, pt0 );
|
||||
endfor
|
||||
box = cvMinAreaRect2( ptseq );
|
||||
box_vtx = cvBoxPoints( box );
|
||||
[success, center, radius] = cvMinEnclosingCircle( ptseq );
|
||||
cv.cvZero( img );
|
||||
for pt = CvSeq_map(ptseq),
|
||||
pt = pt{1};
|
||||
cvCircle( img, pt, 2, CV_RGB( 255, 0, 0 ), cv.CV_FILLED, cv.CV_AA, 0 );
|
||||
endfor
|
||||
|
||||
box_vtx = {cvPointFrom32f(box_vtx{1}), \
|
||||
cvPointFrom32f(box_vtx{2}), \
|
||||
cvPointFrom32f(box_vtx{3}), \
|
||||
cvPointFrom32f(box_vtx{4})};
|
||||
cvCircle( img, cvPointFrom32f(center), cvRound(radius), CV_RGB(255, 255, 0), 1, cv.CV_AA, 0 );
|
||||
cvPolyLine( img, {box_vtx}, 1, CV_RGB(0,255,255), 1, cv.CV_AA );
|
||||
cvClearMemStorage( storage );
|
||||
endfunction
|
||||
|
||||
img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
|
||||
storage = cvCreateMemStorage(0);
|
||||
|
||||
cvNamedWindow( "rect & circle", 1 );
|
||||
|
||||
use_seq=false;
|
||||
|
||||
while (true),
|
||||
count = randint(1,100);
|
||||
if (use_seq)
|
||||
minarea_seq(img, count, storage);
|
||||
else
|
||||
minarea_array(img, count);
|
||||
endif
|
||||
|
||||
cvShowImage("rect & circle", img);
|
||||
key = cvWaitKey();
|
||||
if( key == '\x1b' );
|
||||
break;
|
||||
endif
|
||||
|
||||
use_seq = !use_seq;
|
||||
endwhile
|
||||
15
samples/octave/minidemo.m
Normal file
15
samples/octave/minidemo.m
Normal file
@@ -0,0 +1,15 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
|
||||
cvNamedWindow("win", CV_WINDOW_AUTOSIZE);
|
||||
cap = cvCreateFileCapture("/home/x/work/sneaker/dvgrab-001.avi");
|
||||
img = cvQueryFrame(cap);
|
||||
|
||||
printf("Got frame of dimensions (%i x %i)",img.width,img.height);
|
||||
|
||||
cvShowImage("win", img);
|
||||
cvMoveWindow("win", 200, 200);
|
||||
cvWaitKey(0);
|
||||
|
||||
octimg = cv2im(img);
|
||||
|
||||
86
samples/octave/morphology.m
Normal file
86
samples/octave/morphology.m
Normal file
@@ -0,0 +1,86 @@
|
||||
#! /usr/bin/env octave
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global src;
|
||||
global image
|
||||
global element
|
||||
global element_shape
|
||||
global global_pos;
|
||||
global dest;
|
||||
src = 0;
|
||||
image = 0;
|
||||
dest = 0;
|
||||
element = 0;
|
||||
element_shape = CV_SHAPE_RECT;
|
||||
global_pos = 0;
|
||||
|
||||
function Opening(pos)
|
||||
global src;
|
||||
global image
|
||||
global element
|
||||
global element_shape
|
||||
global global_pos;
|
||||
global dest;
|
||||
element = cvCreateStructuringElementEx( pos*2+1, pos*2+1, pos, pos, element_shape, [] );
|
||||
cvErode(src,image,element,1);
|
||||
cvDilate(image,dest,element,1);
|
||||
cvShowImage("Opening&Closing window",dest);
|
||||
endfunction
|
||||
function Closing(pos)
|
||||
global src;
|
||||
global image
|
||||
global element
|
||||
global element_shape
|
||||
global global_pos;
|
||||
global dest;
|
||||
element = cvCreateStructuringElementEx( pos*2+1, pos*2+1, pos, pos, element_shape, [] );
|
||||
cvDilate(src,image,element,1);
|
||||
cvErode(image,dest,element,1);
|
||||
cvShowImage("Opening&Closing window",dest);
|
||||
endfunction
|
||||
function Erosion(pos)
|
||||
global src;
|
||||
global image
|
||||
global element
|
||||
global element_shape
|
||||
global global_pos;
|
||||
global dest;
|
||||
element = cvCreateStructuringElementEx( pos*2+1, pos*2+1, pos, pos, element_shape, [] );
|
||||
cvErode(src,dest,element,1);
|
||||
cvShowImage("Erosion&Dilation window",dest);
|
||||
endfunction
|
||||
function Dilation(pos)
|
||||
global src;
|
||||
global image
|
||||
global element
|
||||
global element_shape
|
||||
global global_pos;
|
||||
global dest;
|
||||
element = cvCreateStructuringElementEx( pos*2+1, pos*2+1, pos, pos, element_shape, [] );
|
||||
cvDilate(src,dest,element,1);
|
||||
cvShowImage("Erosion&Dilation window",dest);
|
||||
endfunction
|
||||
|
||||
filename = "../c/baboon.jpg";
|
||||
if (size(argv, 1)==1)
|
||||
filename = argv(){1};
|
||||
endif
|
||||
src = cvLoadImage(filename,1);
|
||||
if (! swig_this(src))
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
image = cvCloneImage(src);
|
||||
dest = cvCloneImage(src);
|
||||
cvNamedWindow("Opening&Closing window",1);
|
||||
cvNamedWindow("Erosion&Dilation window",1);
|
||||
cvShowImage("Opening&Closing window",src);
|
||||
cvShowImage("Erosion&Dilation window",src);
|
||||
cvCreateTrackbar("Open","Opening&Closing window",global_pos,10,@Opening);
|
||||
cvCreateTrackbar("Close","Opening&Closing window",global_pos,10,@Closing);
|
||||
cvCreateTrackbar("Dilate","Erosion&Dilation window",global_pos,10,@Dilation);
|
||||
cvCreateTrackbar("Erode","Erosion&Dilation window",global_pos,10,@Erosion);
|
||||
cvWaitKey(0);
|
||||
cvDestroyWindow("Opening&Closing window");
|
||||
cvDestroyWindow("Erosion&Dilation window");
|
||||
126
samples/octave/motempl.m
Normal file
126
samples/octave/motempl.m
Normal file
@@ -0,0 +1,126 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
cv
|
||||
highgui
|
||||
|
||||
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 = []; # MHI
|
||||
orient = []; # orientation
|
||||
mask = []; # valid orientation mask
|
||||
segmask = []; # motion segmentation map
|
||||
storage = []; # temporary storage
|
||||
|
||||
function 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 = cvSize(img.width,img.height); # get current frame size
|
||||
idx1 = last;
|
||||
if (! mhi || mhi.width != size.width || mhi.height != size.height)
|
||||
for i=0:N-1,
|
||||
buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
|
||||
cvZero( buf[i] );
|
||||
mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
|
||||
cvZero( mhi ); # clear MHI at the beginning
|
||||
orient = cvCreateImage( size, IPL_DEPTH_32F, 1 );
|
||||
segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 );
|
||||
mask = cvCreateImage( size, IPL_DEPTH_8U, 1 );
|
||||
|
||||
cvCvtColor( img, buf[last], CV_BGR2GRAY ); # convert frame to grayscale
|
||||
idx2 = (last + 1) % N; # index of (last - (N-1))th frame
|
||||
last = idx2;
|
||||
silh = buf[idx2];
|
||||
cvAbsDiff( buf[idx1], buf[idx2], silh ); # get difference between frames
|
||||
cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); # and threshold it
|
||||
cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); # update MHI
|
||||
cvCvtScale( mhi, mask, 255./MHI_DURATION,
|
||||
(MHI_DURATION - timestamp)*255./MHI_DURATION );
|
||||
cvZero( dst );
|
||||
cvMerge( mask, [], [], [], dst );
|
||||
cvCalcMotionGradient( mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3 );
|
||||
if( not storage )
|
||||
storage = cvCreateMemStorage(0);
|
||||
else
|
||||
cvClearMemStorage(storage);
|
||||
seq = cvSegmentMotion( mhi, segmask, storage, timestamp, MAX_TIME_DELTA );
|
||||
for i=-1:seq.total-1,
|
||||
if( i < 0 ) # case of the whole image
|
||||
comp_rect = cvRect( 0, 0, size.width, size.height );
|
||||
color = CV_RGB(255,255,255);
|
||||
magnitude = 100.;
|
||||
else # i-th motion component
|
||||
comp_rect = seq[i].rect
|
||||
if( comp_rect.width + comp_rect.height < 100 ) # reject very small components
|
||||
continue;
|
||||
endif
|
||||
endif
|
||||
color = CV_RGB(255,0,0);
|
||||
magnitude = 30.;
|
||||
silh_roi = cvGetSubRect(silh, comp_rect);
|
||||
mhi_roi = cvGetSubRect( mhi, comp_rect );
|
||||
orient_roi = cvGetSubRect( orient, comp_rect );
|
||||
mask_roi = cvGetSubRect( mask, comp_rect );
|
||||
angle = cvCalcGlobalOrientation( orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION);
|
||||
angle = 360.0 - angle; # adjust for images with top-left origin
|
||||
count = cvNorm( silh_roi, [], CV_L1, [] ); # calculate number of points within silhouette ROI
|
||||
if( count < comp_rect.width * comp_rect.height * 0.05 )
|
||||
continue;
|
||||
endif
|
||||
center = cvPoint( (comp_rect.x + comp_rect.width/2),
|
||||
(comp_rect.y + comp_rect.height/2) );
|
||||
cvCircle( dst, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 );
|
||||
cvLine( dst, center, cvPoint( cvRound( center.x + magnitude*cos(angle*CV_PI/180)),
|
||||
cvRound( center.y - magnitude*sin(angle*CV_PI/180))), \
|
||||
color, 3, CV_AA, 0 );
|
||||
endfor
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
endfunction
|
||||
|
||||
motion = 0;
|
||||
capture = 0;
|
||||
|
||||
if (size(argv, 1)==1)
|
||||
capture = cvCreateCameraCapture( 0 )
|
||||
elseif (size(argv, 1)==2 && all(isdigit(argv(1, :))))
|
||||
capture = cvCreateCameraCapture( int32(argv(1, :)) )
|
||||
elseif (size(argv, 1)==2)
|
||||
capture = cvCreateFileCapture( argv(1, :) );
|
||||
endif
|
||||
|
||||
if (!capture)
|
||||
print "Could not initialize capturing..."
|
||||
exit(-1)
|
||||
endif
|
||||
|
||||
cvNamedWindow( "Motion", 1 );
|
||||
while (true)
|
||||
image = cvQueryFrame( capture );
|
||||
if( image )
|
||||
if( ! motion )
|
||||
motion = cvCreateImage( cvSize(image.width,image.height), 8, 3 );
|
||||
cvZero( motion );
|
||||
motion.origin = image.origin;
|
||||
endif
|
||||
update_mhi( image, motion, 30 );
|
||||
cvShowImage( "Motion", motion );
|
||||
if( cvWaitKey(10) != -1 )
|
||||
break;
|
||||
endif
|
||||
else
|
||||
break
|
||||
endif
|
||||
endwhile
|
||||
|
||||
cvDestroyWindow( "Motion" );
|
||||
71
samples/octave/pyramid_segmentation.m
Normal file
71
samples/octave/pyramid_segmentation.m
Normal file
@@ -0,0 +1,71 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global g;
|
||||
g.image0 = [];
|
||||
g.image1 = [];
|
||||
g.threshold1 = 255;
|
||||
g.threshold2 = 30;
|
||||
g.l = g.level = 4;
|
||||
g.block_size = 1000;
|
||||
g.filter = CV_GAUSSIAN_5x5;
|
||||
g.storage = [];
|
||||
g.min_comp = CvConnectedComp();
|
||||
|
||||
function set_thresh1( val )
|
||||
global g;
|
||||
g.threshold1 = val;
|
||||
ON_SEGMENT();
|
||||
endfunction
|
||||
|
||||
function set_thresh2( val )
|
||||
global g;
|
||||
g.threshold2 = val;
|
||||
ON_SEGMENT()
|
||||
endfunction
|
||||
|
||||
function ON_SEGMENT()
|
||||
global g;
|
||||
global cv;
|
||||
g
|
||||
swig_this(g.image0)
|
||||
swig_this(g.image1)
|
||||
swig_this(g.storage)
|
||||
g.level
|
||||
g.threshold1
|
||||
g.threshold2
|
||||
comp = cv.cvPyrSegmentation(g.image0, g.image1, g.storage, g.level, g.threshold1+1, g.threshold2+1);
|
||||
cvShowImage("Segmentation", g.image1);
|
||||
endfunction
|
||||
|
||||
filename = "../c/fruits.jpg";
|
||||
if (size(argv, 2) >= 1)
|
||||
filename = argv(){1};
|
||||
endif
|
||||
g.image0 = cvLoadImage( filename, 1);
|
||||
if (! swig_this(g.image0))
|
||||
printf("Error opening %s\n",filename);
|
||||
exit(-1);
|
||||
endif
|
||||
|
||||
cvNamedWindow("Source", 0);
|
||||
cvShowImage("Source", g.image0);
|
||||
cvNamedWindow("Segmentation", 0);
|
||||
g.storage = cvCreateMemStorage ( g.block_size );
|
||||
new_width = bitshift(g.image0.width, -g.level);
|
||||
new_height = bitshift(g.image0.height, -g.level);
|
||||
g.image0 = cvCreateImage( cvSize(new_width,new_height), g.image0.depth, g.image0.nChannels );
|
||||
g.image1 = cvCreateImage( cvSize(new_width,new_height), g.image0.depth, g.image0.nChannels );
|
||||
## segmentation of the color image
|
||||
g.l = 1;
|
||||
g.threshold1 =255;
|
||||
g.threshold2 =30;
|
||||
ON_SEGMENT();
|
||||
g.sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation", g.threshold1, 255, @set_thresh1);
|
||||
g.sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation", g.threshold2, 255, @set_thresh2);
|
||||
cvShowImage("Segmentation", image1);
|
||||
cvWaitKey(0);
|
||||
cvDestroyWindow("Segmentation");
|
||||
cvDestroyWindow("Source");
|
||||
173
samples/octave/squares.m
Normal file
173
samples/octave/squares.m
Normal file
@@ -0,0 +1,173 @@
|
||||
#! /usr/bin/env octave
|
||||
##
|
||||
## The full "Square Detector" program.
|
||||
## It loads several images subsequentally and tries to find squares in
|
||||
## each image
|
||||
##
|
||||
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global g;
|
||||
|
||||
g.thresh = 50;
|
||||
g.img = [];
|
||||
g.img0 = [];
|
||||
g.storage = [];
|
||||
g.wndname = "Square Detection Demo";
|
||||
|
||||
function ret = compute_angle( pt1, pt2, pt0 )
|
||||
dx1 = pt1.x - pt0.x;
|
||||
dy1 = pt1.y - pt0.y;
|
||||
dx2 = pt2.x - pt0.x;
|
||||
dy2 = pt2.y - pt0.y;
|
||||
ret = (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
|
||||
endfunction
|
||||
|
||||
function squares = findSquares4( img, storage )
|
||||
global g;
|
||||
global cv;
|
||||
|
||||
N = 11;
|
||||
sz = cvSize( img.width, img.height );
|
||||
timg = cvCloneImage( img ); # make a copy of input image
|
||||
gray = cvCreateImage( sz, 8, 1 );
|
||||
pyr = cvCreateImage( cvSize(int32(sz.width/2), int32(sz.height/2)), 8, 3 );
|
||||
## create empty sequence that will contain points -
|
||||
## 4 points per square (the square's vertices)
|
||||
squares = cvCreateSeq( 0, cv.sizeof_CvSeq, cv.sizeof_CvPoint, storage );
|
||||
squares = cv.CvSeq_CvPoint.cast( squares );
|
||||
|
||||
## select the maximum ROI in the image
|
||||
## with the width and height divisible by 2
|
||||
subimage = cvGetSubRect( timg, cvRect( 0, 0, sz.width, sz.height ));
|
||||
|
||||
## down-scale and upscale the image to filter out the noise
|
||||
cvPyrDown( subimage, pyr, 7 );
|
||||
cvPyrUp( pyr, subimage, 7 );
|
||||
tgray = cvCreateImage( sz, 8, 1 );
|
||||
## find squares in every color plane of the image
|
||||
for c=1:3,
|
||||
## extract the c-th color plane
|
||||
channels = {[], [], []};
|
||||
channels{c} = tgray;
|
||||
cvSplit( subimage, channels{1}, channels{2}, channels{3}, [] ) ;
|
||||
for l=1:N,
|
||||
## hack: use Canny instead of zero threshold level.
|
||||
## Canny helps to catch squares with gradient shading
|
||||
if( l == 1 )
|
||||
## apply Canny. Take the upper threshold from slider
|
||||
## and set the lower to 0 (which forces edges merging)
|
||||
cvCanny( tgray, gray, 0, g.thresh, 5 );
|
||||
## dilate canny output to remove potential
|
||||
## holes between edge segments
|
||||
cvDilate( gray, gray, [], 1 );
|
||||
else
|
||||
## apply threshold if l!=0
|
||||
## tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
|
||||
cvThreshold( tgray, gray, l*255/N, 255, cv.CV_THRESH_BINARY );
|
||||
endif
|
||||
|
||||
## find contours and store them all as a list
|
||||
[count, contours] = cvFindContours( gray, storage, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
|
||||
|
||||
if (!swig_this(contours))
|
||||
continue;
|
||||
endif
|
||||
|
||||
## test each contour
|
||||
for contour = CvSeq_hrange(contours),
|
||||
## approximate contour with accuracy proportional
|
||||
## to the contour perimeter
|
||||
result = cvApproxPoly( contour, cv.sizeof_CvContour, storage, cv.CV_POLY_APPROX_DP, cvContourPerimeter(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 &&
|
||||
abs(cvContourArea(result)) > 1000 &&
|
||||
cvCheckContourConvexity(result) )
|
||||
s = 0;
|
||||
for i=1:5,
|
||||
## find minimum angle between joint
|
||||
## edges (maximum of cosine)
|
||||
if( i > 2 )
|
||||
t = abs(compute_angle( result{i}, result{i-2}, result{i-1}));
|
||||
if (s<t)
|
||||
s=t;
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
## 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=1:4,
|
||||
squares.append( result{i} )
|
||||
endfor
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
endfor
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
## the function draws all the squares in the image
|
||||
function drawSquares( img, squares )
|
||||
global g;
|
||||
global cv;
|
||||
|
||||
cpy = cvCloneImage( img );
|
||||
## read 4 sequence elements at a time (all vertices of a square)
|
||||
i=0;
|
||||
while (i<squares.total)
|
||||
pt = { squares{i}, squares{i+1}, squares{i+2}, squares{i+3} };
|
||||
|
||||
## draw the square as a closed polyline
|
||||
cvPolyLine( cpy, {pt}, 1, CV_RGB(0,255,0), 3, cv.CV_AA, 0 );
|
||||
i+=4;
|
||||
endwhile
|
||||
|
||||
## show the resultant image
|
||||
cvShowImage( g.wndname, cpy );
|
||||
endfunction
|
||||
|
||||
function on_trackbar( a )
|
||||
global g;
|
||||
|
||||
if( swig_this(g.img) )
|
||||
drawSquares( g.img, findSquares4( g.img, g.storage ) );
|
||||
endif
|
||||
endfunction
|
||||
|
||||
g.names = {"../c/pic1.png", "../c/pic2.png", "../c/pic3.png", \
|
||||
"../c/pic4.png", "../c/pic5.png", "../c/pic6.png" };
|
||||
|
||||
## create memory storage that will contain all the dynamic data
|
||||
g.storage = cvCreateMemStorage(0);
|
||||
for name = g.names,
|
||||
g.img0 = cvLoadImage( name, 1 );
|
||||
if (!swig_this(g.img0))
|
||||
printf("Couldn't load %s\n",name);
|
||||
continue;
|
||||
endif
|
||||
g.img = cvCloneImage( g.img0 );
|
||||
## create window and a trackbar (slider) with parent "image" and set callback
|
||||
## (the slider regulates upper threshold, passed to Canny edge detector)
|
||||
cvNamedWindow( g.wndname, 1 );
|
||||
cvCreateTrackbar( "canny thresh", g.wndname, g.thresh, 1000, @on_trackbar );
|
||||
## force the image processing
|
||||
on_trackbar(0);
|
||||
## wait for key.
|
||||
## Also the function cvWaitKey takes care of event processing
|
||||
c = cvWaitKey(0);
|
||||
## clear memory storage - reset free space position
|
||||
cvClearMemStorage( g.storage );
|
||||
if( c == '\x1b' )
|
||||
break;
|
||||
endif
|
||||
endfor
|
||||
cvDestroyWindow( g.wndname );
|
||||
|
||||
133
samples/octave/watershed.m
Normal file
133
samples/octave/watershed.m
Normal file
@@ -0,0 +1,133 @@
|
||||
#! /usr/bin/env octave
|
||||
|
||||
cv;
|
||||
highgui;
|
||||
|
||||
global g;
|
||||
g.marker_mask = [];
|
||||
g.markers = [];
|
||||
g.img0 = []
|
||||
g.img = []
|
||||
g.img_gray = []
|
||||
g.wshed = []
|
||||
g.prev_pt = cvPoint(-1,-1);
|
||||
|
||||
function on_mouse( event, x, y, flags, param )
|
||||
global g;
|
||||
global cv;
|
||||
global highgui;
|
||||
|
||||
if( !swig_this( g.img) )
|
||||
return;
|
||||
endif
|
||||
if( event == highgui.CV_EVENT_LBUTTONUP || ! bitand(flags,highgui.CV_EVENT_FLAG_LBUTTON) )
|
||||
g.prev_pt = cvPoint(-1,-1);
|
||||
elseif( event == highgui.CV_EVENT_LBUTTONDOWN )
|
||||
g.prev_pt = cvPoint(x,y);
|
||||
elseif( event == highgui.CV_EVENT_MOUSEMOVE && bitand(flags,highgui.CV_EVENT_FLAG_LBUTTON) )
|
||||
pt = cvPoint(x,y);
|
||||
if( g.prev_pt.x < 0 )
|
||||
g.prev_pt = pt;
|
||||
endif
|
||||
cvLine( g.marker_mask, g.prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
|
||||
cvLine( g.img, g.prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
|
||||
g.prev_pt = pt;
|
||||
cvShowImage( "image", g.img );
|
||||
endif
|
||||
endfunction
|
||||
|
||||
filename = "../c/fruits.jpg";
|
||||
if (size(argv, 1)>=1)
|
||||
filename = argv(){1};
|
||||
endif
|
||||
|
||||
rng = cvRNG(-1);
|
||||
g.img0 = cvLoadImage(filename,1);
|
||||
if (!swig_this(g.img0))
|
||||
print "Error opening image '%s'" % filename
|
||||
exit(-1)
|
||||
endif
|
||||
|
||||
printf("Hot keys:\n");
|
||||
printf("\tESC - quit the program\n");
|
||||
printf("\tr - restore the original image\n");
|
||||
printf("\tw - run watershed algorithm\n");
|
||||
printf("\t (before that, roughly outline several g.markers on the image)\n");
|
||||
|
||||
cvNamedWindow( "image", 1 );
|
||||
cvNamedWindow( "watershed transform", 1 );
|
||||
|
||||
g.img = cvCloneImage( g.img0 );
|
||||
g.img_gray = cvCloneImage( g.img0 );
|
||||
g.wshed = cvCloneImage( g.img0 );
|
||||
g.marker_mask = cvCreateImage( cvGetSize(g.img), 8, 1 );
|
||||
g.markers = cvCreateImage( cvGetSize(g.img), IPL_DEPTH_32S, 1 );
|
||||
|
||||
cvCvtColor( g.img, g.marker_mask, CV_BGR2GRAY );
|
||||
cvCvtColor( g.marker_mask, g.img_gray, CV_GRAY2BGR );
|
||||
|
||||
cvZero( g.marker_mask );
|
||||
cvZero( g.wshed );
|
||||
|
||||
cvShowImage( "image", g.img );
|
||||
cvShowImage( "watershed transform", g.wshed );
|
||||
|
||||
cvSetMouseCallback( "image", @on_mouse, [] );
|
||||
while (true)
|
||||
c = cvWaitKey(0);
|
||||
if (c=='\x1b')
|
||||
break;
|
||||
endif
|
||||
if (c == 'r')
|
||||
cvZero( g.marker_mask );
|
||||
cvCopy( g.img0, g.img );
|
||||
cvShowImage( "image", g.img );
|
||||
endif
|
||||
if (c == 'w')
|
||||
storage = cvCreateMemStorage(0);
|
||||
comp_count = 0;
|
||||
##cvSaveImage( "g.wshed_mask.png", g.marker_mask );
|
||||
##g.marker_mask = cvLoadImage( "g.wshed_mask.png", 0 );
|
||||
[nb_cont, contours] = cvFindContours( g.marker_mask, storage, \
|
||||
sizeof_CvContour, \
|
||||
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
|
||||
cvZero( g.markers );
|
||||
swig_this(contours)
|
||||
while (swig_this(contours))
|
||||
cvDrawContours( g.markers, contours, cvScalarAll(comp_count+1), \
|
||||
cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );
|
||||
contours=contours.h_next;
|
||||
comp_count+=1;
|
||||
endwhile
|
||||
comp_count
|
||||
color_tab = cvCreateMat( comp_count, 1, CV_8UC3 );
|
||||
for i=0:comp_count-1,
|
||||
color_tab(i) = cvScalar( mod(cvRandInt(rng),180) + 50, \
|
||||
mod(cvRandInt(rng),180) + 50, \
|
||||
mod(cvRandInt(rng),180) + 50 );
|
||||
endfor
|
||||
t = int32(cvGetTickCount());
|
||||
cvWatershed( g.img0, g.markers );
|
||||
t = int32(cvGetTickCount()) - t;
|
||||
##print "exec time = %f" % t/(cvGetTickFrequency()*1000.)
|
||||
|
||||
cvSet( g.wshed, cvScalarAll(255) );
|
||||
|
||||
## paint the watershed image
|
||||
for j=0:g.markers.height-1,
|
||||
for i=0:g.markers.width-1,
|
||||
{j,i}
|
||||
idx = g.markers({j,i});
|
||||
if (idx==-1)
|
||||
continue
|
||||
endif
|
||||
idx = idx-1;
|
||||
g.wshed({j,i}) = color_tab({idx,0});
|
||||
endfor
|
||||
endfor
|
||||
|
||||
cvAddWeighted( g.wshed, 0.5, g.img_gray, 0.5, 0, g.wshed );
|
||||
cvShowImage( "watershed transform", g.wshed );
|
||||
cvWaitKey();
|
||||
endif
|
||||
endwhile
|
||||
Reference in New Issue
Block a user