Merge remote-tracking branch 'upstream/3.4' into merge-3.4
This commit is contained in:
@@ -20,29 +20,32 @@ using namespace cv;
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
//! [basic-linear-transform-parameters]
|
||||
double alpha = 1.0; /*< Simple contrast control */
|
||||
int beta = 0; /*< Simple brightness control */
|
||||
//! [basic-linear-transform-parameters]
|
||||
|
||||
/// Read image given by user
|
||||
//! [basic-linear-transform-load]
|
||||
String imageName("../data/lena.jpg"); // by default
|
||||
if (argc > 1)
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
|
||||
Mat image = imread( parser.get<String>( "@input" ) );
|
||||
if( image.empty() )
|
||||
{
|
||||
imageName = argv[1];
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
}
|
||||
Mat image = imread( imageName );
|
||||
//! [basic-linear-transform-load]
|
||||
|
||||
//! [basic-linear-transform-output]
|
||||
Mat new_image = Mat::zeros( image.size(), image.type() );
|
||||
//! [basic-linear-transform-output]
|
||||
|
||||
//! [basic-linear-transform-parameters]
|
||||
double alpha = 1.0; /*< Simple contrast control */
|
||||
int beta = 0; /*< Simple brightness control */
|
||||
|
||||
/// Initialize values
|
||||
cout << " Basic Linear Transforms " << endl;
|
||||
cout << "-------------------------" << endl;
|
||||
cout << "* Enter the alpha value [1.0-3.0]: "; cin >> alpha;
|
||||
cout << "* Enter the beta value [0-100]: "; cin >> beta;
|
||||
//! [basic-linear-transform-parameters]
|
||||
|
||||
/// Do the operation new_image(i,j) = alpha*image(i,j) + beta
|
||||
/// Instead of these 'for' loops we could have used simply:
|
||||
@@ -51,19 +54,15 @@ int main( int argc, char** argv )
|
||||
//! [basic-linear-transform-operation]
|
||||
for( int y = 0; y < image.rows; y++ ) {
|
||||
for( int x = 0; x < image.cols; x++ ) {
|
||||
for( int c = 0; c < 3; c++ ) {
|
||||
for( int c = 0; c < image.channels(); c++ ) {
|
||||
new_image.at<Vec3b>(y,x)[c] =
|
||||
saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
|
||||
saturate_cast<uchar>( alpha*image.at<Vec3b>(y,x)[c] + beta );
|
||||
}
|
||||
}
|
||||
}
|
||||
//! [basic-linear-transform-operation]
|
||||
|
||||
//! [basic-linear-transform-display]
|
||||
/// Create Windows
|
||||
namedWindow("Original Image", WINDOW_AUTOSIZE);
|
||||
namedWindow("New Image", WINDOW_AUTOSIZE);
|
||||
|
||||
/// Show stuff
|
||||
imshow("Original Image", image);
|
||||
imshow("New Image", new_image);
|
||||
|
||||
+17
-17
@@ -3,6 +3,8 @@
|
||||
#include "opencv2/highgui.hpp"
|
||||
|
||||
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using namespace cv;
|
||||
|
||||
namespace
|
||||
@@ -19,12 +21,13 @@ void basicLinearTransform(const Mat &img, const double alpha_, const int beta_)
|
||||
img.convertTo(res, -1, alpha_, beta_);
|
||||
|
||||
hconcat(img, res, img_corrected);
|
||||
imshow("Brightness and contrast adjustments", img_corrected);
|
||||
}
|
||||
|
||||
void gammaCorrection(const Mat &img, const double gamma_)
|
||||
{
|
||||
CV_Assert(gamma_ >= 0);
|
||||
//![changing-contrast-brightness-gamma-correction]
|
||||
//! [changing-contrast-brightness-gamma-correction]
|
||||
Mat lookUpTable(1, 256, CV_8U);
|
||||
uchar* p = lookUpTable.ptr();
|
||||
for( int i = 0; i < 256; ++i)
|
||||
@@ -32,9 +35,10 @@ void gammaCorrection(const Mat &img, const double gamma_)
|
||||
|
||||
Mat res = img.clone();
|
||||
LUT(img, lookUpTable, res);
|
||||
//![changing-contrast-brightness-gamma-correction]
|
||||
//! [changing-contrast-brightness-gamma-correction]
|
||||
|
||||
hconcat(img, res, img_gamma_corrected);
|
||||
imshow("Gamma correction", img_gamma_corrected);
|
||||
}
|
||||
|
||||
void on_linear_transform_alpha_trackbar(int, void *)
|
||||
@@ -60,36 +64,32 @@ void on_gamma_correction_trackbar(int, void *)
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
|
||||
String imageName("../data/lena.jpg"); // by default
|
||||
if (argc > 1)
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
|
||||
img_original = imread( parser.get<String>( "@input" ) );
|
||||
if( img_original.empty() )
|
||||
{
|
||||
imageName = argv[1];
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
img_original = imread( imageName );
|
||||
img_corrected = Mat(img_original.rows, img_original.cols*2, img_original.type());
|
||||
img_gamma_corrected = Mat(img_original.rows, img_original.cols*2, img_original.type());
|
||||
|
||||
hconcat(img_original, img_original, img_corrected);
|
||||
hconcat(img_original, img_original, img_gamma_corrected);
|
||||
|
||||
namedWindow("Brightness and contrast adjustments", WINDOW_AUTOSIZE);
|
||||
namedWindow("Gamma correction", WINDOW_AUTOSIZE);
|
||||
namedWindow("Brightness and contrast adjustments");
|
||||
namedWindow("Gamma correction");
|
||||
|
||||
createTrackbar("Alpha gain (contrast)", "Brightness and contrast adjustments", &alpha, 500, on_linear_transform_alpha_trackbar);
|
||||
createTrackbar("Beta bias (brightness)", "Brightness and contrast adjustments", &beta, 200, on_linear_transform_beta_trackbar);
|
||||
createTrackbar("Gamma correction", "Gamma correction", &gamma_cor, 200, on_gamma_correction_trackbar);
|
||||
|
||||
while (true)
|
||||
{
|
||||
imshow("Brightness and contrast adjustments", img_corrected);
|
||||
imshow("Gamma correction", img_gamma_corrected);
|
||||
on_linear_transform_alpha_trackbar(0, 0);
|
||||
on_gamma_correction_trackbar(0, 0);
|
||||
|
||||
int c = waitKey(30);
|
||||
if (c == 27)
|
||||
break;
|
||||
}
|
||||
waitKey();
|
||||
|
||||
imwrite("linear_transform_correction.png", img_corrected);
|
||||
imwrite("gamma_correction.png", img_gamma_corrected);
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
/* Snippet code for Operations with images tutorial (not intended to be run but should built successfully) */
|
||||
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/core/core_c.h"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main(int,char**)
|
||||
{
|
||||
std::string filename = "";
|
||||
// Input/Output
|
||||
{
|
||||
//! [Load an image from a file]
|
||||
Mat img = imread(filename);
|
||||
//! [Load an image from a file]
|
||||
CV_UNUSED(img);
|
||||
}
|
||||
{
|
||||
//! [Load an image from a file in grayscale]
|
||||
Mat img = imread(filename, IMREAD_GRAYSCALE);
|
||||
//! [Load an image from a file in grayscale]
|
||||
CV_UNUSED(img);
|
||||
}
|
||||
{
|
||||
Mat img(4,4,CV_8U);
|
||||
//! [Save image]
|
||||
imwrite(filename, img);
|
||||
//! [Save image]
|
||||
}
|
||||
// Accessing pixel intensity values
|
||||
{
|
||||
Mat img(4,4,CV_8U);
|
||||
int y = 0, x = 0;
|
||||
{
|
||||
//! [Pixel access 1]
|
||||
Scalar intensity = img.at<uchar>(y, x);
|
||||
//! [Pixel access 1]
|
||||
CV_UNUSED(intensity);
|
||||
}
|
||||
{
|
||||
//! [Pixel access 2]
|
||||
Scalar intensity = img.at<uchar>(Point(x, y));
|
||||
//! [Pixel access 2]
|
||||
CV_UNUSED(intensity);
|
||||
}
|
||||
{
|
||||
//! [Pixel access 3]
|
||||
Vec3b intensity = img.at<Vec3b>(y, x);
|
||||
uchar blue = intensity.val[0];
|
||||
uchar green = intensity.val[1];
|
||||
uchar red = intensity.val[2];
|
||||
//! [Pixel access 3]
|
||||
CV_UNUSED(blue);
|
||||
CV_UNUSED(green);
|
||||
CV_UNUSED(red);
|
||||
}
|
||||
{
|
||||
//! [Pixel access 4]
|
||||
Vec3f intensity = img.at<Vec3f>(y, x);
|
||||
float blue = intensity.val[0];
|
||||
float green = intensity.val[1];
|
||||
float red = intensity.val[2];
|
||||
//! [Pixel access 4]
|
||||
CV_UNUSED(blue);
|
||||
CV_UNUSED(green);
|
||||
CV_UNUSED(red);
|
||||
}
|
||||
{
|
||||
//! [Pixel access 5]
|
||||
img.at<uchar>(y, x) = 128;
|
||||
//! [Pixel access 5]
|
||||
}
|
||||
{
|
||||
int i = 0;
|
||||
//! [Mat from points vector]
|
||||
vector<Point2f> points;
|
||||
//... fill the array
|
||||
Mat pointsMat = Mat(points);
|
||||
//! [Mat from points vector]
|
||||
|
||||
//! [Point access]
|
||||
Point2f point = pointsMat.at<Point2f>(i, 0);
|
||||
//! [Point access]
|
||||
CV_UNUSED(point);
|
||||
}
|
||||
}
|
||||
// Memory management and reference counting
|
||||
{
|
||||
//! [Reference counting 1]
|
||||
std::vector<Point3f> points;
|
||||
// .. fill the array
|
||||
Mat pointsMat = Mat(points).reshape(1);
|
||||
//! [Reference counting 1]
|
||||
CV_UNUSED(pointsMat);
|
||||
}
|
||||
{
|
||||
//! [Reference counting 2]
|
||||
Mat img = imread("image.jpg");
|
||||
Mat img1 = img.clone();
|
||||
//! [Reference counting 2]
|
||||
CV_UNUSED(img1);
|
||||
}
|
||||
{
|
||||
//! [Reference counting 3]
|
||||
Mat img = imread("image.jpg");
|
||||
Mat sobelx;
|
||||
Sobel(img, sobelx, CV_32F, 1, 0);
|
||||
//! [Reference counting 3]
|
||||
}
|
||||
// Primitive operations
|
||||
{
|
||||
Mat img;
|
||||
{
|
||||
//! [Set image to black]
|
||||
img = Scalar(0);
|
||||
//! [Set image to black]
|
||||
}
|
||||
{
|
||||
//! [Select ROI]
|
||||
Rect r(10, 10, 100, 100);
|
||||
Mat smallImg = img(r);
|
||||
//! [Select ROI]
|
||||
CV_UNUSED(smallImg);
|
||||
}
|
||||
}
|
||||
{
|
||||
//! [C-API conversion]
|
||||
Mat img = imread("image.jpg");
|
||||
IplImage img1 = img;
|
||||
CvMat m = img;
|
||||
//! [C-API conversion]
|
||||
CV_UNUSED(img1);
|
||||
CV_UNUSED(m);
|
||||
}
|
||||
{
|
||||
//! [BGR to Gray]
|
||||
Mat img = imread("image.jpg"); // loading a 8UC3 image
|
||||
Mat grey;
|
||||
cvtColor(img, grey, COLOR_BGR2GRAY);
|
||||
//! [BGR to Gray]
|
||||
}
|
||||
{
|
||||
Mat dst, src;
|
||||
//! [Convert to CV_32F]
|
||||
src.convertTo(dst, CV_32F);
|
||||
//! [Convert to CV_32F]
|
||||
}
|
||||
// Visualizing images
|
||||
{
|
||||
//! [imshow 1]
|
||||
Mat img = imread("image.jpg");
|
||||
namedWindow("image", WINDOW_AUTOSIZE);
|
||||
imshow("image", img);
|
||||
waitKey();
|
||||
//! [imshow 1]
|
||||
}
|
||||
{
|
||||
//! [imshow 2]
|
||||
Mat img = imread("image.jpg");
|
||||
Mat grey;
|
||||
cvtColor(img, grey, COLOR_BGR2GRAY);
|
||||
Mat sobelx;
|
||||
Sobel(grey, sobelx, CV_32F, 1, 0);
|
||||
double minVal, maxVal;
|
||||
minMaxLoc(sobelx, &minVal, &maxVal); //find minimum and maximum intensities
|
||||
Mat draw;
|
||||
sobelx.convertTo(draw, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
|
||||
namedWindow("image", WINDOW_AUTOSIZE);
|
||||
imshow("image", draw);
|
||||
waitKey();
|
||||
//! [imshow 2]
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -21,13 +21,9 @@ double getOrientation(const vector<Point> &, Mat&);
|
||||
*/
|
||||
void drawAxis(Mat& img, Point p, Point q, Scalar colour, const float scale = 0.2)
|
||||
{
|
||||
//! [visualization1]
|
||||
double angle;
|
||||
double hypotenuse;
|
||||
angle = atan2( (double) p.y - q.y, (double) p.x - q.x ); // angle in radians
|
||||
hypotenuse = sqrt( (double) (p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q.x));
|
||||
// double degrees = angle * 180 / CV_PI; // convert radians to degrees (0-180 range)
|
||||
// cout << "Degrees: " << abs(degrees - 180) << endl; // angle in 0-360 degrees range
|
||||
//! [visualization1]
|
||||
double angle = atan2( (double) p.y - q.y, (double) p.x - q.x ); // angle in radians
|
||||
double hypotenuse = sqrt( (double) (p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q.x));
|
||||
|
||||
// Here we lengthen the arrow by a factor of scale
|
||||
q.x = (int) (p.x - scale * hypotenuse * cos(angle));
|
||||
@@ -42,7 +38,7 @@ void drawAxis(Mat& img, Point p, Point q, Scalar colour, const float scale = 0.2
|
||||
p.x = (int) (q.x + 9 * cos(angle - CV_PI / 4));
|
||||
p.y = (int) (q.y + 9 * sin(angle - CV_PI / 4));
|
||||
line(img, p, q, colour, 1, LINE_AA);
|
||||
//! [visualization1]
|
||||
//! [visualization1]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,11 +46,11 @@ void drawAxis(Mat& img, Point p, Point q, Scalar colour, const float scale = 0.2
|
||||
*/
|
||||
double getOrientation(const vector<Point> &pts, Mat &img)
|
||||
{
|
||||
//! [pca]
|
||||
//! [pca]
|
||||
//Construct a buffer used by the pca analysis
|
||||
int sz = static_cast<int>(pts.size());
|
||||
Mat data_pts = Mat(sz, 2, CV_64FC1);
|
||||
for (int i = 0; i < data_pts.rows; ++i)
|
||||
Mat data_pts = Mat(sz, 2, CV_64F);
|
||||
for (int i = 0; i < data_pts.rows; i++)
|
||||
{
|
||||
data_pts.at<double>(i, 0) = pts[i].x;
|
||||
data_pts.at<double>(i, 1) = pts[i].y;
|
||||
@@ -70,16 +66,16 @@ double getOrientation(const vector<Point> &pts, Mat &img)
|
||||
//Store the eigenvalues and eigenvectors
|
||||
vector<Point2d> eigen_vecs(2);
|
||||
vector<double> eigen_val(2);
|
||||
for (int i = 0; i < 2; ++i)
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
eigen_vecs[i] = Point2d(pca_analysis.eigenvectors.at<double>(i, 0),
|
||||
pca_analysis.eigenvectors.at<double>(i, 1));
|
||||
|
||||
eigen_val[i] = pca_analysis.eigenvalues.at<double>(i);
|
||||
}
|
||||
//! [pca]
|
||||
|
||||
//! [pca]
|
||||
//! [visualization]
|
||||
//! [visualization]
|
||||
// Draw the principal components
|
||||
circle(img, cntr, 3, Scalar(255, 0, 255), 2);
|
||||
Point p1 = cntr + 0.02 * Point(static_cast<int>(eigen_vecs[0].x * eigen_val[0]), static_cast<int>(eigen_vecs[0].y * eigen_val[0]));
|
||||
@@ -88,7 +84,7 @@ double getOrientation(const vector<Point> &pts, Mat &img)
|
||||
drawAxis(img, cntr, p2, Scalar(255, 255, 0), 5);
|
||||
|
||||
double angle = atan2(eigen_vecs[0].y, eigen_vecs[0].x); // orientation in radians
|
||||
//! [visualization]
|
||||
//! [visualization]
|
||||
|
||||
return angle;
|
||||
}
|
||||
@@ -98,10 +94,10 @@ double getOrientation(const vector<Point> &pts, Mat &img)
|
||||
*/
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
//! [pre-process]
|
||||
//! [pre-process]
|
||||
// Load image
|
||||
CommandLineParser parser(argc, argv, "{@input | ../data/pca_test1.jpg | input image}");
|
||||
parser.about( "This program demonstrates how to use OpenCV PCA to extract the orienation of an object.\n" );
|
||||
parser.about( "This program demonstrates how to use OpenCV PCA to extract the orientation of an object.\n" );
|
||||
parser.printMessage();
|
||||
|
||||
Mat src = imread(parser.get<String>("@input"));
|
||||
@@ -122,14 +118,14 @@ int main(int argc, char** argv)
|
||||
// Convert image to binary
|
||||
Mat bw;
|
||||
threshold(gray, bw, 50, 255, THRESH_BINARY | THRESH_OTSU);
|
||||
//! [pre-process]
|
||||
//! [pre-process]
|
||||
|
||||
//! [contours]
|
||||
//! [contours]
|
||||
// Find all the contours in the thresholded image
|
||||
vector<vector<Point> > contours;
|
||||
findContours(bw, contours, RETR_LIST, CHAIN_APPROX_NONE);
|
||||
|
||||
for (size_t i = 0; i < contours.size(); ++i)
|
||||
for (size_t i = 0; i < contours.size(); i++)
|
||||
{
|
||||
// Calculate the area of each contour
|
||||
double area = contourArea(contours[i]);
|
||||
@@ -137,14 +133,14 @@ int main(int argc, char** argv)
|
||||
if (area < 1e2 || 1e5 < area) continue;
|
||||
|
||||
// Draw each contour only for visualisation purposes
|
||||
drawContours(src, contours, static_cast<int>(i), Scalar(0, 0, 255), 2, LINE_8);
|
||||
drawContours(src, contours, static_cast<int>(i), Scalar(0, 0, 255), 2);
|
||||
// Find the orientation of each shape
|
||||
getOrientation(contours[i], src);
|
||||
}
|
||||
//! [contours]
|
||||
//! [contours]
|
||||
|
||||
imshow("output", src);
|
||||
|
||||
waitKey(0);
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include <opencv2/imgcodecs.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/ml.hpp>
|
||||
|
||||
@@ -9,21 +9,16 @@ using namespace cv::ml;
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
// Data for visual representation
|
||||
int width = 512, height = 512;
|
||||
Mat image = Mat::zeros(height, width, CV_8UC3);
|
||||
|
||||
// Set up training data
|
||||
//! [setup1]
|
||||
int labels[4] = {1, -1, -1, -1};
|
||||
float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };
|
||||
//! [setup1]
|
||||
//! [setup2]
|
||||
Mat trainingDataMat(4, 2, CV_32FC1, trainingData);
|
||||
Mat trainingDataMat(4, 2, CV_32F, trainingData);
|
||||
Mat labelsMat(4, 1, CV_32SC1, labels);
|
||||
//! [setup2]
|
||||
|
||||
|
||||
// Train the SVM
|
||||
//! [init]
|
||||
Ptr<SVM> svm = SVM::create();
|
||||
@@ -35,11 +30,16 @@ int main(int, char**)
|
||||
svm->train(trainingDataMat, ROW_SAMPLE, labelsMat);
|
||||
//! [train]
|
||||
|
||||
// Data for visual representation
|
||||
int width = 512, height = 512;
|
||||
Mat image = Mat::zeros(height, width, CV_8UC3);
|
||||
|
||||
// Show the decision regions given by the SVM
|
||||
//! [show]
|
||||
Vec3b green(0,255,0), blue (255,0,0);
|
||||
for (int i = 0; i < image.rows; ++i)
|
||||
for (int j = 0; j < image.cols; ++j)
|
||||
Vec3b green(0,255,0), blue(255,0,0);
|
||||
for (int i = 0; i < image.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < image.cols; j++)
|
||||
{
|
||||
Mat sampleMat = (Mat_<float>(1,2) << j,i);
|
||||
float response = svm->predict(sampleMat);
|
||||
@@ -49,34 +49,33 @@ int main(int, char**)
|
||||
else if (response == -1)
|
||||
image.at<Vec3b>(i,j) = blue;
|
||||
}
|
||||
}
|
||||
//! [show]
|
||||
|
||||
// Show the training data
|
||||
//! [show_data]
|
||||
int thickness = -1;
|
||||
int lineType = 8;
|
||||
circle( image, Point(501, 10), 5, Scalar( 0, 0, 0), thickness, lineType );
|
||||
circle( image, Point(255, 10), 5, Scalar(255, 255, 255), thickness, lineType );
|
||||
circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType );
|
||||
circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType );
|
||||
circle( image, Point(501, 10), 5, Scalar( 0, 0, 0), thickness );
|
||||
circle( image, Point(255, 10), 5, Scalar(255, 255, 255), thickness );
|
||||
circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness );
|
||||
circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness );
|
||||
//! [show_data]
|
||||
|
||||
// Show support vectors
|
||||
//! [show_vectors]
|
||||
thickness = 2;
|
||||
lineType = 8;
|
||||
Mat sv = svm->getUncompressedSupportVectors();
|
||||
|
||||
for (int i = 0; i < sv.rows; ++i)
|
||||
for (int i = 0; i < sv.rows; i++)
|
||||
{
|
||||
const float* v = sv.ptr<float>(i);
|
||||
circle( image, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thickness, lineType);
|
||||
circle(image, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thickness);
|
||||
}
|
||||
//! [show_vectors]
|
||||
|
||||
imwrite("result.png", image); // save the image
|
||||
|
||||
imshow("SVM Simple Example", image); // show it to the user
|
||||
waitKey(0);
|
||||
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -5,9 +5,6 @@
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/ml.hpp>
|
||||
|
||||
#define NTRAINING_SAMPLES 100 // Number of training samples per class
|
||||
#define FRAC_LINEAR_SEP 0.9f // Fraction of samples which compose the linear separable part
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::ml;
|
||||
using namespace std;
|
||||
@@ -16,8 +13,6 @@ static void help()
|
||||
{
|
||||
cout<< "\n--------------------------------------------------------------------------" << endl
|
||||
<< "This program shows Support Vector Machines for Non-Linearly Separable Data. " << endl
|
||||
<< "Usage:" << endl
|
||||
<< "./non_linear_svms" << endl
|
||||
<< "--------------------------------------------------------------------------" << endl
|
||||
<< endl;
|
||||
}
|
||||
@@ -26,13 +21,16 @@ int main()
|
||||
{
|
||||
help();
|
||||
|
||||
const int NTRAINING_SAMPLES = 100; // Number of training samples per class
|
||||
const float FRAC_LINEAR_SEP = 0.9f; // Fraction of samples which compose the linear separable part
|
||||
|
||||
// Data for visual representation
|
||||
const int WIDTH = 512, HEIGHT = 512;
|
||||
Mat I = Mat::zeros(HEIGHT, WIDTH, CV_8UC3);
|
||||
|
||||
//--------------------- 1. Set up training data randomly ---------------------------------------
|
||||
Mat trainData(2*NTRAINING_SAMPLES, 2, CV_32FC1);
|
||||
Mat labels (2*NTRAINING_SAMPLES, 1, CV_32SC1);
|
||||
Mat trainData(2*NTRAINING_SAMPLES, 2, CV_32F);
|
||||
Mat labels (2*NTRAINING_SAMPLES, 1, CV_32S);
|
||||
|
||||
RNG rng(100); // Random value generation class
|
||||
|
||||
@@ -44,10 +42,10 @@ int main()
|
||||
Mat trainClass = trainData.rowRange(0, nLinearSamples);
|
||||
// The x coordinate of the points is in [0, 0.4)
|
||||
Mat c = trainClass.colRange(0, 1);
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(0.4 * WIDTH));
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(0), Scalar(0.4 * WIDTH));
|
||||
// The y coordinate of the points is in [0, 1)
|
||||
c = trainClass.colRange(1,2);
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(0), Scalar(HEIGHT));
|
||||
|
||||
// Generate random points for the class 2
|
||||
trainClass = trainData.rowRange(2*NTRAINING_SAMPLES-nLinearSamples, 2*NTRAINING_SAMPLES);
|
||||
@@ -56,26 +54,26 @@ int main()
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(0.6*WIDTH), Scalar(WIDTH));
|
||||
// The y coordinate of the points is in [0, 1)
|
||||
c = trainClass.colRange(1,2);
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(0), Scalar(HEIGHT));
|
||||
//! [setup1]
|
||||
|
||||
//------------------ Set up the non-linearly separable part of the training data ---------------
|
||||
//! [setup2]
|
||||
// Generate random points for the classes 1 and 2
|
||||
trainClass = trainData.rowRange( nLinearSamples, 2*NTRAINING_SAMPLES-nLinearSamples);
|
||||
trainClass = trainData.rowRange(nLinearSamples, 2*NTRAINING_SAMPLES-nLinearSamples);
|
||||
// The x coordinate of the points is in [0.4, 0.6)
|
||||
c = trainClass.colRange(0,1);
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(0.4*WIDTH), Scalar(0.6*WIDTH));
|
||||
// The y coordinate of the points is in [0, 1)
|
||||
c = trainClass.colRange(1,2);
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
|
||||
rng.fill(c, RNG::UNIFORM, Scalar(0), Scalar(HEIGHT));
|
||||
//! [setup2]
|
||||
|
||||
//------------------------- Set up the labels for the classes ---------------------------------
|
||||
labels.rowRange( 0, NTRAINING_SAMPLES).setTo(1); // Class 1
|
||||
labels.rowRange(NTRAINING_SAMPLES, 2*NTRAINING_SAMPLES).setTo(2); // Class 2
|
||||
|
||||
//------------------------ 2. Set up the support vector machines parameters --------------------
|
||||
//------------------------ 3. Train the svm ----------------------------------------------------
|
||||
cout << "Starting training process" << endl;
|
||||
//! [init]
|
||||
Ptr<SVM> svm = SVM::create();
|
||||
@@ -84,6 +82,8 @@ int main()
|
||||
svm->setKernel(SVM::LINEAR);
|
||||
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
|
||||
//! [init]
|
||||
|
||||
//------------------------ 3. Train the svm ----------------------------------------------------
|
||||
//! [train]
|
||||
svm->train(trainData, ROW_SAMPLE, labels);
|
||||
//! [train]
|
||||
@@ -91,53 +91,54 @@ int main()
|
||||
|
||||
//------------------------ 4. Show the decision regions ----------------------------------------
|
||||
//! [show]
|
||||
Vec3b green(0,100,0), blue (100,0,0);
|
||||
for (int i = 0; i < I.rows; ++i)
|
||||
for (int j = 0; j < I.cols; ++j)
|
||||
Vec3b green(0,100,0), blue(100,0,0);
|
||||
for (int i = 0; i < I.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < I.cols; j++)
|
||||
{
|
||||
Mat sampleMat = (Mat_<float>(1,2) << i, j);
|
||||
Mat sampleMat = (Mat_<float>(1,2) << j, i);
|
||||
float response = svm->predict(sampleMat);
|
||||
|
||||
if (response == 1) I.at<Vec3b>(j, i) = green;
|
||||
else if (response == 2) I.at<Vec3b>(j, i) = blue;
|
||||
if (response == 1) I.at<Vec3b>(i,j) = green;
|
||||
else if (response == 2) I.at<Vec3b>(i,j) = blue;
|
||||
}
|
||||
}
|
||||
//! [show]
|
||||
|
||||
//----------------------- 5. Show the training data --------------------------------------------
|
||||
//! [show_data]
|
||||
int thick = -1;
|
||||
int lineType = 8;
|
||||
float px, py;
|
||||
// Class 1
|
||||
for (int i = 0; i < NTRAINING_SAMPLES; ++i)
|
||||
for (int i = 0; i < NTRAINING_SAMPLES; i++)
|
||||
{
|
||||
px = trainData.at<float>(i,0);
|
||||
py = trainData.at<float>(i,1);
|
||||
circle(I, Point( (int) px, (int) py ), 3, Scalar(0, 255, 0), thick, lineType);
|
||||
circle(I, Point( (int) px, (int) py ), 3, Scalar(0, 255, 0), thick);
|
||||
}
|
||||
// Class 2
|
||||
for (int i = NTRAINING_SAMPLES; i <2*NTRAINING_SAMPLES; ++i)
|
||||
for (int i = NTRAINING_SAMPLES; i <2*NTRAINING_SAMPLES; i++)
|
||||
{
|
||||
px = trainData.at<float>(i,0);
|
||||
py = trainData.at<float>(i,1);
|
||||
circle(I, Point( (int) px, (int) py ), 3, Scalar(255, 0, 0), thick, lineType);
|
||||
circle(I, Point( (int) px, (int) py ), 3, Scalar(255, 0, 0), thick);
|
||||
}
|
||||
//! [show_data]
|
||||
|
||||
//------------------------- 6. Show support vectors --------------------------------------------
|
||||
//! [show_vectors]
|
||||
thick = 2;
|
||||
lineType = 8;
|
||||
Mat sv = svm->getUncompressedSupportVectors();
|
||||
|
||||
for (int i = 0; i < sv.rows; ++i)
|
||||
for (int i = 0; i < sv.rows; i++)
|
||||
{
|
||||
const float* v = sv.ptr<float>(i);
|
||||
circle( I, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thick, lineType);
|
||||
circle(I, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thick);
|
||||
}
|
||||
//! [show_vectors]
|
||||
|
||||
imwrite("result.png", I); // save the Image
|
||||
imwrite("result.png", I); // save the Image
|
||||
imshow("SVM for Non-Linear Training Data", I); // show it to the user
|
||||
waitKey(0);
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user