I have a pointer to a struct and I can print it in gdb using print /x (*s).
However this gives me:
..., intensity = 2 '\002'}, {distance_2cm = 3045, intensity = 3 '\003'}, {distance_2cm = 14735, intensity = 7 '\a'}, {dis
tance_2cm = 3345, intensity = 3 '\003'}, {distance_2cm = 14272, intensity = 7 '\a'}, {distance_2cm = 3735, intensity =
2 '\002'}, {distance_2cm = 14617, intensity = 6 '\006'}, {distance_2cm = 4239, intensity = 2 '\002'}, {distance_2cm =
9741, intensity = 5 '\005'}, {distance_2cm = 4892, intensity = 2 '\002'}, {distance_2cm = 10686, intensity = 4 '\004'
}, {distance_2cm = 6012, intensity = 3 '\003'}, {distance_2cm = 9906, intensity = 3 '\003'}}}, {head = {laser_block_id
= 61183, rotational_theta = 21191}, lasers_arary = {{distance_2cm = 0, intensity = 2 '\002'}, {distance_2cm = 0, inte
nsity = 1 '\001'}, {distance_2cm = 0, intensity = 3 '\003'}, {distance_2cm = 0, intensity = 1 '\001'}, {distance_2cm =
0, intensity = 2 '\002'}, {distance_2cm = 0, intensity = 2 '\002'}, {distance_2cm = 0, intensity = 1 '\001'}, {distan
ce_2cm = 6426, intensity = 1 '\001'}, {distance_2cm = 2040, intensity = 3 '\003'}, {distance_2cm = 6584, intensity = 2
'\002'}, {distance_2cm = 2154, intensity = 3 '\003'}, {distance_2cm = 0, intensity = 1 '\001'}, {distance_2cm = 2282,
intensity = 3 '\003'}, {distance_2cm = 0, intensity = 1 '\001'}, {distance_2cm = 2428, intensity = 2 '\002'}, {distan
ce_2cm = 26580, intensity = 33 '!'}, {distance_2cm = 2592, intensity = 5 '\005'}, {distance_2cm = 27180, intensity = 2
7 '\033'}, {distance_2cm = 2795, intensity = 3 '\003'}, {distance_2cm = 0, intensity = 2 '\002'}, {distance_2cm = 3045
, intensity = 3 '\003'}, {distance_2cm = 14712, intensity = 22 '\026'}, {distance_2cm = 3342, intensity = 3 '\003'}, {
distance_2cm = 14181, intensity = 7 '\a'}, {distance_2cm = 3729, intensity = 2 '\002'}, {distance_2cm = 14474, intensi
ty = 6 '\006'}, {distance_2cm = 4228, intensity = 2 '\002'}, {distance_2cm = 9744, intensity = 1 '\001'}, {distance_2c
m = 4887, intensity = 2 '\002'}, {distance_2cm = 13075, intensity = 7 '\a'}, {distance_2cm = 5994, intensity = 3 '\003
'}, {distance_2cm = 9947, intensity = 4 '\004'}}}, {head = {laser_block_id = 61183, rotational_theta = 21207}, lasers_
arary = {{distance_2cm = 0, intensity = 2 '\002'}, {distance_2cm = 0, intensity = 1 '\001'}, {distance_2cm = 0, intens
ity = 3 '\003'}, {distance_2cm = 0, intensity = 1 '\001'}, {distance_2cm = 0, intensity = 2 '\002'}, {distance_2cm = 0
, intensity = 2 '\002'}, {distance_2cm = 0, intensity = 1 '\001'}, {distance_2cm = 0, intensity = 1 '\001'}, {distance
_2cm = 2038, intensity = 6 '\006'}, {distance_2cm = 6548, intensity = 1 '\001'}, {distance_2cm = 2146, intensity = 3 '
\003'}, {distance_2cm = 0, intensity = 1 '\001'}, {distance_2cm = 2285, intensity = 3 '\003'}, {distance_2cm = 0, inte
nsity = 1 '\001'}, {distance_2cm = 2428, intensity = 2 '\002'}, {distance_2cm = 26498, intensity = 33 '!'}, {distance_
2cm = 2590, intensity = 5 '\005'}, {distance_2cm = 26177, intensity = 29 '\035'}, {distance_2cm = 2792, intensity = 3
'\003'}, {distance_2cm = 0, intensity = 2 '\002'}, {distance_2cm = 3044, intensity = 3 '\003'}, {distance_2cm = 14743,
intensity = 7 '\a'}, {distance_2cm = 3344, intensity = 3 '\003'}, {distance_2cm = 14147, intensity = 7 '\a'}, {distan
ce_2cm = 3728, intensity = 2 '\002'}, {distance_2cm = 14520, intensity = 13 '\r'}, {distance_2cm = 4222, intensity = 2
'\002'}, {distance_2cm = 14567, intensity = 5 '\005'}, {distance_2cm = 4885, intensity = 3 '\003'}, {distance_2cm = 1
0842, intensity = 6 '\006'}, {distance_2cm = 5978, intensity = 3 '\003'}, {distance_2cm = 9964, intensity = 6 '\006'}}
}}}, sd = {gps_time_stamp = 59467414, status_type = 55 '7', status_value = 33 '!'}}
I want to view it in hex, something like the hypothetical output below:
00000000: 2369 6e63 6c75 6465 203c 6368 6563 6b2e
00000010: 683e 0a0a 2369 6e63 6c75 6465 2022 635f
00000020: 7665 6c6f 6479 6e65 2f61 7267 7061 7273
00000030: 652e 6822 0a23 696e 636c 7564 6520 2263
Is there a way to do this with gdb?
This can be done with the x command. The simplest way is to first find the size of your structure, and then pass this as an argument to x. Something like:
(gdb) print sizeof(whatever)
$1 = 1024
(gdb) x/512xh &whatever
Decoding x/512xh:
512 is the repeat count, here half the size of the structure, because your example showed 2 bytes per word;
x means print in hex;
h means "halfword", aka 2 bytes.
The gdb manual explains all the flags that can be used here.
You can do this in a fancier way using eval to compute the length for you. If you're doing this a lot I would suggest perhaps writing a new command in Python that does a bit of processing and then invokes x.
Related
I translated the improved 3D perlin noise from this webside from c# to c++.
But the resulting values are strange.
First lets look at the code:
#include "PerlinNoise.h"
PerlinNoise::PerlinNoise(int repeat)
{
this->repeat = repeat;
for (int i = 0; i < 256; ++i)
{
permutation[i] = i;
}
for (int i = 0; i < 256; ++i)
{
int swapIndex = rand() % 256;
std::swap(permutation[i], permutation[swapIndex]);
}
for (int x = 0; x<512; x++) {
p[x] = permutation[x % 256];
}
}
int PerlinNoise::inc(int num)
{
num++;
if (repeat > 0) num %= repeat;
return num;
}
float PerlinNoise::noise(float x, float y, float z)
{
if (repeat > 0) { // If we have any repeat on, change the coordinates to their "local" repetitions
x = fmodf(x, repeat);
y = fmodf(y, repeat);
z = fmodf(z, repeat);
}
int xi = (int)x & 255; // Calculate the "unit cube" that the point asked will be located in
int yi = (int)y & 255; // The left bound is ( |_x_|,|_y_|,|_z_| ) and the right bound is that
int zi = (int)z & 255; // plus 1. Next we calculate the location (from 0.0 to 1.0) in that cube.
float xf = x - static_cast<int>(x); // We also fade the location to smooth the result.
float yf = y - static_cast<int>(y);
float zf = z - static_cast<int>(z);
float u = fade(xf);
float v = fade(yf);
float w = fade(zf);
int aaa, aba, aab, abb, baa, bba, bab, bbb;
aaa = p[p[p[xi] + yi] + zi];
aba = p[p[p[xi] + inc(yi)] + zi];
aab = p[p[p[xi] + yi] + inc(zi)];
abb = p[p[p[xi] + inc(yi)] + inc(zi)];
baa = p[p[p[inc(xi)] + yi] + zi];
bba = p[p[p[inc(xi)] + inc(yi)] + zi];
bab = p[p[p[inc(xi)] + yi] + inc(zi)];
bbb = p[p[p[inc(xi)] + inc(yi)] + inc(zi)];
float x1, x2, y1, y2;
x1 = lerp(grad(aaa, xf, yf, zf), // The gradient function calculates the dot product between a pseudorandom
grad(baa, xf - 1, yf, zf), // gradient vector and the vector from the input coordinate to the 8
u); // surrounding points in its unit cube.
x2 = lerp(grad(aba, xf, yf - 1, zf), // This is all then lerped together as a sort of weighted average based on the faded (u,v,w)
grad(bba, xf - 1, yf - 1, zf), // values we made earlier.
u);
y1 = lerp(x1, x2, v);
x1 = lerp(grad(aab, xf, yf, zf - 1),
grad(bab, xf - 1, yf, zf - 1),
u);
x2 = lerp(grad(abb, xf, yf - 1, zf - 1),
grad(bbb, xf - 1, yf - 1, zf - 1),
u);
y2 = lerp(x1, x2, v);
return (lerp(y1, y2, w) + 1) / 2; // For convenience we bound it to 0 - 1 (theoretical min/max before is -1 - 1)
}
float PerlinNoise::grad(int hash, float x, float y, float z)
{
int h = hash & 15; // Take the hashed value and take the first 4 bits of it (15 == 0b1111)
float u = h < 8 /* 0b1000 */ ? x : y; // If the most significant bit (MSB) of the hash is 0 then set u = x. Otherwise y.
float v; // In Ken Perlin's original implementation this was another conditional operator (?:). I
// expanded it for readability.
if (h < 4 /* 0b0100 */) // If the first and second significant bits are 0 set v = y
v = y;
else if (h == 12 /* 0b1100 */ || h == 14 /* 0b1110*/)// If the first and second significant bits are 1 set v = x
v = x;
else // If the first and second significant bits are not equal (0/1, 1/0) set v = z
v = z;
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); // Use the last 2 bits to decide if u and v are positive or negative. Then return their addition.
}
float PerlinNoise::fade(float t)
{
// Fade function as defined by Ken Perlin. This eases coordinate values
// so that they will "ease" towards integral values. This ends up smoothing
// the final output.
return t * t * t * (t * (t * 6 - 15) + 10); // 6t^5 - 15t^4 + 10t^3
}
float PerlinNoise::lerp(float a, float b, float x)
{
return a + x * (b - a);
}
void main()
{
PerlinNoise perlinNoise(-1);
std::vector<float> results;
for (int i = 0; i < 10000; ++i)
{
results.push_back(perlinNoise.noise(static_cast<float>(i) * 0.1f, 0.0f, 0.0f));
}
}
And of cause the output:
x = 0.1 value = 0.45428 x = 0.2 value = 0.42896 x = 0.3 value = 0.43154 x = 0.4 value = 0.45872
x = 0.5 value = 0.5 x = 0.6 value = 0.54128 x = 0.7 value = 0.56846 x = 0.8 value = 0.57104
x = 0.9 value = 0.54572 x = 1 value = 0.5 x = 1.1 value = 0.450428 x = 1.2 value = 0.405792
x = 1.3 value = 0.374462 x = 1.4 value = 0.363488 x = 1.5 value = 0.375 x = 1.6 value = 0.404768
x = 1.7 value = 0.442922 x = 1.8 value = 0.476832 x = 1.9 value = 0.496148 x = 2 value = 0.5
x = 2.1 value = 0.5 x = 2.2 value = 0.5 x = 2.3 value = 0.5 x = 2.4 value = 0.5
x = 2.5 value = 0.5 x = 2.6 value = 0.5 x = 2.7 value = 0.5 x = 2.8 value = 0.5
x = 2.9 value = 0.5 x = 3 value = 0.5 x = 3.1 value = 0.5 x = 3.2 value = 0.5
x = 3.3 value = 0.5 x = 3.4 value = 0.5 x = 3.5 value = 0.5 x = 3.6 value = 0.5
x = 3.7 value = 0.5 x = 3.8 value = 0.5 x = 3.9 value = 0.5 x = 4 value = 0.5
x = 4.1 value = 0.496148 x = 4.2 value = 0.476832 x = 4.3 value = 0.442922 x = 4.4 value = 0.404768
x = 4.5 value = 0.375 x = 4.6 value = 0.363488 x = 4.7 value = 0.374462 x = 4.8 value = 0.405792
x = 4.9 value = 0.450428 x = 5 value = 0.5 x = 5.1 value = 0.553424 x = 5.2 value = 0.617376
x = 5.3 value = 0.682616 x = 5.4 value = 0.731744 x = 5.5 value = 0.75 x = 5.6 value = 0.731744
x = 5.7 value = 0.682616 x = 5.8 value = 0.617376 x = 5.9 value = 0.553424 x = 6 value = 0.5
x = 6.1 value = 0.450428 x = 6.2 value = 0.405792 x = 6.3 value = 0.374462 x = 6.4 value = 0.363488
x = 6.5 value = 0.375 x = 6.6 value = 0.404768 x = 6.7 value = 0.442922 x = 6.8 value = 0.476832
x = 6.9 value = 0.496148 x = 7 value = 0.5 x = 7.1 value = 0.503852 x = 7.2 value = 0.523168
x = 7.3 value = 0.557078 x = 7.4 value = 0.595232 x = 7.5 value = 0.625 x = 7.6 value = 0.636512
x = 7.7 value = 0.625538 x = 7.8 value = 0.594208 x = 7.9 value = 0.549572 x = 8 value = 0.5
x = 8.1 value = 0.446576 x = 8.2 value = 0.382624 x = 8.3 value = 0.317384 x = 8.4 value = 0.268256
x = 8.5 value = 0.25 x = 8.6 value = 0.268256 x = 8.7 value = 0.317384 x = 8.8 value = 0.382624
x = 8.9 value = 0.446576 x = 9 value = 0.5 x = 9.1 value = 0.553424 x = 9.2 value = 0.617376
x = 9.3 value = 0.682616 x = 9.4 value = 0.731744 x = 9.5 value = 0.75 x = 9.6 value = 0.731744
x = 9.7 value = 0.682616 x = 9.8 value = 0.617376 x = 9.9 value = 0.553424 x = 10 value = 0.5
x = 10.1 value = 0.450428 x = 10.2 value = 0.405792 x = 10.3 value = 0.374462 x = 10.4 value = 0.363488
x = 10.5 value = 0.375 x = 10.6 value = 0.404768 x = 10.7 value = 0.442922 x = 10.8 value = 0.476832
x = 10.9 value = 0.496148 x = 11 value = 0.5 x = 11.1 value = 0.496148 x = 11.2 value = 0.476832
x = 11.3 value = 0.442922 x = 11.4 value = 0.404768 x = 11.5 value = 0.375 x = 11.6 value = 0.363488
x = 11.7 value = 0.374462 x = 11.8 value = 0.405792 x = 11.9 value = 0.450428 x = 12 value = 0.5
x = 12.1 value = 0.549572 x = 12.2 value = 0.594208 x = 12.3 value = 0.625538 x = 12.4 value = 0.636512
x = 12.5 value = 0.625 x = 12.6 value = 0.595232 x = 12.7 value = 0.557078 x = 12.8 value = 0.523168
x = 12.9 value = 0.503852 x = 13 value = 0.5 x = 13.1 value = 0.496148 x = 13.2 value = 0.476832
x = 13.3 value = 0.442922 x = 13.4 value = 0.404768 x = 13.5 value = 0.375 x = 13.6 value = 0.363488
x = 13.7 value = 0.374462 x = 13.8 value = 0.405792 x = 13.9 value = 0.450428 x = 14 value = 0.5
x = 14.1 value = 0.54572 x = 14.2 value = 0.57104 x = 14.3 value = 0.56846 x = 14.4 value = 0.54128
x = 14.5 value = 0.5 x = 14.6 value = 0.45872 x = 14.7 value = 0.43154 x = 14.8 value = 0.42896
x = 14.9 value = 0.45428 x = 15 value = 0.5 x = 15.1 value = 0.54572 x = 15.2 value = 0.57104
x = 15.3 value = 0.56846 x = 15.4 value = 0.54128 x = 15.5 value = 0.5 x = 15.6 value = 0.45872
x = 15.7 value = 0.43154 x = 15.8 value = 0.42896 x = 15.9 value = 0.45428 x = 16 value = 0.5
x = 16.1 value = 0.549572 x = 16.2 value = 0.594208 x = 16.3 value = 0.625538 x = 16.4 value = 0.636512
x = 16.5 value = 0.625 x = 16.6 value = 0.595232 x = 16.7 value = 0.557078 x = 16.8 value = 0.523168
x = 16.9 value = 0.503852 x = 17 value = 0.5 x = 17.1 value = 0.496148 x = 17.2 value = 0.476832
x = 17.3 value = 0.442922 x = 17.4 value = 0.404768 x = 17.5 value = 0.375 x = 17.6 value = 0.363488
x = 17.7 value = 0.374462 x = 17.8 value = 0.405792 x = 17.9 value = 0.450428 x = 18 value = 0.5
x = 18.1 value = 0.549572 x = 18.2 value = 0.594208 x = 18.3 value = 0.625538 x = 18.4 value = 0.636512
x = 18.5 value = 0.625 x = 18.6 value = 0.595232 x = 18.7 value = 0.557078 x = 18.8 value = 0.523168
x = 18.9 value = 0.503852 x = 19 value = 0.5 x = 19.1 value = 0.503852 x = 19.2 value = 0.523168
x = 19.3 value = 0.557078 x = 19.4 value = 0.595232 x = 19.5 value = 0.625 x = 19.6 value = 0.636512
x = 19.7 value = 0.625538 x = 19.8 value = 0.594208 x = 19.9 value = 0.549572 x = 20 value = 0.5
x = 20.1 value = 0.45428 x = 20.2 value = 0.42896 x = 20.3 value = 0.43154 x = 20.4 value = 0.45872
x = 20.5 value = 0.5 x = 20.6 value = 0.54128 x = 20.7 value = 0.56846 x = 20.8 value = 0.57104
x = 20.9 value = 0.54572 x = 21 value = 0.5 x = 21.1 value = 0.450428 x = 21.2 value = 0.405792
x = 21.3 value = 0.374462 x = 21.4 value = 0.363488 x = 21.5 value = 0.375 x = 21.6 value = 0.404768
x = 21.7 value = 0.442922 x = 21.8 value = 0.476832 x = 21.9 value = 0.496148 x = 22 value = 0.5
x = 22.1 value = 0.5 x = 22.2 value = 0.5 x = 22.3 value = 0.5 x = 22.4 value = 0.5
x = 22.5 value = 0.5 x = 22.6 value = 0.5 x = 22.7 value = 0.5 x = 22.8 value = 0.5
x = 22.9 value = 0.5 x = 23 value = 0.5 x = 23.1 value = 0.503852 x = 23.2 value = 0.523168
x = 23.3 value = 0.557078 x = 23.4 value = 0.595232 x = 23.5 value = 0.625 x = 23.6 value = 0.636512
x = 23.7 value = 0.625538 x = 23.8 value = 0.594208 x = 23.9 value = 0.549572 x = 24 value = 0.5
x = 24.1 value = 0.45428 x = 24.2 value = 0.42896 x = 24.3 value = 0.43154 x = 24.4 value = 0.45872
x = 24.5 value = 0.5 x = 24.6 value = 0.54128 x = 24.7 value = 0.56846 x = 24.8 value = 0.57104
x = 24.9 value = 0.54572 x = 25 value = 0.5 x = 25.1 value = 0.450428 x = 25.2 value = 0.405792
x = 25.3 value = 0.374462 x = 25.4 value = 0.363488 x = 25.5 value = 0.375 x = 25.6 value = 0.404768
x = 25.7 value = 0.442922 x = 25.8 value = 0.476832 x = 25.9 value = 0.496148 x = 26 value = 0.5
x = 26.1 value = 0.5 x = 26.2 value = 0.5 x = 26.3 value = 0.5 x = 26.4 value = 0.5
x = 26.5 value = 0.5 x = 26.6 value = 0.5 x = 26.7 value = 0.5 x = 26.8 value = 0.5
x = 26.9 value = 0.5 x = 27 value = 0.5 x = 27.1 value = 0.503852 x = 27.2 value = 0.523168
x = 27.3 value = 0.557078 x = 27.4 value = 0.595232 x = 27.5 value = 0.625 x = 27.6 value = 0.636512
x = 27.7 value = 0.625538 x = 27.8 value = 0.594208 x = 27.9 value = 0.549572 x = 28 value = 0.5
x = 28.1 value = 0.450428 x = 28.2 value = 0.405792 x = 28.3 value = 0.374462 x = 28.4 value = 0.363488
x = 28.5 value = 0.375 x = 28.6 value = 0.404768 x = 28.7 value = 0.442922 x = 28.8 value = 0.476832
x = 28.9 value = 0.496148 x = 29 value = 0.5 x = 29.1 value = 0.503852 x = 29.2 value = 0.523168
x = 29.3 value = 0.557078 x = 29.4 value = 0.595232 x = 29.5 value = 0.625 x = 29.6 value = 0.636512
x = 29.7 value = 0.625538 x = 29.8 value = 0.594208 x = 29.9 value = 0.549572 x = 30 value = 0.5
x = 30.1 value = 0.446576 x = 30.2 value = 0.382623 x = 30.3 value = 0.317383 x = 30.4 value = 0.268256
x = 30.5 value = 0.25 x = 30.6 value = 0.268256 x = 30.7 value = 0.317384 x = 30.8 value = 0.382625
x = 30.9 value = 0.446576 x = 31 value = 0.5 x = 31.1 value = 0.54572 x = 31.2 value = 0.57104
x = 31.3 value = 0.56846 x = 31.4 value = 0.54128 x = 31.5 value = 0.5 x = 31.6 value = 0.45872
x = 31.7 value = 0.43154 x = 31.8 value = 0.42896 x = 31.9 value = 0.45428 x = 32 value = 0.5
x = 32.1 value = 0.545721 x = 32.2 value = 0.57104 x = 32.3 value = 0.56846 x = 32.4 value = 0.541279
x = 32.5 value = 0.5 x = 32.6 value = 0.458719 x = 32.7 value = 0.43154 x = 32.8 value = 0.42896
x = 32.9 value = 0.45428 x = 33 value = 0.5 x = 33.1 value = 0.545721 x = 33.2 value = 0.57104
x = 33.3 value = 0.56846 x = 33.4 value = 0.541279 x = 33.5 value = 0.5 x = 33.6 value = 0.458719
x = 33.7 value = 0.43154 x = 33.8 value = 0.42896 x = 33.9 value = 0.45428 x = 34 value = 0.5
x = 34.1 value = 0.545721 x = 34.2 value = 0.57104 x = 34.3 value = 0.56846 x = 34.4 value = 0.541279
x = 34.5 value = 0.5 x = 34.6 value = 0.458719 x = 34.7 value = 0.43154 x = 34.8 value = 0.42896
x = 34.9 value = 0.45428 x = 35 value = 0.5 x = 35.1 value = 0.553425 x = 35.2 value = 0.617377
x = 35.3 value = 0.682616 x = 35.4 value = 0.731745 x = 35.5 value = 0.75 x = 35.6 value = 0.731743
x = 35.7 value = 0.682616 x = 35.8 value = 0.617377 x = 35.9 value = 0.553423 x = 36 value = 0.5
x = 36.1 value = 0.450427 x = 36.2 value = 0.405792 x = 36.3 value = 0.374462 x = 36.4 value = 0.363488
x = 36.5 value = 0.375 x = 36.6 value = 0.404769 x = 36.7 value = 0.442922 x = 36.8 value = 0.476832
x = 36.9 value = 0.496148 x = 37 value = 0.5 x = 37.1 value = 0.503852 x = 37.2 value = 0.523168
x = 37.3 value = 0.557078 x = 37.4 value = 0.595233 x = 37.5 value = 0.625 x = 37.6 value = 0.636512
x = 37.7 value = 0.625538 x = 37.8 value = 0.594208 x = 37.9 value = 0.549571 x = 38 value = 0.5
x = 38.1 value = 0.446575 x = 38.2 value = 0.382623 x = 38.3 value = 0.317384 x = 38.4 value = 0.268255
x = 38.5 value = 0.25 x = 38.6 value = 0.268257 x = 38.7 value = 0.317384 x = 38.8 value = 0.382623
x = 38.9 value = 0.446577 x = 39 value = 0.5 x = 39.1 value = 0.545721 x = 39.2 value = 0.57104
x = 39.3 value = 0.56846 x = 39.4 value = 0.541279 x = 39.5 value = 0.5 x = 39.6 value = 0.458719
x = 39.7 value = 0.43154 x = 39.8 value = 0.42896 x = 39.9 value = 0.45428 x = 40 value = 0.5
x = 40.1 value = 0.545721 x = 40.2 value = 0.57104 x = 40.3 value = 0.56846 x = 40.4 value = 0.541279
x = 40.5 value = 0.5 x = 40.6 value = 0.458719 x = 40.7 value = 0.43154 x = 40.8 value = 0.42896
x = 40.9 value = 0.45428 x = 41 value = 0.5 x = 41.1 value = 0.549573 x = 41.2 value = 0.594208
x = 41.3 value = 0.625538 x = 41.4 value = 0.636512 x = 41.5 value = 0.625 x = 41.6 value = 0.595231
x = 41.7 value = 0.557078 x = 41.8 value = 0.523168 x = 41.9 value = 0.503852 x = 42 value = 0.5
x = 42.1 value = 0.496148 x = 42.2 value = 0.476832 x = 42.3 value = 0.442922 x = 42.4 value = 0.404767
x = 42.5 value = 0.375 x = 42.6 value = 0.363488 x = 42.7 value = 0.374462 x = 42.8 value = 0.405792
x = 42.9 value = 0.450429 x = 43 value = 0.5 x = 43.1 value = 0.545721 x = 43.2 value = 0.57104
x = 43.3 value = 0.56846 x = 43.4 value = 0.541279 x = 43.5 value = 0.5 x = 43.6 value = 0.458719
x = 43.7 value = 0.43154 x = 43.8 value = 0.42896 x = 43.9 value = 0.45428 x = 44 value = 0.5
x = 44.1 value = 0.553425 x = 44.2 value = 0.617377 x = 44.3 value = 0.682616 x = 44.4 value = 0.731745
x = 44.5 value = 0.75 x = 44.6 value = 0.731743 x = 44.7 value = 0.682616 x = 44.8 value = 0.617377
x = 44.9 value = 0.553423 x = 45 value = 0.5 x = 45.1 value = 0.454279 x = 45.2 value = 0.42896
x = 45.3 value = 0.43154 x = 45.4 value = 0.458721 x = 45.5 value = 0.5 x = 45.6 value = 0.541281
x = 45.7 value = 0.56846 x = 45.8 value = 0.57104 x = 45.9 value = 0.54572 x = 46 value = 0.5
x = 46.1 value = 0.454279 x = 46.2 value = 0.42896 x = 46.3 value = 0.43154 x = 46.4 value = 0.458721
x = 46.5 value = 0.5 x = 46.6 value = 0.541281 x = 46.7 value = 0.56846 x = 46.8 value = 0.57104
x = 46.9 value = 0.54572 x = 47 value = 0.5 x = 47.1 value = 0.450427 x = 47.2 value = 0.405792
x = 47.3 value = 0.374462 x = 47.4 value = 0.363488 x = 47.5 value = 0.375 x = 47.6 value = 0.404769
x = 47.7 value = 0.442922 x = 47.8 value = 0.476832 x = 47.9 value = 0.496148 x = 48 value = 0.5
x = 48.1 value = 0.5 x = 48.2 value = 0.5 x = 48.3 value = 0.5 x = 48.4 value = 0.5
x = 48.5 value = 0.5 x = 48.6 value = 0.5 x = 48.7 value = 0.5 x = 48.8 value = 0.5
x = 48.9 value = 0.5 x = 49 value = 0.5 x = 49.1 value = 0.5 x = 49.2 value = 0.5
x = 49.3 value = 0.5 x = 49.4 value = 0.5 x = 49.5 value = 0.5 x = 49.6 value = 0.5
x = 49.7 value = 0.5 x = 49.8 value = 0.5 x = 49.9 value = 0.5 x = 50 value = 0.5
x = 50.1 value = 0.496148 x = 50.2 value = 0.476832 x = 50.3 value = 0.442922 x = 50.4 value = 0.404767
x = 50.5 value = 0.375 x = 50.6 value = 0.363488 x = 50.7 value = 0.374462 x = 50.8 value = 0.405792
x = 50.9 value = 0.450429 x = 51 value = 0.5 x = 51.1 value = 0.545721 x = 51.2 value = 0.57104
x = 51.3 value = 0.56846 x = 51.4 value = 0.541279 x = 51.5 value = 0.5 x = 51.6 value = 0.458719
x = 51.7 value = 0.43154 x = 51.8 value = 0.42896 x = 51.9 value = 0.45428 x = 52 value = 0.5
As you can see there are blocks of 0.5 values. How can I change the code, so that these 0.5 values turn into correct perlin noise value?
Want to plot an array in gray scale image.Here is my array:
[[[ 0.27543858 0.30173767 -0.0101363 0.30631673 0.08575112
0.02205707 -0.15502007 0.11055402 -0.11761152]
[ 0.23695524 0.19820367 -0.08758862 0.02446048 0.29235974
-0.11381532 -0.00426369 0.15231356 -0.24601455]]]
Its dimension is (1, 2, 9). It should produce two images with 9 values each.
I have tried this so far:
col_size = 1
row_size = 2
index = 0
fig, ax = plt.subplots(row_size, col_size, figsize=(12,8))
for row in range(0,row_size):
for col in range(0,col_size):
ax[row][col].imshow(result_array[:,:,index],cmap='gray')
index += 1
plt.show()
Here is the answer:
result_array=result_array.reshape(2,3,3)
print(result_array)
print(result_array.shape)
col_size = 2
row_size = 1
filter_index = 0
fig, ax = plt.subplots(row_size, col_size, figsize=(12,8), squeeze=False)
for row in range(0,row_size):
for col in range(0,col_size):
ax[row][col].imshow(result_array[filter_index,:,:],cmap='gray')
filter_index += 1
plt.show()
I am attempting to implement a perceptron. I have loaded a 100x2 array of values between 0 and 100. Each item in the array has a label of either -1 or 1.
I believe the perceptron is working, however I cannot plot decision boundary as shown here: plot decision boundary matplotlib
When I run my code I only see a single color background. I would expect to see two colors, one color for each label in my data set (-1 and 1).
My current output, I expect to see 2 colors for the background (-1 or 1)
An example of what I hope to see, from the sklearn documentation
import numpy as np
from matplotlib import pyplot as plt
def generate_data():
#generate a dataset that is linearly seperable
group_1 = np.random.randint(50, 100, size=(50,2))
group_1_labels = np.full((50,1), 1)
group_2 = np.random.randint(0, 49, size =(50,2))
group_2_labels = np.full((50,1), -1)
#add a bias value of -1
bias = np.full((50,1), -1)
#add labels, upper right quadrant are 1, lower left are -1
group_1_with_bias = np.hstack((group_1, bias))
group_2_with_bias = np.hstack((group_2, bias))
group_1_labeled = np.hstack((group_1_with_bias, group_1_labels))
group_2_labeled = np.hstack((group_2_with_bias, group_2_labels))
#merge our labeled data and shuffle!
merged_data = np.vstack((group_1_labeled, group_2_labeled))
np.random.shuffle(merged_data)
return merged_data
data = generate_data()
#load data, strip labels, add a -1 bias value
X = data[:, :3]
#create labels matrix
l = np.ravel(data[:, 3:])
def perceptron_sgd(X, l, c, epochs):
#initialize weights
w = np.zeros(3)
errors = []
for epoch in range(epochs):
total_error = 0
for i, x in enumerate(X):
if (np.dot(x, w) * l[i]) <= 0:
total_error += (np.dot(x, w) * l[i])
w = w + c * (x * l[i])
errors.append(total_error * -1)
print "epoch " + str(epoch) + ": " + str(w)
return w, errors
def classify(X, l, w):
z = np.dot(X, w)
print z
z[z <= 0] = -1
z[z > 0] = 1
#return a matrix of predicted labels
return z
w, errors = perceptron_sgd(X, l, .001, 36)
# X - some data in 2dimensional np.array
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, .2), np.arange(y_min, y_max, .2))
# here "model" is your model's prediction (classification) function
Z = classify(np.c_[xx.ravel(), yy.ravel()], l, w[:-1]) #strip the bias from weights
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
plt.axis('off')
#Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=l, cmap=plt.cm.Paired)
I got it to work.
Standardized your X
from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(X[:, :-1])
X_trans = np.column_stack((scaler.transform(X[:, :-1]), X[:, -1]))
Better initialization than zero.
#initialize weights
r = np.sqrt(2)
w = np.random.uniform(-r, r, (3,))
Add learned biases during prediction
z = np.dot(X, w[:-1]) + w[-1]
Standardize during prediction as well (using standardization learned from input)
Z = classify(scaler.transform(np.c_[xx.ravel(), yy.ravel()]),
l, w) #strip the bias from weights
Generally, always a good idea to standardize the inputs.
Entire code:
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
def generate_data():
#generate a dataset that is linearly seperable
group_1 = np.random.randint(50, 100, size=(50,2))
group_1_labels = np.full((50,1), 1)
group_2 = np.random.randint(0, 49, size =(50,2))
group_2_labels = np.full((50,1), -1)
#add a bias value of -1
bias = np.full((50,1), -1)
#add labels, upper right quadrant are 1, lower left are -1
group_1_with_bias = np.hstack((group_1, bias))
group_2_with_bias = np.hstack((group_2, bias))
group_1_labeled = np.hstack((group_1_with_bias, group_1_labels))
group_2_labeled = np.hstack((group_2_with_bias, group_2_labels))
#merge our labeled data and shuffle!
merged_data = np.vstack((group_1_labeled, group_2_labeled))
np.random.shuffle(merged_data)
return merged_data
data = generate_data()
#load data, strip labels, add a -1 bias value
X = data[:, :3]
#create labels matrix
l = np.ravel(data[:, 3:])
from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(X[:, :-1])
X_trans = np.column_stack((scaler.transform(X[:, :-1]), X[:, -1]))
def perceptron_sgd(X, l, c, epochs):
#initialize weights
r = np.sqrt(2)
w = np.random.uniform(-r, r, (3,))
errors = []
for epoch in range(epochs):
total_error = 0
for i, x in enumerate(X):
if (np.dot(x, w) * l[i]) <= 0:
total_error += (np.dot(x, w) * l[i])
w = w + c * (x * l[i])
errors.append(total_error * -1)
print("epoch " + str(epoch) + ": " + str(w))
return w, errors
def classify(X, l, w):
z = np.dot(X, w[:-1]) + w[-1]
print(z)
z[z <= 0] = -1
z[z > 0] = 1
#return a matrix of predicted labels
return z
w, errors = perceptron_sgd(X_trans, l, .01, 25)
# X - some data in 2dimensional np.array
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, .1), np.arange(y_min, y_max, .1))
# here "model" is your model's prediction (classification) function
Z = classify(scaler.transform(np.c_[xx.ravel(), yy.ravel()]), l, w) #strip the bias from weights
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.4)
#plt.axis('off')
#Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=l, cmap=plt.cm.Paired)
I'm trying to rotate an image by small degrees like 1 degree or below 1 degree,
Consider this is my Source Image and I'm trying to rotate it by 1 degree in MATLAB and OpenCV (C++) :
0 0 0 0
0 255 255 0
0 255 255 0
0 0 0 0
When I rotate it in MATLAB by 1 degree this is the result:
0 0 2.223835069639144 0
2.223835069639553 252.7942370468069 2.527942370468065 0
0 252.7942370468065 252.794237046807 2.223835069639648
0 2.223835069639184 0 0
This is the code in MATLAB:
sourceImage = [0 0 0 0; 0 255.0 255.0 0; 0 255.0 255.0 0; 0 0 0 0];
rotationDegree = 1.0;
shearInX_Y1 = (cosd(rotationDegree)-1)/sind(rotationDegree);
shearInX_Y2 = sind(rotationDegree);
transformationMatrix = [(1 + shearInX_Y1*shearInX_Y2), (2*shearInX_Y1 + ...
((shearInX_Y1)^2)*shearInX_Y2), 0; (shearInX_Y2), (1 + shearInX_Y1*shearInX_Y2), 0; 0, 0, 1];
tform = affine2d(transformationMatrix);
imref = imref2d(size(sourceImage));
imref.XWorldLimits = imref.XWorldLimits-mean(imref.XWorldLimits);
imref.YWorldLimits = imref.YWorldLimits-mean(imref.YWorldLimits);
transformedImg2 = imwarp(sourceImage , imref, tform, 'OutputView', imref, 'Interp', 'bilinear');
transformedImg2
The transformationMatrix in Matlab (which is our rotation matrix) is:
transformationMatrix =
9.998476951563913e-01 -1.745240643728003e-02 0
1.745240643728351e-02 9.998476951563913e-01 0
0 0 1.000000000000000e+00
And the content of "tform.T" is:
9.998476951563913e-01 -1.745240643728003e-02 0
1.745240643728351e-02 9.998476951563913e-01 0
0 0 1
And the content of "rotationMatrix" in OpenCV is:
9.998476951563913e-01 1.745240643728351e-02 -0.008650050796837391
-1.745240643728351e-02 9.998476951563913e-01 0.008802355640446121
But when I do the rotation by 1 degree in OpenCV (C++) This is the result:
(which is same as source image!) which means openCV has a problem with small rotations!
0 0 0 0
0 255 255 0
0 255 255 0
0 0 0 0
This is the code I use for rotation in OpenCV (C++): (Rotation is done with respect to image center)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
# define INTERPOLATION_METHOD INTER_CUBIC
// INTER_AREA
// INTER_LINEAR
// INTER_NEAREST
Mat rotateImage(Mat sourceImage, double rotationDegree);
int main(){
Mat sourceImage = Mat::zeros(4, 4, CV_64F);
sourceImage.at<double>(1, 1) = 255.0;
sourceImage.at<double>(1, 2) = 255.0;
sourceImage.at<double>(2, 1) = 255.0;
sourceImage.at<double>(2, 2) = 255.0;
double rotationDegree = 1.0;
Mat rotatedImage = rotateImage(sourceImage, rotationDegree);
cout << "sourceImage: \n" << sourceImage << endl << endl;
cout << "rotatedImage : \n" << rotatedImage << endl << endl;
return 0;
}
Mat rotateImage(Mat sourceImage, double rotationDegree){
double rowOfImgCenter;
double colOfImgCenter;
rowOfImgCenter = sourceImage.rows / 2.0 - 0.5;
colOfImgCenter = sourceImage.cols / 2.0 - 0.5;
Point2d imageCenter(colOfImgCenter, rowOfImgCenter);
Mat rotationMatrix;
rotationMatrix = getRotationMatrix2D(imageCenter, rotationDegree, 1.0);
Mat rotatedImage;
warpAffine(sourceImage, rotatedImage, rotationMatrix, sourceImage.size(), INTERPOLATION_METHOD);
return rotatedImage;
}
Any idea would be appreciated.
How can I swap two colors using a color matrix? For instance swapping red and blue is easy. The matrix would look like:
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
0 0 0 1 0
0 0 0 0 1
So how can I swap any two colors in general? For example, there is Color1 with R1, G1, B1 and Color2 with R2, G2, B2.
EDIT: By swap I mean Color1 will translate into color2 and color2 will translate into color1. Looks like I need a reflection transformation. How to calculate it?
GIMP reference removed. Sorry for confusion.
This appears to be the section of the color-exchange.c file in the GIMP source that cycles through all the pixels and if a pixel meets the chosen criteria(which can be a range of colors), swaps it with the chosen color:
for (y = y1; y < y2; y++)
{
gimp_pixel_rgn_get_row (&srcPR, src_row, x1, y, width);
for (x = 0; x < width; x++)
{
guchar pixel_red, pixel_green, pixel_blue;
guchar new_red, new_green, new_blue;
guint idx;
/* get current pixel-values */
pixel_red = src_row[x * bpp];
pixel_green = src_row[x * bpp + 1];
pixel_blue = src_row[x * bpp + 2];
idx = x * bpp;
/* want this pixel? */
if (pixel_red >= min_red &&
pixel_red <= max_red &&
pixel_green >= min_green &&
pixel_green <= max_green &&
pixel_blue >= min_blue &&
pixel_blue <= max_blue)
{
guchar red_delta, green_delta, blue_delta;
red_delta = pixel_red > from_red ?
pixel_red - from_red : from_red - pixel_red;
green_delta = pixel_green > from_green ?
pixel_green - from_green : from_green - pixel_green;
blue_delta = pixel_blue > from_blue ?
pixel_blue - from_blue : from_blue - pixel_blue;
new_red = CLAMP (to_red + red_delta, 0, 255);
new_green = CLAMP (to_green + green_delta, 0, 255);
new_blue = CLAMP (to_blue + blue_delta, 0, 255);
}
else
{
new_red = pixel_red;
new_green = pixel_green;
new_blue = pixel_blue;
}
/* fill buffer */
dest_row[idx + 0] = new_red;
dest_row[idx + 1] = new_green;
dest_row[idx + 2] = new_blue;
/* copy alpha-channel */
if (has_alpha)
dest_row[idx + 3] = src_row[x * bpp + 3];
}
/* store the dest */
gimp_pixel_rgn_set_row (&destPR, dest_row, x1, y, width);
/* and tell the user what we're doing */
if (!preview && (y % 10) == 0)
gimp_progress_update ((gdouble) y / (gdouble) height);
}
EDIT/ADDITION
Another way you could have transformed red to blue would be with this matrix:
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
-1 0 1 0 1
The only values that really matter are the bottom ones in this matrix.
This would be the same as saying subtract 255 from red, keep green the same, and then add 255 to blue. You could cut the alpha in half like so as well like so:
-1 0 1 -0.5 1
So (just like the gimp source) you just need to find the difference between your current color and your target color, for each channel, and then apply the difference. Instead of channel values from 0 to 255 you would use values from 0 to 1.
You could have changed it from red to green like so:
-1 1 0 0 1
See here for some good info:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms533875%28v=vs.85%29.aspx
Good luck.
I solved it by creating a reflection matrix via D3DXMatrixReflect using a plane that's perpendicular to the vector AB and intersects the midpoint of the AB.
D3DXVECTOR3 AB( colorA.r-colorB.r, colorA.g-colorB.g, colorA.b-colorB.b );
D3DXPLANE plane( AB.x, AB.y, AB.z, -AB.x*midpoint.x-AB.y*midpoint.y-AB.z*midpoint.z );
D3DXMatrixReflect