Skip to content

Commit

Permalink
Exercises_5 modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
garybradski committed Jun 13, 2017
2 parents fb0a156 + afd23c8 commit d1057c4
Show file tree
Hide file tree
Showing 9 changed files with 8,406 additions and 75 deletions.
66 changes: 0 additions & 66 deletions Exercises_5.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,3 @@

Please sign in to a Gmail account
connected to SalesforceIQ
in order to view contact details
or, Log Out of SalesforceIQ.

Attachment Bradski Residence P9 Gary.pdf successfully uploaded and added.Conversation opened. 1 unread message.

Skip to content
Using Gmail with screen readers
4
Search



Gmail
COMPOSE
Labels
Inbox (17,079)
Important
Sent Mail
Drafts (1)
Spam (186)
Trash
Categories
Book
Deleted Messages
GSOC_2012 (51)
GSoC_lists (2,529)
GSoC2013
Intel
OpenCV
still trying
More
Hangouts




More
1 of 65,455

Print all In new window
files
Inbox
x

Gary Bradski <[email protected]>
Attachments1:33 PM (4 minutes ago)

to me
3 Attachments


Click here to Reply or Forward
29.13 GB (29%) of 100 GB used
Manage
Terms - Privacy
Last account activity: 1 minute ago
Open in 2 other locations Details


//Exercises at end of Chapter 5
// 1-6
#include <opencv2/opencv.hpp>
Expand Down Expand Up @@ -211,8 +149,4 @@ int main( int argc, const char** argv )
cout << "6" << endl;
waitKey(-1); //Wait here until any key pressed
return 0;

}
Exercises_5.cpp
Open with
Displaying CMakeLists.txt.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@

This is the example code that accompanies Learning OpenCV 3 by Adrian Kaehler and Gary Bradski (9781491937990).

**In progress May 24, 2017**
--In progress May 24, 2017--
**In progress June 13, 2017**

This code is in progress, currently at Chapter 15, bear with us, the code is coming, both Authors are busy, as always, building startups. If you want to help, request to join the project. Keep pull requests to one function at a time. It doesn't have to be just examples from the book, it can be other code snippets from the book, exercises, new functionality or even a "how to use" opencv_contrib functions.
This code is in progress, currently at --Chapter 15-- Chapter 21 (but missing 19, want to make 18 also read from disk, and add in a few other functions), bear with us, the code is coming, both Authors are busy, as always, building startups. If you want to help, request to join the project. Keep pull requests to one function at a time. It doesn't have to be just examples from the book, it can be other code snippets from the book, **exercises**, new functionality or even a "how to use" opencv_contrib functions.

Note, for your interest, included here is a _Docker_ file that
* Shares a directory with the host operating system and shares the camera
* Loads Ubuntu 16.04
* Loads all dependencies for OpenCV 3.2 and opencv_contrib
* Loads and builds OpenCV 3.2 and opencv_contrib into a build directory
* executable files end up in opencv-3.2.0/build/bin
* Git clones the code (and Docker file) for Learning OpenCV 3 and builds it
* executable files end up in Learning_OpenCV-3_examples/build
* To get to the top level directory, just type: `cd`
* If you just want to do this "by hand" in your home, replace the "RUN"s below with "sudo"


New code is added regularly. We apologize that all of the code from the
book is not up yet, but it will be uploaded as soon as possible. Please
bear with us, the authors and the staff are very busy people and we are
doing our best.

Click the Download Zip button to the right to download example code.

