OpenCV: Accessing values of a matrix - c++

I am using the OpenCV function decomposeHomographyMat(). Because I want to decompose my H1 and H2 matrices I have got from the OpenCV function stereoRectifyUncalibrated().
Just for testing I wanted to multiply the rotation and translation back together to see if the result are the same and everything works correct.
decomposeHomographyMat(H1, Mat(Identity), result_rot, result_trans, result_normals);
Matx44f rotM(result_rot[id].at<float>(0,0), result_rot[id].at<float>(0,0), result_rot[id].at<float>(0,0), 0,
result_rot[id].at<float>(0,0), result_rot[id].at<float>(0,0), result_rot[id].at<float>(0,0), 0,
result_rot[id].at<float>(0,0), result_rot[id].at<float>(0,0), result_rot[id].at<float>(0,0), 0,
0, 0, 0, 1);
Matx44f transM(1, 0, 0, result_trans[id].at<float>(0,0),
0, 1, 0, result_trans[id].at<float>(0,1),
0, 0, 1, result_trans[id].at<float>(0,2),
0, 0, 0, 1);
H1 = Mat(transM) * Mat(rotM);
I know that for the rotM matrix I am referencing the same value all the time, this is jut temporary.
My problem is, that if I print out result_rot[id] and rotM I get weir result:
result_rot[id]: [0.9920521909211247, -0.1258265282306712, 0.0003678069998755973;
0.1258266147675855, 0.9920522232074044, -0.0002223635722913286;
-0.0003369043855492455, 0.0002668761040765017, 0.9999999076363849]
rotM: [-0.015474273, -0.015474273, -0.015474273, 0;
-0.015474273, -0.015474273, -0.015474273, 0;
-0.015474273, -0.015474273, -0.015474273, 0;
0, 0, 0, -0.99892187]
The value -0.015474273 is not even in the original rotationmatrix and the value 1 seems to be stored as -0.99892187.
I also tried to replace float with double, then the result changes again but still not correct.
How can I properly get the correct value that was stored in that matrix? and what is going wrong here?

Related

VTK: plot line segments

I need to plot line segments (not continous) combined in one chart.
In order to create a plot I use the code from the VTK example but when I skip some values when filling vtkTable line goes down to zero value. And if creating another plot and skip some of the first elements the line goes up from zero. So I need to solve these issues.
I have figured out the solution myself. It appears very simple at last. The basic idea is to represent each line segment by its own plot and to use a separate table for each of the plots. One more improtant point all of the tables do not need to have the same step on the X-axis.
Here is the sample code:
vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
vtkSmartPointer<vtkTable> table1 = vtkSmartPointer<vtkTable>::New();
vtkSmartPointer<vtkFloatArray> xAxis1 = vtkSmartPointer<vtkFloatArray>::New();
xAxis1->SetName("X");
vtkSmartPointer<vtkFloatArray> yAxis1 = vtkSmartPointer<vtkFloatArray>::New();
yAxis1->SetName("");
table1->AddColumn(xAxis1);
table1->AddColumn(yAxis1);
table1->SetNumberOfRows(2);
table1->SetValue(0, 0, 1);
table1->SetValue(0, 1, 1);
table1->SetValue(1, 0, 2);
table1->SetValue(1, 1, 2);
vtkPlot* plot1 = chart->AddPlot(vtkChart::LINE);
plot1->SetInputData(table1, 0, 1);
plot1->SetColor(0, 0, 255, 255);
vtkSmartPointer<vtkTable> table2 = vtkSmartPointer<vtkTable>::New();
vtkSmartPointer<vtkFloatArray> xAxis2 = vtkSmartPointer<vtkFloatArray>::New();
xAxis2->SetName("X");
vtkSmartPointer<vtkFloatArray> yAxis2 = vtkSmartPointer<vtkFloatArray>::New();
yAxis2->SetName("");
table2->AddColumn(xAxis2);
table2->AddColumn(yAxis2);
table2->SetNumberOfRows(3);
table2->SetValue(0, 0, 4);
table2->SetValue(0, 1, 4);
table2->SetValue(1, 0, 5);
table2->SetValue(1, 1, 5);
table2->SetValue(2, 0, 6);
table2->SetValue(2, 1, 6);
vtkPlot* plot2 = chart->AddPlot(vtkChart::LINE);
plot2->SetInputData(table2, 0, 1);
plot2->SetColor(255, 0, 0, 255);
view->GetScene()->AddItem(chart);
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
P.S. You can also specify several rows with the same x coordinate.

