Add Java and Python code for the following imgproc tutorials: Canny, Remap, threshold and threshold inRange. Use HSV colorspace instead of RGB for inRange threshold tutorial.
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user