Visit the catalog page [here](http://shop.oreilly.com/product/0636920044765.do).
Expand Down
2 changes: 1 addition & 1 deletion example_18-01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// the
// requested number of views, and calibrating the camera
//
// **GARY WILL MODIFY THIS TO ALSO READ FROM DISK**
// **GARY WILL MODIFY THIS TO ALSO READ FROM DISK**
//
#include <opencv2/opencv.hpp>
#include <iostream>
Expand Down
119 changes: 119 additions & 0 deletions example_21-01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <iostream>

using namespace std;
using namespace cv;

void help(char **argv) {
cout << "\n\n"
<< "Using binary decision trees to learn to recognize poisonous\n"
<< " from edible mushrooms based on visible attributes.\n"
<< " This program demonstrates how to create and a train a \n"
<< " decision tree using ml library in OpenCV.\n"
<< "Call:\n" << argv[0] << " <csv-file-path>\n\n"
<< endl;
}

int main(int argc, char *argv[]) {
// If the caller gave a filename, great. Otherwise, use a default.
//
const char *csv_file_name = argc >= 2 ? argv[1] : "../mushroom/agaricus-lepiota.data";
cout << "OpenCV Version: " << CV_VERSION << endl;
help(argv);

// Read in the CSV file that we were given.
//
cv::Ptr<cv::ml::TrainData> data_set =
cv::ml::TrainData::loadFromCSV(csv_file_name, // Input file name
0, // Header lines (ignore this many)
0, // Responses are (start) at thie column
1, // Inputs start at this column
"cat[0-22]" // All 23 columns are categorical
);
// Use defaults for delimeter (',') and missch ('?')
// Verify that we read in what we think.
//
int n_samples = data_set->getNSamples();
if (n_samples == 0) {
cerr << "Could not read file: " << csv_file_name << endl;
exit(-1);
} else {
cout << "Read " << n_samples << " samples from " << csv_file_name << endl;
}

// Split the data, so that 90% is train data
//
data_set->setTrainTestSplitRatio(0.90, false);
int n_train_samples = data_set->getNTrainSamples();
int n_test_samples = data_set->getNTestSamples();
cout << "Found " << n_train_samples << " Train Samples, and "
<< n_test_samples << " Test Samples" << endl;

// Create a DTrees classifier.
//
cv::Ptr<cv::ml::RTrees> dtree = cv::ml::RTrees::create();
// set parameters
//
// These are the parameters from the old mushrooms.cpp code
// Set up priors to penalize "poisonous" 10x as much as "edible"
//
float _priors[] = {1.0, 10.0};
cv::Mat priors(1, 2, CV_32F, _priors);
dtree->setMaxDepth(8);
dtree->setMinSampleCount(10);
dtree->setRegressionAccuracy(0.01f);
dtree->setUseSurrogates(false /* true */);
dtree->setMaxCategories(15);
dtree->setCVFolds(0 /*10*/); // nonzero causes core dump
dtree->setUse1SERule(true);
dtree->setTruncatePrunedTree(true);
// dtree->setPriors( priors );
dtree->setPriors(cv::Mat()); // ignore priors for now...
// Now train the model
// NB: we are only using the "train" part of the data set
//
dtree->train(data_set);

// Having successfully trained the data, we should be able
// to calculate the error on both the training data, as well
// as the test data that we held out.
//
cv::Mat results;
float train_performance = dtree->calcError(data_set,
false, // use train data
results // cv::noArray()
);
std::vector<cv::String> names;
data_set->getNames(names);
Mat flags = data_set->getVarSymbolFlags();

// Compute some statistics on our own:
//
{
cv::Mat expected_responses = data_set->getResponses();
int good = 0, bad = 0, total = 0;
for (int i = 0; i < data_set->getNTrainSamples(); ++i) {
float received = results.at<float>(i, 0);
float expected = expected_responses.at<float>(i, 0);
cv::String r_str = names[(int)received];
cv::String e_str = names[(int)expected];
cout << "Expected: " << e_str << ", got: " << r_str << endl;
if (received == expected)
good++;
else
bad++;
total++;
}
cout << "Correct answers: " <<(float(good)/total) <<" % " << endl;
cout << "Incorrect answers: " << (float(bad) / total) << "%"
<< endl;
}
float test_performance = dtree->calcError(data_set,
true, // use test data
results // cv::noArray()
);
cout << "Performance on training data: " << train_performance << "%" << endl;
cout << "Performance on test data: " <<test_performance <<" % " <<endl;
return 0;
}
7 changes: 7 additions & 0 deletions mushroom/Index
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Index of mushroom

02 Dec 1996 193 Index
25 Jun 1990 111577 expanded.Z
26 Feb 1990 4167 agaricus-lepiota.names
30 May 1989 853 README
30 May 1989 373704 agaricus-lepiota.data
Loading

0 comments on commit d1057c4

Please sign in to comment.