NumCpp  1.0
A C++ implementation of the Python Numpy library
InterfaceWithOpenCV.cpp

Example for interfaceing with OpenCV Mat

#include "NumCpp.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main()
{
// create a random image with NumCpp
auto ncArray = nc::random::randInt<nc::uint8>({ 500, 500 }, 0, nc::DtypeInfo<nc::uint8>::max());
// convert to OpenCV Mat
auto cvArray = cv::Mat(ncArray.numRows(), ncArray.numCols(), CV_8SC1, ncArray.data());
// display the OpenCV Mat
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE); // Create a window for display.
cv::imshow("Display window", cvArray); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window
// tranpose the Mat with OpenCV
auto transposedCvArray = cv::Mat(cvArray.cols, cvArray.rows, CV_8SC1);
cv::transpose(cvArray, transposedCvArray);
// display the transposed Mat
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE); // Create a window for display.
cv::imshow("Display window", transposedCvArray); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window
// convert the transposed OpenCV Mat to a NumCpp array
auto transposedNcArray = nc::NdArray<nc::uint8>(transposedCvArray.data, transposedCvArray.rows, transposedCvArray.cols);
// make sure the two transposed arrays are the same
if (nc::array_equal(transposedNcArray, ncArray.transpose()))
{
std::cout << "Arrays are equal.\n";
}
else
{
std::cout << "Arrays are not equal.\n";
}
return 0;
}
NumCpp.hpp
nc::NdArray
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:75
nc::array_equal
bool array_equal(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2) noexcept
Definition: array_equal.hpp:49
nc::DtypeInfo::max
static constexpr dtype max() noexcept
Definition: DtypeInfo.hpp:111
nc::transpose
NdArray< dtype > transpose(const NdArray< dtype > &inArray)
Definition: transpose.hpp:48