Is there an easy way to fill a 2d array after initialization?

I currently have a 2d array in my program which stores the coordinates for a cube. It is initialized at the top and is easily filled with the values I want in it like so.
float cubeRef[3][8] = { { -1, 1, 1, -1, -1, 1, 1, -1 },
{ 1, 1, -1, -1, 1, 1, -1, -1 },
{ 1, 1, 1, 1, -1, -1, -1, -1, } };
However during my program the co ordinates change and I have a function which 'resets' them back to the default values you see above. However if I try to reset them using the same method as before.
cubeRef = { { -1, 1, 1, -1, -1, 1, 1, -1 },
{ 1, 1, -1, -1, 1, 1, -1, -1 },
{ 1, 1, 1, 1, -1, -1, -1, -1, } };
It will not work here, with error message "Expression must be a modifiable value".
Why can I not fill a 2d array after initializing the same way I can when I created it? And how can I return the 2d array to it's default values without having to fill each space individually?
You can't assign to an array, it's simply not possible.
You could have two arrays, the one you modify, and one which contains the default values. Then when you want to reset the modifiable array you just copy from the default array, either using std::copy, memcpy or in a loop.
Otherwise you have to manually set each entry, one by one.

inverse fft of fft not returning expected data

I'm trying to make sure FFTW does what I think it should do, but am having problems. I'm using OpenCV's cv::Mat. I made a test program that, given a Mat f, computes ifft(fft(f)) and compares the result to f. I would expect the difference between the two to be negligible, but there's a strange pattern in the data..
In this case, f is initialized to be an 8x8 array of floats with positive values less than 1.
Here's my test program code:
Mat f = .. //populate f
if (f.type() != CV_32FC1)
DLOG << "Bad f type";
const int y = f.rows;
const int x = f.cols;
double* input = fftw_alloc_real(y * 2*(x/2 + 1));
// forward fft
fftw_plan plan = fftw_plan_dft_r2c_2d(x, y, input, (fftw_complex*)input, FFTW_MEASURE);
// inverse fft
fftw_plan iplan = fftw_plan_dft_c2r_2d(x, y, (fftw_complex*)input, input, FFTW_MEASURE);
// populate fftw data from f
for (int yi = 0; yi < y; ++yi)
{
const float* yptr = f.ptr<float>(yi);
for (int xi = 0; xi < x; ++xi)
input[yi*x + xi] = (double)yptr[xi];
}
fftw_execute(plan);
fftw_execute(iplan);
// put data into another cv::Mat for comparison
Mat check(y, x, f.type());
for (int yi = 0; yi < y; ++yi)
{
float* yptr = check.ptr<float>(yi);
for (int xi = 0; xi < x ; ++xi)
yptr[xi] = (float)input[yi*x + xi];
}
DLOG << Util::summary(f, "f");
DLOG << f;
DLOG << Util::summary(check, "check");
DLOG << check;
Mat diff = f*x*y - check;
DLOG << Util::summary(diff, "diff");
DLOG << diff;
Where DLOG is my logger and Util::summary(cv::Mat m) just prints passed string and the dimensions, channels, min, and max of the mat.
Here's what the data looks like (output):
f: rows:8 cols:8 chans:1 min:0.00257996 max:0.4
[0.050668437, 0.04509116, 0.033668514, 0.10986148, 0.12855141, 0.048241843, 0.12613985,.09731093;
0.028602425, 0.0092236707, 0.037089188, 0.118964, 0.075040311, 0.40000001, 0.11959606, 0.071930833;
0.0025799556, 0.051522054, 0.22233701, 0.052993439, 0.032000393, 0.12673819, 0.015244827, 0.044803992;
0.13946071, 0.019708242, 0.0112687, 0.047459811, 0.019342113, 0.030085485, 0.018739942, 0.0098618753;
0.041809395, 0.029681522, 0.026837418, 0.16038358, 0.29034778, 0.17247421, 0.1789207, 0.042179305;
0.025630442, 0.017192598, 0.060540862, 0.1854037, 0.21287154, 0.04813192, 0.042614728, 0.034764063;
0.0030835248, 0.018511582, 0.0071733585, 0.017076733, 0.064545207, 0.0026390438, 0.088922881, 0.045725599;
0.12798512, 0.23215951, 0.027465452, 0.03174505, 0.04352935, 0.025079668, 0.044403922, 0.035459157]
check: rows:8 cols:8 chans:1 min:-3.26489 max:25.6
[3.24278, 2.8858342, 2.1547849, 7.0311346, 8.2272902, 3.0874779, 8.0729504, 6.2278996;
0.30818239, 0, 2.373708, 7.6136961, 4.8025799, 25.6, 7.6541481, 4.6035733;
0.16511716, 3.2974114, -3.2648909, 0, 2.0480251, 8.1112442, 0.97566891, 2.8674555;
8.9254856, 1.2613275, 0.72119683, 3.0374279, -0.32588482, 0, 1.1993563, 0.63116002;
2.6758013, 1.8996174, 1.7175947, 10.264549, 18.582258, 11.038349, 0.042666838, 0;
1.6403483, 1.1003263, 3.8746152, 11.865837, 13.623778, 3.0804429, 2.7273426, 2.2249;
0.44932228, 0, 0.45909494, 1.0929109, 4.1308932, 0.16889881, 5.6910644, 2.9264383;
8.1910477, 14.858209, -0.071794562, 0, 2.7858784, 1.6050987, 2.841851, 2.2693861]
diff: rows:8 cols:8 chans:1 min:-0.251977 max:17.4945
[0, 0, 0, 0, 0, 0, 0, 0;
1.5223728, 0.59031492, 0, 0, 0, 0, 0, 0;
0, 0, 17.494459, 3.3915801, 0, 0, 0, 0;
0, 0, 0, 0, 1.5637801, 1.9254711, 0, 0;
0, 0, 0, 0, 0, 0, 11.408258, 2.6994755;
0, 0, 0, 0, 0, 0, 0, 0;
-0.2519767, 1.1847413, 0, 0, 0, 0, 0, 0;
0, 0, 1.8295834, 2.0316832, 0, 0, 0, 0]
The difficult part for me is the nonzero entries in the diff matrix. I've accounted for the scaling FFTW does on the values and the padding needed to do an in-place fft on real data; what am I missing?
I find it surprising that the data could be off by a value of 17 (which is 66% of the max value), when there are so many zeros. Also, the data irregularities seem to form a diagonal pattern.
As you may have noticed when writting fftw_alloc_real(y * 2*(x/2 + 1)); fftw needs extra space in the x direction to store complex data. In your case, as x=8, it needs 2*(x/2+1)=10 reals.
http://www.fftw.org/doc/Real_002ddata-DFT-Array-Format.html#Real_002ddata-DFT-Array-Format
So...you should take care of this as you populate the input array or retreive values from it.
You way change
input[yi*x + xi] = (double)yptr[xi];
for
int xfft=2*(x/2 + 1);
...
input[yi*xfft + xi] = (double)yptr[xi];
And
yptr[xi] = (float)input[yi*x + xi];
for
yptr[xi] = (float)input[yi*xfft + xi];
It should solve your problem since the non-nul points in your diff correspond to the extra padding.
Bye,

