How to obtain 256 bins/pixels from an LBP image? - c

I am implementing uniform LBP in C. But I am confused regarding the concept. I have implemented LBP. Suppose i have an image of 512*512 dimensions. After LBP it will be 510*510. Now how to get 256 bins/pixels from this LBP image.
for(i=1; i < image_src->width - 1; i++)
{
for(j=1; j < image_src->height - 1; j++)
{
const unsigned char center = image_get_pixel_value(image_src, i, j , 0);
unsigned char code = 0;
if(center <= image_get_pixel_value(image_src, i-1, j-1 , 0))
code += 128;
if(center <= image_get_pixel_value(image_src, i-1, j , 0))
code += 64;
if(center <= image_get_pixel_value(image_src, i-1, j+1 , 0))
code += 32;
if(center <= image_get_pixel_value(image_src, i, j+1 , 0))
code += 16;
if(center <= image_get_pixel_value(image_src, i+1, j+1 , 0))
code += 8;
if(center <= image_get_pixel_value(image_src, i+1, j , 0))
code += 4;
if(center <= image_get_pixel_value(image_src, i+1, j-1 , 0))
code += 2;
if(center <= image_get_pixel_value(image_src, i, j-1 , 0))
code += 1;
image_set_pixel_value(image_tar, i-1, j-1, 0, code);
}
}
And this is the lookup table:
int UniformPattern59[16][16] = {
1, 2, 3, 4, 5, 0, 6, 7, 8, 0, 0, 0, 9, 0, 10, 11,
12, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 14, 0, 15, 16,
17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 20, 0, 21, 22,
23, 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,
24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 27, 0, 28, 29,
30, 31, 0, 32, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 34,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35,
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, 36,
37, 38, 0, 39, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 41,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42,
43, 44, 0, 45, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 47,
48, 49, 0, 50, 0, 0, 0, 51, 52, 53, 0, 54, 55, 56, 57, 58
};

I guess you may misunderstand the concepts about LBP. The LBP has couple of variants: basic LBP, uniform LBP and rotation-invariant uniform LBP.
In the basic LBP, we compare the gray-scale value between the centre pixel and one of its neighbour pixels (interpolated pixels for more accurate result) to get the binary coding (0 or 1) for the bit. Usually, we set the radius of this coding scheme as 1 and the number of neighbours as 8 as default configuration. Therefore, we can get the local binary pattern (LBP) for the centre pixel after comparing all the 8 neighbours, which is a 8-bit binary number, e.g., 01011110 or 11110000. Here, we can see that the range of this kind of LBP is from 0 to 255 if we convert the binary number to decimal number. We usually classify each pixel's LBP code into one of the 256 kinds of patterns to form the histogram for further classification or recognition tasks, which are the 256bins/pixels in your question.
However, there is something different in uniform and rotation-invariant uniform LBPs. That is, the number of patterns is not 256, where the rotation-invariant uniform LBP only has 10 kinds of patterns. The rotation-uniform LBP has 9 rotation-uniform LBPs and 1 others, in which the 9 rotation-uniform LBPs cover 90% patterns over an image usually.
So, in rotation-invariant uniform LBP, you only need to produce a 10-bins histogram for further processing, e.g., classification or recognition. First, you code each pixels as the rotation-invariant uniform LBP, which may produce an image.rows*image.cols matrix. Then, classifying each pattern (the matrix element) to one of the 10 patterns to form an array to represent the histogram.
Some papers for your information:
LBP on Scholarpedia, http://www.scholarpedia.org/article/Local_Binary_Patterns
C++ code by LBP authors, http://www.cse.oulu.fi/CMV/Downloads/LBPSoftware
Multiresolution gray-scale and rotation invariant texture classification with local binary patterns, http://www.rafbis.it/biplab15/images/stories/docenti/Danielriccio/Articoliriferimento/LBP.pdf

Related

Selecting best data structure for a 2-dimensional map that will shift the data

