Add Java and Python code for the following imgproc tutorials: Affine Transformations, Histogram Equalization, Histogram Calculation, Histogram Comparison, Back Projection.

This commit is contained in:
catree
2018-05-23 19:44:27 +02:00
parent 3654fb10d7
commit 4c1c3147d9
24 changed files with 2014 additions and 608 deletions
@@ -17,37 +17,35 @@ using namespace std;
*/
int main( int argc, char** argv )
{
Mat src, dst;
//! [Load image]
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
Mat src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
if( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
//! [Load image]
const char* source_window = "Source image";
const char* equalized_window = "Equalized Image";
//! [Convert to grayscale]
cvtColor( src, src, COLOR_BGR2GRAY );
//! [Convert to grayscale]
/// Load image
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
if( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
//! [Apply Histogram Equalization]
Mat dst;
equalizeHist( src, dst );
//! [Apply Histogram Equalization]
/// Convert to grayscale
cvtColor( src, src, COLOR_BGR2GRAY );
//! [Display results]
imshow( "Source image", src );
imshow( "Equalized Image", dst );
//! [Display results]
/// Apply Histogram Equalization
equalizeHist( src, dst );
//! [Wait until user exits the program]
waitKey();
//! [Wait until user exits the program]
/// Display results
namedWindow( source_window, WINDOW_AUTOSIZE );
namedWindow( equalized_window, WINDOW_AUTOSIZE );
imshow( source_window, src );
imshow( equalized_window, dst );
/// Wait until user exits the program
waitKey(0);
return 0;
return 0;
}
@@ -14,79 +14,93 @@ using namespace cv;
using namespace std;
/// Global Variables
Mat src; Mat hsv; Mat hue;
Mat hue;
int bins = 25;
/// Function Headers
void Hist_and_Backproj(int, void* );
/**
* @function main
*/
int main( int, char** argv )
int main( int argc, char* argv[] )
{
/// Read the image
src = imread( argv[1], IMREAD_COLOR );
if( src.empty() )
{ cout<<"Usage: ./calcBackProject_Demo1 <path_to_image>"<<endl;
return -1;
//! [Read the image]
CommandLineParser parser( argc, argv, "{@input | | input image}" );
Mat src = imread( parser.get<String>( "@input" ) );
if( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
//! [Read the image]
/// Transform it to HSV
cvtColor( src, hsv, COLOR_BGR2HSV );
//! [Transform it to HSV]
Mat hsv;
cvtColor( src, hsv, COLOR_BGR2HSV );
//! [Transform it to HSV]
/// Use only the Hue value
hue.create( hsv.size(), hsv.depth() );
int ch[] = { 0, 0 };
mixChannels( &hsv, 1, &hue, 1, ch, 1 );
//! [Use only the Hue value]
hue.create(hsv.size(), hsv.depth());
int ch[] = { 0, 0 };
mixChannels( &hsv, 1, &hue, 1, ch, 1 );
//! [Use only the Hue value]
/// Create Trackbar to enter the number of bins
const char* window_image = "Source image";
namedWindow( window_image, WINDOW_AUTOSIZE );
createTrackbar("* Hue bins: ", window_image, &bins, 180, Hist_and_Backproj );
Hist_and_Backproj(0, 0);
//! [Create Trackbar to enter the number of bins]
const char* window_image = "Source image";
namedWindow( window_image );
createTrackbar("* Hue bins: ", window_image, &bins, 180, Hist_and_Backproj );
Hist_and_Backproj(0, 0);
//! [Create Trackbar to enter the number of bins]
/// Show the image
imshow( window_image, src );
//! [Show the image]
imshow( window_image, src );
// Wait until user exits the program
waitKey();
//! [Show the image]
/// Wait until user exits the program
waitKey(0);
return 0;
return 0;
}
/**
* @function Hist_and_Backproj
* @brief Callback to Trackbar
*/
void Hist_and_Backproj(int, void* )
{
MatND hist;
int histSize = MAX( bins, 2 );
float hue_range[] = { 0, 180 };
const float* ranges = { hue_range };
//! [initialize]
int histSize = MAX( bins, 2 );
float hue_range[] = { 0, 180 };
const float* ranges = { hue_range };
//! [initialize]
/// Get the Histogram and normalize it
calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, &ranges, true, false );
normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );
//! [Get the Histogram and normalize it]
Mat hist;
calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, &ranges, true, false );
normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );
//! [Get the Histogram and normalize it]
/// Get Backprojection
MatND backproj;
calcBackProject( &hue, 1, 0, hist, backproj, &ranges, 1, true );
//! [Get Backprojection]
Mat backproj;
calcBackProject( &hue, 1, 0, hist, backproj, &ranges, 1, true );
//! [Get Backprojection]
/// Draw the backproj
imshow( "BackProj", backproj );
//! [Draw the backproj]
imshow( "BackProj", backproj );
//! [Draw the backproj]
/// Draw the histogram
int w = 400; int h = 400;
int bin_w = cvRound( (double) w / histSize );
Mat histImg = Mat::zeros( w, h, CV_8UC3 );
//! [Draw the histogram]
int w = 400, h = 400;
int bin_w = cvRound( (double) w / histSize );
Mat histImg = Mat::zeros( h, w, CV_8UC3 );
for( int i = 0; i < bins; i ++ )
{ rectangle( histImg, Point( i*bin_w, h ), Point( (i+1)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ), Scalar( 0, 0, 255 ), -1 ); }
imshow( "Histogram", histImg );
for (int i = 0; i < bins; i++)
{
rectangle( histImg, Point( i*bin_w, h ), Point( (i+1)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ),
Scalar( 0, 0, 255 ), FILLED );
}
imshow( "Histogram", histImg );
//! [Draw the histogram]
}
@@ -14,10 +14,9 @@ using namespace cv;
using namespace std;
/// Global Variables
Mat src; Mat hsv;
Mat mask;
Mat src, hsv, mask;
int lo = 20; int up = 20;
int low = 20, up = 20;
const char* window_image = "Source image";
/// Function Headers
@@ -29,23 +28,24 @@ void pickPoint (int event, int x, int y, int, void* );
*/
int main( int, char** argv )
{
/// Read the image
src = imread( argv[1], IMREAD_COLOR );
/// Transform it to HSV
cvtColor( src, hsv, COLOR_BGR2HSV );
/// Read the image
src = imread( argv[1] );
/// Show the image
namedWindow( window_image, WINDOW_AUTOSIZE );
imshow( window_image, src );
/// Transform it to HSV
cvtColor( src, hsv, COLOR_BGR2HSV );
/// Set Trackbars for floodfill thresholds
createTrackbar( "Low thresh", window_image, &lo, 255, 0 );
createTrackbar( "High thresh", window_image, &up, 255, 0 );
/// Set a Mouse Callback
setMouseCallback( window_image, pickPoint, 0 );
/// Show the image
namedWindow( window_image );
imshow( window_image, src );
waitKey(0);
return 0;
/// Set Trackbars for floodfill thresholds
createTrackbar( "Low thresh", window_image, &low, 255, 0 );
createTrackbar( "High thresh", window_image, &up, 255, 0 );
/// Set a Mouse Callback
setMouseCallback( window_image, pickPoint, 0 );
waitKey();
return 0;
}
/**
@@ -53,25 +53,27 @@ int main( int, char** argv )
*/
void pickPoint (int event, int x, int y, int, void* )
{
if( event != EVENT_LBUTTONDOWN )
{ return; }
if( event != EVENT_LBUTTONDOWN )
{
return;
}
// Fill and get the mask
Point seed = Point( x, y );
// Fill and get the mask
Point seed = Point( x, y );
int newMaskVal = 255;
Scalar newVal = Scalar( 120, 120, 120 );
int newMaskVal = 255;
Scalar newVal = Scalar( 120, 120, 120 );
int connectivity = 8;
int flags = connectivity + (newMaskVal << 8 ) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
int connectivity = 8;
int flags = connectivity + (newMaskVal << 8 ) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
Mat mask2 = Mat::zeros( src.rows + 2, src.cols + 2, CV_8UC1 );
floodFill( src, mask2, seed, newVal, 0, Scalar( lo, lo, lo ), Scalar( up, up, up), flags );
mask = mask2( Range( 1, mask2.rows - 1 ), Range( 1, mask2.cols - 1 ) );
Mat mask2 = Mat::zeros( src.rows + 2, src.cols + 2, CV_8U );
floodFill( src, mask2, seed, newVal, 0, Scalar( low, low, low ), Scalar( up, up, up), flags );
mask = mask2( Range( 1, mask2.rows - 1 ), Range( 1, mask2.cols - 1 ) );
imshow( "Mask", mask );
imshow( "Mask", mask );
Hist_and_Backproj( );
Hist_and_Backproj( );
}
/**
@@ -79,26 +81,25 @@ void pickPoint (int event, int x, int y, int, void* )
*/
void Hist_and_Backproj( )
{
MatND hist;
int h_bins = 30; int s_bins = 32;
int histSize[] = { h_bins, s_bins };
Mat hist;
int h_bins = 30; int s_bins = 32;
int histSize[] = { h_bins, s_bins };
float h_range[] = { 0, 179 };
float s_range[] = { 0, 255 };
const float* ranges[] = { h_range, s_range };
float h_range[] = { 0, 180 };
float s_range[] = { 0, 256 };
const float* ranges[] = { h_range, s_range };
int channels[] = { 0, 1 };
int channels[] = { 0, 1 };
/// Get the Histogram and normalize it
calcHist( &hsv, 1, channels, mask, hist, 2, histSize, ranges, true, false );
/// Get the Histogram and normalize it
calcHist( &hsv, 1, channels, mask, hist, 2, histSize, ranges, true, false );
normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );
normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );
/// Get Backprojection
MatND backproj;
calcBackProject( &hsv, 1, channels, hist, backproj, ranges, 1, true );
/// Draw the backproj
imshow( "BackProj", backproj );
/// Get Backprojection
Mat backproj;
calcBackProject( &hsv, 1, channels, hist, backproj, ranges, 1, true );
/// Draw the backproj
imshow( "BackProj", backproj );
}
@@ -17,72 +17,73 @@ using namespace cv;
*/
int main(int argc, char** argv)
{
Mat src, dst;
//! [Load image]
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
Mat src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
if( src.empty() )
{
return -1;
}
//! [Load image]
/// Load image
String imageName( "../data/lena.jpg" ); // by default
//! [Separate the image in 3 places ( B, G and R )]
vector<Mat> bgr_planes;
split( src, bgr_planes );
//! [Separate the image in 3 places ( B, G and R )]
if (argc > 1)
{
imageName = argv[1];
}
//! [Establish the number of bins]
int histSize = 256;
//! [Establish the number of bins]
src = imread( imageName, IMREAD_COLOR );
//! [Set the ranges ( for B,G,R) )]
float range[] = { 0, 256 }; //the upper boundary is exclusive
const float* histRange = { range };
//! [Set the ranges ( for B,G,R) )]
if( src.empty() )
{ return -1; }
//! [Set histogram param]
bool uniform = true, accumulate = false;
//! [Set histogram param]
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split( src, bgr_planes );
//! [Compute the histograms]
Mat b_hist, g_hist, r_hist;
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
//! [Compute the histograms]
/// Establish the number of bins
int histSize = 256;
//! [Draw the histograms for B, G and R]
int hist_w = 512, hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256 } ;
const float* histRange = { range };
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
//! [Draw the histograms for B, G and R]
bool uniform = true; bool accumulate = false;
//! [Normalize the result to ( 0, histImage.rows )]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
//! [Normalize the result to ( 0, histImage.rows )]
Mat b_hist, g_hist, r_hist;
//! [Draw for each channel]
for( int i = 1; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ),
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ),
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ),
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 );
}
//! [Draw for each channel]
/// Compute the histograms:
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
/// Draw for each channel
for( int i = 1; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 );
}
/// Display
namedWindow("calcHist Demo", WINDOW_AUTOSIZE );
imshow("calcHist Demo", histImage );
waitKey(0);
return 0;
//! [Display]
imshow("Source image", src );
imshow("calcHist Demo", histImage );
waitKey();
//! [Display]
return 0;
}
@@ -12,42 +12,43 @@
using namespace std;
using namespace cv;
const char* keys =
"{ help h| | Print help message. }"
"{ input1 | | Path to input image 1. }"
"{ input2 | | Path to input image 2. }"
"{ input3 | | Path to input image 3. }";
/**
* @function main
*/
int main( int argc, char** argv )
{
Mat src_base, hsv_base;
Mat src_test1, hsv_test1;
Mat src_test2, hsv_test2;
Mat hsv_half_down;
/// Load three images with different environment settings
if( argc < 4 )
//! [Load three images with different environment settings]
CommandLineParser parser( argc, argv, keys );
Mat src_base = imread( parser.get<String>("input1") );
Mat src_test1 = imread( parser.get<String>("input2") );
Mat src_test2 = imread( parser.get<String>("input3") );
if( src_base.empty() || src_test1.empty() || src_test2.empty() )
{
printf("** Error. Usage: ./compareHist_Demo <image_settings0> <image_settings1> <image_settings2>\n");
cout << "Could not open or find the images!\n" << endl;
parser.printMessage();
return -1;
}
//! [Load three images with different environment settings]
src_base = imread( argv[1], IMREAD_COLOR );
src_test1 = imread( argv[2], IMREAD_COLOR );
src_test2 = imread( argv[3], IMREAD_COLOR );
if(src_base.empty() || src_test1.empty() || src_test2.empty())
{
cout << "Can't read one of the images" << endl;
return -1;
}
/// Convert to HSV
//! [Convert to HSV]
Mat hsv_base, hsv_test1, hsv_test2;
cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );
cvtColor( src_test2, hsv_test2, COLOR_BGR2HSV );
//! [Convert to HSV]
hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows - 1 ), Range( 0, hsv_base.cols - 1 ) );
//! [Convert to HSV half]
Mat hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows ), Range( 0, hsv_base.cols ) );
//! [Convert to HSV half]
/// Using 50 bins for hue and 60 for saturation
int h_bins = 50; int s_bins = 60;
//! [Using 50 bins for hue and 60 for saturation]
int h_bins = 50, s_bins = 60;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 179, saturation from 0 to 255
@@ -56,17 +57,13 @@ int main( int argc, char** argv )
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
// Use the 0-th and 1-st channels
int channels[] = { 0, 1 };
//! [Using 50 bins for hue and 60 for saturation]
//! [Calculate the histograms for the HSV images]
Mat hist_base, hist_half_down, hist_test1, hist_test2;
/// Histograms
MatND hist_base;
MatND hist_half_down;
MatND hist_test1;
MatND hist_test2;
/// Calculate the histograms for the HSV images
calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() );
@@ -78,20 +75,21 @@ int main( int argc, char** argv )
calcHist( &hsv_test2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, true, false );
normalize( hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat() );
//! [Calculate the histograms for the HSV images]
/// Apply the histogram comparison methods
for( int i = 0; i < 4; i++ )
//! [Apply the histogram comparison methods]
for( int compare_method = 0; compare_method < 4; compare_method++ )
{
int compare_method = i;
double base_base = compareHist( hist_base, hist_base, compare_method );
double base_half = compareHist( hist_base, hist_half_down, compare_method );
double base_test1 = compareHist( hist_base, hist_test1, compare_method );
double base_test2 = compareHist( hist_base, hist_test2, compare_method );
printf( " Method [%d] Perfect, Base-Half, Base-Test(1), Base-Test(2) : %f, %f, %f, %f \n", i, base_base, base_half , base_test1, base_test2 );
cout << "Method " << compare_method << " Perfect, Base-Half, Base-Test(1), Base-Test(2) : "
<< base_base << " / " << base_half << " / " << base_test1 << " / " << base_test2 << endl;
}
//! [Apply the histogram comparison methods]
printf( "Done \n" );
cout << "Done \n";
return 0;
}