1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-27 22:33:03 +04:00

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
@@ -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 );
}