Segmentation Fault when loading image pixel by pixel using CImg

I am trying to compute the mean of an image by loading pixel by pixel.
My image has 6 channels, height and with are 512 and depth is 1.
It is stored at the first position of an ImgList containing 2 elements.
My code is as follows:
int main(){
float mean = 0;
CImgList<float> img;
int c, x, y;
for(c=0; c<6; ++c)
for(x=0; x<512; ++x)
for(y=0; y<512; ++y){
img.load_cimg("test_images/Simul_PolSAR.cimg", 0, 0, x, y, 0, c, x, y, 0, c);
mean += img(0)(0,0,0,0);
}
mean = mean/(6*512*512);
}
When I run it, everything works fine until the value of "c" changes from 0 to 1. Then, the line accessing img(0)(0,0,0,0) makes the program crash with a segmentation fault error.
Also if I check:
img.load_cimg("image.cimg", 0, 0, 0, 0, 0, 1, 0, 0, 0, 1);
img(0).print();
The result is:
CImg<float>: this = 0x14330f8, size = (0,0,0,0) [0 b], data = (float*)(nil) (non-shared) = [ ].
I am very sure about the correctness of the code, and the integrity of the image (I tried with different ones). Any idea why it is happening?

Which is a good C++ BigInteger class for programming contests?

