OpenCV getting started guide
NXP eIQ™ Machine Learning Software Development Environment for i.MX Applications Processors, Rev. 2, 06/2019
18 NXP Semiconductors
Figure 11. PCA graphics output
4.2.4.
Logistic regression
In this example, logistic regression is used to predict two characters (0 or 1) from an image. Every image
matrix is reshaped from its original size of 28 x 28 to 1 x 784. A logistic regression model is created and
trained on 20 images. After the training, the model can predict the labels of test images. The source code
is at this link and can be run using the below command.
Demo dependencies (preparing the train data files):
$: wget raw.githubusercontent.com/opencv/opencv/4.0.1/samples/data/data01.xml
After running the demo, the graphics result is shown on the screen (Qt 5 support is required):
$: ./example_cpp_logistic_regression
Result:
• The training and test data and the comparison between the original and predicted labels are
shown. The trained model reaches 95 % accuracy. The console text output is as follows:
original vs predicted:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1]
accuracy: 95%
saving the classifier to NewLR_Trained.xml
loading a new classifier from NewLR_Trained.xml
predicting the dataset using the loaded classifier...done!
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1]
accuracy: 95%