mirror of
https://github.com/zebrajr/opencv.git
synced 2026-01-15 12:15:17 +00:00
Merge pull request #11543 from catree:add_tutorial_imgproc_java_python
This commit is contained in:
@@ -7,8 +7,10 @@
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
using std::cout;
|
||||
|
||||
/// Global variables
|
||||
|
||||
@@ -16,7 +18,7 @@ int threshold_value = 0;
|
||||
int threshold_type = 3;
|
||||
int const max_value = 255;
|
||||
int const max_type = 4;
|
||||
int const max_BINARY_value = 255;
|
||||
int const max_binary_value = 255;
|
||||
|
||||
Mat src, src_gray, dst;
|
||||
const char* window_name = "Threshold Demo";
|
||||
@@ -24,69 +26,62 @@ const char* window_name = "Threshold Demo";
|
||||
const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
|
||||
const char* trackbar_value = "Value";
|
||||
|
||||
/// Function headers
|
||||
void Threshold_Demo( int, void* );
|
||||
//![Threshold_Demo]
|
||||
/**
|
||||
* @function Threshold_Demo
|
||||
*/
|
||||
static void Threshold_Demo( int, void* )
|
||||
{
|
||||
/* 0: Binary
|
||||
1: Binary Inverted
|
||||
2: Threshold Truncated
|
||||
3: Threshold to Zero
|
||||
4: Threshold to Zero Inverted
|
||||
*/
|
||||
threshold( src_gray, dst, threshold_value, max_binary_value, threshold_type );
|
||||
imshow( window_name, dst );
|
||||
}
|
||||
//![Threshold_Demo]
|
||||
|
||||
/**
|
||||
* @function main
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
//! [load]
|
||||
String imageName("../data/stuff.jpg"); // by default
|
||||
if (argc > 1)
|
||||
{
|
||||
imageName = argv[1];
|
||||
}
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
|
||||
//! [load]
|
||||
|
||||
//! [window]
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
|
||||
//! [window]
|
||||
|
||||
//! [trackbar]
|
||||
createTrackbar( trackbar_type,
|
||||
window_name, &threshold_type,
|
||||
max_type, Threshold_Demo ); // Create Trackbar to choose type of Threshold
|
||||
|
||||
createTrackbar( trackbar_value,
|
||||
window_name, &threshold_value,
|
||||
max_value, Threshold_Demo ); // Create Trackbar to choose Threshold value
|
||||
//! [trackbar]
|
||||
|
||||
Threshold_Demo( 0, 0 ); // Call the function to initialize
|
||||
|
||||
/// Wait until user finishes program
|
||||
for(;;)
|
||||
//! [load]
|
||||
String imageName("../data/stuff.jpg"); // by default
|
||||
if (argc > 1)
|
||||
{
|
||||
char c = (char)waitKey( 20 );
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
imageName = argv[1];
|
||||
}
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
|
||||
if (src.empty())
|
||||
{
|
||||
cout << "Cannot read image: " << imageName << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
|
||||
//! [load]
|
||||
|
||||
//! [window]
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
|
||||
//! [window]
|
||||
|
||||
//! [trackbar]
|
||||
createTrackbar( trackbar_type,
|
||||
window_name, &threshold_type,
|
||||
max_type, Threshold_Demo ); // Create Trackbar to choose type of Threshold
|
||||
|
||||
createTrackbar( trackbar_value,
|
||||
window_name, &threshold_value,
|
||||
max_value, Threshold_Demo ); // Create Trackbar to choose Threshold value
|
||||
//! [trackbar]
|
||||
|
||||
Threshold_Demo( 0, 0 ); // Call the function to initialize
|
||||
|
||||
/// Wait until user finishes program
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
//![Threshold_Demo]
|
||||
/**
|
||||
* @function Threshold_Demo
|
||||
*/
|
||||
void Threshold_Demo( int, void* )
|
||||
{
|
||||
/* 0: Binary
|
||||
1: Binary Inverted
|
||||
2: Threshold Truncated
|
||||
3: Threshold to Zero
|
||||
4: Threshold to Zero Inverted
|
||||
*/
|
||||
|
||||
threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
|
||||
|
||||
imshow( window_name, dst );
|
||||
}
|
||||
//![Threshold_Demo]
|
||||
|
||||
@@ -1,102 +1,104 @@
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
/** Function Headers */
|
||||
void on_low_r_thresh_trackbar(int, void *);
|
||||
void on_high_r_thresh_trackbar(int, void *);
|
||||
void on_low_g_thresh_trackbar(int, void *);
|
||||
void on_high_g_thresh_trackbar(int, void *);
|
||||
void on_low_b_thresh_trackbar(int, void *);
|
||||
void on_high_b_thresh_trackbar(int, void *);
|
||||
|
||||
/** Global Variables */
|
||||
int low_r=30, low_g=30, low_b=30;
|
||||
int high_r=100, high_g=100, high_b=100;
|
||||
const int max_value_H = 360/2;
|
||||
const int max_value = 255;
|
||||
const String window_capture_name = "Video Capture";
|
||||
const String window_detection_name = "Object Detection";
|
||||
int low_H = 0, low_S = 0, low_V = 0;
|
||||
int high_H = max_value_H, high_S = max_value, high_V = max_value;
|
||||
|
||||
/** @function main */
|
||||
int main()
|
||||
//! [low]
|
||||
static void on_low_H_thresh_trackbar(int, void *)
|
||||
{
|
||||
low_H = min(high_H-1, low_H);
|
||||
setTrackbarPos("Low H", window_detection_name, low_H);
|
||||
}
|
||||
//! [low]
|
||||
|
||||
//! [high]
|
||||
static void on_high_H_thresh_trackbar(int, void *)
|
||||
{
|
||||
high_H = max(high_H, low_H+1);
|
||||
setTrackbarPos("High H", window_detection_name, high_H);
|
||||
}
|
||||
|
||||
//! [high]
|
||||
static void on_low_S_thresh_trackbar(int, void *)
|
||||
{
|
||||
low_S = min(high_S-1, low_S);
|
||||
setTrackbarPos("Low S", window_detection_name, low_S);
|
||||
}
|
||||
|
||||
static void on_high_S_thresh_trackbar(int, void *)
|
||||
{
|
||||
high_S = max(high_S, low_S+1);
|
||||
setTrackbarPos("High S", window_detection_name, high_S);
|
||||
}
|
||||
|
||||
static void on_low_V_thresh_trackbar(int, void *)
|
||||
{
|
||||
low_V = min(high_V-1, low_V);
|
||||
setTrackbarPos("Low V", window_detection_name, low_V);
|
||||
}
|
||||
|
||||
static void on_high_V_thresh_trackbar(int, void *)
|
||||
{
|
||||
high_V = max(high_V, low_V+1);
|
||||
setTrackbarPos("High V", window_detection_name, high_V);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
//! [mat]
|
||||
Mat frame, frame_threshold;
|
||||
//! [mat]
|
||||
//! [cap]
|
||||
VideoCapture cap(0);
|
||||
VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
|
||||
//! [cap]
|
||||
|
||||
//! [window]
|
||||
namedWindow("Video Capture", WINDOW_NORMAL);
|
||||
namedWindow("Object Detection", WINDOW_NORMAL);
|
||||
namedWindow(window_capture_name);
|
||||
namedWindow(window_detection_name);
|
||||
//! [window]
|
||||
|
||||
//! [trackbar]
|
||||
//-- Trackbars to set thresholds for RGB values
|
||||
createTrackbar("Low R","Object Detection", &low_r, 255, on_low_r_thresh_trackbar);
|
||||
createTrackbar("High R","Object Detection", &high_r, 255, on_high_r_thresh_trackbar);
|
||||
createTrackbar("Low G","Object Detection", &low_g, 255, on_low_g_thresh_trackbar);
|
||||
createTrackbar("High G","Object Detection", &high_g, 255, on_high_g_thresh_trackbar);
|
||||
createTrackbar("Low B","Object Detection", &low_b, 255, on_low_b_thresh_trackbar);
|
||||
createTrackbar("High B","Object Detection", &high_b, 255, on_high_b_thresh_trackbar);
|
||||
// Trackbars to set thresholds for HSV values
|
||||
createTrackbar("Low H", window_detection_name, &low_H, max_value_H, on_low_H_thresh_trackbar);
|
||||
createTrackbar("High H", window_detection_name, &high_H, max_value_H, on_high_H_thresh_trackbar);
|
||||
createTrackbar("Low S", window_detection_name, &low_S, max_value, on_low_S_thresh_trackbar);
|
||||
createTrackbar("High S", window_detection_name, &high_S, max_value, on_high_S_thresh_trackbar);
|
||||
createTrackbar("Low V", window_detection_name, &low_V, max_value, on_low_V_thresh_trackbar);
|
||||
createTrackbar("High V", window_detection_name, &high_V, max_value, on_high_V_thresh_trackbar);
|
||||
//! [trackbar]
|
||||
while((char)waitKey(1)!='q'){
|
||||
|
||||
Mat frame, frame_HSV, frame_threshold;
|
||||
while (true) {
|
||||
//! [while]
|
||||
cap>>frame;
|
||||
cap >> frame;
|
||||
if(frame.empty())
|
||||
{
|
||||
break;
|
||||
//-- Detect the object based on RGB Range Values
|
||||
inRange(frame,Scalar(low_b,low_g,low_r), Scalar(high_b,high_g,high_r),frame_threshold);
|
||||
}
|
||||
|
||||
// Convert from BGR to HSV colorspace
|
||||
cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
|
||||
// Detect the object based on HSV Range Values
|
||||
inRange(frame_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), frame_threshold);
|
||||
//! [while]
|
||||
|
||||
//! [show]
|
||||
//-- Show the frames
|
||||
imshow("Video Capture",frame);
|
||||
imshow("Object Detection",frame_threshold);
|
||||
// Show the frames
|
||||
imshow(window_capture_name, frame);
|
||||
imshow(window_detection_name, frame_threshold);
|
||||
//! [show]
|
||||
|
||||
char key = (char) waitKey(30);
|
||||
if (key == 'q' || key == 27)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//! [low]
|
||||
/** @function on_low_r_thresh_trackbar */
|
||||
void on_low_r_thresh_trackbar(int, void *)
|
||||
{
|
||||
low_r = min(high_r-1, low_r);
|
||||
setTrackbarPos("Low R","Object Detection", low_r);
|
||||
}
|
||||
//! [low]
|
||||
//! [high]
|
||||
/** @function on_high_r_thresh_trackbar */
|
||||
void on_high_r_thresh_trackbar(int, void *)
|
||||
{
|
||||
high_r = max(high_r, low_r+1);
|
||||
setTrackbarPos("High R", "Object Detection", high_r);
|
||||
}
|
||||
//![high]
|
||||
/** @function on_low_g_thresh_trackbar */
|
||||
void on_low_g_thresh_trackbar(int, void *)
|
||||
{
|
||||
low_g = min(high_g-1, low_g);
|
||||
setTrackbarPos("Low G","Object Detection", low_g);
|
||||
}
|
||||
|
||||
/** @function on_high_g_thresh_trackbar */
|
||||
void on_high_g_thresh_trackbar(int, void *)
|
||||
{
|
||||
high_g = max(high_g, low_g+1);
|
||||
setTrackbarPos("High G", "Object Detection", high_g);
|
||||
}
|
||||
|
||||
/** @function on_low_b_thresh_trackbar */
|
||||
void on_low_b_thresh_trackbar(int, void *)
|
||||
{
|
||||
low_b= min(high_b-1, low_b);
|
||||
setTrackbarPos("Low B","Object Detection", low_b);
|
||||
}
|
||||
|
||||
/** @function on_high_b_thresh_trackbar */
|
||||
void on_high_b_thresh_trackbar(int, void *)
|
||||
{
|
||||
high_b = max(high_b, low_b+1);
|
||||
setTrackbarPos("High B", "Object Detection", high_b);
|
||||
}
|
||||
|
||||
@@ -14,11 +14,10 @@ using namespace cv;
|
||||
Mat src, src_gray;
|
||||
Mat dst, detected_edges;
|
||||
|
||||
int edgeThresh = 1;
|
||||
int lowThreshold;
|
||||
int const max_lowThreshold = 100;
|
||||
int ratio = 3;
|
||||
int kernel_size = 3;
|
||||
int lowThreshold = 0;
|
||||
const int max_lowThreshold = 100;
|
||||
const int ratio = 3;
|
||||
const int kernel_size = 3;
|
||||
const char* window_name = "Edge Map";
|
||||
//![variables]
|
||||
|
||||
|
||||
@@ -11,90 +11,104 @@
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global variables
|
||||
Mat src, dst;
|
||||
Mat map_x, map_y;
|
||||
const char* remap_window = "Remap demo";
|
||||
int ind = 0;
|
||||
|
||||
/// Function Headers
|
||||
void update_map( void );
|
||||
void update_map( int &ind, Mat &map_x, Mat &map_y );
|
||||
|
||||
/**
|
||||
* @function main
|
||||
*/
|
||||
int main(int argc, const char** argv)
|
||||
{
|
||||
/// Load the image
|
||||
CommandLineParser parser(argc, argv, "{@image |../data/chicky_512.png|input image name}");
|
||||
std::string filename = parser.get<std::string>(0);
|
||||
src = imread( filename, IMREAD_COLOR );
|
||||
CommandLineParser parser(argc, argv, "{@image |../data/chicky_512.png|input image name}");
|
||||
std::string filename = parser.get<std::string>(0);
|
||||
//! [Load]
|
||||
/// Load the image
|
||||
Mat src = imread( filename, IMREAD_COLOR );
|
||||
if (src.empty())
|
||||
{
|
||||
std::cout << "Cannot read image: " << filename << std::endl;
|
||||
return -1;
|
||||
}
|
||||
//! [Load]
|
||||
|
||||
/// Create dst, map_x and map_y with the same size as src:
|
||||
dst.create( src.size(), src.type() );
|
||||
map_x.create( src.size(), CV_32FC1 );
|
||||
map_y.create( src.size(), CV_32FC1 );
|
||||
//! [Create]
|
||||
/// Create dst, map_x and map_y with the same size as src:
|
||||
Mat dst(src.size(), src.type());
|
||||
Mat map_x(src.size(), CV_32FC1);
|
||||
Mat map_y(src.size(), CV_32FC1);
|
||||
//! [Create]
|
||||
|
||||
/// Create window
|
||||
namedWindow( remap_window, WINDOW_AUTOSIZE );
|
||||
//! [Window]
|
||||
/// Create window
|
||||
const char* remap_window = "Remap demo";
|
||||
namedWindow( remap_window, WINDOW_AUTOSIZE );
|
||||
//! [Window]
|
||||
|
||||
/// Loop
|
||||
for(;;)
|
||||
{
|
||||
/// Each 1 sec. Press ESC to exit the program
|
||||
char c = (char)waitKey( 1000 );
|
||||
//! [Loop]
|
||||
/// Index to switch between the remap modes
|
||||
int ind = 0;
|
||||
for(;;)
|
||||
{
|
||||
/// Update map_x & map_y. Then apply remap
|
||||
update_map(ind, map_x, map_y);
|
||||
remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0) );
|
||||
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
/// Display results
|
||||
imshow( remap_window, dst );
|
||||
|
||||
/// Update map_x & map_y. Then apply remap
|
||||
update_map();
|
||||
remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0) );
|
||||
|
||||
// Display results
|
||||
imshow( remap_window, dst );
|
||||
}
|
||||
return 0;
|
||||
/// Each 1 sec. Press ESC to exit the program
|
||||
char c = (char)waitKey( 1000 );
|
||||
if( c == 27 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
//! [Loop]
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function update_map
|
||||
* @brief Fill the map_x and map_y matrices with 4 types of mappings
|
||||
*/
|
||||
void update_map( void )
|
||||
//! [Update]
|
||||
void update_map( int &ind, Mat &map_x, Mat &map_y )
|
||||
{
|
||||
ind = ind%4;
|
||||
|
||||
for( int j = 0; j < src.rows; j++ )
|
||||
{ for( int i = 0; i < src.cols; i++ )
|
||||
{
|
||||
switch( ind )
|
||||
{
|
||||
case 0:
|
||||
if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
|
||||
{
|
||||
map_x.at<float>(j,i) = 2*( i - src.cols*0.25f ) + 0.5f ;
|
||||
map_y.at<float>(j,i) = 2*( j - src.rows*0.25f ) + 0.5f ;
|
||||
}
|
||||
else
|
||||
{ map_x.at<float>(j,i) = 0 ;
|
||||
map_y.at<float>(j,i) = 0 ;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
map_x.at<float>(j,i) = (float)i ;
|
||||
map_y.at<float>(j,i) = (float)(src.rows - j) ;
|
||||
break;
|
||||
case 2:
|
||||
map_x.at<float>(j,i) = (float)(src.cols - i) ;
|
||||
map_y.at<float>(j,i) = (float)j ;
|
||||
break;
|
||||
case 3:
|
||||
map_x.at<float>(j,i) = (float)(src.cols - i) ;
|
||||
map_y.at<float>(j,i) = (float)(src.rows - j) ;
|
||||
break;
|
||||
} // end of switch
|
||||
}
|
||||
for( int i = 0; i < map_x.rows; i++ )
|
||||
{
|
||||
for( int j = 0; j < map_x.cols; j++ )
|
||||
{
|
||||
switch( ind )
|
||||
{
|
||||
case 0:
|
||||
if( j > map_x.cols*0.25 && j < map_x.cols*0.75 && i > map_x.rows*0.25 && i < map_x.rows*0.75 )
|
||||
{
|
||||
map_x.at<float>(i, j) = 2*( j - map_x.cols*0.25f ) + 0.5f;
|
||||
map_y.at<float>(i, j) = 2*( i - map_x.rows*0.25f ) + 0.5f;
|
||||
}
|
||||
else
|
||||
{
|
||||
map_x.at<float>(i, j) = 0;
|
||||
map_y.at<float>(i, j) = 0;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
map_x.at<float>(i, j) = (float)j;
|
||||
map_y.at<float>(i, j) = (float)(map_x.rows - i);
|
||||
break;
|
||||
case 2:
|
||||
map_x.at<float>(i, j) = (float)(map_x.cols - j);
|
||||
map_y.at<float>(i, j) = (float)i;
|
||||
break;
|
||||
case 3:
|
||||
map_x.at<float>(i, j) = (float)(map_x.cols - j);
|
||||
map_y.at<float>(i, j) = (float)(map_x.rows - i);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
} // end of switch
|
||||
}
|
||||
}
|
||||
ind++;
|
||||
ind = (ind+1) % 4;
|
||||
}
|
||||
//! [Update]
|
||||
|
||||
Reference in New Issue
Block a user