Element-wise square of a matrix in EMGU CV - emgucv

I need the element-wise square of a matrix. In Matlab I find this code:
if A is matrix then A.^2 calculates the element wise square of the matrix. Is there any function in Emgu Cv that does the same?
Actually I need standard deviation of a matrix. If there a function of computing standard deviation of method for computing standard deviation then it will more helpful for me.

Element-wise square is the same as element-wise multiplication with the matrix itself. So, the following code line should do the trick (assuming the Matrix is called mat):
mat._Mul(mat);
Be aware though that this replaces the original mat. If you want to have it saved, you can do:
Matrix<byte> squaredMatrix = mat.Copy();
squaredMatrix._Mul(mat);

If you want the standard deviation, then the Image class provides a method AvgSdv that wraps the OpenCV equivalent function cvAvgSdv. Maybe you can do this by copying your Matrix to an Image first.
Implementation copied below for information:
/// <summary>
/// Calculates the average value and standard deviation of array elements, independently for each channel
/// </summary>
/// <param name="average">The avg color</param>
/// <param name="sdv">The standard deviation for each channel</param>
/// <param name="mask">The operation mask</param>
public void AvgSdv(out TColor average, out MCvScalar sdv, Image<Gray, Byte> mask)
{
average = new TColor();
MCvScalar avgScalar = new MCvScalar();
sdv = new MCvScalar();
CvInvoke.cvAvgSdv(Ptr, ref avgScalar, ref sdv, mask == null ? IntPtr.Zero : mask.Ptr);
average.MCvScalar = avgScalar;
}

If your data is an image rather than a matrix, you can use Image(TColor, TDepth).Mul Method:
http://www.emgu.com/wiki/files/2.3.0/document/html/01b9ba23-0058-3167-16ee-206a4befd7f3.htm
Here is the code:
Iamge.Mul(Iamge, 1);
For standard deviation of a matrix,try Image(TColor, TDepth).AvgSdv Method.

Related

Understanding log-softmax gradient implementation for Policy Gradient RL

I am trying to understand how to compute the gradient of log-softmax function in matrix form for policy function parameter update in Policy Gradient RL.
I found one Python implementation online from here using the Cartpole environment as an example.
In the example, the author uses softmax policy. This is implemented as per below:
def policy(state, w):
"""Computes softmax policy, i.e. the decision probabilities
for each action in our action space.
:param state: state vector,
:param theta: parameters,
:return: vector of decision probabilities.
"""
z = state.dot(w)
exp = np.exp(z)
return exp/np.sum(exp)
In the article, instead of computing the gradient of log-softmax directly, we obtain it by (1) first computing the Jacobian-matrix of the softmax, (2) then by dividing the a "sliced-Jacobian" with the policy probability and finally (3) by multiplying this ratio from (1) and (2) with the state vector.
The Jacobian of the softmax is computed using the following code:
def softmax_grad(softmax):
"""Computes the Jacobian of the softmax function."""
s = softmax.reshape(-1,1)
return np.diagflat(s) — np.dot(s, s.T)
The derivation of the Jacobian for the softmax function is rather easy. However, it is the next part that is confusing me:
# Computation of the gradient for log-softmax using Jacobian of softmax.
dsoftmax = softmax_grad(probs)[action, :] # Why do we only take one row?
dlog = dsoftmax / probs[0,action]
grad = state.T.dot(dlog[None, :]) # Why do we multiply with the state?
In the above, the grad is the final gradient for log-softmax function. I just don't understand why.
My questions are the following:
What is the mathematical justification for this computation?
How can we derive this computation mathematically?
Why are we slicing the Jacobian?
Why are we multiplying the resulting probabilities with the state?
I'd appreciate a formal and mathematical derivation for this. Any ideas?
Thanks!

Flow matrix from Earth Mover's Distance

I was reading some paper and it said:
By adopting the Earth Mover's Distance (EMD) algorithm, a flow matrix
f = {fij} from one histogram to another can be obtained.
I found an implementation for EMD in OpenCV. However, this implementation looks like:
float EMDL1(InputArray signature1, InputArray signature2);
It returns a single float value rather than a flow matrix. Is there a way to obtain the flow matrix using OpenCV?
While I was Writing the post I found the answer. It might help someone...
There is another function which is:
float EMD(InputArray signature1, InputArray signature2, int distType, InputArray cost=noArray(), float* lowerBound=0, OutputArray flow=noArray() );
flow is an output parameter that return the flow matrix.

