Error reading values from Mat - opencv

I am trying to implement HMM in opencv.
First i create arrays of double, and copy them to Mat variables,
Mat INIT = Mat(0,3,CV_64F,trans).clone();
Then i am trying to access the individual pixel/position values from the matrix as:
cout << INIT.at<double>(r,c) << " ";//Where r and c are row and column values.
I am getting error like:
OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si
ze.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channel
s()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3
) - 1))*4) & 15) == elemSize1()) in unknown function, file c:\opencv2.4.4\includ
e\opencv2\core\mat.hpp, line 537
I searched over the forums and couldnot find anything wrong with the code. Any ideas?
Thanks alot in advance.

Declare the Matrix INIT as :-
Mat INIT=Mat(1,3,CV_64FC1,trans).clone();
Now access the individual pixel/position values from the matrix as:
cout << INIT.at<double>(r,c) << " ";

Related

cv2.stereoCalibrate criteria error

I'm trying to perform a stereo calibration of two cameras using opencv3.4.
I have performed the calibration of the two single cameras from which I obtained the mtxleft and right to use in stereocalibrate.
But I get the following error:
in <module>
criteria = termination_criteria_extrinsics
error: C:\projects\opencv-python\opencv\modules\calib3d\src\calibration.cpp:3083:
error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())
in function cv::collectCalibrationData
This is my code:
termination_criteria_extrinsics = (cv2.TERM_CRITERIA_EPS +
cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-5)
ret, mtx, dist, mtx2, dist2, R, T, E, F = cv2.stereoCalibrate(
objpoints_l,
imgpoints_l ,imgpoints_r, mtxL, distL, mtxR, distR,
gray_l.shape[::-1],
flags = flags,
criteria = termination_criteria_extrinsics
)
Does anyone know what the cause could be?

How to loop over array in Z3Py

As part of a reverse engineering exercise, I'm trying to write a Z3 solver to find a username and password that satisfy the program below. This is especially tough because the z3py tutorial that everyone refers to (rise4fun) is down.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name, pass;
cout << "Name: ";
cin >> name;
cout << "Pass: ";
cin >> pass;
int sum = 0;
for (size_t i = 0; i < name.size(); i++) {
char c = name[i];
if (c < 'A') {
cout << "Lose: char is less than A" << endl;
return 1;
}
if (c > 'Z') {
sum += c - 32;
} else {
sum += c;
}
}
int r1 = 0x5678 ^ sum;
int r2 = 0;
for (size_t i = 0; i < pass.size(); i++) {
char c = pass[i];
c -= 48;
r2 *= 10;
r2 += c;
}
r2 ^= 0x1234;
cout << "r1: " << r1 << endl;
cout << "r2: " << r2 << endl;
if (r1 == r2) {
cout << "Win" << endl;
} else {
cout << "Lose: r1 and r2 don't match" << endl;
}
}
I got that code from the assembly of a binary, and while it may be wrong I want to focus on writing the solver. I'm starting with the first part, just calculating r1, and this is what I have:
from z3 import *
s = Solver()
sum = Int('sum')
name = Array('name', IntSort(), IntSort())
for c in name:
s.add(c < 65)
if c > 90:
sum += c - 32
else:
sum += c
r1 = Xor(sum, 0x5678)
print s.check()
print s.model()
All I'm asserting is that there are no letters less than 'A' in the array, so I expect to get back an array of any size that has numbers greater than 65.
Obviously this is completely wrong, mainly because it infinite loops. Also, I'm not sure I'm calculating sum correctly, because I don't know if it's initialized to 0. Could someone help figure out how to get this first loop working?
EDIT: I was able to get a z3 script that is close to the C++ code shown above:
from z3 import *
s = Solver()
sum = 0
name = Array('name', BitVecSort(32), BitVecSort(32))
i = Int('i')
for i in xrange(0, 1):
s.add(name[i] >= 65)
s.add(name[i] < 127)
if name[i] > 90:
sum += name[i] - 32
else:
sum += name[i]
r1 = sum ^ 0x5678
passwd = Array('passwd', BitVecSort(32), BitVecSort(32))
r2 = 0
for i in xrange(0, 5):
s.add(passwd[i] < 127)
s.add(passwd[i] >= 48)
c = passwd[i] - 48
r2 *= 10
r2 += c
r2 ^= 0x1234
s.add(r1 == r2)
print s.check()
print s.model()
This code was able to give me a correct username and password. However, I hardcoded the lengths of one for the username and five for the password. How would I change the script so I wouldn't have to hard code the lengths? And how would I generate a different solution each time I run the program?
Arrays in Z3 do not necessarily have any bounds. In this case the index-sort is Int, which means unbounded integers (not machine integers). Consequently, for c in name will run forever because it enumerates name[0], name[1], name[2], ...
It seems that you actually have a bound in the original program (name.size()), so it would suffice to enumerate up to that limit. Otherwise you might need a quantifier, e.g., \forall x of Int sort . name[x] < 65. This comes with all the warnings about quantifiers, of course (see e.g., the Z3 Guide)
Suppose the length is to be determined. Here is what I think you could do:
length = Int('length')
x = Int('x')
s.add(ForAll(x,Implies(And(x>=0,x<length),And(passwd[x] < 127,passwd[x] >=48))))

