Related
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
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).
Given a number, n, I need to efficiently find how many times this number is a multiple of all powers of 4 less than the given number.
For examples:
16 is a multiple of 4, and 16, so the result would be 2.
64 is a multiple of 4, 16, and 64, so the result would be 3.
256 is a multiple of 4, 16, 64, and 256, so the result would be 4.
14 is not a multiple of any power of 4, so the result would be 0.
35 is not a multiple of any power of 4, so the result would be 0.
Bitwise operations are preferred, and this is in a very tight loop so it is inside of a bottleneck that needs to be efficient. My code at the moment is the obvious answer, but I have to believe there is something more mathematical that can figure out the result in less steps:
power = 4;
while (power < n) {
result += !(n & (power - 1));
power *= 4;
}
You could use logarithms. A quick Google search for "fast log2 c++" brought up a pretty long list of ideas. Then your answer is log2(x)/2, and you'd have to find some way to make sure that your result is a whole number if you only want an answer for exact powers of 4.
If you are programming for an x86 processor, you can use BitScanForward & BitScanReverse to find the set bit, and use it to compute log2. The following code works in Visual Studio, for GCC or others, there are other ways to do inline assembly.
uint32_t exact_power_of_4_scan(uint32_t num)
{
unsigned long reverse;
unsigned long forward;
if (!_BitScanReverse(&reverse, num)) return 0;
_BitScanForward(&forward, num);
if (reverse != forward) return 0; // makes sure only a single bit is set
if (reverse & 0x1) return 0; // only want every other power of 2
return reverse / 2;
}
If you need a portable solution, table lookup might be the way to go, but is more complicated.
uint8_t not_single_bit[256] = {
1, 0, 0, 1, 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,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
uint8_t log2_table[256] = {
0, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
uint32_t exact_power_of_2(uint32_t num)
{
auto a = not_single_bit[num & 0xff];
auto b = not_single_bit[(num >> 8) & 0xff];
auto c = not_single_bit[(num >> 16) & 0xff];
auto d = not_single_bit[(num >> 24) & 0xff];
if (a + b + c + d != 3) {
return 0;
}
if (!a) {
return log2_table[num & 0xff];
}
if (!b) {
return log2_table[(num >> 8) & 0xff] + 8;
}
if (!c) {
return log2_table[(num >> 16) & 0xff] + 16;
}
return log2_table[(num >> 24) & 0xff] + 24;
}
uint32_t exact_power_of_4(uint32_t num)
{
auto ret = exact_power_of_2(num);
if (ret & 0x1) return 0;
return ret / 2;
}
Both are linear algorithms. The first will probably beat out looping for almost any value of num, but I haven't tested it. The second is probably only good for largish nums.
The mathematics would be to keep dividing by 4 until the result is no longer divisible by 4.
If you really want to do it with bitwise operations, techniques here can be used to count the number of trailing zero bits (i.e. the number of times a value is divisible by 2). Those can be adjusted to count pairs of trailing bits (i.e. divisibility by a power of 4 rather than 2).
Note that you will need to work with unsigned values to avoid certain cases of undefined or unspecified behaviours.
I would dispute your assertion that bitwise operations will make for a more efficient solution. It is not a given without testing, particularly with modern compilers.
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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 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.
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));
}