Computing minimum and maximum excess of byte over binary function - c++

I have a function PI (input 0 or 1), which gives PI[0] = -1, PI[1] = 1.
Given a byte B, I would like to have a function computing the minimum excess over PI from left to right. Similarly, I need a function computing the maximum excess over PI from left to right. Example:
PI_MIN[0] = -8, PI_MAX[0] = -1
PI_MIN[1] = -7, PI_MAX[1] = -1
PI_MIN[2] = -6, PI_MAX[2] = -1
PI_MIN[3] = -6, PI_MAX[3] = -1
At the moment I precompute the function values, store these in a universal table, and access it at runtime. Or alternatively I compute the result naively (for loop over bits). For PI_MIN and PI_MAX we have:
static constexpr int8_t PI_MIN[] { -8, -7, -6, -6, -6, -5, -5, -5, -6, -5, -4, -4, -4, -4, -4,
-4, -6, -5, -4, -4, -4, -3, -3, -3, -4, -3, -3, -3, -3, -3, -3, -3, -6, -5, -4, -4, -4, -3, -3, -3, -4, -3, -2, -2,
-2, -2, -2, -2, -4, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -6, -5, -4, -4, -4, -3, -3, -3, -4,
-3, -2, -2, -2, -2, -2, -2, -4, -3, -2, -2, -2, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -4, -3, -2, -2, -2, -1,
-1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -6, -5, -4,
-4, -4, -3, -3, -3, -4, -3, -2, -2, -2, -2, -2, -2, -4, -3, -2, -2, -2, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1,
-4, -3, -2, -2, -2, -1, -1, -1, -2, -1, 0, 0, 0, 0, 0, 0, -2, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, -3,
-2, -2, -2, -1, -1, -1, -2, -1, 0, 0, 0, 0, 0, 0, -2, -1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, -2, -1, 0, 0, 0,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
static constexpr int8_t PI_MAX[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0, 0, 0, 1, 2, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0, 0, 0, 1,
2, 0, 0, 0, 0, 0, 0, 1, 2, 1, 1, 1, 2, 2, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0,
1, 2, 1, 1, 1, 2, 2, 2, 3, 4, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 3, 4, 2, 2, 2, 2, 2, 2, 3, 4, 3, 3, 3, 4, 4,
4, 5, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 3, 4, 1, 1, 1, 1,
1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 3, 4, 2, 2, 2, 2, 2, 2, 3, 4, 3, 3, 3, 4, 4, 4, 5, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 3, 4, 2, 2, 2, 2, 2, 2, 3, 4, 3, 3, 3, 4, 4, 4, 5, 6, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 4, 4, 4, 5, 6, 4, 4,
4, 4, 4, 4, 5, 6, 5, 5, 5, 6, 6, 6, 7, 8 };
Unfortunately I couldn't find a pattern for all functions I need to use (e.g. PI_MIN, PI_MAX, but there is more). The question is: how can I find out whether a function exists which can compute this in a non-naive way (i.e., no for loop from left to right in the input byte). My goal is to reach maximum performance, since this function is in the inner-most loop of a larger program.
I'm thankful for any hints!

A non-branching version of pi_min (assuming the loop is unrolled).
/*
Calculate:
min(
pi(b7),
pi(b7)+pi(b6),
pi(b7)+pi(b6)+pi(b5),
pi(b7)+pi(b6)+pi(b5)+pi(b4),
pi(b7)+pi(b6)+pi(b5)+pi(b4)+pi(b3),
pi(b7)+pi(b6)+pi(b5)+pi(b4)+pi(b3)+pi(b2),
pi(b7)+pi(b6)+pi(b5)+pi(b4)+pi(b3)+pi(b2)+pi(b1),
pi(b7)+pi(b6)+pi(b5)+pi(b4)+pi(b3)+pi(b2)+pi(b1)+pi(b0))
Where,
pi(b) = b ? 1 : -1
and bits in byte b are numbered with the least significant bit (LSB) as 0.
This problem is essentially one of counting leading zeros where a string
of leading zeros may be interrupted by a one if it is eventually followed
by a zero. What happens if there are no leading zeros, then the count is -1.
The algorithm uses two stacks, "c0" and "c1". c0 is the leading zero count
and c1 is a stack of potentially intervening 1's.
foreach bit (following 4 cases are mutually exclusive, only 1 will execute)
0: if the '1' stack is empty => push a '0' onto the '0' stack
0: if the '1' stack is not empty => pop a '1'
1: if the first bit is a '1' => put the '0' stack in underflow state
1: if it is not the first bit => push a '1' onto the '1' stack
return -c0 because zeros actually count as -1
*/
int pi_min(uint8_t byte) {
int c0 = 0;
int c1 = 0;
for (int i = 0; i < 8; ++i) {
uint8_t b = !!(byte & (1 << (7-i)));
c0 -= (b & (i == 0));
c0 += ((!b) & (0 >= c1));
c1 -= ((!b) & (0 < c1));
c1 += (b & (i != 0));
}
return -c0;
}
int pi_max(uint8_t byte) { return -pi_min(~byte); }
// The obvious version for comparison.
int pi(uint8_t bit) { return bit ? 1 : -1; }
int pi_min_simple(uint8_t byte) {
int sum = 0;
int m = 9;
for (int i = 0; i < 8; ++i) {
uint8_t b = byte & (1 << (7-i));
sum += pi(b);
m = std::min(m, sum);
}
return m;
}

Sorry for the delay, I have measured the performance of the different methods now.
http://s12.postimg.org/v400xibxp/prefix_Sums.png
I am very glad to see that the solution suggested by Adam Burry is very efficient (the yellow line). As you can see, even the naive algorithm is slightly faster than the table lookup (green and brown lines) for both minimum and maximum prefix sum computation, which are indeed very similar... The most surprising thing (at least for me) is the terrible performance of maxExcess (which simply returned -pi_min(~byte), as Adam Burry suggested, where pi_min is the function representing the yellow line). I guess that it has to do with the additional overhead to compute the binary complement of each and every byte being analyzed, so I will switch to the original algorithm (pi_min) and return -c1 instead to implement pi_max.

Related

How to use cv::Mat in python?

I'm working on a eye pupil detection project. I found this link for eye center tracking using image gradient method provided in this link.
link- http://thume.ca/projects/2012/11/04/simple-accurate-eye-center-tracking-in-opencv/
He has implemented the idea in c++ and i want to convert it into python code. Everything is going smooth until i get to this void createCornerKernel() function.
cv::Mat *leftCornerKernel;
cv::Mat *rightCornerKernel;
// not constant because stupid opencv type signatures
float kEyeCornerKernel[4][6] = {
{-1,-1,-1, 1, 1, 1},
{-1,-1,-1,-1, 1, 1},
{-1,-1,-1,-1, 0, 3},
{ 1, 1, 1, 1, 1, 1},
};
void createCornerKernels() {
rightCornerKernel = new cv::Mat(4,6,CV_32F,kEyeCornerKernel);
leftCornerKernel = new cv::Mat(4,6,CV_32F);
// flip horizontally
cv::flip(*rightCornerKernel, *leftCornerKernel, 1);
}
How would i convert this cv::mat(4, 6, CV_32F, kEyeCornerKernel) in python?
Any help will be appreciated.
rightCornerKernel = np.array([[-1, -1, -1, 1, 1, 1],
[-1, -1, -1, -1, 1, 1],
[-1, -1, -1, -1, 0, 3],
[1, 1, 1, 1, 1, 1]])
I've been working a bit more on this just to find the solution. As #stormzhou suggested i finally able to solve the problem.
Answering my own question, void createCornerkernels() function in python would be
def createCornerkernels():
leftCornerkernel = None
rightCornerKernel = np.array([[-1, -1, -1, 1, 1, 1],
[-1, -1, -1, -1, 1, 1],
[-1, -1, -1, -1, 0, 3],
[1, 1, 1, 1, 1, 1]])
leftCornerKernel = cv2.flip(rightCornerkernel, 1)
return leftCornerKernel, rightCornerKernel

how to print 2-D array row by row in GDB?

map[22][22];
I want to see 'map(2-D array)' by GDB and the result was like this
$1 = {{-1 repeats 22 times}, {-1, 4, 4, 4, 4, 2, 3, 2, 1, 0, 4, -1 repeats 11 times}, {-1, 1, 1, 2, 2, 5, 2, 0, 0, 0, 2, -1 repeats 11 times}, {-1, 3, 0, 0, 1, 1, 1, 0, 0, 0, 0, -1 repeats 11 times}, {-1, 1, 0, 0, 0, 0, 0, -1, 4, 4, 1, -1 repeats 11 times}, {-1, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, -1 repeats 11 times}, {-1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1 repeats 11 times}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 repeats 11 times}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 repeats 11 times}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 repeats 11 times}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 repeats 11 times>}, {-1 repeats 22 times} repeats 11 times>}
and it was so unuseful to see..
I want to see like this
{-1,-1,-1,-1,-1,-1,-1,1}
{1,2,3,4,5,2,4,}
{2,1,4,5,3,4,2,2}
...
can you tell how to print 2-D array row by row??
I want to see like this
There are 2 ways to achieve this:
Implement debug_print() function in your program, call it from GDB with the call command.
Implement Python pretty-printer. Documentation. Tutorial.
The first solution is trivial to implement, but (unlike the second) doesn't work when you don't have a live process (e.g. for core postmortem debugging).