OpenCV line detection and extraction using ios

Hi i am using opencv for the line detection in ios. This is for my final year project. I want to upload the image of ecg and then extract the line from this for that i have used the opencv and sucessfuly i have extracted the line but now i want to save the line and facing much problem in that can anyone help me or any tutorial ?
I wrote something quickly, I think it can help you:
void write_lines_to_file ( string file_name, vector<vector<Point> > lines )
{
FileStorage fs ( file_name, FileStorage::WRITE );
fs << "lines" << "[";
for ( unsigned int i = 0; i < lines.size(); i++ )
{
// I've assumed each line is a vector< Point >, in index [0] the head and index [1] the tail.
fs << "{:" << "x1" << lines[i].at ( 0 ).x << "y1" << lines[i].at ( 0 ).y << "x2" << lines[i].at ( 1 ).x << "y2" << lines[i].at ( 1 ).y << "}";
}
fs.release();
}
I didn't test it and I made my own assumptions, so please be cautious when you want to use it.

Issue with cv::Mat::zeros initialization

my problem is just astonishing. This is the code
#define NCHANNEL 3
#define NFRAME 100
Mat RR = Mat::zeros(NCHANNEL, NFRAME-1, CV_64FC1);
double *p_0 = RR.ptr<double>(0);
double *p_1 = RR.ptr<double>(1);
double *p_2 = RR.ptr<double>(2);
cout<< p_0[NFRAME-1] << endl << p_1[NFRAME-1] << endl << p_2[NFRAME-1] << endl;
And the output is: 0 0 -6.27744e+066 .
Where is that awful number come from? it seems I'm printing a pointer or something rough in memory. (uh, 0 is the value of all other elements, of course).
You are accessing after the last element of Mat. If you use NFRAME-1 for initialization then the last element has NFRAME-2 index.

How to set up element in multichannel matrix via CV_MAT_ELEM?

I created OpenCV matrix:
CvMat * src = cvCreateMat(1, 2, CV_32FC2);
Then I want to set up element row=0, col=1, channel=1
According to the example in description of documentation for CvMat
I tried to set element with the following code:
CV_MAT_ELEM(*src, float, 0, 1 * 2 + 1) = 123;
But assert is fired.
And the reason is obvious:
We have following defintions in OpenCV sources:
#define CV_MAT_ELEM_PTR_FAST( mat, row, col, pix_size ) \
(assert( (unsigned)(row) < (unsigned)(mat).rows && \
(unsigned)(col) < (unsigned)(mat).cols ), \
(mat).data.ptr + (size_t)(mat).step*(row) + (pix_size)*(col))
#define CV_MAT_ELEM( mat, elemtype, row, col ) \
(*(elemtype*)CV_MAT_ELEM_PTR_FAST( mat, row, col, sizeof(elemtype)))
In my case mat.cols == 2 and col == 1 * 2 + 1 == 3.
What is wrong: documentation or assert in sources of OpenCV?
How to manage this?
How can I set up element of multichannel matrix?
Thanks.
P.S. To OpenCV developers if anyone here.
When I press "you can create one now" to create new account to report a bug from the page http://opencv.willowgarage.com/wiki/Welcome?action=login, I obtain the error "Unknown action newaccount."
UPDATE:
I use OpenCV 2.1.
I have worked around usage of CV_MAT_ELEM:
float * src_ptr = (float*)src->data.ptr;
*(src_ptr + 1 * 2 + 1) = 123;
Not an answer to your question, but a good suggestion:
Neither the documentation, nor the source code are wrong.
But why don't you use the C++ interface? I can bet you do not use the C interface because you really need it (you build for some strange embedded platform, that can't compile c++).
Mat src(1, 2, CV_32FC2);
// isn't it nicer than CV_UGLY_AND_SCARY_MACRO()?
src.at<Vec2f>(0,1)[1] = 123; // (0,1) means row 0, col 1. [1] means channel 1.
EDIT
From OpenCV:
For single-channel matrices there is a macro CV_MAT_ELEM( matrix, elemtype, row, col ), i.e. for 32-bit floating point real matrix

Resources