Changing an OpenCV function Standard Parameters - c++

Is there a way to permanently change the standard parameters in a OpenCV function?
For example, how can I modify the MSER Feature Detector so that I can call
MserFeatureDetector detector
instead of
MserFeatureDetector detector(10,50,1000)
I am not precisley well versed in the inner mechanisms of C++ libraries, but I imagine the actual program code has to be somewhere, right?
A bit of information on my actual problem:
I'm currently using MEXOpenCV to run OpenCV functions in MatLab, and some MEX-Functions lack (as far as I know) the option to pass input parameters and run with the defaults like this:
detector = cv.FeatureDetector('MSER'); % 'MSER' is the only parameter taken
I recon changing the standard parameters directly at the OpenCV programs would be a way to do it.
Any other ideas on how to solve the actual problem are welcome too!

I solved the actual problem by setting the parameters with the 'set' method of DescriptorExtractor like this
detector=cv.FeatureDetector('MSER'); detector.set('delta',10);

Related

how to find index by value in ChunkedArray from Apache Arrow?

The closest I can find seems to be the index from ComputeFunction:
https://arrow.apache.org/docs/python/api/compute.html
But I do not find a working code example for it in C++ from the apache arrow codebase.
Here's the documentation for that function in the cpp docs:
https://arrow.apache.org/docs/cpp/compute.html#aggregations
And here's a short example of how to call the function in C++:
8.0.0: https://github.com/apache/arrow/blob/apache-arrow-8.0.0/cpp/src/arrow/compute/kernels/aggregate_test.cc#L2234
7.0.0: https://github.com/apache/arrow/blob/apache-arrow-7.0.0/cpp/src/arrow/compute/kernels/aggregate_test.cc#L2206
[2022-05-23 Edit]
Here is an example that calls the Index function, using arrow 7.0.0:
https://github.com/drin/cookbooks/blob/mainline/arrow/compute-api/recipe.cpp#L18
The recipe.hpp file should show the required includes and types that are used (I tried to minimize to just what's necessary).
Also, here is corresponding code for usage, including making some test data and using the IndexOf function and viewing the result:
https://github.com/drin/cookbooks/blob/mainline/arrow/compute-api/index.cpp#L18
I wrote IndexOf to show how you can use the Index function yourself, so you can use it directly, or write a wrapper function in a similar style.
NOTE: I thought I needed to upgrade to 8.0.0 to use Scalar types, but I think 8.0.0 mostly introduced the documentation for Scalar rather than introducing code for it, as this works with arrow 7.0.0.

CUDA for pytorch: CUDA C++ stream and state

I am trying to follow this tutorial and make a simple c++ extension with CUDA backend.
My CPU implementation seems to work fine.
I am having trouble finding examples and documentation (it seems like things are constantly changing).
Specifically,
I see pytorch cuda functions getting THCState *state argument - where does this argument come from? How can I get a state for my function as well?
For instance, in cuda implementation of tensor.cat:
void THCTensor_(cat)(THCState *state, THCTensor *result, THCTensor *ta, THCTensor *tb, int dimension)
However, when calling tensor.cat() from python one does not provide any state argument, pytorch provides it "behind the scene". How pytorch provides this information and how can I get it?
state is then converted to cudaStream_t stream = THCState_getCurrentStream(state);
For some reason, THCState_getCurrentStream is no longer defined? How can I get the stream from my state?
I also tried asking on pytorch forum - so far to no avail.
It's deprecated (without documentation!)
See here:
https://github.com/pytorch/pytorch/pull/14500
In short: use at::cuda::getCurrentCUDAStream()

Can I read a SMT2 file into a solver through the z3 c++ interface?

I've got a problem where the z3 code embedded in a larger system isn't finding a solution to a certain set of constraints (added through the C++ interface) despite some fairly long timeouts. When I dump the constraints to a file (using the to_smt2() method on the solver, just before the call to check()), and run the file through the standalone z3 executable, it solves the system in about 4 seconds (returning sat). For what it's worth, the file is 476,587 lines long, so a fairly big set of constraints.
Is there a way I can read that file back into the embedded solver using the C++ interface, replacing the existing constraints, to see if the embedded version can solve starting from the exact same starting point as the standalone solver? (Essentially, how could I create a corresponding from_smt2(stream) method on the solver class?)
They should be the same set of constraints as now, of course, but maybe there's some ordering effect going on when they are read from the file, or maybe there are some subtle differences in the solver introduced when we embedded it, or something that didn't get written out with to_smt2(). So I'd like to try reading the file back, if I can, to narrow down the possible sources of the difference. Suggestions on what to look for while debugging the long-running version would also be helpful.
Further note: it looks like another user is having similar issues here. Unlike that user, my problem uses all bit-vectors, and the only unknown result is the one from the embedded code. Is there a way to invoke the (get-info :reason-unknown) from the C++ interface, as suggested there, to find out why the embedded version is having a problem?
You can use the method "solver::reason_unknown()" to retrieve explanations for search failure.
There are methods for parsing files and strings into a single expression.
In case of a set of assertions, the expression is a conjunction.
It is perhaps a good idea to add such a method directly to the solver class for convenience. It would be:
void from_smt2_string(char const* smt2benchmark) {
expr fml = ctx().parse_string(smt2benchmark);
add(fml);
}
So if you were to write it outside of the solver class you need to:
expr fml = solver.ctx().parse_string(smt2benchmark);
solver.add(fml);

Different ways to make kernel

In this tutorial
There are 2 methods to run the kernel, and another one mentioned in the comments:
1.
cl::KernelFunctor simple_add(cl::Kernel(program,"simple_add"),queue,cl::NullRange,cl::NDRange(10),cl::NullRange);
simple_add(buffer_A,buffer_B,buffer_C);
However, I found out, that KernelFunctor has gone.
So I tried the alternative way:
2.
cl::Kernel kernel_add=cl::Kernel(program,"simple_add");
kernel_add.setArg(0,buffer_A);
kernel_add.setArg(1,buffer_B);
kernel_add.setArg(2,buffer_C);
queue.enqueueNDRangeKernel(kernel_add,cl::NullRange,cl::NDRange(10),cl::NullRange);
queue.finish();
It compiles and runs succussfully.
However, there is a 3rd option in the comments:
3.
cl::make_kernel simple_add(cl::Kernel(program,"simple_add"));
cl::EnqueueArgs eargs(queue,cl::NullRange,cl::NDRange(10),cl::NullRange);
simple_add(eargs, buffer_A,buffer_B,buffer_C).wait();
Which does not compile, I think the make_kernel needs template arguments.
I'm new to OpenCl, and didn't manage to fix the code.
My question is:
1. How should I modify the 3. code to compile?
2. Which way is better and why? 2. vs. 3.?
You can check the OpenCL C++ Bindings Specification for a detailed description of the cl::make_kernel API (in section 3.6.1), which includes an example of usage.
In your case, you could write something like this to create the kernel functor:
auto simple_add = cl::make_kernel<cl::Buffer&, cl::Buffer&, cl::Buffer&>(program, "simple_add");
Your second question is primarily opinion based, and so is difficult to answer. One could argue that the kernel functor approach is simpler, as it allows you to 'call' the kernel almost as if it were just a function and pass the arguments in a familiar manner. The alternative approach (option 2 in your question) is more explicit about setting arguments and enqueuing the kernel, but more closely represents how you would write the same code using the OpenCL C API. Which method you use is entirely down to personal preference.

How to get the value of a variable that's deep in the source code (C++)? (eg. value of stage_sum in haar.cpp, OpenCV)

I'd like to get a value from a variable that's located deeply in the source code of the OpenCV library. Specifically, I'm trying to print out the value of stage_sum from the file haar.cpp. My starting point, facedetect.cpp, calls the method detectMultiScale, which then calls the function cvHaarDetectObjects, which calls cvHaarDetectObjectsForROC etc., until it finally reaches the function cvRunHaarClassifierCascadeSum, where stage_sum is calculated.
Is there a way I could get the value out to facedetect.cpp easily, without changing the declarations of all the preceding functions/methods, headers etc.? Simply trying to cout or printf the value directly in the source code hasn't given any results.
Thanks everyone for your help!
One option is simply to use a debugger.
However, if you want to do this programatically (i.e. access the variable as part of your application code), then unless the variable is exposed in the library's public interface, there are two options available:
Modify the library's source code, and recompile it.
Resort to undefined-behaviour (fiddling around with the raw bytes that make up an object, etc.).
Just to point the obvious, adding a std::cout() or printf() call inside haar.cpp won't do the trick. You need to recompile OpenCV for this changes to take effect and then reinstall the libraries on your system.