I was just wondering which will the best BigInteger class in C++ for programming contests which do not allow external libraries?
Mainly I was looking for a class which could be used in my code( I will of course write it on my own, on similar grounds ).
The primary factors which I think are important are( according to their importance ):
Arbitrary length numbers and their operations should be supported.
Should be as small as possible, code-wise. Usually there's a limit on the size of the source code which can be submitted to ~50KB, so the code should be ( much )smaller than that.
Should be as fast as possible. I read somewhere that bigInt classes take O( log( n ) ) time, so this should have a similiar complexity. What I mean is that it should be as fast as possible.
So far I've only needed unsigned integer big numbers for codechef, but codechef only gives 2KB, so I don't have the full implementation up there anywhere, just the members needed for that problem. My code also assumes that long long has at least twice as many bits as a unsigned, though that's pretty safe. The only real trick to it is that different biguint classes may have different data lengths. Here's summaries of the more interesting functions.
#define BIG_LEN() (data.size()>rhs.data.size()?data.size():rhs.data.size())
//the length of data or rhs.data, whichever is bigger
#define SML_LEN() (data.size()<rhs.data.size()?data.size():rhs.data.size())
//the length of data or rhs.data, whichever is smaller
const unsigned char baselut[256]={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
0,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
};
const unsigned char base64lut[256]={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,62, 0, 0, 0,63,
52,53,54,55,56,57,58,59,60,61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
15,16,17,18,19,20,21,22,23,24,25, 0, 0, 0, 0, 0,
0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0
};
//lookup tables for creating from strings
void add(unsigned int amount, unsigned int index)
adds amount at index with carry, simplifies other members
void sub(unsigned int amount, unsigned int index)
subtracts amount at index with borrow, simplifies other members
biguint& operator+=(const biguint& rhs)
resize data to BIG_LEN()
int carry = 0
for each element i in data up to SML_LEN()
data[i] += rhs.data[i] + carry
carry = ((data[i]<rhs[i]+carry || (carry && rhs[i]+carry==0)) ? 1u : 0u);
if data.length > rhs.length
add(carry, SML_LEN())
biguint& operator*=(const biguint& rhs)
biguint lhs = *this;
resize data to data.length + rhs.length
zero out data
for each element j in lhs
long long t = lhs[j]
for each element i in rhs (and j+i<data.size)
t*=rhs[i];
add(t&UINT_MAX, k);
if (k+1<data.size())
add(t>>uint_bits, k+1);
//note this was public, so I could do both at the same time when needed
//operator /= and %= both just call this
//I have never needed to divide by a biguint yet.
biguint& div(unsigned int rhs, unsigned int & mod)
long long carry = 0
for each element i from data length to zero
carry = (carry << uint_bits) | data[i]
data[i] = carry/rhs;
carry %= rhs
mod = carry
//I have never needed to shift by a biguint yet
biguint& operator<<=(unsigned int rhs)
resize to have enough room, always at least 1 bigger
const unsigned int bigshift = rhs/uint_bits;
const unsigned int lilshift = rhs%uint_bits;
const unsigned int carry_shift = (uint_bits-lilshift)%32;
for each element i from bigshift to zero
t = data[i-bigshift] << lilshift;
t |= data[i-bigshift-1] >> carry_shift;
data[i] = t;
if bigshift < data.size
data[bigshift] = data[0] << lilshift
zero each element i from 0 to bigshift
std::ofstream& operator<<(std::ofstream& out, biguint num)
unsigned int mod
vector reverse
do
num.div(10,mod);
push back mod onto reverse
while num greater than 0
print out elements of reverse in reverse
std::ifstream& operator>>(std::ifstream& in, biguint num)
char next
do
in.get(next)
while next is whitespace
num = 0
do
num = num * 10 + next
while in.get(next) and next is digit
//these are handy for initializing to known values.
//I also have constructors that simply call these
biguint& assign(const char* rhs, unsigned int base)
for each char c in rhs
data *= base
add(baselut[c], 0)
biguint& assign(const char* rhs, std::integral_constant<unsigned int, 64> base)
for each char c in rhs
data *= base
add(base64lut[c], 0)
//despite being 3 times as much, code, the hex version is _way_ faster.
biguint& assign(const char* rhs, std::integral_constant<unsigned int, 16>)
if first two characters are "0x" skip them
unsigned int len = strlen(rhs);
grow(len/4+1);
zero out data
const unsigned int hex_per_int = uint_bits/4;
if (len > hex_per_int*data.size()) { //calculate where first digit goes
rhs += len-hex_per_int*data.size();
len = hex_per_int*data.size();
}
for(unsigned int i=len; i --> 0; ) { //place each digit directly in it's place
unsigned int t = (unsigned int)(baselut[*(rhs++)]) << (i%hex_per_int)*4u;
data[i/hex_per_int] |= t;
}
I also made specializations for multiplication, divide, modulo, shifts and others for std::integral_constant<unsigned int, Value>, which made massive improvements to my serializing and deserializing functions amongst others.