Beispiel #1
0
/* 1st and 2nd derivative of 1D gaussian
*/
void getGaussianDerivs(double sigma, int M, vector<double>& gaussian, vector<double>& dg, vector<double>& d2g)
{
	//	static double sqrt_two_pi = sqrt(TwoPi);
	int L;
	if (sigma < 0)
	{
		M = 1;
		L = 0;
		dg.resize(M); d2g.resize(M); gaussian.resize(M);
		gaussian[0] = dg[0] = d2g[0] = 1.0;
		return;
	}

	L = (M - 1) / 2;
	dg.resize(M); d2g.resize(M); gaussian.resize(M);
	getGaussianKernel(M, sigma, CV_64F).copyTo(Mat(gaussian));

	double sigma_sq = sigma * sigma;
	double sigma_quad = sigma_sq*sigma_sq;
	for (double i = -L; i < L + 1.0; i += 1.0)
	{
		int idx = (int)(i + L);
		// from http://www.cedar.buffalo.edu/~srihari/CSE555/Normal2.pdf
		dg[idx] = (-i / sigma_sq) * gaussian[idx];
		d2g[idx] = (-sigma_sq + i*i) / sigma_quad * gaussian[idx];
	}
}
Beispiel #2
0
Mat Histogram::applyKernel(int size,int type)
{

    if(_histMat.channels()>1)
        cvError(1,__FUNCTION__,"Only for 1D histograms",__FILE__,__LINE__);

    Mat output;
    Mat input=cv::Mat(1,3,CV_32FC1);
    Mat kernel=cv::Mat(size,1,CV_32FC1);

    if(type==1)
    {
    kernel.setTo(cv::Scalar::all(1));
    input.setTo(cv::Scalar::all(1));
    }
    else if(type==2)
    {
        kernel=getGaussianKernel(256,size,CV_32FC1);
    }

    Scalar sum=cv::sum(kernel);

    cv::filter2D(_histMat,output,_histMat.depth(),kernel,Point(-1,-1),0,BORDER_CONSTANT);
    if(type==1)
    {
    output.convertTo(output,output.type(),1.0/sum[0],0);
    }

    return output;


}
Beispiel #3
0
// Internal method to initialize to zero all vectors
void mdgkt::initialize()
{
    // Matrix with gaussian values for processing temporal frames.
    // thia return the following vector [0.106507,0.786986,0.106507]
    temporalGaussFilter = getGaussianKernel(TIME_WINDOW,SIGMA,CV_32F);

    spatialGaussFilter= (Mat_<double>(1,9) << 0.0113, 0.0838, 0.0113, 0.0838, 0.6193, 0.0838, 0.0113, 0.0838, 0.0113);
}
Beispiel #4
0
/*
img         :  input image
degradedImg :  degraded output image
filterDev   :  standard deviation of kernel for gaussian blur
snr         :  signal to noise ratio for additive gaussian noise
return      :  the used gaussian kernel
*/
Mat Dip4::degradeImage(Mat& img, Mat& degradedImg, double filterDev, double snr){

    int kSize = round(filterDev*3)*2 - 1;
   
    Mat gaussKernel = getGaussianKernel(kSize, filterDev, CV_32FC1);
    gaussKernel = gaussKernel * gaussKernel.t();
    filter2D(img, degradedImg, -1, gaussKernel);

    Mat mean, stddev;
    meanStdDev(img, mean, stddev);

    Mat noise = Mat::zeros(img.rows, img.cols, CV_32FC1);
    randn(noise, 0, stddev.at<double>(0)/snr);
    degradedImg = degradedImg + noise;
    threshold(degradedImg, degradedImg, 255, 255, CV_THRESH_TRUNC);
    threshold(degradedImg, degradedImg, 0, 0, CV_THRESH_TOZERO);

    return gaussKernel;
}
/* Create the heatmap kernel. This is applied when heat_point() is called. 
 */
