mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 07:43:03 +04:00
"atomic bomb" commit. Reorganized OpenCV directory structure
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
# ----------------------------------------------------------------------------
|
||||
# CMake file for C samples. See root CMakeLists.txt
|
||||
#
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
if (BUILD_EXAMPLES)
|
||||
project(cpp_samples)
|
||||
|
||||
include_directories(
|
||||
"${CMAKE_SOURCE_DIR}/modules/core/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/imgproc/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/video/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/highgui/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/ml/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/calib3d/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/features2d/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/objdetect/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/legacy/include"
|
||||
"${CMAKE_SOURCE_DIR}/modules/contrib/include"
|
||||
)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
|
||||
endif()
|
||||
|
||||
# ---------------------------------------------
|
||||
# Define executable targets
|
||||
# ---------------------------------------------
|
||||
MACRO(MY_DEFINE_EXAMPLE name srcs)
|
||||
add_executable(${name} ${srcs})
|
||||
set_target_properties(${name} PROPERTIES PROJECT_LABEL "(EXAMPLE) ${name}")
|
||||
add_dependencies(${name} opencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_objdetect opencv_features2d opencv_calib3d opencv_contrib)
|
||||
target_link_libraries(${name} ${OPENCV_LINKER_LIBS} opencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_objdetect opencv_features2d opencv_calib3d opencv_contrib)
|
||||
|
||||
if(WIN32)
|
||||
install(TARGETS ${name}
|
||||
RUNTIME DESTINATION "samples/cpp" COMPONENT main)
|
||||
endif()
|
||||
ENDMACRO(MY_DEFINE_EXAMPLE)
|
||||
|
||||
MY_DEFINE_EXAMPLE(connected_components connected_components.cpp)
|
||||
MY_DEFINE_EXAMPLE(contours2 contours2.cpp)
|
||||
MY_DEFINE_EXAMPLE(morphology2 morphology2.cpp)
|
||||
MY_DEFINE_EXAMPLE(segment_objects segment_objects.cpp)
|
||||
endif(BUILD_EXAMPLES)
|
||||
|
||||
if (INSTALL_C_EXAMPLES AND NOT WIN32)
|
||||
file(GLOB C_SAMPLES *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd )
|
||||
install(FILES ${C_SAMPLES}
|
||||
DESTINATION share/opencv/samples/cpp
|
||||
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)
|
||||
endif ()
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "cv.h"
|
||||
#include "highgui.h"
|
||||
|
||||
using namespace cv;
|
||||
|
||||
Mat img;
|
||||
int threshval = 100;
|
||||
|
||||
void on_trackbar(int, void*)
|
||||
{
|
||||
Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
|
||||
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
|
||||
findContours( bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
|
||||
|
||||
Mat dst = Mat::zeros(img.size(), CV_8UC3);
|
||||
|
||||
if( contours.size() > 0 )
|
||||
{
|
||||
// iterate through all the top-level contours,
|
||||
// draw each connected component with its own random color
|
||||
int idx = 0;
|
||||
for( ; idx >= 0; idx = hierarchy[idx][0] )
|
||||
{
|
||||
Scalar color( (rand()&255), (rand()&255), (rand()&255) );
|
||||
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
|
||||
}
|
||||
}
|
||||
|
||||
imshow( "Connected Components", dst );
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
// the first command line parameter must be file name of binary
|
||||
// (black-n-white) image
|
||||
if( !(img=imread(argc == 2 ? argv[1] : "stuff.jpg", 0)).data)
|
||||
return -1;
|
||||
|
||||
namedWindow( "Image", 1 );
|
||||
imshow( "Image", img );
|
||||
|
||||
namedWindow( "Connected Components", 1 );
|
||||
createTrackbar( "Threshold", "Connected Components", &threshval, 255, on_trackbar );
|
||||
on_trackbar(threshval, 0);
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
#include "cv.h"
|
||||
#include "highgui.h"
|
||||
#include <math.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
const int w = 500;
|
||||
int levels = 3;
|
||||
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
|
||||
void on_trackbar(int pos, void*)
|
||||
{
|
||||
Mat cnt_img = Mat::zeros(w, w, CV_8UC3);
|
||||
int _levels = levels - 3;
|
||||
drawContours( cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar(128,255,255),
|
||||
3, CV_AA, hierarchy, std::abs(_levels) );
|
||||
|
||||
imshow("contours", cnt_img);
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
Mat img = Mat::zeros(w, w, CV_8UC1);
|
||||
|
||||
for( int i = 0; i < 6; i++ )
|
||||
{
|
||||
int dx = (i%2)*250 - 30;
|
||||
int dy = (i/2)*150;
|
||||
const Scalar white = Scalar(255);
|
||||
const Scalar black = Scalar(0);
|
||||
|
||||
if( i == 0 )
|
||||
{
|
||||
for( int j = 0; j <= 10; j++ )
|
||||
{
|
||||
double angle = (j+5)*CV_PI/21;
|
||||
line(img, Point(cvRound(dx+100+j*10-80*cos(angle)),
|
||||
cvRound(dy+100-90*sin(angle))),
|
||||
Point(cvRound(dx+100+j*10-30*cos(angle)),
|
||||
cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
|
||||
}
|
||||
}
|
||||
|
||||
ellipse( img, Point(dx+150, dy+100), Size(100,70), 0, 0, 360, white, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+115, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+185, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+115, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+185, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+115, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+185, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+150, dy+100), Size(10,5), 0, 0, 360, black, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+150, dy+150), Size(40,10), 0, 0, 360, black, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+27, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
|
||||
ellipse( img, Point(dx+273, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
|
||||
}
|
||||
|
||||
namedWindow( "image", 1 );
|
||||
imshow( "image", img );
|
||||
|
||||
vector<vector<Point> > contours0;
|
||||
findContours( img, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
|
||||
|
||||
contours.resize(contours0.size());
|
||||
for( size_t k = 0; k < contours0.size(); k++ )
|
||||
approxPolyDP(Mat(contours0[k]), contours[k], 3, true);
|
||||
|
||||
namedWindow( "contours", 1 );
|
||||
createTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
|
||||
|
||||
on_trackbar(0,0);
|
||||
waitKey();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#define CV_NO_BACKWARD_COMPATIBILITY
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
Mat src, dst;
|
||||
|
||||
int element_shape = MORPH_RECT;
|
||||
|
||||
//the address of variable which receives trackbar position update
|
||||
int max_iters = 10;
|
||||
int open_close_pos = 0;
|
||||
int erode_dilate_pos = 0;
|
||||
|
||||
// callback function for open/close trackbar
|
||||
void OpenClose(int, void*)
|
||||
{
|
||||
int n = open_close_pos - max_iters;
|
||||
int an = n > 0 ? n : -n;
|
||||
Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
|
||||
if( n < 0 )
|
||||
morphologyEx(src, dst, CV_MOP_OPEN, element);
|
||||
else
|
||||
morphologyEx(src, dst, CV_MOP_CLOSE, element);
|
||||
imshow("Open/Close",dst);
|
||||
}
|
||||
|
||||
// callback function for erode/dilate trackbar
|
||||
void ErodeDilate(int, void*)
|
||||
{
|
||||
int n = erode_dilate_pos - max_iters;
|
||||
int an = n > 0 ? n : -n;
|
||||
Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
|
||||
if( n < 0 )
|
||||
erode(src, dst, element);
|
||||
else
|
||||
dilate(src, dst, element);
|
||||
imshow("Erode/Dilate",dst);
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
char* filename = argc == 2 ? argv[1] : (char*)"baboon.jpg";
|
||||
if( (src = imread(filename,1)).data == 0 )
|
||||
return -1;
|
||||
|
||||
printf( "Hot keys: \n"
|
||||
"\tESC - quit the program\n"
|
||||
"\tr - use rectangle structuring element\n"
|
||||
"\te - use elliptic structuring element\n"
|
||||
"\tc - use cross-shaped structuring element\n"
|
||||
"\tSPACE - loop through all the options\n" );
|
||||
|
||||
//create windows for output images
|
||||
namedWindow("Open/Close",1);
|
||||
namedWindow("Erode/Dilate",1);
|
||||
|
||||
open_close_pos = erode_dilate_pos = max_iters;
|
||||
createTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
|
||||
createTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int c;
|
||||
|
||||
OpenClose(open_close_pos, 0);
|
||||
ErodeDilate(erode_dilate_pos, 0);
|
||||
c = cvWaitKey(0);
|
||||
|
||||
if( (char)c == 27 )
|
||||
break;
|
||||
if( (char)c == 'e' )
|
||||
element_shape = MORPH_ELLIPSE;
|
||||
else if( (char)c == 'r' )
|
||||
element_shape = MORPH_RECT;
|
||||
else if( (char)c == 'c' )
|
||||
element_shape = MORPH_CROSS;
|
||||
else if( (char)c == ' ' )
|
||||
element_shape = (element_shape + 1) % 3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "cvaux.h"
|
||||
#include "highgui.h"
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
|
||||
void refineSegments(const Mat& img, Mat& mask, Mat& dst)
|
||||
{
|
||||
int niters = 3;
|
||||
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
|
||||
Mat temp;
|
||||
|
||||
dilate(mask, temp, Mat(), Point(-1,-1), niters);
|
||||
erode(temp, temp, Mat(), Point(-1,-1), niters*2);
|
||||
dilate(temp, temp, Mat(), Point(-1,-1), niters);
|
||||
|
||||
findContours( temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
|
||||
|
||||
dst = Mat::zeros(img.size(), CV_8UC3);
|
||||
|
||||
if( contours.size() == 0 )
|
||||
return;
|
||||
|
||||
// iterate through all the top-level contours,
|
||||
// draw each connected component with its own random color
|
||||
int idx = 0, largestComp = 0;
|
||||
double maxArea = 0;
|
||||
|
||||
for( ; idx >= 0; idx = hierarchy[idx][0] )
|
||||
{
|
||||
const vector<Point>& c = contours[idx];
|
||||
double area = fabs(contourArea(Mat(c)));
|
||||
if( area > maxArea )
|
||||
{
|
||||
maxArea = area;
|
||||
largestComp = idx;
|
||||
}
|
||||
}
|
||||
Scalar color( 0, 0, 255 );
|
||||
drawContours( dst, contours, largestComp, color, CV_FILLED, 8, hierarchy );
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
VideoCapture cap;
|
||||
bool update_bg_model = true;
|
||||
|
||||
if( argc < 2 )
|
||||
cap.open(0);
|
||||
else
|
||||
cap.open(std::string(argv[1]));
|
||||
|
||||
if( !cap.isOpened() )
|
||||
{
|
||||
printf("can not open camera or video file\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
Mat tmp_frame, bgmask, out_frame;
|
||||
|
||||
cap >> tmp_frame;
|
||||
if(!tmp_frame.data)
|
||||
{
|
||||
printf("can not read data from the video source\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
namedWindow("video", 1);
|
||||
namedWindow("segmented", 1);
|
||||
|
||||
BackgroundSubtractorMOG bgsubtractor;
|
||||
bgsubtractor.noiseSigma = 10;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
cap >> tmp_frame;
|
||||
if( !tmp_frame.data )
|
||||
break;
|
||||
bgsubtractor(tmp_frame, bgmask, update_bg_model ? -1 : 0);
|
||||
//CvMat _bgmask = bgmask;
|
||||
//cvSegmentFGMask(&_bgmask);
|
||||
refineSegments(tmp_frame, bgmask, out_frame);
|
||||
imshow("video", tmp_frame);
|
||||
imshow("segmented", out_frame);
|
||||
char keycode = waitKey(30);
|
||||
if( keycode == 27 )
|
||||
break;
|
||||
if( keycode == ' ' )
|
||||
update_bg_model = !update_bg_model;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user