// Laplacian-of-Gaussian (LoG) operator.
cv::Mat ImageFilter::LaplacianOfGaussianOperator::operator()(const cv::Mat& img, const std::size_t apertureSize, const double sigma) const
{
	const int halfApertureSize = (int)apertureSize / 2;

	// Laplacian of Gaussian (LoG).
	cv::Mat LoG(apertureSize, apertureSize, CV_64F);
	{
		//const double sigma2 = sigma * sigma, _2_sigma2 = 2.0 * sigma2, pi_sigma4 = M_PI * sigma2 * sigma2;
		const double sigma2 = sigma * sigma, _2_sigma2 = 2.0 * sigma2;
		for (int y = -halfApertureSize, yy = 0; y <= halfApertureSize; ++y, ++yy)
			for (int x = -halfApertureSize, xx = 0; x <= halfApertureSize; ++x, ++xx)
			{
				const double x2 = double(x) * double(x), y2 = double(y) * double(y), val = (x2 + y2) / _2_sigma2;
				const double exp = std::exp(-val);
				//LoG.at<double>(yy, xx) = (val - 1.0) * exp / pi_sigma4;
				LoG.at<double>(yy, xx) = (val - 1.0) * exp;
			}

		// Make sure that the sum (or average) of all elements of the kernel has to be zero (similar to the Laplace kernel) so that the convolution result of a homogeneous regions is always zero.
		//	REF [site] >> http://fourier.eng.hmc.edu/e161/lectures/gradient/node8.html
		LoG -= cv::sum(LoG) / ((double)apertureSize * (double)apertureSize);
	}

	cv::Mat deltaF;
	cv::filter2D(img, deltaF, CV_64F, LoG, cv::Point(-1, -1), 0, cv::BORDER_DEFAULT);

	return deltaF;
}
Exemple #2
0
cv::Mat laneTracker::laneMarkerDetect()
{
  int kernel_size = 11;
  float k[kernel_size] ;

  //calc 1-D kernel;
  int i,j;
  for(i = 0, j = -(kernel_size/2); i <= kernel_size; ++i, ++j)
  {
      k[i] = LoG(j);
      //std::cout<<" k["<<j<<"]: "<<k[i];
  }
  //std::cout<<std::endl;

  cv::Mat src, dst;
  cv::Mat roadRegion = gray_(ROAD_RECT(gray_.cols, gray_.rows));
  // blur gray source image
  // src region only has down-half size of origin gray image
  GaussianBlur(roadRegion, src, cv::Size(5,5), 0, 0);

  // dst has same size as origin gray image
  dst.create(gray_.size(), CV_MAKETYPE(CV_8U, src.channels()));
  dst = cv::Scalar::all(0);

  uchar *src_row, *src_pos_in_row, *dst_row;
  
  // so far, size of Mats are (if image w = 1, h = 1):
  // gray_      (1, 1)
  // roadRegion (1, 1/2)
  // src        (1, 1/2)
  // dst        (1, 1)
  std::cout<<"dst size: "<<dst.size()<<" cols:"<<dst.cols<<" rows:"<<dst.rows<<std::endl;
  std::cout<<"src size: "<<src.size()<<" cols:"<<src.cols<<" rows:"<<src.rows<<std::endl;
  float sum_f;
  // y-th for dst and src
  int yd,ys;
  for(yd = gray_.rows - src.rows, ys = 0; yd < gray_.rows; ++yd, ++ys)
  {
      // get pointer to y-th row
      src_row = src.ptr(ys); 
      dst_row = dst.ptr(yd); 
      for(int x = gray_.cols - src.cols; x < src.cols - kernel_size/2; ++x)
      {
          sum_f = 0.0;
          src_pos_in_row = src_row + x;
          for (int kx = 0; kx < kernel_size; ++kx)
          {
              sum_f += *(src_pos_in_row++) * k[kx];
          }
          *(dst_row + x + kernel_size/2) = static_cast<int>(sum_f);
      }
  }

  return dst;
}
Exemple #3
0
void EdgeDetectMarrHill::marr(float s, QImage *img)
{
    int width;
    float **smx;
    int i, j, n;
    float **lgau;

    /* Create a Gaussian and a derivative of Gaussian filter mask  */
    width = 3.35*s + 0.33;
    n = width + width + 1;

    qDebug() << ">>> Suavizando com Gaussiana de tamanho " << n << "x" << n;

    lgau = f2d (n, n);

    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            lgau[i][j] = LoG (distance((float)i, (float)j,
                                        (float)width, (float)width), s);

    /* Convolution of source image with a Gaussian in X and Y directions  */
    smx = f2d (img->width(), img->height());

    qDebug() << ">>> Convolucao com LoG";

    convolution (img, lgau, n, n, smx, img->height(), img->width());

    /* Locate the zero crossings */
    qDebug() << ">>> Passagens por Zero";
    zero_cross (smx, img);

    /* Clear the boundary */
    for (i = 0; i < img->width(); i++) {
        for (j = 0; j <= width; j++)
            img->setPixel(QPoint(i, j), qRgb(0, 0, 0));
        for (j = img->height() - width - 1; j < img->height(); j++)
            img->setPixel(QPoint(i, j), qRgb(0, 0, 0));
    }

    for (j = 0; j < img->height(); j++) {
        for (i = 0; i <= width; i++)
            img->setPixel(QPoint(i, j), qRgb(0, 0, 0));
        for (i = img->width() - width - 1; i < img->width(); i++)
            img->setPixel(QPoint(i, j), qRgb(0, 0, 0));
    }

    free(smx[0]); free(smx);
    free(lgau[0]); free(lgau);
}
Exemple #4
0
void EdgeDetection(double *src,double *dst,int width,int height,int detector,double threshold,int m_width,int m_height,double deta){
    double maxvalue=0;
    switch (detector) {
        case EDGE_DETECTOR_ROBERT:
        {
            maxvalue=Robert(src, dst,NULL, width, height);
            Threshold(dst, dst, width, height, maxvalue*threshold, THRESHOLD_TYPE3);
            break;
        }
        case EDGE_DETECTOR_PREWITT:
        {
            maxvalue=Prewitt(src, dst, width, height);
            Threshold(dst, dst, width, height, maxvalue*threshold, THRESHOLD_TYPE3);
            break;
        }
        case EDGE_DETECTOR_SOBEL:
        {
            maxvalue=Sobel(src, dst,NULL, width, height,(int)deta);
            Threshold(dst, dst, width, height, maxvalue*threshold, THRESHOLD_TYPE3);
            break;
        }
        case EDGE_DETECTOR_KIRSCH:
        {
            maxvalue=Kirsch(src, dst, width, height);
            Threshold(dst, dst, width, height, maxvalue*threshold, THRESHOLD_TYPE3);
            break;
        }
        case EDGE_DETECTOR_LOG:
        {
            maxvalue=LoG(src, dst, width, height,m_width,m_height,deta,threshold);
            Threshold(dst, dst, width, height, maxvalue*threshold, THRESHOLD_TYPE3);
            break;
        }
        case EDGE_DETECTOR_LAPLACE:
        {
            maxvalue=Laplace(src, dst, width, height);
            Threshold(dst, dst, width, height, maxvalue*threshold, THRESHOLD_TYPE3);
            break;
        }
        default:
            break;
    }
}
Exemple #5
0
int main( int argc, char** argv ) {
  /* opencv variables */
  Mat originalImage, filteredImage;

  /* command line variables */
  Argument args;

  char *inFile = NULL;
  char *outFile = NULL;

  /* command-line processing */
  if(commandProcessing(argc, argv, "hlgi:o:", &args) != 0) {
    return -1;
  }

  if(args.input_optarg != NULL) { /* check for input file */
    inFile = args.input_optarg;

    // TODO: open input file
    debug("input file: %s", inFile);
  } else {
    fprintf(stderr, "%s: missing input file operand\n", argv[0]);
    fprintf(stderr, "Try \'%s -h\' for help.\n", argv[0]);

    return -1;
  }

  if(args.output_optarg != NULL) { /* check for output file */
    outFile = args.output_optarg;

    // TODO: open output file
    debug("output file: %s", outFile);
  } else {
    fprintf(stderr, "%s: missing output file operand\n", argv[0]);
    fprintf(stderr, "Try \'%s -h\' for help.\n", argv[0]);

    return -1;
  }

  originalImage = imread(inFile, IMREAD_GRAYSCALE);

  if(originalImage.data == NULL) {
    fprintf(stderr, "%s: fatal error: couldn't open %s\n", argv[0],
              args.input_optarg);

    return -1;
  }

  /* check for filter option */
  if(args.laplacian_opt == 1
      && args.gaussian_opt == 1) { /* check for Laplacian of Gaussian filter */

    debug("LoG filter");

    // TODO: apply laplacian filter
    LoG(originalImage, filteredImage, (unsigned int) 3, 1.0, (unsigned int) 3);
  } else if(args.laplacian_opt == 1) { /* check for Laplacian filter */
    debug("Laplacian filter");

    // TODO: apply Laplacian filter
    Laplacian(originalImage, filteredImage, (unsigned int) 3);

  } else if(args.gaussian_opt == 1) { /* check for Gaussian filter */
    debug("Gaussian filter");

    // TODO: apply Gaussian filter
    Gaussian(originalImage, filteredImage, (unsigned int) 3, 1.0);
  } else {
    debug("none filter apllyed");
  }

  if(imwrite(outFile, filteredImage) == false) {
    fprintf(stderr, "%s: fatal error: couldn't save %s\n", argv[0],
            args.output_optarg);

    return -1;
  }

  #ifndef NDEBUG
    namedWindow("Original", WINDOW_AUTOSIZE);
    imshow("Original", originalImage);

    namedWindow("Filtered", WINDOW_AUTOSIZE);
    imshow("Filtered", filteredImage);

    waitKey(0);
  #endif

  return 0;
}
// ******************  Problem 1: Edge Detection ***********************
int p1a(void)
{
	// initialize data of input
	const char *filename1 = "elaine.raw";
	const char *filename2 = "noisy_elaine.raw";
	int SizeW = 256;
	int SizeH = 256;
	int BytesPerPixel = 1;
	unsigned char ImageData1[SizeH][SizeW][BytesPerPixel];
	unsigned char ImageData2[SizeH][SizeW][BytesPerPixel];

	// initialize data of output
	const char *newfilename1a = "1a elaine_soble_ans.raw";
	const char *newfilename1b = "1a elaine_LoG_ans.raw";
	const char *newfilename2a = "1a noisyelaine_soble_ans.raw";
	const char *newfilename2b = "1a noisyelaine_LoG_ans.raw";
	int newSizeW = SizeW;
	int newSizeH = SizeH;
	int newBytesPerPixel = BytesPerPixel;
	unsigned char newImageData1a[newSizeH][newSizeW][newBytesPerPixel];
	unsigned char newImageData1b[newSizeH][newSizeW][newBytesPerPixel];
	unsigned char newImageData2a[newSizeH][newSizeW][newBytesPerPixel];
	unsigned char newImageData2b[newSizeH][newSizeW][newBytesPerPixel];

	// read input
	readimage(filename1, &ImageData1[0][0][0], SizeH, SizeW, BytesPerPixel);
	readimage(filename2, &ImageData2[0][0][0], SizeH, SizeW, BytesPerPixel);

	// --------------------Sobel Detector------------------------
	int extend = 2;
	int thr1a = 24;
	int thr2a = 37;
	unsigned char Gradient1aR[SizeH][SizeW];
	unsigned char Gradient1aC[SizeH][SizeW];
	unsigned char Gradient1a[SizeH][SizeW];
	const char *gradient1aR = "1a elaine_s_R_grad.raw";
	const char *gradient1aC = "1a elaine_s_C_grad.raw";
	const char *gradient1a = "1a elaine_soble_grad.raw";
	unsigned char Gradient2aR[SizeH][SizeW];
	unsigned char Gradient2aC[SizeH][SizeW];
	unsigned char Gradient2a[SizeH][SizeW];
	const char *gradient2aR = "1a noisingelaine_s_R_grad.raw";
	const char *gradient2aC = "1a noisingelaine_s_C_grad.raw";
	const char *gradient2a = "1a noisingelaine_soble_grad.raw";

	GetSobleGradient(thr1a, &ImageData1[0][0][0], SizeH, SizeW, extend, &Gradient1aR[0][0], &Gradient1aC[0][0], &Gradient1a[0][0], &newImageData1a[0][0][0]);
	GetSobleGradient(thr2a, &ImageData2[0][0][0], SizeH, SizeW, extend, &Gradient2aR[0][0], &Gradient2aC[0][0], &Gradient2a[0][0], &newImageData2a[0][0][0]);

	// output
	writeimage(gradient1aR, &Gradient1aR[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(gradient1aC, &Gradient1aC[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(gradient1a, &Gradient1a[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(newfilename1a, &newImageData1a[0][0][0], newSizeH, newSizeW, newBytesPerPixel);
	writeimage(gradient2aR, &Gradient2aR[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(gradient2aC, &Gradient2aC[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(gradient2a, &Gradient2a[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(newfilename2a, &newImageData2a[0][0][0], newSizeH, newSizeW, newBytesPerPixel);

	// ----------------------LoG Detector------------------------------
	int Dfiltersize = 9;
	float sigma = 1.0;
	float thr1b = 0.9;
	float thr2b = 1.5;
	unsigned char Gradient1b[SizeH][SizeW];
	unsigned char ternarymap1[SizeH][SizeW];
	unsigned char Gradient2b[SizeH][SizeW];
	unsigned char ternarymap2[SizeH][SizeW];
	const char *gradient1b = "1a elaine_LoG_grad.raw";
	const char *ternary1 = "1a elaine_LoG_ter.raw";
	const char *gradient2b = "1a noisingelaine_LoG_grad.raw";
	const char *ternary2 = "1a noisingelaine_LoG_ter.raw";

	LoG(Dfiltersize, sigma, thr1b, &ImageData1[0][0][0], SizeH, SizeW, &Gradient1b[0][0], &ternarymap1[0][0], &newImageData1b[0][0][0]);
	LoG(Dfiltersize, sigma, thr2b, &ImageData2[0][0][0], SizeH, SizeW, &Gradient2b[0][0], &ternarymap2[0][0], &newImageData2b[0][0][0]);

	writeimage(gradient1b, &Gradient1b[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(ternary1, &ternarymap1[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(newfilename1b, &newImageData1b[0][0][0], newSizeH, newSizeW, newBytesPerPixel);
	writeimage(gradient2b, &Gradient2b[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(ternary2, &ternarymap2[0][0], SizeH, SizeW, BytesPerPixel);
	writeimage(newfilename2b, &newImageData2b[0][0][0], newSizeH, newSizeW, newBytesPerPixel);

	return 0;
}