void AttentionMap::create_kernel()
{
	if (g_linear_kernel)
	{
		// Linear kernel
		const float max_val = 1.0 * g_base_intensity;
		const float min_val = 0.0;
		const float interval = max_val - min_val;

		const int center = g_kernel_size / 2 + 1;
		const float radius = g_kernel_size / 2;

		m_kernel = Mat::zeros(g_kernel_size, g_kernel_size, CV_32F);
		for (int r = 0; r < g_kernel_size; ++r)
		{
			float* ptr = m_kernel.ptr<float>(r);
			for (int c = 0; c < g_kernel_size; ++c)
			{
				// Calculate the distance from the center	
				const float diff_x = static_cast<float>(abs(r - center));
				const float diff_y = static_cast<float>(abs(c - center));
				const float length = sqrt(diff_x*diff_x + diff_y*diff_y);
				if (length <= radius)
				{
					const float b = 1.0 - (length / radius);
					const float val = b*interval + min_val;
					ptr[c] = val;
				}
			}
		}
	}
	else
	{
		// Gaussian kernel
		Mat coeffs = getGaussianKernel(g_kernel_size, 0.0, CV_32F)*150*g_base_intensity;
		m_kernel = coeffs * coeffs.t();
	}
}
Beispiel #6
0
void myBlend::multiBandBlend(cv::Mat &limg, cv::Mat &rimg, int dx, int dy)
{
    if(dx % 2 == 0)
    {
        if(dx + 1 <= limg.cols && dx + 1 <=rimg.cols)
        {
            dx += 1;
        }
        else
        {
            dx -= 1;
        }
    }
    if(dy % 2 == 0)
    {
        if(dy + 1 <= limg.rows && dy + 1 <=rimg.rows)
        {
            dy += 1;
        }
        else
        {
            dy -= 1;
        }
    }


    std::vector<cv::Mat> llpyr, rlpyr;

    buildLaplacianMap(limg, llpyr, dx, dy, LEFT);
    buildLaplacianMap(rimg, rlpyr, dx, dy, RIGHT);

    int center = 0;
    int i, c;
    std::vector<cv::Mat> LS(level);
    for(int a = 0; a < llpyr.size(); a++)
    {
        cv::Mat k = getGaussianKernel(llpyr[a].cols, llpyr[a].rows, llpyr[a].cols);
        LS[a] = cv::Mat(llpyr[a].rows, llpyr[a].cols, CV_32FC3).clone();
        center = (int)(llpyr[a].cols / 2.0);
#pragma omp parallel for private(i, c)
        for(int j = 0; j < LS[a].rows; j++)
        {
            for(i = 0; i < LS[a].cols; i++)
            {
                for(c = 0; c < 3; c++)
                {
                    if(a == llpyr.size() - 1)
                        LS[a].at<cv::Vec3f>(j, i)[c] = llpyr[a].at<cv::Vec3f>(j, i)[c] * k.at<float>(j, i) + rlpyr[a].at<cv::Vec3f>(j, i)[c] * (1.0 - k.at<float>(j, i));
                    else
                        if(i == center)
                        {
                            LS[a].at<cv::Vec3f>(j, i)[c] = (llpyr[a].at<cv::Vec3f>(j, i)[c] + rlpyr[a].at<cv::Vec3f>(j, i)[c]) / 2.0;
                        }
                        else if(i > center)
                        {
                            LS[a].at<cv::Vec3f>(j, i)[c] = rlpyr[a].at<cv::Vec3f>(j, i)[c];
                        }
                        else
                        {
                            LS[a].at<cv::Vec3f>(j, i)[c] = llpyr[a].at<cv::Vec3f>(j, i)[c];
                        }
                }
            }
        }
    }

    cv::Mat result;
    for(int a = level - 1; a > 0; a--)
    {
        cv::pyrUp(LS[a], result, LS[a - 1].size());
#pragma omp parallel for private(i, c)
        for(int j = 0; j < LS[a - 1].rows; j++)
        {
            for(i =0; i < LS[a - 1].cols; i++)
            {
                for(c = 0; c < 3; c++)
                {
                    LS[a - 1].at<cv::Vec3f>(j, i)[c] = cv::saturate_cast<uchar>(LS[a - 1].at<cv::Vec3f>(j, i)[c] + result.at<cv::Vec3f>(j, i)[c]);
                }
            }
        }
    }

    result = LS[0].clone();

    blendImg(limg, result, dx, dy, LEFT);
    blendImg(rimg, result, dx, dy, RIGHT);
}
Beispiel #7
0
int main()
{
  size_t dataSize = 5;
  float factor = 2;
	std::vector<float> input(dataSize), output(dataSize);

	for (size_t i = 0; i < dataSize; ++i)
  {
		input[i] = static_cast<float>(23 ^ i);
    output[i] = 0.0f;
  }

  int kernelRadius = 5;
  std::vector<float> filter = getGaussianKernel(1.5, kernelRadius);

  try
  {
    std::string program(USE_BLUR ? "blur" : "saxpy");

    std::cout << "# Launching '" << program << "'" << std::endl;

    Processor p("src/kernels/" + program + ".cl", Processor::All_Devices);

    std::list<Processor::KernelArg> args;

    if (program == "blur")
    {
      args.push_back(Processor::KernelArg(Processor::KernelArg::IMAGE, "res/input.ppm", 0, false, Processor::KernelArg::INPUT));
      args.push_back(Processor::KernelArg(Processor::KernelArg::BUFFER, filter.data(), sizeof(float) * filter.size()));
      args.push_back(Processor::KernelArg(Processor::KernelArg::RAW, &kernelRadius, sizeof(kernelRadius)));
      args.push_back(Processor::KernelArg(Processor::KernelArg::IMAGE, "res/output.ppm", 0, false, Processor::KernelArg::OUTPUT));
    }
    else
    {
      args.push_back(Processor::KernelArg(Processor::KernelArg::BUFFER, input.data(), sizeof(float) * input.size(), true, Processor::KernelArg::INPUT));
      args.push_back(Processor::KernelArg(Processor::KernelArg::BUFFER, output.data(), sizeof(float) * output.size(), true, Processor::KernelArg::OUTPUT));
      args.push_back(Processor::KernelArg(Processor::KernelArg::RAW, &factor, sizeof(float)));
    }
    p.execute(program, args);
  }
  catch (std::exception const & e)
  {
    std::cerr << "Processor failed: " << e.what() << std::endl;
    return 1;
  }

#if USE_BLUR
  size_t kernelSize = kernelRadius * 2 + 1;
  for (size_t i = 0; i < kernelSize; ++i)
  {
    for (size_t j = 0; j < kernelSize; ++j)
      std::cout << (j == 0 ? "" : ", ") << filter[i * kernelSize + j];
    std::cout << std::endl;
  }
#else
  for (size_t i = 0; i < dataSize; ++i)
    std::cout << output[i] << " = " << factor << " * " << input[i] << std::endl;
#endif

	return 0;
}