opencv: convert Scalar to float or double type opencv: convert Scalar to float or double type c c

opencv: convert Scalar to float or double type


I use

double s;s = sum(arg1)[0];


EDIT

From the OpenCV docs:

sum
...
The functions sum calculate and return the sum of array elements, independently for each channel.

The output images which Sobel generates are binary images with one channel, as your Sum1 and Sum2 Scalars result out of the you need to use atan(Sum1[0]/Sum2[0]) to compute principal value of the arc tangent.

WRONG as Log-Gabor filter should be applied …

Looks like you try to do a lot of stuff, which could be handled by cv::filter2D() … If you want to apply a Gabor filter to your image then take a look at this, which I found here:

#include <opencv2/core/core.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>#include <math.h>cv::Mat mkKernel(int ks, double sig, double th, double lm, double ps){    int hks = (ks-1)/2;    double theta = th*CV_PI/180;    double psi = ps*CV_PI/180;    double del = 2.0/(ks-1);    double lmbd = lm;    double sigma = sig/ks;    double x_theta;    double y_theta;    cv::Mat kernel(ks,ks, CV_32F);    for (int y=-hks; y<=hks; y++)    {        for (int x=-hks; x<=hks; x++)        {            x_theta = x*del*cos(theta)+y*del*sin(theta);            y_theta = -x*del*sin(theta)+y*del*cos(theta);            kernel.at<float>(hks+y,hks+x) = (float)exp(-0.5*(pow(x_theta,2)+pow(y_theta,2))/pow(sigma,2))* cos(2*CV_PI*x_theta/lmbd + psi);        }    }    return kernel;}int kernel_size=21;int pos_sigma= 5;int pos_lm = 50;int pos_th = 0;int pos_psi = 90;cv::Mat src_f;cv::Mat dest;void Process(int , void *){    double sig = pos_sigma;    double lm = 0.5+pos_lm/100.0;    double th = pos_th;    double ps = pos_psi;    cv::Mat kernel = mkKernel(kernel_size, sig, th, lm, ps);    cv::filter2D(src_f, dest, CV_32F, kernel);    cv::imshow("Process window", dest);    cv::Mat Lkernel(kernel_size*20, kernel_size*20, CV_32F);    cv::resize(kernel, Lkernel, Lkernel.size());    Lkernel /= 2.;    Lkernel += 0.5;    cv::imshow("Kernel", Lkernel);    cv::Mat mag;    cv::pow(dest, 2.0, mag);    cv::imshow("Mag", mag);}int main(int argc, char** argv){    cv::Mat image = cv::imread("cat.jpg",1);    cv::imshow("Src", image);    cv::Mat src;    cv::cvtColor(image, src, CV_BGR2GRAY);    src.convertTo(src_f, CV_32F, 1.0/255, 0);    if (!kernel_size%2)    {        kernel_size+=1;    }    cv::namedWindow("Process window", 1);    cv::createTrackbar("Sigma", "Process window", &pos_sigma, kernel_size, Process);    cv::createTrackbar("Lambda", "Process window", &pos_lm, 100, Process);    cv::createTrackbar("Theta", "Process window", &pos_th, 180, Process);    cv::createTrackbar("Psi", "Process window", &pos_psi, 360, Process);    Process(0,0);    cv::waitKey(0);    return 0;}


Scalar is a 4-element vector of doubles derived from Vec, as shown in the opencv documentation (http://docs.opencv.org/2.4.9/modules/core/doc/basic_structures.html#scalar)

The function cv::sum sums the elements from each channel separately, if the matrix has more than one channel, and stores them on the Scalar Vec. Therefore, to access the doubles for each channel you must access the positions on the vector. (documentation: http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#sum)

//sum for first channeldouble sum1 = cv::sum(my_mat)[0];//sum for second channeldouble sum2 = cv::sum(my_mat)[1];//sum for third channeldouble sum3 = cv::sum(my_mat)[2];