Skip to content

samples: Add C++ implementation of digits.py - #14260

Merged
opencv-pushbot merged 1 commit into
opencv:3.4from
allanrodriguez:sample-digits-cpp
Apr 23, 2019
Merged

opencv-pushbot merged 1 commit into
opencv:3.4from
allanrodriguez:sample-digits-cpp

Conversation

@allanrodriguez

@allanrodriguez allanrodriguez commented Apr 4, 2019 •

Copy link
Copy Markdown
Contributor

Currently, only a Python implementation of the SVM and KNearest digit recognition sample exists. This pull request adds a C++ implementation.

@allanrodriguez
allanrodriguez force-pushed the sample-digits-cpp branch 2 times, most recently from ca38319 to 17a15fd Compare April 5, 2019 18:29
Comment thread samples/cpp/digits.cpp Outdated
warpAffine(img, deskewed_img, M, Size(SZ, SZ), WARP_INVERSE_MAP | INTER_LINEAR);
}

class StatModel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this redundant (and misleading) ?
there's already a ml::StatModel, a ml::KNearest and a ml::SVM

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the Python implementation of this sample, which wraps all three classes, but I agree that it's redundant.

Comment thread samples/cpp/digits.cpp Outdated

void load(const char* fn)
{
_model->load<ml::KNearest>(fn);

@berak berak Apr 9, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't work (same problem with SVM).
Algorithm::load() is a static method, and returns a new instance,

see here for a usage example

Comment thread samples/cpp/digits.cpp
cout << "preprocessing..." << endl;

// shuffle digits
shuffle(digits, labels);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about KNearest, but the SVM will do internal shuffeling already

Comment thread samples/cpp/digits.cpp Outdated
" ./digits\n" << endl;
}

static void split2d(Mat& image, Size cell_size, vector<Mat>& cells)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, please use "const reference" for input parameters:

  • const Mat&
  • const std::vector<...>&
  • const Ptr<...>&

Comment thread samples/cpp/digits.cpp Outdated
{
for (int j = 0; j < width; j += sx)
{
cells.push_back(image(Rect(j, i, sx, sy)).clone());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.clone()

Do we need this? Is it required?

Comment thread samples/cpp/digits.cpp Outdated
for (int j = 0; j < 10; j++)
{
int val = confusion[i][j];
cout << (val / 10 > 0 ? "" : " ") << val << ' ';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use this here:

cout << cv::format("%2d ", val);

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allanrodriguez Thank you for the contribution 👍

@berak Thank you for reviewing!

@opencv-pushbot
opencv-pushbot merged commit 86561fe into opencv:3.4 Apr 23, 2019
@alalek alalek mentioned this pull request Apr 30, 2019
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants