Iterator of Vector<Vector<Point> > - opencv

I know these iterator questions have been asked and answered a thousand times, however when I compile the following code, I still get this error:
error C2440: 'initializing' : cannot convert from
'std::_Vector_iterator<_Myvec>' to 'cv::Vector<_Tp> *'
.
void iterate(vector<vector<cv::Point> >& contours){
Vector<Vector<cv::Point>>::iterator it = contours.begin();
for( int i = 0; i< contours.size(); i++ ){
if(contourArea(contours[i])>1000){
it++;
}else{
contours.erase(it);
}
}
}
I don't see anything wrong with this code. The template type of the vector for the new iterator is the same like the vector I'm getting the iterator from.

Can not convert errors are generally very literal. Often the types used can give hints where in your code somethng is wrong.
Here we have the use of uppercase Vector class to define your iterator, and you are assigning a lowercase Vector class to it.
Note you would probably have gotten a different error if Vector was not a real class.

Related

What function would you use to list the symbols?

Is there a way to set a condition that includes a list of currency symbol pairs and be alerted when that condition is met? What function would you use to list the symbols? Thanks in advance. The following code gives you a rough idea of what i'm trying to do.
for(i=0;i<Bars; i++)
{
//Want a list of symbols to scan multiple currency pairs for the following condition
if(Symbol("EURUSD";"AUDCAD";"USDJPY")Close[i+1]>Close[i+2])
{
a=a+1;
//This is what I want to happen if condition is met
Alert(Symbol()+" 1");
}
You can use SymbolsTotal and SymbolName to get a list of all the symbols in Market Watch. The following code should give you a start (although it will be permanently giving alerts, I think you need to check exactly what you want alerts for) .
for(int i=0; i<SymbolsTotal(true); i++)
{
string currencySymbol=SymbolName(i,true);
int a=0;
for(j=0; j<iBars(currencySymbol,0); j++)
{
if(iClose(currencySymbol,0,j)>iClose(currencySymbol,0,j+1)) a++;
{
if(a>0) Alert(StringConcatenate(currencySymbol,":",IntegerToString(a)));
}

Getting an error while adding a integer to array object at index (objective c)

while adding a integer to object at index of an array, I am getting an error of "arithmetic on pointer to interface id which is not a constant size for this architecture and platform", didn't know how to resolve it.
Please help.
my code is -
if (arrayTotalAmount.count>0) {
int sum = 0;
for (int i = 0; i<=arrayTotalAmount.count; i++) {
sum = (sum+[arrayTotalAmount objectAtIndex:i]);
}
In 4th line I am getting that error.
Thanks
Objective C array only accepts NSObject type. That means it is impossible to insert a primitive value into an NSArray. You are getting an error because objectAtIndex method returns a pointer which points to that NSObject, the arithmetic operations are still valid on pointers but the thing is that the size of a pointer as integer (32bit, 64bit) may differ on device. So one of the solution is typecasting the pointer sum+(int)[arrayTotalAmount objectAtIndex:i] which makes no sense in your case.
The solution you are looking for is probably sum+[[arrayTotalAmount objectAtIndex:i] intValue] or similar. Assuming that array contains NSNumber objects. If the object inside an array is not an NSNumber then your app will fail in runtime showing an error indicating that an object X does not have a method called intValue in which case you will need to figure how to convert object X to your int.
You just need to convert your array object to integer and then add it will work for you.
if (arrayTotalAmount.count>0) {
int sum = 0;
for (int i = 0; i<=arrayTotalAmount.count; i++) {
sum = (sum+[[arrayTotalAmount objectAtIndex:i] intValue]);
}

How do I allocate an array at runtime in Rust?

Once I have allocated the array, how do I manually free it? Is pointer arithmetic possible in unsafe mode?
Like in C++:
double *A=new double[1000];
double *p=A;
int i;
for(i=0; i<1000; i++)
{
*p=(double)i;
p++;
}
delete[] A;
Is there any equivalent code in Rust?
Based on your question, I'd recommend reading the Rust Book if you haven't done so already. Idiomatic Rust will almost never involve manually freeing memory.
As for the equivalent to a dynamic array, you want a vector. Unless you're doing something unusual, you should avoid pointer arithmetic in Rust. You can write the above code variously as:
// Pre-allocate space, then fill it.
let mut a = Vec::with_capacity(1000);
for i in 0..1000 {
a.push(i as f64);
}
// Allocate and initialise, then overwrite
let mut a = vec![0.0f64; 1000];
for i in 0..1000 {
a[i] = i as f64;
}
// Construct directly from iterator.
let a: Vec<f64> = (0..1000).map(|n| n as f64).collect();
It is completely possible to allocate a fixed-sized array on the heap:
let a = Box::new([0.0f64; 1000]);
Because of deref coercion, you can still use this as an array:
for i in 0..1000 {
a[i] = i as f64;
}
You can manually free it by doing:
std::mem::drop(a);
drop takes ownership of the array, so this is completely safe. As mentioned in the other answer, it is almost never necessary to do this, the box will be freed automatically when it goes out of scope.

OpenCV- Cant access element of Mat (of descriptors)

I'm writing a simple program that extracts descriptors from images and writes them to files.
I'm saving the descriptors in a Mat variable, but I'm getting wrong values when trying to access them.
Here is the code:
string s = format("%s\\%s\\img%d.ppm", dataset_dir.c_str(), dsname, k);
Mat imgK = imread(s, 0);
if( imgK.empty() )
break;
detector->detect(imgK, kp);
descriptor->compute(imgK, kp, desc);
//writing the descriptors to a file
char fileName[512];
sprintf(fileName,"C:\\BinaryDescriptors\\OpenCVRes\\%s\\%s\\Descriptors%d.txt",descriptor_name,dsname,k);
FILE * fid;
fid=fopen(fileName,"a+");
for (int ix=0; ix< kp.size(); ix++){
fprintf(fid,"%f \t%f", kp[ix].pt.x,kp[ix].pt.y);
fprintf(fid, "\t1 \t0 \t1");
fflush(fid);
//writing the descriptor
for (int jx=0;jx<desc.cols;jx++){
int gil = desc.at<int>(ix,jx);
printf("AAAA %d", gil);
fprintf(fid,"\t%d",desc.at<int>(ix,jx));
fflush(fid);
}
}
fprintf(fid,"\n");
fclose(fid);
The line where I'm accessing the descriptors matrix is int gil = desc.at int(ix,jx); Is there something I'm doing wrong?
Any help will be greatly appreciated, as I'm quite stuck :)
Thanks,
Gil.
You are accessing the descriptor matrix with int, so that the matrix must be of type CV_32SC1. Are you sure it is that type? Most of the descriptors are coded with float (CV_32F) or unsigned char (CV_8U). Check that desc.type() == CV_32SC1.
By the way, you should use cv::FileStorage to save and load descriptors, it is much easier than directly accessing files.

Assign value is garbage or undefined

I have posted screenshot of my error code.
heights output
please any one can help me?
I think the static analyzer is not seeing how _numberOfColumns can become non-zero, and hence its insistence that garbage is being assigned. You need to check that you are actually providing some means for _numberOfColumns to become non-zero.
Generally when I am writing loops that want to find the largest or the smallest value, I initialize the size variable to the largest (if I want the smallest) or smallest (if I want the largest) amount, and I think this will solve most of your issues:
float shortestHeight = FLT_MAX;
for (unsigned i = 0; i < _numberOfColumns; i++)
{
// etc.
}
The analyzer is correct. Your code will access garbage memory if _numberOfColumns is 0, thus allocating 0 bytes for heights, making heights[0] garbage. The analyzer doesn't know what values _numberOfColumns can have, but you can tell it by using assert(_numberOfColumns>0).
Take this C program for example:
int main(int argc, const char * argv[])
{
int n = argc-1;
int *a = malloc(n*sizeof(int));
for (int i=0; i<n; i++) {
a[i] = i;
}
int foo = a[0];
free(a);
return foo;
}
the size of a is determined by the number of arguments. If you have no arguments n == 0. If you are sure that your program (or just that part of your program) will always assign something greater than 0 to a, you can use an assertion. Adding assert(n>0) will tell the analyzer exactly that.

Resources