Create Set of Every Permutation and Sign Variation of Members of a Set (C++)

Suppose I am given a vector of unknown size. For example, it could be
std::vector<int> myset1({1, 2, 3});
Or it could be anything else. That's just an example. Now, suppose I want to write a function that returns a vector of vectors. However, each vector in this vector must be distinct permutations of the original. So in this case, I would expect the function to return a vector containing the sets {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, and {3, 2, 1} (pay no attention to the ordering).
That might be straightforward with some kind of recursion. But what if I also wanted to also account for the sign of each element? So for example:
std::vector<int> myset2({1, 2});
I would expect the function to return {1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1} (I'm not concerned with the ordering).
I'm struggling to think of a way to design this elegantly. As you can imagine, with larger sets, it becomes a necessity to use such a function rather than to list out each set by hand, but no ideas are coming to my head at the moment. How would you achieve this?
First attempt:
#include <vector>
#include <algorithm>
#include <iostream>
std::vector<std::vector<int>> all_permutations(std::vector<int> input)
{
std::vector<std::vector<int>> result;
std::sort(begin(input), end(input));
input.erase(std::unique(begin(input), end(input)), end(input));
do {
result.push_back(input);
} while(std::next_permutation(begin(input), end(input)));
return result;
}
template<class T>
void emit(std::ostream& os, std::vector<T> const& v)
{
os << " [";
const char* sep = " ";
for (auto&& x : v) {
os << sep << x;
sep = ", ";
}
os << "]\n";
}
template<class T>
void emit(std::ostream& os, std::vector<std::vector<T>> const& v)
{
os << "[\n";
for (auto&& x : v) {
emit(os, x);
}
os << "]\n";
}
int main()
{
emit(std::cout, all_permutations({ 1, 2, 3 }));
}
expected output;
[
[ 1, 2, 3]
[ 1, 3, 2]
[ 2, 1, 3]
[ 2, 3, 1]
[ 3, 1, 2]
[ 3, 2, 1]
]
Now add code for plus and minus:
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
std::vector<std::vector<int>> all_permutations(std::vector<int> input)
{
std::vector<std::vector<int>> result;
std::sort(begin(input), end(input));
input.erase(std::unique(begin(input), end(input)), end(input));
do {
result.push_back(input);
} while(std::next_permutation(begin(input), end(input)));
return result;
}
template<class T>
void emit(std::ostream& os, std::vector<T> const& v)
{
os << " [";
const char* sep = " ";
for (auto&& x : v) {
os << sep << x;
sep = ", ";
}
os << "]\n";
}
template<class T>
void emit(std::ostream& os, std::vector<std::vector<T>> const& v)
{
os << "[\n";
for (auto&& x : v) {
emit(os, x);
}
os << "]\n";
}
std::vector<int> plus_and_minus(std::vector<int> v)
{
std::vector<int> inverse;
inverse.reserve(v.size());
std::transform(begin(v), end(v), back_inserter(inverse), [](auto&& x) { return -x; });
v.insert(end(v), begin(inverse), end(inverse));
sort(begin(v), end(v));
inverse.erase(unique(begin(v), end(v)), end(v));
return v;
}
int main()
{
emit(std::cout, all_permutations(plus_and_minus({ 1, 2, 3 })));
}
expected:
[
[ -3, -2, -1, 1, 2, 3]
[ -3, -2, -1, 1, 3, 2]
[ -3, -2, -1, 2, 1, 3]
[ -3, -2, -1, 2, 3, 1]
[ -3, -2, -1, 3, 1, 2]
[ -3, -2, -1, 3, 2, 1]
[ -3, -2, 1, -1, 2, 3]
[ -3, -2, 1, -1, 3, 2]
[ -3, -2, 1, 2, -1, 3]
[ -3, -2, 1, 2, 3, -1]
[ -3, -2, 1, 3, -1, 2]
[ -3, -2, 1, 3, 2, -1]
[ -3, -2, 2, -1, 1, 3]
[ -3, -2, 2, -1, 3, 1]
[ -3, -2, 2, 1, -1, 3]
[ -3, -2, 2, 1, 3, -1]
[ -3, -2, 2, 3, -1, 1]
[ -3, -2, 2, 3, 1, -1]
[ -3, -2, 3, -1, 1, 2]
[ -3, -2, 3, -1, 2, 1]
[ -3, -2, 3, 1, -1, 2]
[ -3, -2, 3, 1, 2, -1]
[ -3, -2, 3, 2, -1, 1]
[ -3, -2, 3, 2, 1, -1]
[ -3, -1, -2, 1, 2, 3]
[ -3, -1, -2, 1, 3, 2]
[ -3, -1, -2, 2, 1, 3]
[ -3, -1, -2, 2, 3, 1]
[ -3, -1, -2, 3, 1, 2]
[ -3, -1, -2, 3, 2, 1]
[ -3, -1, 1, -2, 2, 3]
[ -3, -1, 1, -2, 3, 2]
[ -3, -1, 1, 2, -2, 3]
[ -3, -1, 1, 2, 3, -2]
[ -3, -1, 1, 3, -2, 2]
[ -3, -1, 1, 3, 2, -2]
[ -3, -1, 2, -2, 1, 3]
[ -3, -1, 2, -2, 3, 1]
[ -3, -1, 2, 1, -2, 3]
[ -3, -1, 2, 1, 3, -2]
[ -3, -1, 2, 3, -2, 1]
[ -3, -1, 2, 3, 1, -2]
[ -3, -1, 3, -2, 1, 2]
[ -3, -1, 3, -2, 2, 1]
[ -3, -1, 3, 1, -2, 2]
[ -3, -1, 3, 1, 2, -2]
[ -3, -1, 3, 2, -2, 1]
[ -3, -1, 3, 2, 1, -2]
[ -3, 1, -2, -1, 2, 3]
[ -3, 1, -2, -1, 3, 2]
[ -3, 1, -2, 2, -1, 3]
[ -3, 1, -2, 2, 3, -1]
[ -3, 1, -2, 3, -1, 2]
[ -3, 1, -2, 3, 2, -1]
[ -3, 1, -1, -2, 2, 3]
[ -3, 1, -1, -2, 3, 2]
[ -3, 1, -1, 2, -2, 3]
[ -3, 1, -1, 2, 3, -2]
[ -3, 1, -1, 3, -2, 2]
[ -3, 1, -1, 3, 2, -2]
[ -3, 1, 2, -2, -1, 3]
[ -3, 1, 2, -2, 3, -1]
[ -3, 1, 2, -1, -2, 3]
[ -3, 1, 2, -1, 3, -2]
[ -3, 1, 2, 3, -2, -1]
[ -3, 1, 2, 3, -1, -2]
[ -3, 1, 3, -2, -1, 2]
[ -3, 1, 3, -2, 2, -1]
[ -3, 1, 3, -1, -2, 2]
[ -3, 1, 3, -1, 2, -2]
[ -3, 1, 3, 2, -2, -1]
[ -3, 1, 3, 2, -1, -2]
[ -3, 2, -2, -1, 1, 3]
[ -3, 2, -2, -1, 3, 1]
[ -3, 2, -2, 1, -1, 3]
[ -3, 2, -2, 1, 3, -1]
[ -3, 2, -2, 3, -1, 1]
[ -3, 2, -2, 3, 1, -1]
[ -3, 2, -1, -2, 1, 3]
[ -3, 2, -1, -2, 3, 1]
[ -3, 2, -1, 1, -2, 3]
...etc
http://coliru.stacked-crooked.com/a/82a4c5784dc0070d
Just for exposition, another way. This time using a generator object which offers an iterator-based approach.
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <ciso646>
template<class Vector> struct permutation_engine;
template<class T>
struct permutation_engine<std::vector<T>>
{
using perm_vector = std::vector<T>;
template<class VectorArg>
permutation_engine(VectorArg&& arg) : current_permutation(std::forward<VectorArg>(arg)) {}
struct iterator
{
using value_type = const perm_vector;
using reference = perm_vector&;
using pointer = perm_vector*;
using difference_type = int;
using iterator_category = std::input_iterator_tag;
reference operator*() const { return parent->current_permutation; }
auto operator != (iterator const& r) const -> bool {
return parent != r.parent;
}
auto operator++() {
if(not parent->advance()) {
parent = nullptr;
}
return *this;
}
permutation_engine* parent;
};
iterator begin()
{
reset();
return iterator { this };
}
iterator end() { return iterator { nullptr }; }
bool advance() {
return next_permutation(Begin(), End());
}
void reset() {
sort(Begin(), End());
current_permutation.erase(unique(Begin(), End()), End());
}
private:
auto Begin() { return std::begin(current_permutation); }
auto End() { return std::end(current_permutation); }
std::vector<T> current_permutation;
};
template<class Vector>
auto make_permutation_engine(Vector&& vector)
{
return permutation_engine<std::decay_t<Vector>>(std::forward<Vector>(vector));
}
template<class T>
void emit(std::ostream& os, std::vector<T> const& v)
{
os << " [";
const char* sep = " ";
for (auto&& x : v) {
os << sep << x;
sep = ", ";
}
os << "]\n";
}
std::vector<int> append_negatives(std::vector<int> v)
{
using negateElement = std::negate<>;
v.reserve(v.size() * 2);
std::transform(begin(v), end(v),
back_inserter(v),
negateElement());
return v;
}
int main()
{
std::cout << "[\n";
for(auto&& vec : make_permutation_engine(append_negatives({ 1, 2, 3 })))
{
emit(std::cout, vec);
}
std::cout << "]\n";
}

Assigning to an array from an initializer list [duplicate]

This question already has answers here:
Error: Assigning to an array from an initializer list
(2 answers)
Closed 9 years ago.
I've checked on SO already for a simple way to fix this error. I didn't get this when compiling on another computer but suddenly now it's not compiling on my PC. Here's the error I'm getting:
Error: Assigning to an array from an initializer list
And here's the code:
int maze[12][12];
void print(bool playing);
int main()
{
printMaze(false);
playGame();
return 0;
}
void print(bool playing)
{
if (!playing) maze = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 3},
{1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
}
It might also be worth mentioning that I get a warning on the same line:
Warning: Extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
I know that clearly means I have to use one of these two to use extended initializer lists, but have no idea what to do to resolve the matter.
Edit:
Having g++ follow the C++11 ISO C++ language standard in the settings removes the warning, but not the error.
What do your compilations steps look like? The warning is fairly clear: you are trying to use a feature that requires -std=c++11 or -std=gnu++11, and although that is apparently enabled by default, it is possible that you have overridden it (i.e. explicitly turned it off) somehow. You should examine your compilation process closer and make sure you aren't preventing that feature from being allowed.
A workaround is to use the old-style C function memcpy. This will work with older compilers.
int maze[12][12];
void printMaze(bool playing);
int main()
{
printMaze(false);
playGame();
return 0;
}
void printMaze(bool playing)
{
static int maze1[12][12] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 3},
{1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
if (!playing) memcpy(maze, maze1, 12*12*sizeof(int));
}

how to create own matrix in openCV

Hi in the following link http://www.prism.gatech.edu/~ahuaman3/docs/OpenCV_Docs/tutorials/basic_0/basic_0.html it is shown how to create a uniform matrix where all elements are 23.
How can I create a matrix of
-1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2,
0, 0, 0, 0, 0, 0, 0,
-1, -1, -1, -1, -1, -1, -1,
in openCV. I want the user to input the no. of rows of 2. How I can do that?
uchar mydata[]={1, 2, 1, 1, 2, 1, 1, 2, 1};
cv::Mat mymat(3,3,CV_8UC1,mydata);
mymat:
1 2 1
1 2 1
1 2 1