Merge pull request #9424 from Cartucho:update_imgproc_tutorials
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
static void help()
|
||||
{
|
||||
cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
|
||||
"Usage:\n"
|
||||
"./houghcircles <image_name>, Default is ../data/board.jpg\n" << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
cv::CommandLineParser parser(argc, argv,
|
||||
"{help h ||}{@image|../data/board.jpg|}"
|
||||
);
|
||||
if (parser.has("help"))
|
||||
{
|
||||
help();
|
||||
return 0;
|
||||
}
|
||||
//![load]
|
||||
string filename = parser.get<string>("@image");
|
||||
Mat img = imread(filename, IMREAD_COLOR);
|
||||
if(img.empty())
|
||||
{
|
||||
help();
|
||||
cout << "can not open " << filename << endl;
|
||||
return -1;
|
||||
}
|
||||
//![load]
|
||||
|
||||
//![convert_to_gray]
|
||||
Mat gray;
|
||||
cvtColor(img, gray, COLOR_BGR2GRAY);
|
||||
//![convert_to_gray]
|
||||
|
||||
//![reduce_noise]
|
||||
medianBlur(gray, gray, 5);
|
||||
//![reduce_noise]
|
||||
|
||||
//![houghcircles]
|
||||
vector<Vec3f> circles;
|
||||
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
|
||||
gray.rows/16, // change this value to detect circles with different distances to each other
|
||||
100, 30, 1, 30 // change the last two parameters
|
||||
// (min_radius & max_radius) to detect larger circles
|
||||
);
|
||||
//![houghcircles]
|
||||
|
||||
//![draw]
|
||||
for( size_t i = 0; i < circles.size(); i++ )
|
||||
{
|
||||
Vec3i c = circles[i];
|
||||
circle( img, Point(c[0], c[1]), c[2], Scalar(0,0,255), 3, LINE_AA);
|
||||
circle( img, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, LINE_AA);
|
||||
}
|
||||
//![draw]
|
||||
|
||||
//![display]
|
||||
imshow("detected circles", img);
|
||||
waitKey();
|
||||
//![display]
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
static void help()
|
||||
{
|
||||
cout << "\nThis program demonstrates line finding with the Hough transform.\n"
|
||||
"Usage:\n"
|
||||
"./houghlines <image_name>, Default is ../data/pic1.png\n" << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
cv::CommandLineParser parser(argc, argv,
|
||||
"{help h||}{@image|../data/pic1.png|}"
|
||||
);
|
||||
if (parser.has("help"))
|
||||
{
|
||||
help();
|
||||
return 0;
|
||||
}
|
||||
string filename = parser.get<string>("@image");
|
||||
if (filename.empty())
|
||||
{
|
||||
help();
|
||||
cout << "no image_name provided" << endl;
|
||||
return -1;
|
||||
}
|
||||
Mat src = imread(filename, 0);
|
||||
if(src.empty())
|
||||
{
|
||||
help();
|
||||
cout << "can not open " << filename << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Mat dst, cdst;
|
||||
Canny(src, dst, 50, 200, 3);
|
||||
cvtColor(dst, cdst, COLOR_GRAY2BGR);
|
||||
|
||||
#if 0
|
||||
vector<Vec2f> lines;
|
||||
HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
|
||||
|
||||
for( size_t i = 0; i < lines.size(); i++ )
|
||||
{
|
||||
float rho = lines[i][0], theta = lines[i][1];
|
||||
Point pt1, pt2;
|
||||
double a = cos(theta), b = sin(theta);
|
||||
double 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));
|
||||
line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
|
||||
}
|
||||
#else
|
||||
vector<Vec4i> lines;
|
||||
HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
|
||||
for( size_t i = 0; i < lines.size(); i++ )
|
||||
{
|
||||
Vec4i l = lines[i];
|
||||
line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
|
||||
}
|
||||
#endif
|
||||
imshow("source", src);
|
||||
imshow("detected lines", cdst);
|
||||
|
||||
waitKey();
|
||||
|
||||
return 0;
|
||||
}
|
||||
+8
-1
@@ -23,15 +23,22 @@ int main(){
|
||||
Mat output_image;
|
||||
morphologyEx(input_image, output_image, MORPH_HITMISS, kernel);
|
||||
|
||||
const int rate = 10;
|
||||
const int rate = 50;
|
||||
kernel = (kernel + 1) * 127;
|
||||
kernel.convertTo(kernel, CV_8U);
|
||||
|
||||
resize(kernel, kernel, Size(), rate, rate, INTER_NEAREST);
|
||||
imshow("kernel", kernel);
|
||||
moveWindow("kernel", 0, 0);
|
||||
|
||||
resize(input_image, input_image, Size(), rate, rate, INTER_NEAREST);
|
||||
imshow("Original", input_image);
|
||||
moveWindow("Original", 0, 200);
|
||||
|
||||
resize(output_image, output_image, Size(), rate, rate, INTER_NEAREST);
|
||||
imshow("Hit or Miss", output_image);
|
||||
moveWindow("Hit or Miss", 500, 200);
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/**
|
||||
* @file Pyramids.cpp
|
||||
* @brief Sample code of image pyramids (pyrDown and pyrUp)
|
||||
* @author OpenCV team
|
||||
*/
|
||||
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global variables
|
||||
Mat src, dst, tmp;
|
||||
|
||||
const char* window_name = "Pyramids Demo";
|
||||
|
||||
|
||||
/**
|
||||
* @function main
|
||||
*/
|
||||
int main( void )
|
||||
{
|
||||
/// General instructions
|
||||
printf( "\n Zoom In-Out demo \n " );
|
||||
printf( "------------------ \n" );
|
||||
printf( " * [u] -> Zoom in \n" );
|
||||
printf( " * [d] -> Zoom out \n" );
|
||||
printf( " * [ESC] -> Close program \n \n" );
|
||||
|
||||
//![load]
|
||||
src = imread( "../data/chicky_512.png" ); // Loads the test image
|
||||
if( src.empty() )
|
||||
{ printf(" No data! -- Exiting the program \n");
|
||||
return -1; }
|
||||
//![load]
|
||||
|
||||
tmp = src;
|
||||
dst = tmp;
|
||||
|
||||
//![create_window]
|
||||
imshow( window_name, dst );
|
||||
//![create_window]
|
||||
|
||||
//![infinite_loop]
|
||||
for(;;)
|
||||
{
|
||||
char c = (char)waitKey(0);
|
||||
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
//![pyrup]
|
||||
if( c == 'u' )
|
||||
{ pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
|
||||
printf( "** Zoom In: Image x 2 \n" );
|
||||
}
|
||||
//![pyrup]
|
||||
//![pyrdown]
|
||||
else if( c == 'd' )
|
||||
{ pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
|
||||
printf( "** Zoom Out: Image / 2 \n" );
|
||||
}
|
||||
//![pyrdown]
|
||||
imshow( window_name, dst );
|
||||
|
||||
//![update_tmp]
|
||||
tmp = dst;
|
||||
//![update_tmp]
|
||||
}
|
||||
//![infinite_loop]
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* @file Pyramids.cpp
|
||||
* @brief Sample code of image pyramids (pyrDown and pyrUp)
|
||||
* @author OpenCV team
|
||||
*/
|
||||
|
||||
#include "iostream"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
const char* window_name = "Pyramids Demo";
|
||||
|
||||
/**
|
||||
* @function main
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// General instructions
|
||||
cout << "\n Zoom In-Out demo \n "
|
||||
"------------------ \n"
|
||||
" * [i] -> Zoom in \n"
|
||||
" * [o] -> Zoom out \n"
|
||||
" * [ESC] -> Close program \n" << endl;
|
||||
|
||||
//![load]
|
||||
const char* filename = argc >=2 ? argv[1] : "../data/chicky_512.png";
|
||||
|
||||
// Loads an image
|
||||
Mat src = imread( filename );
|
||||
|
||||
// Check if image is loaded fine
|
||||
if(src.empty()){
|
||||
printf(" Error opening image\n");
|
||||
printf(" Program Arguments: [image_name -- default ../data/chicky_512.png] \n");
|
||||
return -1;
|
||||
}
|
||||
//![load]
|
||||
|
||||
//![loop]
|
||||
for(;;)
|
||||
{
|
||||
//![show_image]
|
||||
imshow( window_name, src );
|
||||
//![show_image]
|
||||
char c = (char)waitKey(0);
|
||||
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
//![pyrup]
|
||||
else if( c == 'i' )
|
||||
{ pyrUp( src, src, Size( src.cols*2, src.rows*2 ) );
|
||||
printf( "** Zoom In: Image x 2 \n" );
|
||||
}
|
||||
//![pyrup]
|
||||
//![pyrdown]
|
||||
else if( c == 'o' )
|
||||
{ pyrDown( src, src, Size( src.cols/2, src.rows/2 ) );
|
||||
printf( "** Zoom Out: Image / 2 \n" );
|
||||
}
|
||||
//![pyrdown]
|
||||
}
|
||||
//![loop]
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
/**
|
||||
* file Smoothing.cpp
|
||||
* brief Sample code for simple filters
|
||||
* author OpenCV team
|
||||
*/
|
||||
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
/// Global Variables
|
||||
int DELAY_CAPTION = 1500;
|
||||
int DELAY_BLUR = 100;
|
||||
int MAX_KERNEL_LENGTH = 31;
|
||||
|
||||
Mat src; Mat dst;
|
||||
char window_name[] = "Smoothing Demo";
|
||||
|
||||
/// Function headers
|
||||
int display_caption( const char* caption );
|
||||
int display_dst( int delay );
|
||||
|
||||
|
||||
/**
|
||||
* function main
|
||||
*/
|
||||
int main( void )
|
||||
{
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
|
||||
/// Load the source image
|
||||
src = imread( "../data/lena.jpg", IMREAD_COLOR );
|
||||
|
||||
if( display_caption( "Original Image" ) != 0 ) { return 0; }
|
||||
|
||||
dst = src.clone();
|
||||
if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; }
|
||||
|
||||
|
||||
/// Applying Homogeneous blur
|
||||
if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }
|
||||
|
||||
//![blur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ blur( src, dst, Size( i, i ), Point(-1,-1) );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
//![blur]
|
||||
|
||||
/// Applying Gaussian blur
|
||||
if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }
|
||||
|
||||
//![gaussianblur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ GaussianBlur( src, dst, Size( i, i ), 0, 0 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
//![gaussianblur]
|
||||
|
||||
/// Applying Median blur
|
||||
if( display_caption( "Median Blur" ) != 0 ) { return 0; }
|
||||
|
||||
//![medianblur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ medianBlur ( src, dst, i );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
//![medianblur]
|
||||
|
||||
/// Applying Bilateral Filter
|
||||
if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }
|
||||
|
||||
//![bilateralfilter]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ bilateralFilter ( src, dst, i, i*2, i/2 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
//![bilateralfilter]
|
||||
|
||||
/// Wait until user press a key
|
||||
display_caption( "End: Press a key!" );
|
||||
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function display_caption
|
||||
*/
|
||||
int display_caption( const char* caption )
|
||||
{
|
||||
dst = Mat::zeros( src.size(), src.type() );
|
||||
putText( dst, caption,
|
||||
Point( src.cols/4, src.rows/2),
|
||||
FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );
|
||||
|
||||
imshow( window_name, dst );
|
||||
int c = waitKey( DELAY_CAPTION );
|
||||
if( c >= 0 ) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function display_dst
|
||||
*/
|
||||
int display_dst( int delay )
|
||||
{
|
||||
imshow( window_name, dst );
|
||||
int c = waitKey ( delay );
|
||||
if( c >= 0 ) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* file Smoothing.cpp
|
||||
* brief Sample code for simple filters
|
||||
* author OpenCV team
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
/// Global Variables
|
||||
int DELAY_CAPTION = 1500;
|
||||
int DELAY_BLUR = 100;
|
||||
int MAX_KERNEL_LENGTH = 31;
|
||||
|
||||
Mat src; Mat dst;
|
||||
char window_name[] = "Smoothing Demo";
|
||||
|
||||
/// Function headers
|
||||
int display_caption( const char* caption );
|
||||
int display_dst( int delay );
|
||||
|
||||
|
||||
/**
|
||||
* function main
|
||||
*/
|
||||
int main( int argc, char ** argv )
|
||||
{
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
|
||||
/// Load the source image
|
||||
const char* filename = argc >=2 ? argv[1] : "../data/lena.jpg";
|
||||
|
||||
src = imread( filename, IMREAD_COLOR );
|
||||
if(src.empty()){
|
||||
printf(" Error opening image\n");
|
||||
printf(" Usage: ./Smoothing [image_name -- default ../data/lena.jpg] \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if( display_caption( "Original Image" ) != 0 ) { return 0; }
|
||||
|
||||
dst = src.clone();
|
||||
if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; }
|
||||
|
||||
|
||||
/// Applying Homogeneous blur
|
||||
if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }
|
||||
|
||||
//![blur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ blur( src, dst, Size( i, i ), Point(-1,-1) );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
//![blur]
|
||||
|
||||
/// Applying Gaussian blur
|
||||
if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }
|
||||
|
||||
//![gaussianblur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ GaussianBlur( src, dst, Size( i, i ), 0, 0 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
//![gaussianblur]
|
||||
|
||||
/// Applying Median blur
|
||||
if( display_caption( "Median Blur" ) != 0 ) { return 0; }
|
||||
|
||||
//![medianblur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ medianBlur ( src, dst, i );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
//![medianblur]
|
||||
|
||||
/// Applying Bilateral Filter
|
||||
if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }
|
||||
|
||||
//![bilateralfilter]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ bilateralFilter ( src, dst, i, i*2, i/2 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
//![bilateralfilter]
|
||||
|
||||
/// Done
|
||||
display_caption( "Done!" );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function display_caption
|
||||
*/
|
||||
int display_caption( const char* caption )
|
||||
{
|
||||
dst = Mat::zeros( src.size(), src.type() );
|
||||
putText( dst, caption,
|
||||
Point( src.cols/4, src.rows/2),
|
||||
FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );
|
||||
|
||||
return display_dst(DELAY_CAPTION);
|
||||
}
|
||||
|
||||
/**
|
||||
* @function display_dst
|
||||
*/
|
||||
int display_dst( int delay )
|
||||
{
|
||||
imshow( window_name, dst );
|
||||
int c = waitKey ( delay );
|
||||
if( c >= 0 ) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
+42
-32
@@ -4,28 +4,32 @@
|
||||
* @author OpenCV team
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
void show_wait_destroy(const char* winname, cv::Mat img);
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
int main(int, char** argv)
|
||||
{
|
||||
//! [load_image]
|
||||
//! [load_image]
|
||||
// Load the image
|
||||
Mat src = imread(argv[1]);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if(!src.data)
|
||||
cerr << "Problem loading image!!!" << endl;
|
||||
if(src.empty()){
|
||||
printf(" Error opening image\n");
|
||||
printf(" Program Arguments: [image_path]\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Show source image
|
||||
imshow("src", src);
|
||||
//! [load_image]
|
||||
//! [load_image]
|
||||
|
||||
//! [gray]
|
||||
// Transform source image to gray if it is not
|
||||
//! [gray]
|
||||
// Transform source image to gray if it is not already
|
||||
Mat gray;
|
||||
|
||||
if (src.channels() == 3)
|
||||
@@ -38,58 +42,58 @@ int main(int, char** argv)
|
||||
}
|
||||
|
||||
// Show gray image
|
||||
imshow("gray", gray);
|
||||
//! [gray]
|
||||
show_wait_destroy("gray", gray);
|
||||
//! [gray]
|
||||
|
||||
//! [bin]
|
||||
//! [bin]
|
||||
// Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
|
||||
Mat bw;
|
||||
adaptiveThreshold(~gray, bw, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
|
||||
|
||||
// Show binary image
|
||||
imshow("binary", bw);
|
||||
//! [bin]
|
||||
show_wait_destroy("binary", bw);
|
||||
//! [bin]
|
||||
|
||||
//! [init]
|
||||
//! [init]
|
||||
// Create the images that will use to extract the horizontal and vertical lines
|
||||
Mat horizontal = bw.clone();
|
||||
Mat vertical = bw.clone();
|
||||
//! [init]
|
||||
//! [init]
|
||||
|
||||
//! [horiz]
|
||||
//! [horiz]
|
||||
// Specify size on horizontal axis
|
||||
int horizontalsize = horizontal.cols / 30;
|
||||
int horizontal_size = horizontal.cols / 30;
|
||||
|
||||
// Create structure element for extracting horizontal lines through morphology operations
|
||||
Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(horizontalsize,1));
|
||||
Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(horizontal_size, 1));
|
||||
|
||||
// Apply morphology operations
|
||||
erode(horizontal, horizontal, horizontalStructure, Point(-1, -1));
|
||||
dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1));
|
||||
|
||||
// Show extracted horizontal lines
|
||||
imshow("horizontal", horizontal);
|
||||
//! [horiz]
|
||||
show_wait_destroy("horizontal", horizontal);
|
||||
//! [horiz]
|
||||
|
||||
//! [vert]
|
||||
//! [vert]
|
||||
// Specify size on vertical axis
|
||||
int verticalsize = vertical.rows / 30;
|
||||
int vertical_size = vertical.rows / 30;
|
||||
|
||||
// Create structure element for extracting vertical lines through morphology operations
|
||||
Mat verticalStructure = getStructuringElement(MORPH_RECT, Size( 1,verticalsize));
|
||||
Mat verticalStructure = getStructuringElement(MORPH_RECT, Size(1, vertical_size));
|
||||
|
||||
// Apply morphology operations
|
||||
erode(vertical, vertical, verticalStructure, Point(-1, -1));
|
||||
dilate(vertical, vertical, verticalStructure, Point(-1, -1));
|
||||
|
||||
// Show extracted vertical lines
|
||||
imshow("vertical", vertical);
|
||||
//! [vert]
|
||||
show_wait_destroy("vertical", vertical);
|
||||
//! [vert]
|
||||
|
||||
//! [smooth]
|
||||
//! [smooth]
|
||||
// Inverse vertical image
|
||||
bitwise_not(vertical, vertical);
|
||||
imshow("vertical_bit", vertical);
|
||||
show_wait_destroy("vertical_bit", vertical);
|
||||
|
||||
// Extract edges and smooth image according to the logic
|
||||
// 1. extract edges
|
||||
@@ -101,12 +105,12 @@ int main(int, char** argv)
|
||||
// Step 1
|
||||
Mat edges;
|
||||
adaptiveThreshold(vertical, edges, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, -2);
|
||||
imshow("edges", edges);
|
||||
show_wait_destroy("edges", edges);
|
||||
|
||||
// Step 2
|
||||
Mat kernel = Mat::ones(2, 2, CV_8UC1);
|
||||
dilate(edges, edges, kernel);
|
||||
imshow("dilate", edges);
|
||||
show_wait_destroy("dilate", edges);
|
||||
|
||||
// Step 3
|
||||
Mat smooth;
|
||||
@@ -119,9 +123,15 @@ int main(int, char** argv)
|
||||
smooth.copyTo(vertical, edges);
|
||||
|
||||
// Show final result
|
||||
imshow("smooth", vertical);
|
||||
//! [smooth]
|
||||
show_wait_destroy("smooth - final", vertical);
|
||||
//! [smooth]
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void show_wait_destroy(const char* winname, cv::Mat img) {
|
||||
imshow(winname, img);
|
||||
moveWindow(winname, 500, 0);
|
||||
waitKey(0);
|
||||
destroyWindow(winname);
|
||||
}
|
||||
@@ -15,50 +15,53 @@ using namespace cv;
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
//![variables]
|
||||
Mat src, src_gray, dst;
|
||||
int kernel_size = 3;
|
||||
int scale = 1;
|
||||
int delta = 0;
|
||||
int ddepth = CV_16S;
|
||||
const char* window_name = "Laplace Demo";
|
||||
//![variables]
|
||||
//![variables]
|
||||
// Declare the variables we are going to use
|
||||
Mat src, src_gray, dst;
|
||||
int kernel_size = 3;
|
||||
int scale = 1;
|
||||
int delta = 0;
|
||||
int ddepth = CV_16S;
|
||||
const char* window_name = "Laplace Demo";
|
||||
//![variables]
|
||||
|
||||
//![load]
|
||||
String imageName("../data/lena.jpg"); // by default
|
||||
if (argc > 1)
|
||||
{
|
||||
imageName = argv[1];
|
||||
}
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
//![load]
|
||||
const char* imageName = argc >=2 ? argv[1] : "../data/lena.jpg";
|
||||
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
//![load]
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
|
||||
//![reduce_noise]
|
||||
/// Reduce noise by blurring with a Gaussian filter
|
||||
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
|
||||
//![reduce_noise]
|
||||
// Check if image is loaded fine
|
||||
if(src.empty()){
|
||||
printf(" Error opening image\n");
|
||||
printf(" Program Arguments: [image_name -- default ../data/lena.jpg] \n");
|
||||
return -1;
|
||||
}
|
||||
//![load]
|
||||
|
||||
//![convert_to_gray]
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
|
||||
//![convert_to_gray]
|
||||
//![reduce_noise]
|
||||
// Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
GaussianBlur( src, src, Size(3, 3), 0, 0, BORDER_DEFAULT );
|
||||
//![reduce_noise]
|
||||
|
||||
/// Apply Laplace function
|
||||
Mat abs_dst;
|
||||
//![laplacian]
|
||||
Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
|
||||
//![laplacian]
|
||||
//![convert_to_gray]
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
|
||||
//![convert_to_gray]
|
||||
|
||||
//![convert]
|
||||
convertScaleAbs( dst, abs_dst );
|
||||
//![convert]
|
||||
/// Apply Laplace function
|
||||
Mat abs_dst;
|
||||
//![laplacian]
|
||||
Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
|
||||
//![laplacian]
|
||||
|
||||
//![display]
|
||||
imshow( window_name, abs_dst );
|
||||
waitKey(0);
|
||||
//![display]
|
||||
//![convert]
|
||||
// converting back to CV_8U
|
||||
convertScaleAbs( dst, abs_dst );
|
||||
//![convert]
|
||||
|
||||
return 0;
|
||||
//![display]
|
||||
imshow( window_name, abs_dst );
|
||||
waitKey(0);
|
||||
//![display]
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ int main( int argc, char** argv )
|
||||
cout << "\nPress 'ESC' to exit program.\nPress 'R' to reset values ( ksize will be -1 equal to Scharr function )";
|
||||
|
||||
//![variables]
|
||||
// First we declare the variables we are going to use
|
||||
Mat image,src, src_gray;
|
||||
Mat grad;
|
||||
const String window_name = "Sobel Demo - Simple Edge Detector";
|
||||
@@ -40,11 +41,14 @@ int main( int argc, char** argv )
|
||||
//![variables]
|
||||
|
||||
//![load]
|
||||
String imageName = parser.get<String>("@input"); // by default
|
||||
String imageName = parser.get<String>("@input");
|
||||
// As usual we load our source image (src)
|
||||
image = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( image.empty() )
|
||||
{
|
||||
printf("Error opening image: %s\n", imageName.c_str());
|
||||
return 1;
|
||||
}
|
||||
//![load]
|
||||
@@ -52,10 +56,12 @@ int main( int argc, char** argv )
|
||||
for (;;)
|
||||
{
|
||||
//![reduce_noise]
|
||||
// Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
|
||||
//![reduce_noise]
|
||||
|
||||
//![convert_to_gray]
|
||||
// Convert the image to grayscale
|
||||
cvtColor(src, src_gray, COLOR_BGR2GRAY);
|
||||
//![convert_to_gray]
|
||||
|
||||
@@ -72,6 +78,7 @@ int main( int argc, char** argv )
|
||||
//![sobel]
|
||||
|
||||
//![convert]
|
||||
// converting back to CV_8U
|
||||
convertScaleAbs(grad_x, abs_grad_x);
|
||||
convertScaleAbs(grad_y, abs_grad_y);
|
||||
//![convert]
|
||||
|
||||
@@ -11,9 +11,10 @@
|
||||
using namespace cv;
|
||||
|
||||
//![variables]
|
||||
// Declare the variables
|
||||
Mat src, dst;
|
||||
int top, bottom, left, right;
|
||||
int borderType;
|
||||
int borderType = BORDER_CONSTANT;
|
||||
const char* window_name = "copyMakeBorder Demo";
|
||||
RNG rng(12345);
|
||||
//![variables]
|
||||
@@ -23,65 +24,61 @@ RNG rng(12345);
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
//![load]
|
||||
String imageName("../data/lena.jpg"); // by default
|
||||
if (argc > 1)
|
||||
{
|
||||
imageName = argv[1];
|
||||
}
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
//![load]
|
||||
const char* imageName = argc >=2 ? argv[1] : "../data/lena.jpg";
|
||||
|
||||
if( src.empty() )
|
||||
{
|
||||
printf(" No data entered, please enter the path to an image file \n");
|
||||
return -1;
|
||||
// Loads an image
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty()) {
|
||||
printf(" Error opening image\n");
|
||||
printf(" Program Arguments: [image_name -- default ../data/lena.jpg] \n");
|
||||
return -1;
|
||||
}
|
||||
//![load]
|
||||
//![load]
|
||||
|
||||
/// Brief how-to for this program
|
||||
printf( "\n \t copyMakeBorder Demo: \n" );
|
||||
printf( "\t -------------------- \n" );
|
||||
printf( " ** Press 'c' to set the border to a random constant value \n");
|
||||
printf( " ** Press 'r' to set the border to be replicated \n");
|
||||
printf( " ** Press 'ESC' to exit the program \n");
|
||||
// Brief how-to for this program
|
||||
printf( "\n \t copyMakeBorder Demo: \n" );
|
||||
printf( "\t -------------------- \n" );
|
||||
printf( " ** Press 'c' to set the border to a random constant value \n");
|
||||
printf( " ** Press 'r' to set the border to be replicated \n");
|
||||
printf( " ** Press 'ESC' to exit the program \n");
|
||||
|
||||
//![create_window]
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
//![create_window]
|
||||
//![create_window]
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
//![create_window]
|
||||
|
||||
//![init_arguments]
|
||||
/// Initialize arguments for the filter
|
||||
top = (int) (0.05*src.rows); bottom = (int) (0.05*src.rows);
|
||||
left = (int) (0.05*src.cols); right = (int) (0.05*src.cols);
|
||||
//![init_arguments]
|
||||
//![init_arguments]
|
||||
// Initialize arguments for the filter
|
||||
top = (int) (0.05*src.rows); bottom = top;
|
||||
left = (int) (0.05*src.cols); right = left;
|
||||
//![init_arguments]
|
||||
|
||||
dst = src;
|
||||
imshow( window_name, dst );
|
||||
for(;;)
|
||||
{
|
||||
//![update_value]
|
||||
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
|
||||
//![update_value]
|
||||
|
||||
for(;;)
|
||||
{
|
||||
//![check_keypress]
|
||||
char c = (char)waitKey(500);
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
else if( c == 'c' )
|
||||
{ borderType = BORDER_CONSTANT; }
|
||||
else if( c == 'r' )
|
||||
{ borderType = BORDER_REPLICATE; }
|
||||
//![check_keypress]
|
||||
//![copymakeborder]
|
||||
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
|
||||
//![copymakeborder]
|
||||
|
||||
//![update_value]
|
||||
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
|
||||
//![update_value]
|
||||
//![display]
|
||||
imshow( window_name, dst );
|
||||
//![display]
|
||||
|
||||
//![copymakeborder]
|
||||
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
|
||||
//![copymakeborder]
|
||||
//![check_keypress]
|
||||
char c = (char)waitKey(500);
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
else if( c == 'c' )
|
||||
{ borderType = BORDER_CONSTANT; }
|
||||
else if( c == 'r' )
|
||||
{ borderType = BORDER_REPLICATE; }
|
||||
//![check_keypress]
|
||||
}
|
||||
|
||||
//![display]
|
||||
imshow( window_name, dst );
|
||||
//![display]
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -15,56 +15,60 @@ using namespace cv;
|
||||
*/
|
||||
int main ( int argc, char** argv )
|
||||
{
|
||||
/// Declare variables
|
||||
Mat src, dst;
|
||||
// Declare variables
|
||||
Mat src, dst;
|
||||
|
||||
Mat kernel;
|
||||
Point anchor;
|
||||
double delta;
|
||||
int ddepth;
|
||||
int kernel_size;
|
||||
const char* window_name = "filter2D Demo";
|
||||
Mat kernel;
|
||||
Point anchor;
|
||||
double delta;
|
||||
int ddepth;
|
||||
int kernel_size;
|
||||
const char* window_name = "filter2D Demo";
|
||||
|
||||
//![load]
|
||||
String imageName("../data/lena.jpg"); // by default
|
||||
if (argc > 1)
|
||||
{
|
||||
imageName = argv[1];
|
||||
}
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
//![load]
|
||||
const char* imageName = argc >=2 ? argv[1] : "../data/lena.jpg";
|
||||
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
//![load]
|
||||
// Loads an image
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
|
||||
//![init_arguments]
|
||||
/// Initialize arguments for the filter
|
||||
anchor = Point( -1, -1 );
|
||||
delta = 0;
|
||||
ddepth = -1;
|
||||
//![init_arguments]
|
||||
if( src.empty() )
|
||||
{
|
||||
printf(" Error opening image\n");
|
||||
printf(" Program Arguments: [image_name -- default ../data/lena.jpg] \n");
|
||||
return -1;
|
||||
}
|
||||
//![load]
|
||||
|
||||
/// Loop - Will filter the image with different kernel sizes each 0.5 seconds
|
||||
int ind = 0;
|
||||
for(;;)
|
||||
{
|
||||
char c = (char)waitKey(500);
|
||||
/// Press 'ESC' to exit the program
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
//![init_arguments]
|
||||
// Initialize arguments for the filter
|
||||
anchor = Point( -1, -1 );
|
||||
delta = 0;
|
||||
ddepth = -1;
|
||||
//![init_arguments]
|
||||
|
||||
//![update_kernel]
|
||||
/// Update kernel size for a normalized box filter
|
||||
kernel_size = 3 + 2*( ind%5 );
|
||||
kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);
|
||||
//![update_kernel]
|
||||
// Loop - Will filter the image with different kernel sizes each 0.5 seconds
|
||||
int ind = 0;
|
||||
for(;;)
|
||||
{
|
||||
//![update_kernel]
|
||||
// Update kernel size for a normalized box filter
|
||||
kernel_size = 3 + 2*( ind%5 );
|
||||
kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);
|
||||
//![update_kernel]
|
||||
|
||||
//![apply_filter]
|
||||
filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );
|
||||
//![apply_filter]
|
||||
imshow( window_name, dst );
|
||||
ind++;
|
||||
}
|
||||
//![apply_filter]
|
||||
// Apply filter
|
||||
filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );
|
||||
//![apply_filter]
|
||||
imshow( window_name, dst );
|
||||
|
||||
return 0;
|
||||
char c = (char)waitKey(500);
|
||||
// Press 'ESC' to exit the program
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
|
||||
ind++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @file houghcircles.cpp
|
||||
* @brief This program demonstrates circle finding with the Hough transform
|
||||
*/
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
//![load]
|
||||
const char* filename = argc >=2 ? argv[1] : "../../../data/smarties.png";
|
||||
|
||||
// Loads an image
|
||||
Mat src = imread( filename, IMREAD_COLOR );
|
||||
|
||||
// Check if image is loaded fine
|
||||
if(src.empty()){
|
||||
printf(" Error opening image\n");
|
||||
printf(" Program Arguments: [image_name -- default %s] \n", filename);
|
||||
return -1;
|
||||
}
|
||||
//![load]
|
||||
|
||||
//![convert_to_gray]
|
||||
Mat gray;
|
||||
cvtColor(src, gray, COLOR_BGR2GRAY);
|
||||
//![convert_to_gray]
|
||||
|
||||
//![reduce_noise]
|
||||
medianBlur(gray, gray, 5);
|
||||
//![reduce_noise]
|
||||
|
||||
//![houghcircles]
|
||||
vector<Vec3f> circles;
|
||||
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
|
||||
gray.rows/16, // change this value to detect circles with different distances to each other
|
||||
100, 30, 1, 30 // change the last two parameters
|
||||
// (min_radius & max_radius) to detect larger circles
|
||||
);
|
||||
//![houghcircles]
|
||||
|
||||
//![draw]
|
||||
for( size_t i = 0; i < circles.size(); i++ )
|
||||
{
|
||||
Vec3i c = circles[i];
|
||||
Point center = Point(c[0], c[1]);
|
||||
// circle center
|
||||
circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA);
|
||||
// circle outline
|
||||
int radius = c[2];
|
||||
circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA);
|
||||
}
|
||||
//![draw]
|
||||
|
||||
//![display]
|
||||
imshow("detected circles", src);
|
||||
waitKey();
|
||||
//![display]
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* @file houghclines.cpp
|
||||
* @brief This program demonstrates line finding with the Hough transform
|
||||
*/
|
||||
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Declare the output variables
|
||||
Mat dst, cdst, cdstP;
|
||||
|
||||
//![load]
|
||||
const char* default_file = "../../../data/sudoku.png";
|
||||
const char* filename = argc >=2 ? argv[1] : default_file;
|
||||
|
||||
// Loads an image
|
||||
Mat src = imread( filename, IMREAD_GRAYSCALE );
|
||||
|
||||
// Check if image is loaded fine
|
||||
if(src.empty()){
|
||||
printf(" Error opening image\n");
|
||||
printf(" Program Arguments: [image_name -- default %s] \n", default_file);
|
||||
return -1;
|
||||
}
|
||||
//![load]
|
||||
|
||||
//![edge_detection]
|
||||
// Edge detection
|
||||
Canny(src, dst, 50, 200, 3);
|
||||
//![edge_detection]
|
||||
|
||||
// Copy edges to the images that will display the results in BGR
|
||||
cvtColor(dst, cdst, COLOR_GRAY2BGR);
|
||||
cdstP = cdst.clone();
|
||||
|
||||
//![hough_lines]
|
||||
// Standard Hough Line Transform
|
||||
vector<Vec2f> lines; // will hold the results of the detection
|
||||
HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 ); // runs the actual detection
|
||||
//![hough_lines]
|
||||
//![draw_lines]
|
||||
// Draw the lines
|
||||
for( size_t i = 0; i < lines.size(); i++ )
|
||||
{
|
||||
float rho = lines[i][0], theta = lines[i][1];
|
||||
Point pt1, pt2;
|
||||
double a = cos(theta), b = sin(theta);
|
||||
double 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));
|
||||
line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
|
||||
}
|
||||
//![draw_lines]
|
||||
|
||||
//![hough_lines_p]
|
||||
// Probabilistic Line Transform
|
||||
vector<Vec4i> linesP; // will hold the results of the detection
|
||||
HoughLinesP(dst, linesP, 1, CV_PI/180, 50, 50, 10 ); // runs the actual detection
|
||||
//![hough_lines_p]
|
||||
//![draw_lines_p]
|
||||
// Draw the lines
|
||||
for( size_t i = 0; i < linesP.size(); i++ )
|
||||
{
|
||||
Vec4i l = linesP[i];
|
||||
line( cdstP, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
|
||||
}
|
||||
//![draw_lines_p]
|
||||
|
||||
//![imshow]
|
||||
// Show results
|
||||
imshow("Source", src);
|
||||
imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
|
||||
imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);
|
||||
//![imshow]
|
||||
|
||||
//![exit]
|
||||
// Wait and Exit
|
||||
waitKey();
|
||||
return 0;
|
||||
//![exit]
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 89 KiB |
@@ -0,0 +1,58 @@
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class HitMissRun{
|
||||
|
||||
public void run() {
|
||||
Mat input_image = new Mat( 8, 8, CvType.CV_8UC1 );
|
||||
int row = 0, col = 0;
|
||||
input_image.put(row ,col,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 255, 255, 255, 0, 0, 0, 255,
|
||||
0, 255, 255, 255, 0, 0, 0, 0,
|
||||
0, 255, 255, 255, 0, 255, 0, 0,
|
||||
0, 0, 255, 0, 0, 0, 0, 0,
|
||||
0, 0, 255, 0, 0, 255, 255, 0,
|
||||
0, 255, 0, 255, 0, 0, 255, 0,
|
||||
0, 255, 255, 255, 0, 0, 0, 0);
|
||||
|
||||
Mat kernel = new Mat( 3, 3, CvType.CV_16S );
|
||||
kernel.put(row ,col,
|
||||
0, 1, 0,
|
||||
1, -1, 1,
|
||||
0, 1, 0 );
|
||||
|
||||
Mat output_image = new Mat();
|
||||
Imgproc.morphologyEx(input_image, output_image, Imgproc.MORPH_HITMISS, kernel);
|
||||
|
||||
int rate = 50;
|
||||
Core.add(kernel, new Scalar(1), kernel);
|
||||
Core.multiply(kernel, new Scalar(127), kernel);
|
||||
kernel.convertTo(kernel, CvType.CV_8U);
|
||||
|
||||
Imgproc.resize(kernel, kernel, new Size(), rate, rate, Imgproc.INTER_NEAREST);
|
||||
HighGui.imshow("kernel", kernel);
|
||||
HighGui.moveWindow("kernel", 0, 0);
|
||||
|
||||
Imgproc.resize(input_image, input_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
|
||||
HighGui.imshow("Original", input_image);
|
||||
HighGui.moveWindow("Original", 0, 200);
|
||||
|
||||
Imgproc.resize(output_image, output_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
|
||||
HighGui.imshow("Hit or Miss", output_image);
|
||||
HighGui.moveWindow("Hit or Miss", 500, 200);
|
||||
|
||||
HighGui.waitKey(0);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class HitMiss
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
// load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new HitMissRun().run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class PyramidsRun {
|
||||
|
||||
String window_name = "Pyramids Demo";
|
||||
|
||||
public void run(String[] args) {
|
||||
/// General instructions
|
||||
System.out.println("\n" +
|
||||
" Zoom In-Out demo \n" +
|
||||
"------------------ \n" +
|
||||
" * [i] -> Zoom [i]n \n" +
|
||||
" * [o] -> Zoom [o]ut \n" +
|
||||
" * [ESC] -> Close program \n");
|
||||
|
||||
//! [load]
|
||||
String filename = ((args.length > 0) ? args[0] : "../data/chicky_512.png");
|
||||
|
||||
// Load the image
|
||||
Mat src = Imgcodecs.imread(filename);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image!");
|
||||
System.out.println("Program Arguments: [image_name -- default ../data/chicky_512.png] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
//! [loop]
|
||||
while (true){
|
||||
//! [show_image]
|
||||
HighGui.imshow( window_name, src );
|
||||
//! [show_image]
|
||||
char c = (char) HighGui.waitKey(0);
|
||||
c = Character.toLowerCase(c);
|
||||
|
||||
if( c == 27 ){
|
||||
break;
|
||||
//![pyrup]
|
||||
}else if( c == 'i'){
|
||||
Imgproc.pyrUp( src, src, new Size( src.cols()*2, src.rows()*2 ) );
|
||||
System.out.println( "** Zoom In: Image x 2" );
|
||||
//![pyrup]
|
||||
//![pyrdown]
|
||||
}else if( c == 'o'){
|
||||
Imgproc.pyrDown( src, src, new Size( src.cols()/2, src.rows()/2 ) );
|
||||
System.out.println( "** Zoom Out: Image / 2" );
|
||||
//![pyrdown]
|
||||
}
|
||||
}
|
||||
//! [loop]
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class Pyramids {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new PyramidsRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class SmoothingRun {
|
||||
|
||||
/// Global Variables
|
||||
int DELAY_CAPTION = 1500;
|
||||
int DELAY_BLUR = 100;
|
||||
int MAX_KERNEL_LENGTH = 31;
|
||||
|
||||
Mat src = new Mat(), dst = new Mat();
|
||||
String windowName = "Filter Demo 1";
|
||||
|
||||
public void run(String[] args) {
|
||||
|
||||
String filename = ((args.length > 0) ? args[0] : "../data/lena.jpg");
|
||||
|
||||
src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image");
|
||||
System.out.println("Usage: ./Smoothing [image_name -- default ../data/lena.jpg] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
if( displayCaption( "Original Image" ) != 0 ) { System.exit(0); }
|
||||
|
||||
dst = src.clone();
|
||||
if( displayDst( DELAY_CAPTION ) != 0 ) { System.exit(0); }
|
||||
|
||||
/// Applying Homogeneous blur
|
||||
if( displayCaption( "Homogeneous Blur" ) != 0 ) { System.exit(0); }
|
||||
|
||||
//! [blur]
|
||||
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
|
||||
Imgproc.blur(src, dst, new Size(i, i), new Point(-1, -1));
|
||||
displayDst(DELAY_BLUR);
|
||||
}
|
||||
//! [blur]
|
||||
|
||||
/// Applying Gaussian blur
|
||||
if( displayCaption( "Gaussian Blur" ) != 0 ) { System.exit(0); }
|
||||
|
||||
//! [gaussianblur]
|
||||
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
|
||||
Imgproc.GaussianBlur(src, dst, new Size(i, i), 0, 0);
|
||||
displayDst(DELAY_BLUR);
|
||||
}
|
||||
//! [gaussianblur]
|
||||
|
||||
/// Applying Median blur
|
||||
if( displayCaption( "Median Blur" ) != 0 ) { System.exit(0); }
|
||||
|
||||
//! [medianblur]
|
||||
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
|
||||
Imgproc.medianBlur(src, dst, i);
|
||||
displayDst(DELAY_BLUR);
|
||||
}
|
||||
//! [medianblur]
|
||||
|
||||
/// Applying Bilateral Filter
|
||||
if( displayCaption( "Bilateral Blur" ) != 0 ) { System.exit(0); }
|
||||
|
||||
//![bilateralfilter]
|
||||
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
|
||||
Imgproc.bilateralFilter(src, dst, i, i * 2, i / 2);
|
||||
displayDst(DELAY_BLUR);
|
||||
}
|
||||
//![bilateralfilter]
|
||||
|
||||
/// Done
|
||||
displayCaption( "Done!" );
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
int displayCaption(String caption) {
|
||||
dst = Mat.zeros(src.size(), src.type());
|
||||
Imgproc.putText(dst, caption,
|
||||
new Point(src.cols() / 4, src.rows() / 2),
|
||||
Core.FONT_HERSHEY_COMPLEX, 1, new Scalar(255, 255, 255));
|
||||
|
||||
return displayDst(DELAY_CAPTION);
|
||||
}
|
||||
|
||||
int displayDst(int delay) {
|
||||
HighGui.imshow( windowName, dst );
|
||||
int c = HighGui.waitKey( delay );
|
||||
if (c >= 0) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public class Smoothing {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new SmoothingRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* @file Morphology_3.java
|
||||
* @brief Use morphology transformations for extracting horizontal and vertical lines sample code
|
||||
*/
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class Morphology_3Run {
|
||||
|
||||
public void run(String[] args) {
|
||||
|
||||
//! [load_image]
|
||||
// Check number of arguments
|
||||
if (args.length == 0){
|
||||
System.out.println("Not enough parameters!");
|
||||
System.out.println("Program Arguments: [image_path]");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
// Load the image
|
||||
Mat src = Imgcodecs.imread(args[0]);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image: " + args[0]);
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
// Show source image
|
||||
HighGui.imshow("src", src);
|
||||
//! [load_image]
|
||||
|
||||
//! [gray]
|
||||
// Transform source image to gray if it is not already
|
||||
Mat gray = new Mat();
|
||||
|
||||
if (src.channels() == 3)
|
||||
{
|
||||
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
gray = src;
|
||||
}
|
||||
|
||||
// Show gray image
|
||||
showWaitDestroy("gray" , gray);
|
||||
//! [gray]
|
||||
|
||||
//! [bin]
|
||||
// Apply adaptiveThreshold at the bitwise_not of gray
|
||||
Mat bw = new Mat();
|
||||
Core.bitwise_not(gray, gray);
|
||||
Imgproc.adaptiveThreshold(gray, bw, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, -2);
|
||||
|
||||
// Show binary image
|
||||
showWaitDestroy("binary" , bw);
|
||||
//! [bin]
|
||||
|
||||
//! [init]
|
||||
// Create the images that will use to extract the horizontal and vertical lines
|
||||
Mat horizontal = bw.clone();
|
||||
Mat vertical = bw.clone();
|
||||
//! [init]
|
||||
|
||||
//! [horiz]
|
||||
// Specify size on horizontal axis
|
||||
int horizontal_size = horizontal.cols() / 30;
|
||||
|
||||
// Create structure element for extracting horizontal lines through morphology operations
|
||||
Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size,1));
|
||||
|
||||
// Apply morphology operations
|
||||
Imgproc.erode(horizontal, horizontal, horizontalStructure);
|
||||
Imgproc.dilate(horizontal, horizontal, horizontalStructure);
|
||||
|
||||
// Show extracted horizontal lines
|
||||
showWaitDestroy("horizontal" , horizontal);
|
||||
//! [horiz]
|
||||
|
||||
//! [vert]
|
||||
// Specify size on vertical axis
|
||||
int vertical_size = vertical.rows() / 30;
|
||||
|
||||
// Create structure element for extracting vertical lines through morphology operations
|
||||
Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size( 1,vertical_size));
|
||||
|
||||
// Apply morphology operations
|
||||
Imgproc.erode(vertical, vertical, verticalStructure);
|
||||
Imgproc.dilate(vertical, vertical, verticalStructure);
|
||||
|
||||
// Show extracted vertical lines
|
||||
showWaitDestroy("vertical", vertical);
|
||||
//! [vert]
|
||||
|
||||
//! [smooth]
|
||||
// Inverse vertical image
|
||||
Core.bitwise_not(vertical, vertical);
|
||||
showWaitDestroy("vertical_bit" , vertical);
|
||||
|
||||
// Extract edges and smooth image according to the logic
|
||||
// 1. extract edges
|
||||
// 2. dilate(edges)
|
||||
// 3. src.copyTo(smooth)
|
||||
// 4. blur smooth img
|
||||
// 5. smooth.copyTo(src, edges)
|
||||
|
||||
// Step 1
|
||||
Mat edges = new Mat();
|
||||
Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
|
||||
showWaitDestroy("edges", edges);
|
||||
|
||||
// Step 2
|
||||
Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
|
||||
Imgproc.dilate(edges, edges, kernel);
|
||||
showWaitDestroy("dilate", edges);
|
||||
|
||||
// Step 3
|
||||
Mat smooth = new Mat();
|
||||
vertical.copyTo(smooth);
|
||||
|
||||
// Step 4
|
||||
Imgproc.blur(smooth, smooth, new Size(2, 2));
|
||||
|
||||
// Step 5
|
||||
smooth.copyTo(vertical, edges);
|
||||
|
||||
// Show final result
|
||||
showWaitDestroy("smooth - final", vertical);
|
||||
//! [smooth]
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private void showWaitDestroy(String winname, Mat img) {
|
||||
HighGui.imshow(winname, img);
|
||||
HighGui.moveWindow(winname, 500, 0);
|
||||
HighGui.waitKey(0);
|
||||
HighGui.destroyWindow(winname);
|
||||
}
|
||||
}
|
||||
|
||||
public class Morphology_3 {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new Morphology_3Run().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* @file Filter2D_demo.java
|
||||
* @brief Sample code that shows how to implement your own linear filters by using filter2D function
|
||||
*/
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class Filter2D_DemoRun {
|
||||
|
||||
public void run(String[] args) {
|
||||
// Declare variables
|
||||
Mat src, dst = new Mat();
|
||||
|
||||
Mat kernel = new Mat();
|
||||
Point anchor;
|
||||
double delta;
|
||||
int ddepth;
|
||||
int kernel_size;
|
||||
String window_name = "filter2D Demo";
|
||||
|
||||
//! [load]
|
||||
String imageName = ((args.length > 0) ? args[0] : "../data/lena.jpg");
|
||||
|
||||
// Load an image
|
||||
src = Imgcodecs.imread(imageName, Imgcodecs.IMREAD_COLOR);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image!");
|
||||
System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
//! [init_arguments]
|
||||
// Initialize arguments for the filter
|
||||
anchor = new Point( -1, -1);
|
||||
delta = 0.0;
|
||||
ddepth = -1;
|
||||
//! [init_arguments]
|
||||
|
||||
// Loop - Will filter the image with different kernel sizes each 0.5 seconds
|
||||
int ind = 0;
|
||||
while( true )
|
||||
{
|
||||
//! [update_kernel]
|
||||
// Update kernel size for a normalized box filter
|
||||
kernel_size = 3 + 2*( ind%5 );
|
||||
Mat ones = Mat.ones( kernel_size, kernel_size, CvType.CV_32F );
|
||||
Core.multiply(ones, new Scalar(1/(double)(kernel_size*kernel_size)), kernel);
|
||||
//! [update_kernel]
|
||||
|
||||
//! [apply_filter]
|
||||
// Apply filter
|
||||
Imgproc.filter2D(src, dst, ddepth , kernel, anchor, delta, Core.BORDER_DEFAULT );
|
||||
//! [apply_filter]
|
||||
HighGui.imshow( window_name, dst );
|
||||
|
||||
int c = HighGui.waitKey(500);
|
||||
// Press 'ESC' to exit the program
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
|
||||
ind++;
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class Filter2D_Demo {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new Filter2D_DemoRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package sample;
|
||||
/**
|
||||
* @file HoughCircles.java
|
||||
* @brief This program demonstrates circle finding with the Hough transform
|
||||
*/
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class HoughCirclesRun {
|
||||
|
||||
public void run(String[] args) {
|
||||
|
||||
//! [load]
|
||||
String default_file = "../../../../data/smarties.png";
|
||||
String filename = ((args.length > 0) ? args[0] : default_file);
|
||||
|
||||
// Load an image
|
||||
Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image!");
|
||||
System.out.println("Program Arguments: [image_name -- default "
|
||||
+ default_file +"] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
//! [convert_to_gray]
|
||||
Mat gray = new Mat();
|
||||
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
|
||||
//! [convert_to_gray]
|
||||
|
||||
//![reduce_noise]
|
||||
Imgproc.medianBlur(gray, gray, 5);
|
||||
//![reduce_noise]
|
||||
|
||||
//! [houghcircles]
|
||||
Mat circles = new Mat();
|
||||
Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
|
||||
(double)gray.rows()/16, // change this value to detect circles with different distances to each other
|
||||
100.0, 30.0, 1, 30); // change the last two parameters
|
||||
// (min_radius & max_radius) to detect larger circles
|
||||
//! [houghcircles]
|
||||
|
||||
//! [draw]
|
||||
for (int x = 0; x < circles.cols(); x++) {
|
||||
double[] c = circles.get(0, x);
|
||||
Point center = new Point(Math.round(c[0]), Math.round(c[1]));
|
||||
// circle center
|
||||
Imgproc.circle(src, center, 1, new Scalar(0,100,100), 3, 8, 0 );
|
||||
// circle outline
|
||||
int radius = (int) Math.round(c[2]);
|
||||
Imgproc.circle(src, center, radius, new Scalar(255,0,255), 3, 8, 0 );
|
||||
}
|
||||
//! [draw]
|
||||
|
||||
//! [display]
|
||||
HighGui.imshow("detected circles", src);
|
||||
HighGui.waitKey();
|
||||
//! [display]
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class HoughCircles {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new HoughCirclesRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* @file HoughLines.java
|
||||
* @brief This program demonstrates line finding with the Hough transform
|
||||
*/
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class HoughLinesRun {
|
||||
|
||||
public void run(String[] args) {
|
||||
// Declare the output variables
|
||||
Mat dst = new Mat(), cdst = new Mat(), cdstP;
|
||||
|
||||
//! [load]
|
||||
String default_file = "../../../../data/sudoku.png";
|
||||
String filename = ((args.length > 0) ? args[0] : default_file);
|
||||
|
||||
// Load an image
|
||||
Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image!");
|
||||
System.out.println("Program Arguments: [image_name -- default "
|
||||
+ default_file +"] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
//! [edge_detection]
|
||||
// Edge detection
|
||||
Imgproc.Canny(src, dst, 50, 200, 3, false);
|
||||
//! [edge_detection]
|
||||
|
||||
// Copy edges to the images that will display the results in BGR
|
||||
Imgproc.cvtColor(dst, cdst, Imgproc.COLOR_GRAY2BGR);
|
||||
cdstP = cdst.clone();
|
||||
|
||||
//! [hough_lines]
|
||||
// Standard Hough Line Transform
|
||||
Mat lines = new Mat(); // will hold the results of the detection
|
||||
Imgproc.HoughLines(dst, lines, 1, Math.PI/180, 150); // runs the actual detection
|
||||
//! [hough_lines]
|
||||
//! [draw_lines]
|
||||
// Draw the lines
|
||||
for (int x = 0; x < lines.rows(); x++) {
|
||||
double rho = lines.get(x, 0)[0],
|
||||
theta = lines.get(x, 0)[1];
|
||||
|
||||
double a = Math.cos(theta), b = Math.sin(theta);
|
||||
double x0 = a*rho, y0 = b*rho;
|
||||
Point pt1 = new Point(Math.round(x0 + 1000*(-b)), Math.round(y0 + 1000*(a)));
|
||||
Point pt2 = new Point(Math.round(x0 - 1000*(-b)), Math.round(y0 - 1000*(a)));
|
||||
Imgproc.line(cdst, pt1, pt2, new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
|
||||
}
|
||||
//! [draw_lines]
|
||||
|
||||
//! [hough_lines_p]
|
||||
// Probabilistic Line Transform
|
||||
Mat linesP = new Mat(); // will hold the results of the detection
|
||||
Imgproc.HoughLinesP(dst, linesP, 1, Math.PI/180, 50, 50, 10); // runs the actual detection
|
||||
//! [hough_lines_p]
|
||||
//! [draw_lines_p]
|
||||
// Draw the lines
|
||||
for (int x = 0; x < linesP.rows(); x++) {
|
||||
double[] l = linesP.get(x, 0);
|
||||
Imgproc.line(cdstP, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
|
||||
}
|
||||
//! [draw_lines_p]
|
||||
|
||||
//! [imshow]
|
||||
// Show results
|
||||
HighGui.imshow("Source", src);
|
||||
HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
|
||||
HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);
|
||||
//! [imshow]
|
||||
|
||||
//! [exit]
|
||||
// Wait and Exit
|
||||
HighGui.waitKey();
|
||||
System.exit(0);
|
||||
//! [exit]
|
||||
}
|
||||
}
|
||||
|
||||
public class HoughLines {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new HoughLinesRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* @file LaplaceDemo.java
|
||||
* @brief Sample code showing how to detect edges using the Laplace operator
|
||||
*/
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class LaplaceDemoRun {
|
||||
|
||||
public void run(String[] args) {
|
||||
//! [variables]
|
||||
// Declare the variables we are going to use
|
||||
Mat src, src_gray = new Mat(), dst = new Mat();
|
||||
int kernel_size = 3;
|
||||
int scale = 1;
|
||||
int delta = 0;
|
||||
int ddepth = CvType.CV_16S;
|
||||
String window_name = "Laplace Demo";
|
||||
//! [variables]
|
||||
|
||||
//! [load]
|
||||
String imageName = ((args.length > 0) ? args[0] : "../data/lena.jpg");
|
||||
|
||||
src = Imgcodecs.imread(imageName, Imgcodecs.IMREAD_COLOR); // Load an image
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image");
|
||||
System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
//! [reduce_noise]
|
||||
// Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
Imgproc.GaussianBlur( src, src, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT );
|
||||
//! [reduce_noise]
|
||||
|
||||
//! [convert_to_gray]
|
||||
// Convert the image to grayscale
|
||||
Imgproc.cvtColor( src, src_gray, Imgproc.COLOR_RGB2GRAY );
|
||||
//! [convert_to_gray]
|
||||
|
||||
/// Apply Laplace function
|
||||
Mat abs_dst = new Mat();
|
||||
//! [laplacian]
|
||||
Imgproc.Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, Core.BORDER_DEFAULT );
|
||||
//! [laplacian]
|
||||
|
||||
//! [convert]
|
||||
// converting back to CV_8U
|
||||
Core.convertScaleAbs( dst, abs_dst );
|
||||
//! [convert]
|
||||
|
||||
//! [display]
|
||||
HighGui.imshow( window_name, abs_dst );
|
||||
HighGui.waitKey(0);
|
||||
//! [display]
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class LaplaceDemo {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new LaplaceDemoRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @file CopyMakeBorder.java
|
||||
* @brief Sample code that shows the functionality of copyMakeBorder
|
||||
*/
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
class CopyMakeBorderRun {
|
||||
|
||||
public void run(String[] args) {
|
||||
|
||||
//! [variables]
|
||||
// Declare the variables
|
||||
Mat src, dst = new Mat();
|
||||
int top, bottom, left, right;
|
||||
int borderType = Core.BORDER_CONSTANT;
|
||||
String window_name = "copyMakeBorder Demo";
|
||||
Random rng;
|
||||
//! [variables]
|
||||
|
||||
//! [load]
|
||||
String imageName = ((args.length > 0) ? args[0] : "../data/lena.jpg");
|
||||
|
||||
// Load an image
|
||||
src = Imgcodecs.imread(imageName, Imgcodecs.IMREAD_COLOR);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image!");
|
||||
System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
// Brief how-to for this program
|
||||
System.out.println("\n" +
|
||||
"\t copyMakeBorder Demo: \n" +
|
||||
"\t -------------------- \n" +
|
||||
" ** Press 'c' to set the border to a random constant value \n" +
|
||||
" ** Press 'r' to set the border to be replicated \n" +
|
||||
" ** Press 'ESC' to exit the program \n");
|
||||
|
||||
//![create_window]
|
||||
HighGui.namedWindow( window_name, HighGui.WINDOW_AUTOSIZE );
|
||||
//![create_window]
|
||||
|
||||
//! [init_arguments]
|
||||
// Initialize arguments for the filter
|
||||
top = (int) (0.05*src.rows()); bottom = top;
|
||||
left = (int) (0.05*src.cols()); right = left;
|
||||
//! [init_arguments]
|
||||
|
||||
while( true ) {
|
||||
//! [update_value]
|
||||
rng = new Random();
|
||||
Scalar value = new Scalar( rng.nextInt(256),
|
||||
rng.nextInt(256), rng.nextInt(256) );
|
||||
//! [update_value]
|
||||
|
||||
//! [copymakeborder]
|
||||
Core.copyMakeBorder( src, dst, top, bottom, left, right, borderType, value);
|
||||
//! [copymakeborder]
|
||||
//! [display]
|
||||
HighGui.imshow( window_name, dst );
|
||||
//! [display]
|
||||
|
||||
//![check_keypress]
|
||||
char c = (char) HighGui.waitKey(500);
|
||||
c = Character.toLowerCase(c);
|
||||
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
else if( c == 'c' )
|
||||
{ borderType = Core.BORDER_CONSTANT;}
|
||||
else if( c == 'r' )
|
||||
{ borderType = Core.BORDER_REPLICATE;}
|
||||
//![check_keypress]
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class CopyMakeBorder {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new CopyMakeBorderRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @file SobelDemo.java
|
||||
* @brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
|
||||
*/
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class SobelDemoRun {
|
||||
|
||||
public void run(String[] args) {
|
||||
|
||||
//! [declare_variables]
|
||||
// First we declare the variables we are going to use
|
||||
Mat src, src_gray = new Mat();
|
||||
Mat grad = new Mat();
|
||||
String window_name = "Sobel Demo - Simple Edge Detector";
|
||||
int scale = 1;
|
||||
int delta = 0;
|
||||
int ddepth = CvType.CV_16S;
|
||||
//! [declare_variables]
|
||||
|
||||
//! [load]
|
||||
// As usual we load our source image (src)
|
||||
// Check number of arguments
|
||||
if (args.length == 0){
|
||||
System.out.println("Not enough parameters!");
|
||||
System.out.println("Program Arguments: [image_path]");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
// Load the image
|
||||
src = Imgcodecs.imread(args[0]);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image: " + args[0]);
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
//! [reduce_noise]
|
||||
// Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
Imgproc.GaussianBlur( src, src, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT );
|
||||
//! [reduce_noise]
|
||||
|
||||
//! [convert_to_gray]
|
||||
// Convert the image to grayscale
|
||||
Imgproc.cvtColor( src, src_gray, Imgproc.COLOR_RGB2GRAY );
|
||||
//! [convert_to_gray]
|
||||
|
||||
//! [sobel]
|
||||
/// Generate grad_x and grad_y
|
||||
Mat grad_x = new Mat(), grad_y = new Mat();
|
||||
Mat abs_grad_x = new Mat(), abs_grad_y = new Mat();
|
||||
|
||||
/// Gradient X
|
||||
//Imgproc.Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, Core.BORDER_DEFAULT );
|
||||
Imgproc.Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, Core.BORDER_DEFAULT );
|
||||
|
||||
/// Gradient Y
|
||||
//Imgproc.Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, Core.BORDER_DEFAULT );
|
||||
Imgproc.Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, Core.BORDER_DEFAULT );
|
||||
//! [sobel]
|
||||
|
||||
//![convert]
|
||||
// converting back to CV_8U
|
||||
Core.convertScaleAbs( grad_x, abs_grad_x );
|
||||
Core.convertScaleAbs( grad_y, abs_grad_y );
|
||||
//![convert]
|
||||
|
||||
//! [add_weighted]
|
||||
/// Total Gradient (approximate)
|
||||
Core.addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
|
||||
//! [add_weighted]
|
||||
|
||||
//! [display]
|
||||
HighGui.imshow( window_name, grad );
|
||||
HighGui.waitKey(0);
|
||||
//! [display]
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class SobelDemo {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new SobelDemoRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
@file filter2D.py
|
||||
@brief Sample code that shows how to implement your own linear filters by using filter2D function
|
||||
"""
|
||||
import sys
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
|
||||
def main(argv):
|
||||
window_name = 'filter2D Demo'
|
||||
|
||||
## [load]
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(imageName, cv2.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: filter2D.py [image_name -- default ../data/lena.jpg] \n')
|
||||
return -1
|
||||
## [load]
|
||||
## [init_arguments]
|
||||
# Initialize ddepth argument for the filter
|
||||
ddepth = -1
|
||||
## [init_arguments]
|
||||
# Loop - Will filter the image with different kernel sizes each 0.5 seconds
|
||||
ind = 0
|
||||
while True:
|
||||
## [update_kernel]
|
||||
# Update kernel size for a normalized box filter
|
||||
kernel_size = 3 + 2 * (ind % 5)
|
||||
kernel = np.ones((kernel_size, kernel_size), dtype=np.float32)
|
||||
kernel /= (kernel_size * kernel_size)
|
||||
## [update_kernel]
|
||||
## [apply_filter]
|
||||
# Apply filter
|
||||
dst = cv2.filter2D(src, ddepth, kernel)
|
||||
## [apply_filter]
|
||||
cv2.imshow(window_name, dst)
|
||||
|
||||
c = cv2.waitKey(500)
|
||||
if c == 27:
|
||||
break
|
||||
|
||||
ind += 1
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,59 @@
|
||||
import sys
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [load]
|
||||
default_file = "../../../../data/smarties.png"
|
||||
filename = argv[0] if len(argv) > 0 else default_file
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(filename, cv2.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: hough_circle.py [image_name -- default ' + default_file + '] \n')
|
||||
return -1
|
||||
## [load]
|
||||
|
||||
## [convert_to_gray]
|
||||
# Convert it to gray
|
||||
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
## [convert_to_gray]
|
||||
|
||||
## [reduce_noise]
|
||||
# Reduce the noise to avoid false circle detection
|
||||
gray = cv2.medianBlur(gray, 5)
|
||||
## [reduce_noise]
|
||||
|
||||
## [houghcircles]
|
||||
rows = gray.shape[0]
|
||||
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8,
|
||||
param1=100, param2=30,
|
||||
minRadius=1, maxRadius=30)
|
||||
## [houghcircles]
|
||||
|
||||
## [draw]
|
||||
if circles is not None:
|
||||
circles = np.uint16(np.around(circles))
|
||||
for i in circles[0, :]:
|
||||
center = (i[0], i[1])
|
||||
# circle center
|
||||
cv2.circle(src, center, 1, (0, 100, 100), 3)
|
||||
# circle outline
|
||||
radius = i[2]
|
||||
cv2.circle(src, center, radius, (255, 0, 255), 3)
|
||||
## [draw]
|
||||
|
||||
## [display]
|
||||
cv2.imshow("detected circles", src)
|
||||
cv2.waitKey(0)
|
||||
## [display]
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,79 @@
|
||||
"""
|
||||
@file hough_lines.py
|
||||
@brief This program demonstrates line finding with the Hough transform
|
||||
"""
|
||||
import sys
|
||||
import math
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [load]
|
||||
default_file = "../../../../data/sudoku.png"
|
||||
filename = argv[0] if len(argv) > 0 else default_file
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n')
|
||||
return -1
|
||||
## [load]
|
||||
|
||||
## [edge_detection]
|
||||
# Edge detection
|
||||
dst = cv2.Canny(src, 50, 200, None, 3)
|
||||
## [edge_detection]
|
||||
|
||||
# Copy edges to the images that will display the results in BGR
|
||||
cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
|
||||
cdstP = np.copy(cdst)
|
||||
|
||||
## [hough_lines]
|
||||
# Standard Hough Line Transform
|
||||
lines = cv2.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)
|
||||
## [hough_lines]
|
||||
## [draw_lines]
|
||||
# Draw the lines
|
||||
if lines is not None:
|
||||
for i in range(0, len(lines)):
|
||||
rho = lines[i][0][0]
|
||||
theta = lines[i][0][1]
|
||||
a = math.cos(theta)
|
||||
b = math.sin(theta)
|
||||
x0 = a * rho
|
||||
y0 = b * rho
|
||||
pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
|
||||
pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
|
||||
|
||||
cv2.line(cdst, pt1, pt2, (0,0,255), 3, cv2.LINE_AA)
|
||||
## [draw_lines]
|
||||
|
||||
## [hough_lines_p]
|
||||
# Probabilistic Line Transform
|
||||
linesP = cv2.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10)
|
||||
## [hough_lines_p]
|
||||
## [draw_lines_p]
|
||||
# Draw the lines
|
||||
if linesP is not None:
|
||||
for i in range(0, len(linesP)):
|
||||
l = linesP[i][0]
|
||||
cv2.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv2.LINE_AA)
|
||||
## [draw_lines_p]
|
||||
## [imshow]
|
||||
# Show results
|
||||
cv2.imshow("Source", src)
|
||||
cv2.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
|
||||
cv2.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP)
|
||||
## [imshow]
|
||||
## [exit]
|
||||
# Wait and Exit
|
||||
cv2.waitKey()
|
||||
return 0
|
||||
## [exit]
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,59 @@
|
||||
"""
|
||||
@file laplace_demo.py
|
||||
@brief Sample code showing how to detect edges using the Laplace operator
|
||||
"""
|
||||
import sys
|
||||
import cv2
|
||||
|
||||
def main(argv):
|
||||
# [variables]
|
||||
# Declare the variables we are going to use
|
||||
ddepth = cv2.CV_16S
|
||||
kernel_size = 3
|
||||
window_name = "Laplace Demo"
|
||||
# [variables]
|
||||
|
||||
# [load]
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
src = cv2.imread(imageName, cv2.IMREAD_COLOR) # Load an image
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image')
|
||||
print ('Program Arguments: [image_name -- default ../data/lena.jpg]')
|
||||
return -1
|
||||
# [load]
|
||||
|
||||
# [reduce_noise]
|
||||
# Remove noise by blurring with a Gaussian filter
|
||||
src = cv2.GaussianBlur(src, (3, 3), 0)
|
||||
# [reduce_noise]
|
||||
|
||||
# [convert_to_gray]
|
||||
# Convert the image to grayscale
|
||||
src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
# [convert_to_gray]
|
||||
|
||||
# Create Window
|
||||
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
|
||||
|
||||
# [laplacian]
|
||||
# Apply Laplace function
|
||||
dst = cv2.Laplacian(src_gray, ddepth, kernel_size)
|
||||
# [laplacian]
|
||||
|
||||
# [convert]
|
||||
# converting back to uint8
|
||||
abs_dst = cv2.convertScaleAbs(dst)
|
||||
# [convert]
|
||||
|
||||
# [display]
|
||||
cv2.imshow(window_name, abs_dst)
|
||||
cv2.waitKey(0)
|
||||
# [display]
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
@file copy_make_border.py
|
||||
@brief Sample code that shows the functionality of copyMakeBorder
|
||||
"""
|
||||
import sys
|
||||
from random import randint
|
||||
import cv2
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [variables]
|
||||
# First we declare the variables we are going to use
|
||||
borderType = cv2.BORDER_CONSTANT
|
||||
window_name = "copyMakeBorder Demo"
|
||||
## [variables]
|
||||
## [load]
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(imageName, cv2.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: copy_make_border.py [image_name -- default ../data/lena.jpg] \n')
|
||||
return -1
|
||||
## [load]
|
||||
# Brief how-to for this program
|
||||
print ('\n'
|
||||
'\t copyMakeBorder Demo: \n'
|
||||
' -------------------- \n'
|
||||
' ** Press \'c\' to set the border to a random constant value \n'
|
||||
' ** Press \'r\' to set the border to be replicated \n'
|
||||
' ** Press \'ESC\' to exit the program ')
|
||||
## [create_window]
|
||||
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
|
||||
## [create_window]
|
||||
## [init_arguments]
|
||||
# Initialize arguments for the filter
|
||||
top = int(0.05 * src.shape[0]) # shape[0] = rows
|
||||
bottom = top
|
||||
left = int(0.05 * src.shape[1]) # shape[1] = cols
|
||||
right = left
|
||||
## [init_arguments]
|
||||
while 1:
|
||||
## [update_value]
|
||||
value = [randint(0, 255), randint(0, 255), randint(0, 255)]
|
||||
## [update_value]
|
||||
## [copymakeborder]
|
||||
dst = cv2.copyMakeBorder(src, top, bottom, left, right, borderType, None, value)
|
||||
## [copymakeborder]
|
||||
## [display]
|
||||
cv2.imshow(window_name, dst)
|
||||
## [display]
|
||||
## [check_keypress]
|
||||
c = cv2.waitKey(500)
|
||||
|
||||
if c == 27:
|
||||
break
|
||||
elif c == 99: # 99 = ord('c')
|
||||
borderType = cv2.BORDER_CONSTANT
|
||||
elif c == 114: # 114 = ord('r')
|
||||
borderType = cv2.BORDER_REPLICATE
|
||||
## [check_keypress]
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,74 @@
|
||||
"""
|
||||
@file sobel_demo.py
|
||||
@brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
|
||||
"""
|
||||
import sys
|
||||
import cv2
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [variables]
|
||||
# First we declare the variables we are going to use
|
||||
window_name = ('Sobel Demo - Simple Edge Detector')
|
||||
scale = 1
|
||||
delta = 0
|
||||
ddepth = cv2.CV_16S
|
||||
## [variables]
|
||||
|
||||
## [load]
|
||||
# As usual we load our source image (src)
|
||||
# Check number of arguments
|
||||
if len(argv) < 1:
|
||||
print ('Not enough parameters')
|
||||
print ('Usage:\nmorph_lines_detection.py < path_to_image >')
|
||||
return -1
|
||||
|
||||
# Load the image
|
||||
src = cv2.imread(argv[0], cv2.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image: ' + argv[0])
|
||||
return -1
|
||||
## [load]
|
||||
|
||||
## [reduce_noise]
|
||||
# Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
src = cv2.GaussianBlur(src, (3, 3), 0)
|
||||
## [reduce_noise]
|
||||
|
||||
## [convert_to_gray]
|
||||
# Convert the image to grayscale
|
||||
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
## [convert_to_gray]
|
||||
|
||||
## [sobel]
|
||||
# Gradient-X
|
||||
# grad_x = cv2.Scharr(gray,ddepth,1,0)
|
||||
grad_x = cv2.Sobel(gray, ddepth, 1, 0, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
|
||||
|
||||
# Gradient-Y
|
||||
# grad_y = cv2.Scharr(gray,ddepth,0,1)
|
||||
grad_y = cv2.Sobel(gray, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
|
||||
## [sobel]
|
||||
|
||||
## [convert]
|
||||
# converting back to uint8
|
||||
abs_grad_x = cv2.convertScaleAbs(grad_x)
|
||||
abs_grad_y = cv2.convertScaleAbs(grad_y)
|
||||
## [convert]
|
||||
|
||||
## [blend]
|
||||
## Total Gradient (approximate)
|
||||
grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
|
||||
## [blend]
|
||||
|
||||
## [display]
|
||||
cv2.imshow(window_name, grad)
|
||||
cv2.waitKey(0)
|
||||
## [display]
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,38 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
input_image = np.array((
|
||||
[0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 255, 255, 255, 0, 0, 0, 255],
|
||||
[0, 255, 255, 255, 0, 0, 0, 0],
|
||||
[0, 255, 255, 255, 0, 255, 0, 0],
|
||||
[0, 0, 255, 0, 0, 0, 0, 0],
|
||||
[0, 0, 255, 0, 0, 255, 255, 0],
|
||||
[0,255, 0, 255, 0, 0, 255, 0],
|
||||
[0, 255, 255, 255, 0, 0, 0, 0]), dtype="uint8")
|
||||
|
||||
kernel = np.array((
|
||||
[0, 1, 0],
|
||||
[1, -1, 1],
|
||||
[0, 1, 0]), dtype="int")
|
||||
|
||||
output_image = cv2.morphologyEx(input_image, cv2.MORPH_HITMISS, kernel)
|
||||
|
||||
rate = 50
|
||||
kernel = (kernel + 1) * 127
|
||||
kernel = np.uint8(kernel)
|
||||
|
||||
kernel = cv2.resize(kernel, None, fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
|
||||
cv2.imshow("kernel", kernel)
|
||||
cv2.moveWindow("kernel", 0, 0)
|
||||
|
||||
input_image = cv2.resize(input_image, None, fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
|
||||
cv2.imshow("Original", input_image)
|
||||
cv2.moveWindow("Original", 0, 200)
|
||||
|
||||
output_image = cv2.resize(output_image, None , fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
|
||||
cv2.imshow("Hit or Miss", output_image)
|
||||
cv2.moveWindow("Hit or Miss", 500, 200)
|
||||
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
||||
@@ -0,0 +1,51 @@
|
||||
import sys
|
||||
import cv2
|
||||
|
||||
|
||||
def main(argv):
|
||||
print("""
|
||||
Zoom In-Out demo
|
||||
------------------
|
||||
* [i] -> Zoom [i]n
|
||||
* [o] -> Zoom [o]ut
|
||||
* [ESC] -> Close program
|
||||
""")
|
||||
## [load]
|
||||
filename = argv[0] if len(argv) > 0 else "../data/chicky_512.png"
|
||||
|
||||
# Load the image
|
||||
src = cv2.imread(filename)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: pyramids.py [image_name -- default ../data/chicky_512.png] \n')
|
||||
return -1
|
||||
## [load]
|
||||
## [loop]
|
||||
while 1:
|
||||
rows, cols, _channels = map(int, src.shape)
|
||||
## [show_image]
|
||||
cv2.imshow('Pyramids Demo', src)
|
||||
## [show_image]
|
||||
k = cv2.waitKey(0)
|
||||
|
||||
if k == 27:
|
||||
break
|
||||
## [pyrup]
|
||||
elif chr(k) == 'i':
|
||||
src = cv2.pyrUp(src, dstsize=(2 * cols, 2 * rows))
|
||||
print ('** Zoom In: Image x 2')
|
||||
## [pyrup]
|
||||
## [pyrdown]
|
||||
elif chr(k) == 'o':
|
||||
src = cv2.pyrDown(src, dstsize=(cols // 2, rows // 2))
|
||||
print ('** Zoom Out: Image / 2')
|
||||
## [pyrdown]
|
||||
## [loop]
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,107 @@
|
||||
import sys
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
# Global Variables
|
||||
|
||||
DELAY_CAPTION = 1500
|
||||
DELAY_BLUR = 100
|
||||
MAX_KERNEL_LENGTH = 31
|
||||
|
||||
src = None
|
||||
dst = None
|
||||
window_name = 'Smoothing Demo'
|
||||
|
||||
|
||||
def main(argv):
|
||||
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
|
||||
|
||||
# Load the source image
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
global src
|
||||
src = cv2.imread(imageName, 1)
|
||||
if src is None:
|
||||
print ('Error opening image')
|
||||
print ('Usage: smoothing.py [image_name -- default ../data/lena.jpg] \n')
|
||||
return -1
|
||||
|
||||
if display_caption('Original Image') != 0:
|
||||
return 0
|
||||
|
||||
global dst
|
||||
dst = np.copy(src)
|
||||
if display_dst(DELAY_CAPTION) != 0:
|
||||
return 0
|
||||
|
||||
# Applying Homogeneous blur
|
||||
if display_caption('Homogeneous Blur') != 0:
|
||||
return 0
|
||||
|
||||
## [blur]
|
||||
for i in range(1, MAX_KERNEL_LENGTH, 2):
|
||||
dst = cv2.blur(src, (i, i))
|
||||
if display_dst(DELAY_BLUR) != 0:
|
||||
return 0
|
||||
## [blur]
|
||||
|
||||
# Applying Gaussian blur
|
||||
if display_caption('Gaussian Blur') != 0:
|
||||
return 0
|
||||
|
||||
## [gaussianblur]
|
||||
for i in range(1, MAX_KERNEL_LENGTH, 2):
|
||||
dst = cv2.GaussianBlur(src, (i, i), 0)
|
||||
if display_dst(DELAY_BLUR) != 0:
|
||||
return 0
|
||||
## [gaussianblur]
|
||||
|
||||
# Applying Median blur
|
||||
if display_caption('Median Blur') != 0:
|
||||
return 0
|
||||
|
||||
## [medianblur]
|
||||
for i in range(1, MAX_KERNEL_LENGTH, 2):
|
||||
dst = cv2.medianBlur(src, i)
|
||||
if display_dst(DELAY_BLUR) != 0:
|
||||
return 0
|
||||
## [medianblur]
|
||||
|
||||
# Applying Bilateral Filter
|
||||
if display_caption('Bilateral Blur') != 0:
|
||||
return 0
|
||||
|
||||
## [bilateralfilter]
|
||||
# Remember, bilateral is a bit slow, so as value go higher, it takes long time
|
||||
for i in range(1, MAX_KERNEL_LENGTH, 2):
|
||||
dst = cv2.bilateralFilter(src, i, i * 2, i / 2)
|
||||
if display_dst(DELAY_BLUR) != 0:
|
||||
return 0
|
||||
## [bilateralfilter]
|
||||
|
||||
# Done
|
||||
display_caption('Done!')
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def display_caption(caption):
|
||||
global dst
|
||||
dst = np.zeros(src.shape, src.dtype)
|
||||
rows, cols, ch = src.shape
|
||||
cv2.putText(dst, caption,
|
||||
(int(cols / 4), int(rows / 2)),
|
||||
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255))
|
||||
|
||||
return display_dst(DELAY_CAPTION)
|
||||
|
||||
|
||||
def display_dst(delay):
|
||||
cv2.imshow(window_name, dst)
|
||||
c = cv2.waitKey(delay)
|
||||
if c >= 0 : return -1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,136 @@
|
||||
"""
|
||||
@file morph_lines_detection.py
|
||||
@brief Use morphology transformations for extracting horizontal and vertical lines sample code
|
||||
"""
|
||||
import numpy as np
|
||||
import sys
|
||||
import cv2
|
||||
|
||||
|
||||
def show_wait_destroy(winname, img):
|
||||
cv2.imshow(winname, img)
|
||||
cv2.moveWindow(winname, 500, 0)
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyWindow(winname)
|
||||
|
||||
|
||||
def main(argv):
|
||||
# [load_image]
|
||||
# Check number of arguments
|
||||
if len(argv) < 1:
|
||||
print ('Not enough parameters')
|
||||
print ('Usage:\nmorph_lines_detection.py < path_to_image >')
|
||||
return -1
|
||||
|
||||
# Load the image
|
||||
src = cv2.imread(argv[0], cv2.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image: ' + argv[0])
|
||||
return -1
|
||||
|
||||
# Show source image
|
||||
cv2.imshow("src", src)
|
||||
# [load_image]
|
||||
|
||||
# [gray]
|
||||
# Transform source image to gray if it is not already
|
||||
if len(src.shape) != 2:
|
||||
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
else:
|
||||
gray = src
|
||||
|
||||
# Show gray image
|
||||
show_wait_destroy("gray", gray)
|
||||
# [gray]
|
||||
|
||||
# [bin]
|
||||
# Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
|
||||
gray = cv2.bitwise_not(gray)
|
||||
bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
|
||||
cv2.THRESH_BINARY, 15, -2)
|
||||
# Show binary image
|
||||
show_wait_destroy("binary", bw)
|
||||
# [bin]
|
||||
|
||||
# [init]
|
||||
# Create the images that will use to extract the horizontal and vertical lines
|
||||
horizontal = np.copy(bw)
|
||||
vertical = np.copy(bw)
|
||||
# [init]
|
||||
|
||||
# [horiz]
|
||||
# Specify size on horizontal axis
|
||||
cols = horizontal.shape[1]
|
||||
horizontal_size = cols / 30
|
||||
|
||||
# Create structure element for extracting horizontal lines through morphology operations
|
||||
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontal_size, 1))
|
||||
|
||||
# Apply morphology operations
|
||||
horizontal = cv2.erode(horizontal, horizontalStructure)
|
||||
horizontal = cv2.dilate(horizontal, horizontalStructure)
|
||||
|
||||
# Show extracted horizontal lines
|
||||
show_wait_destroy("horizontal", horizontal)
|
||||
# [horiz]
|
||||
|
||||
# [vert]
|
||||
# Specify size on vertical axis
|
||||
rows = vertical.shape[0]
|
||||
verticalsize = rows / 30
|
||||
|
||||
# Create structure element for extracting vertical lines through morphology operations
|
||||
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalsize))
|
||||
|
||||
# Apply morphology operations
|
||||
vertical = cv2.erode(vertical, verticalStructure)
|
||||
vertical = cv2.dilate(vertical, verticalStructure)
|
||||
|
||||
# Show extracted vertical lines
|
||||
show_wait_destroy("vertical", vertical)
|
||||
# [vert]
|
||||
|
||||
# [smooth]
|
||||
# Inverse vertical image
|
||||
vertical = cv2.bitwise_not(vertical)
|
||||
show_wait_destroy("vertical_bit", vertical)
|
||||
|
||||
'''
|
||||
Extract edges and smooth image according to the logic
|
||||
1. extract edges
|
||||
2. dilate(edges)
|
||||
3. src.copyTo(smooth)
|
||||
4. blur smooth img
|
||||
5. smooth.copyTo(src, edges)
|
||||
'''
|
||||
|
||||
# Step 1
|
||||
edges = cv2.adaptiveThreshold(vertical, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
|
||||
cv2.THRESH_BINARY, 3, -2)
|
||||
show_wait_destroy("edges", edges)
|
||||
|
||||
# Step 2
|
||||
kernel = np.ones((2, 2), np.uint8)
|
||||
edges = cv2.dilate(edges, kernel)
|
||||
show_wait_destroy("dilate", edges)
|
||||
|
||||
# Step 3
|
||||
smooth = np.copy(vertical)
|
||||
|
||||
# Step 4
|
||||
smooth = cv2.blur(smooth, (2, 2))
|
||||
|
||||
# Step 5
|
||||
(rows, cols) = np.where(edges != 0)
|
||||
vertical[rows, cols] = smooth[rows, cols]
|
||||
|
||||
# Show final result
|
||||
show_wait_destroy("smooth - final", vertical)
|
||||
# [smooth]
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
Reference in New Issue
Block a user