I need some help in the ways of implementing a faster way to do a full shift on a two-dimensional array.
My problem is I have a 2 dimensional array that has a game's map data. This game is kind of like Geometry Dash, but on the Game Boy Advance. So far, I have a map that looks something like this:
int map1[9][40] = {
{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, 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, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0 },
{0, 0, 0, 0, 2, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0 },
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 },
};
But to read this map, I have to loop through every single element and see if it should be drawn onto the screen (x >= 0 && x <= screen_width).
Giving it some thought, I think I should use a doubly-linked list. This way, I could shift the list by moving the header node to the trailer and then just draw the first 15 nodes' (or so) data. Would this be a performance improvement over looping through every element in the 2D-array? While the looping-through isn't necessarily a game-crushing performance drawback, I do want to optimize it.
If so, how would I go about implementing this doubly-linked list to contain the same data as the 2d-array?
First using a doubly-linked list will make the performance worser. Your current approach is the fastest that comes to my mind right now. First you'll need to understand how arrays and how linked lists work under the hood. For arrays I would highly suggest you to watch this 5 minute video because explaining this in text would take too long. After you understood how arrays work you'll need to compare it with a linked list. When accessing an element in the array, all what the computer does it just to increment a memory address by the index times the size of the array type and then de reference that address. When accessing or better say looping through a linked list, you'll do much more. First you will de reference the item, then you process the data, then you look if the next item is NULL, then you jump to the next item, de reference that one and so on.
To put it short, the thumb rule is: Only use linked lists if you don't know the length of the array.
Now to your question.
If you want to implement such a 2D linked list your struct will look like this:
struct LinkedList {
struct LinkedList* data;
struct LinkedList* prev;
struct LinkedList* next;
prev and next will just be pointers to the previous and next item. Where as data will be the actual data.
Here is a graphic that will hopefully explain what I mean:
So all in all your parsing method is better than a linked list. However I think the fastest parsing would be parsing on the fly.

Loop variables on arduino

I want do to a loop with some variables on my arduino , I have 24 variables ( ConfigSonde[0][3] to ConfigSonde[24][3] ) to change, i need do to a loop:
EX :
ConfigSonde_0[] = {'xxx', 3, 'A', 1, 0, 0, 0, 0, 0, 0};
ConfigSonde_1[] = {'xxx', 1, 'A', 1, 0, 0, 0, 0, 0, 0};
for (i = 0; i < 25; i = i + 1) { ConfigSonde_[i][3]=0;}
Is it possible ?
Thank
You are mixing ints and 'xxx'? (if it's supposed to be a string it must be "xxx"), and this is not legal in C.
ConfigSonde_[i][3] where i = 0 doesn't give you a reference to the variable ConfigSonde_0[3]
Maybe you are looking for:
int ConfigSonde_0[] = {'x', 3, 'A', 1, 0, 0, 0, 0, 0, 0};
int ConfigSonde_1[] = {'x', 1, 'A', 1, 0, 0, 0, 0, 0, 0};
int *ConfigSonde [] = {ConfigSonde_0, ConfigSonde_1};
for (i = 0; i < 2; i = i + 1) {ConfigSonde[i][3] = 0;}
or
int ConfigSonde[][10] = {
{'x', 3, 'A', 1, 0, 0, 0, 0, 0, 0},
{'x', 1, 'A', 1, 0, 0, 0, 0, 0, 0}
};
for (i = 0; i < 2; i = i + 1) {ConfigSonde[i][3] = 0;}
Rewrite: Do do what you need, you're going to have to either add additional variables that point at the things you want to change in the loop, or change your 25 distinct arrays to be a single 2-dimensional array.
You cannot dynamically "compute" names of variables to access their values in C (or C++), since these languages are compiled the names of variables are no longer relevant when the program runs.

SSE2: Multiplying signed integers from a 2d array with doubles and summing the results in C

I am currently trying to vectorize the following piece of code:
velocity[0] = 0.0;
velocity[1] = 0.0;
velocity[2] = 0.0;
for (int i = 0; i < PARAMQ; i++)
{
velocity[0] += currentCell[i] * LATTICEVELOCITIES[i][0];
velocity[1] += currentCell[i] * LATTICEVELOCITIES[i][1];
velocity[2] += currentCell[i] * LATTICEVELOCITIES[i][2];
}
where LATTICEVELOCITIES is an 2d integer array
static const int32_t LATTICEVELOCITIES[PARAMQ][3] = {{0, -1, -1},
{-1, 0, -1},
{0, 0, -1},
{1, 0, -1},
{0, 1, -1},
{-1, -1, 0},
{0, -1, 0},
{1, -1, 0},
{-1, 0, 0},
{0, 0, 0},
{1, 0, 0},
{-1, 1, 0},
{0, 1, 0},
{1, 1, 0},
{0, -1, 1},
{-1, 0, 1},
{0, 0, 1},
{1, 0, 1},
{0, 1, 1}
};
and currentCell is an array of doubles.
Unfortunately, all the examples I could find only deal with arrays of the same type and I don't know how to load only two integers into one 128bit register and then convert them to doubles.
Thanks for your help in advance.
Firstly, as per the comments above, I'm going to assume that it's OK to transpose LATTICEVELOCITIES:
static const int32_t LATTICEVELOCITIES[3][PARAMQ] = {
{ 0, -1, 0, 1, 0, -1, 0, 1, -1, 0, 1, -1, 0, 1, 0, -1, 0, 1, 0 },
{ -1, 0, 0, 0, 1, -1, -1, -1, 0, 0, 0, 1, 1, 1, -1, 0, 0, 0, 1 },
{ -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 }
};
Now let's iterate through the data, processing two elements per iteration, and have a single scalar iteration at the end to take care of the last (odd) element:
__m128d v0, v2, v2;
v0 = v1 = v2 = _mm_setzero_pd();
for (int i = 0; i < PARAMQ - 1; i += 2)
{
__m128d vc, vl0, vl1, vl2;
__m128i vtemp;
vc = _mm_loadu_pd(&currentCell[i]);
vtemp = _mm_loadu_si128((__m128i *)&LATTICEVELOCITIES[0][i]);
vl0 = _mm_cvtepi32_pd(vtemp);
vtemp = _mm_loadu_si128((__m128i *)&LATTICEVELOCITIES[1][i]);
vl1 = _mm_cvtepi32_pd(vtemp);
vtemp = _mm_loadu_si128((__m128i *)&LATTICEVELOCITIES[2][i]);
vl2 = _mm_cvtepi32_pd(vtemp);
v0 = _mm_add_pd(v0, _mm_mul_pd(vc, vl0));
v1 = _mm_add_pd(v1, _mm_mul_pd(vc, vl1));
v2 = _mm_add_pd(v2, _mm_mul_pd(vc, vl2));
}
v0 = _mm_hadd_pd(v0, v0);
v1 = _mm_hadd_pd(v1, v1);
v2 = _mm_hadd_pd(v2, v2);
_mm_store_sd(&velocity[0], v0);
_mm_store_sd(&velocity[1], v1);
_mm_store_sd(&velocity[2], v2);
if (i < PARAMQ)
{
velocity[0] += currentCell[i] * LATTICEVELOCITIES[0][i];
velocity[1] += currentCell[i] * LATTICEVELOCITIES[1][i];
velocity[2] += currentCell[i] * LATTICEVELOCITIES[2][i];
}
Note that this is completely untested code - apologies for typos or bugs that need to be fixed, but the basic idea should be sound.
Note also that you should test and benchmark this against equivalent scalar code - modern CPUs typically have two FPUs so there may not be much to be gained from SSE. If you can assume Sandy Bridge/Ivy Bridge/Haswell or later though, then an AVX/AVX2 implementation should do better.

Memory leak in C program, can't see where to free memory

I am writing a C program to generate Keys and test them in an encryption function. However since I have NEVER written a C program before, and I'm completely unused to having manage memory manually, I have run into a problem. I have a memory leak and to be honest, I don't know how to solve it. I understand I need to release the memory at some point but can't until I've run through all keys and I run out of memory before I get through all of them. Writing the program in a different language is NOT an option so please do not suggest that. The code that is leaking is shown below, any help would be appreciated.
EDIT: I know i haven't called a free function to free the memory. I don't see where I can put it because I need the memory until i run through all keys. Putting it outside the loops doesn't solve the problem because the leak occurs inside the loops
2nd EDIT: Posted the full program. I do not have the option of using data structures (i.e. the bool arrays) other than those shown because of how the DES encrypt function(which I did not write) works
#include <stdio.h>
#include <stdlib.h>
#include "des.h"
void dec2bin(bool *testaRR, bool *to_return, int convert);
int main(int argc, const char * argv[])
{
// insert code here...
bool testKey[56] = {
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1
};
bool testKey2[56] = {//intuitive key reversed for testing
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0
};
bool output[64];
bool input[64] = {//the reverse of below... DES bits are numbered left to right, in order of least to most significant so we must enter the bit values in reverse.
//forexample the binary vale of N is 01001110 but below is displayed as 01110010
1, 0, 0, 0, 1, 1, 0, 0,//1
0, 0, 0, 0, 1, 1, 0, 0,//0
1, 1, 0, 0, 0, 0, 1, 0,//C
1, 0, 1, 0, 0, 0, 1, 0,//E
1, 1, 0, 0, 1, 0, 1, 0,//S
0, 0, 1, 0, 1, 0, 1, 0,//T
1, 0, 1, 0, 0, 0, 1, 0,//E
0, 1, 1, 1, 0, 0, 1, 0 //N
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int i = y-4; i >= 0; i-=4)
printf("%X", input[i]+2*input[i+1]+4*input[i+2]+8*input[i+3]);//this is the conversion to hex code
printf(")\n");
/*
use char[] to store the key as set of
*/
/*bool input[64] = {//this is the given plaintext message in the intuitive order (opposite of what it is)
0, 1, 0, 0, 1, 1, 1, 0,//N
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 1, 0, 1, 0, 0,//T
0, 1, 0, 1, 0, 0, 1, 1,//S
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 0, 0, 0, 1, 1,//C
0, 0, 1, 1, 0, 0, 0, 0,//0
0, 0, 1, 1, 0, 0, 0, 1 //1
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int j = 0; j < y; j+=4)
printf("%X", input[j+3]+2*input[j+2]+4*input[j+1]+8*input[j]);//this is the conversion to hex code
printf(")\n");*/
bool test [8];
bool returned[8];
char keyphrase [8];
keyphrase[7] = 0;
for(int start = 65; start<=90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for(int two = 65; two<=90; two++){
keyphrase[1]=two;
for(int three = 65; three<=90; three++){
keyphrase[2]=three;
for(int four = 65; four<=90; four++){
keyphrase[3]=four;
for(int five = 65;five<=90;five++){
keyphrase[4]=five;
for( int six = 65; six <=90; six++){
keyphrase[5]=six;
for(int seven = 65; seven <=90; seven++){
keyphrase[6]=seven;
printf("%s \n", keyphrase);
}
}}
}
}
}
//once i fix the memory leak I will be calling the EncryptDes Function here and checking the outputblk agains the given cipher text
}
free(keyphrase);
int k = sizeof(testKey);
printf("(Test Key: ");
for (int z = 0; z < k; z+=7)
printf("%d", testKey[z+7]+2*testKey[z+6]+4*testKey[z+5]+8*testKey[z+4]+16*testKey[z+3]+32*testKey[z+2]+64*testKey[z+1]+ 128*testKey[z]);//this is the conversion to hex code
printf(")\n");
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
printf("(Output MSG: ");
for (int i = 0; i < x; i+=4)
printf("%X", output[i+3]+2*output[i+2]+4*output[i+1]+8*output[i]);//this is the conversion to hex code
printf(")\n");
return 0;
}
void dec2bin (bool *testaRR, bool *to_return, int convert)
{
printf("%d : ", convert);
printf("%c", convert);
printf("\n ");
//bool testaRR [8];
for(int st = 0; st<8; st++){
testaRR[st] = convert%2;
to_return[7-st] = testaRR[st];
//printf("%d :", 7-st);
//printf(" %d spot ", st);
convert = convert/2;
//testaRR stores the arrays in one direction
//to_return stores them in the other
//Example:
//65 = 01000001 testaRR least significant on the far right (m0st sig is in index 7)better for storage and keeping track of where the bits actually are in binary
//65 = 10000010 to_return least significant on the far left (same as DES) (most significant bit is index 0) good for printing to screen
}
You do not need dynamic memory management here.
Start with
char keyphrase[8];
keyphrase[7]=0;
instead of your malloc and you will be good to go. Your highest array index is 7 (the terminating NUL), and hence you need an array of 8 items (0..7).
If you really want to use malloc, simply a free() at the end will be fine, but you need to malloc 8 characters and set keyphrase[7] to 0 to do the terminating NUL still.
Here's a tested version that works:
#include <stdio.h>
/* compile with gcc -Wall -std=c99 keyphrase.c -o keyphrase */
int
main (int argc, char **argv)
{
char keyphrase[8];
keyphrase[7] = 0;
for (int start = 65; start <= 90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for (int two = 65; two <= 90; two++)
{
keyphrase[1] = two;
for (int three = 65; three <= 90; three++)
{
keyphrase[2] = three;
for (int four = 65; four <= 90; four++)
{
keyphrase[3] = four;
for (int five = 65; five <= 90; five++)
{
keyphrase[4] = five;
for (int six = 65; six <= 90; six++)
{
keyphrase[5] = six;
for (int seven = 65; seven <= 90; seven++)
{
keyphrase[6] = seven;
printf ("%s \n", keyphrase);
}
}
}
}
}
}
}
}
The real problem is the use of printf. You did not NULL terminate keyphrase, so every time you printf you overflow.
Also, to avoid the memory leak, simply replace char *keyphrase = (char *)malloc(7); with char keyphrase[8];.
You're calling malloc on the first line but I don't see a single free to release what you allocated. After all the loops are completed (i.e. usage of the allocated data is complete) you must call free(keyphrase);
New answer as the program has been modified.
You say your program is using all memory 'because I'm watching the available memory go down from over 4 free GB to about 5 MB'.
I'm guessing the answer is not the loops but these lines:
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
We can't see the source to or declaration of EncryptDES, but you aren't passing a length to it. If 0 is meant to be the length, this would explain it.
The next line however suggests that output is meant to be an array of 64 bytes (rather than 2 strings). But EncryptDES would have no way of knowing this.
I suggest you run the whole thing under valgrind to find out what is happening.

Arduino EEPROM write and read array?

When the Arduino is powered up it has an int array stored in the flash, for example:
int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
When the program button is pressed, it then waits for the piezo to pick up a knock and this array then changes to, for example:
int secretCode[maximumKnocks] = {25, 50, 25, 50, 100, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
(based on http://grathio.com/assets/secret_knock_detector.pde)
How would I write and read the array to/from the EEPROM? This is completely new to me, so any help would be great.
You would write the values using the EEPROM.Write function - loop over the array, writing each value in turn.
Assuming you don't need to store integer values > 254 ( in which case you'd have to write two bytes for each element in secretCode ), this would be:
for ( int i = 0; i < maximumKnocks; ++i )
EEPROM.write ( i, secretCode [ i ] );
Having written them, you would read them back on start-up using the read function in the setup. If the values in the EEPROM are 0xff, which they will be when you first flash the chip, don't copy them into the secret code.
if ( EEPROM.read ( 0 ) != 0xff )
for (int i = 0; i < maximumKnocks; ++i )
secretCode [ i ] = EEPROM.read ( i );

Resources