What I'm trying is to calculate the average of a column using openCV in android.
The initial idea was to copy each column to a temp matrix, then use Core.mean() to get the average value.
But the problem is :
To use Mat.put(), it is expected to have a row, column and a Array[] data and Core.Mean() returns a scalar, so I can't do something like:
myMat.put(row,1,Core.Mean(myTempColumn)).
So how this operation can be done?
I'm wondering that I'll need to get each element from myMat using get and then sum. But the problem is get returns also a Array[] data (that I think is the RGB value), and to sum it, will be necessary another for structure (which I don't think it is the easiest way).
Thank you in advance.
Ok solve it:
Core.reduce(imageMat,averageMat,0,Core.REDUCE_AVG);
Where:
0 means that the matrix is reduced to a single row. 1 means that the matrix is reduced to a single column.
Core.REDUCE_AVG - does the average
Related
I have a n x 2 array or 2 independent onedimensional arrays but connected. I would like to sort one of the vectors (or one of the 2 columns of the n x 2 array) and sort the other accordingly.
It’s no use saying that I should create a class and make a class array, because I want to have ready access to the 2 vectors.
This is scientific programming, so I don't want to have to extract number arrays from class arrays. It is very inefficient.
I believe that or I will have to do the sorting by hand, because apparently I cannot use a comparator function model because it apparently does not give me access to the indexes of an individual value exchange, that is being made depending on the result of the comparison.
Is there no way or is there a good solution to this problem?
I have an array of precomputed intensity (computed using a fuzzy logic inference system on a desktop machine). Now I want to use this array as a lookup table for a contrast enhancement application on android, using renderscript.
What I want to do, at a highlevel is to process every pixel in an image, and using the lookup table create a new image where the pixel at the corresponding position has the value looked up in the array. Before I start looking at how to implement this, is this even feasible?
Yes, it is feasible and this is something RS can handle with no problems. You'll need to provide your RS "kernel" with the pre-computed array data as either a separate Allocation or just a data array.
This talk will help get you started: https://youtu.be/3ynA92x8WQo
I'm actually using Math.sin() in my android app to calculate a sinus of a given angle (using Math.toRadians(angle_in_degrees)). For exemple when I want to get the Math.cos(90) which is 0, the result is 6.123233... E-17. Thanks you.
For floating point numbers, the system can often only approximate their values. For instance, the system would return something like 0.333333 for the expression (1.0 / 3). The number of 3s after the decimal point will be different depending on whether you're a floats or doubles, but it will still be limited to some finite length.
If you're just displaying the value, then you can limit the number of digits using something like String.format("%0.2f", value) or by rounding it using one of the rounding functions such as Math.round().
The tricky part comes when you need to compare the value to something. You can't just use if (value == some_constant) or even if (value == some_variable). At minimum, you usually have to use something like if (Math.abs(value - some_constant) < 0.001). The actual value of the '0.001' depends on the needs of your particular application and is customarily defined as a named constant.
For more complicated needs, you can implement the algorithm in the Floating-Point Guide.
You're getting back an approximation from Math.cos(Math.toRadians(90)) which is
6.123233... E-17 == 0.00000000000000006123233... which is basically 0
The following link should help clear things up as far as the precision of doubles/floats in programming.
http://www.java67.com/2015/09/float-and-double-value-comparison-in-java-use-relational.html
Why is it that when I call a SUM on one of my SQLite columns, it doesnt return a precise answer? Heres what I mean:
Lets say I have 4 rows in my column to sum
row1 8362.82
row2 +18837.42
row3 +7294.12
row4 +73.23
___________________
17567.59
Now these should add up to 17567.59 right? Well my sum returns 17567.6. This may not sound like a big deal but I need accurate decimals, not rounded ones. It rounds further as the numbers get larger too. Can anyone explain a solution to this? Thanks.
Store the values as integers instead of floats.
I had the same roundoff problem, except mine were rounding to integers instead of maintaining one place after the decimal point. I switched to using total() instead of sum().
From the documentation:
The result of total() is always a floating point value. The result of
sum() is an integer value if all non-NULL inputs are integers. If any
input to sum() is neither an integer or a NULL then sum() returns a
floating point value which might be an approximation to the true sum.
This doesn't explain the issue in my case, because my column contained a mix of integer and floating-point values. But I decided to try it because of the difference, and it worked. It also sums all NULLs to 0 instead of NULL which is useful in many cases.
I am wondering how would I be able to run a SQLite order by in this manner
select * from contacts order by jarowinkler(contacts.name,'john smith');
I know Android has a bottleneck with user defined functions, do I have an alternative?
Step #1: Do the query minus the ORDER BY portion
Step #2: Create a CursorWrapper that wraps your Cursor, calculates the Jaro-Winkler distance for each position, sorts the positions, then uses the sorted positions when overriding all methods that require a position (e.g., moveToPosition(), moveToNext()).
Pre calculate string lengths and add them into separate column. Then sort entired table by that that length. Add indexes (if you can). Then add extra filters for example you don't want to compare "Srivastava Brahmaputra" to "John Smith". The length are out of wack by way too much so exclude these kind of comparison by length as a percentage of the total length. So if your word is 10 characters compare it only to words with 10+-2 or 10+-3 characters.
This way you will significantly reduce the number of times this algorithm needs to run.
Typically in the vocalbulary of 100 000 entries such filters reduce the number of comparisons to about 300. Unless you are doing a full blown record linkage and then I would wonder why use Android for that. You would still need to apply probabilistic methods for that and calculate scores and this is not a job for Android (at least not for now).
Also in MS SQL Server Jaro Winkler string distance wrapped into CLR function perform much better, since SQL Server doesn't supprt arays natively and much of the processing is around arrays. So implementation in T-SQL add too much overhead, but SQL-CLR works extremely fast.