Specify the timeshift parameter in Matlab's cwt()? (continuous 1-D wavelet transform)

I want to compute the wavelet of a signal with a given scale and timeshift.
In Matlab using the cwt() function (Continuous 1-D wavelet transform) provided in the Wavelet Toolbox I can specify the scale(s) I want as a parameter to cwt(), and it will return all possible timeshifts:
x = [1, 2, 3, 4];
scales = [3];
wavelet_name = 'db1';
coefs = cwt(x,scales, wavelet_name);
>> coefs =
-1.1553 -1.1553 -1.1553 1.7371
How can I specify the timeshift (instead of having cwt() computing all possible timeshifts)? I'm aiming at reducing the computation time as I have a bunch of signals to analyze.
To put it visually:
[coefs,frequencies] = cwt(x,scales,wname, samplingperiod) returns the
frequencies in cycles per unit time corresponding to the scales and
the analyzing wavelet wname. samplingperiod is a positive real-valued
scalar. If the units of samplingperiod are seconds, the frequencies
are in hertz.
This is taken directly from the Matlab cwt page. I think this might be what you're looking for.

opencv flann module: knn-search for hierarchical kmeans tree giving weird result

I have about 130,000 SIFT descriptors. I am building a hierarchical Kmeans-index using Opencv's flann module. After this I want to quantize these 130,000 descriptors (will quantize more later). I am using flann's knnsearch method for doing this. But the result of this method is something weird. For every descriptor the nearest index it is showing is the index of the descriptor itself. However, it should be displaying the cluster-ID of the nearest cluster which will be one of the leaves of the HIK-tree.
Should I try k=2
Here is a code snippet -
int k=1;
cv::flann::KMeansIndexParams indexParams(8,4,cvflann::FLANN_CENTERS_KMEANSPP) ;
cv::flann::Index hik_tree(cluster_data, indexParams);
Mat indices,dist;
hik_tree.knnSearch(cluster_data, indices, dist, k, cv::flann::SearchParams(64));
knnSearch is looking for the k-nearest neighbours in the index (it does not give the cluster-ID!). You build your index using cluster_data, and then you try to match cluster_data against itself. In this situation, it is not surprising that the closest neighbour to each descriptor is itself...
EDIT: If you want to get the centers, have a look at this (from the source of the FLANN library):
/**
* Chooses the initial centers using the algorithm proposed in the KMeans++ paper:
* Arthur, David; Vassilvitskii, Sergei - k-means++: The Advantages of Careful Seeding
*/
template <typename Distance>
class KMeansppCenterChooser : public CenterChooser<Distance>
{
...
k-NN is a supervised classification algorithm, that's why you are supposed to construct an Index object with your training samples, so use
cv::flann::Index hik_tree(samples, indexParams);
instead of
cv::flann::Index hik_tree(cluster_data, indexParams);

How to use flann based matcher, or generally flann in opencv?

http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_matchers.html#flannbasedmatcher
Please can somebody show me sample code or tell me how to use this class and methods.
I just want to match SURF's from a query image to those with an image set by applying Flann. I have seen many image match code in the samples but what still eludes me is a metric to quantify how similar an image is to other. Any help will be much appreciated.
Here's untested sample code
using namespace std;
using namespace cv;
Mat query; //the query image
vector<Mat> images; //set of images in your db
/* ... get the images from somewhere ... */
vector<vector<KeyPoint> > dbKeypoints;
vector<Mat> dbDescriptors;
vector<KeyPoint> queryKeypoints;
Mat queryDescriptors;
/* ... Extract the descriptors ... */
FlannBasedMatcher flannmatcher;
//train with descriptors from your db
flannmatcher.add(dbDescriptors);
flannmatcher.train();
vector<DMatch > matches;
flannmatcher.match(queryDescriptors, matches);
/* for kk=0 to matches.size()
the best match for queryKeypoints[matches[kk].queryIdx].pt
is dbKeypoints[matches[kk].imgIdx][matches[kk].trainIdx].pt
*/
Finding the most 'similar' image to the query image depends on your application. Perhaps the number of matched keypoints is adequate. Or you may need a more complex measure of similarity.
To reduce the number of false positives, you can compare the first most nearest neighbor to the second most nearest neighbor by taking the ratio of there distances.
distance(query,mostnearestneighbor)/distance(query,secondnearestneighbor) < T, the smaller the ratio is, the higher the distance of the second nearest neighbor to the query descriptor. This thus is a translation of high distinctiveness. Used in many computer vision papers that envision registration.

Resources