Ejemplo n.º 1
0
void Nieto::nietoLaneMarkingsDetector(Mat1b &srcGRAY, Mat1b &dstGRAY, int tauInicio, int tauFim) {

	Mat1b tempDst = Mat1b(srcGRAY.size(), 0);

	int aux = 0;
	double alturaInicioVariacao = (double)srcGRAY.rows / 2;
	double tauTaxaVariacao = double(tauFim - tauInicio) / alturaInicioVariacao;
	int tau = tauInicio;
	for (int j = 0; j < srcGRAY.rows; ++j) {
		unsigned char *ptRowSrc = srcGRAY.ptr<uchar>(j);
		unsigned char *ptRowDst = tempDst.ptr<uchar>(j);
		if (j > alturaInicioVariacao) tau = int(tauInicio + tauTaxaVariacao * (j - alturaInicioVariacao));
		for (int i = tau; i < srcGRAY.cols - tau; ++i) {

			unsigned char aux2 = ptRowSrc[i];

			if (ptRowSrc[i] != 0) {
				aux = 2 * ptRowSrc[i];
				aux += -ptRowSrc[i - tau];
				aux += -ptRowSrc[i + tau];
				aux += -abs((int)(ptRowSrc[i - tau] - ptRowSrc[i + tau]));

				aux = (aux < 0) ? 0 : aux;
				aux = (aux > 255) ? 255 : aux;

				ptRowDst[i] = (unsigned char)aux;
			}
		}
	}
	dstGRAY = tempDst.clone();
}
Ejemplo n.º 2
0
void test_wcv( const char* path, const char *txt_path )
{
  Mat1b img;
  try { 
    img = imread( path, IMREAD_GRAYSCALE ); }
  catch (...) {
    return; }
  if (img.empty())
    return;

  //resize?if (img.rows())

  cout << path << endl;

#if 0
  blur( img, img, Size(3,3) );
#endif

//  imshow("kotsu", img);

  int hist[256]={0};
#if 1
  for (int y=0; y<img.rows; y++)
  {
    for (int x=0; x<img.cols; x++)
    {
      hist[ img[y][x] ]++;
    }
  }
#endif

  KOtsu kotsu( hist, sizeof(hist)/sizeof(hist[0]), 255 );

  ifstream cin(txt_path);
  int n;
  cin >> n;
  vector<int> arr(n + 1);
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  arr[n] = 256;
  int beg = 0;
  double ans = 0;
  for (int i = 0; i < n + 1; ++i)
  {
    int last_jump = beg;
    int pos = arr[i];
    
    double _sigsq  = kotsu._sigma_sq(last_jump, pos);
    ans +=  (kotsu.sum[pos] - kotsu.sum[last_jump])* _sigsq;
    beg = arr[i];
  }
  
  cout << "Kotsu wcv for input file with " << n << "cuts  result = " << (n + 1) * (n + 1) * ans / (img.cols * img.rows) << endl; 


}
Ejemplo n.º 3
0
void fht_vertical(Mat1b &input, Mat1i &outputl, Mat1i &outputr)
{
  
  // увеличение размера
  int st = 1;
  while (st < input.rows)
    st *= 2;
  Mat1b myInput(st, input.cols);
  myInput = 0;
  Mat roi(myInput, Rect(0,0,input.cols,input.rows));
  input.copyTo(roi);
 
  fht_vertical_iteration_l(myInput, outputl, 0, st);
  fht_vertical_iteration_r(myInput, outputr, 0, st);
  //imshow("output_fht_l", outputl * 10);
  //imshow("output_fht_r", outputr * 10);

  Mat1i revtestl, testl;
  
  //imshow ("testl", testl * 10);
 // cout << outputl.rows << " " << testr.rows << endl;
  //imshow("0", (testl - outputl) * 100);
  
  //imshow("output_test", out * 10);
  //cvWaitKey(0);
  // DrawLines(myInput, outputl, outputr, 4000);
   
}
Ejemplo n.º 4
0
void test_fht_vertical_l(Mat1b &input,  Mat1i &output)
{
  int st = 1;
  while (st < input.rows)
    st *= 2;
  Mat1b myInput(st, input.cols);
  myInput = 0;
  Mat roi(myInput, Rect(0,0,input.cols,input.rows));
  input.copyTo(roi);

  Mat1b rev = myInput - myInput;
  Mat1i revtestl, testl;
  for (int x = 0; x < myInput.cols; ++x)
  {
    for (int y = 0; y < myInput.rows; ++y)
    {
      rev(y, x) = myInput(y, myInput.cols - x - 1); 
    }
  }
  test_fht_vertical_r(rev, revtestl);
  testl = revtestl - revtestl;
  for (int x = 0; x < myInput.cols; ++x)
  {
    for (int y = 0; y < myInput.rows; ++y)
    {
      testl(y, x) = revtestl(y, myInput.cols - x - 1); 
    }
  }

  output = testl;
}
Ejemplo n.º 5
0
void displayBoolean(Mat1b& mat) {
  if (!img_widget) {
      img_widget = new ImageWidget(0);
      img_widget->show();
  }
  Mat1b dup = mat.clone();
  dup *= 255;

  img_widget->setImage(dup);
}
void computeEigenVector(const Mat1f& X, const Mat1b& mask, Mat1f& dst, int num_pca_iterations, const Mat1f& rand_vec)
{
    CV_DbgAssert( X.cols == rand_vec.cols );
    CV_DbgAssert( X.rows == mask.size().area() );
    CV_DbgAssert( rand_vec.rows == 1 );

    dst.create(rand_vec.size());
    rand_vec.copyTo(dst);

    Mat1f t(X.size());

    float* dst_row = dst[0];

    for (int i = 0; i < num_pca_iterations; ++i)
    {
        t.setTo(Scalar::all(0));

        for (int y = 0, ind = 0; y < mask.rows; ++y)
        {
            const uchar* mask_row = mask[y];

            for (int x = 0; x < mask.cols; ++x, ++ind)
            {
                if (mask_row[x])
                {
                    const float* X_row = X[ind];
                    float* t_row = t[ind];

                    float dots = 0.0;
                    for (int c = 0; c < X.cols; ++c)
                        dots += dst_row[c] * X_row[c];

                    for (int c = 0; c < X.cols; ++c)
                        t_row[c] = dots * X_row[c];
                }
            }
        }

        dst.setTo(0.0);
        for (int k = 0; k < X.rows; ++k)
        {
            const float* t_row = t[k];

            for (int c = 0; c < X.cols; ++c)
            {
                dst_row[c] += t_row[c];
            }
        }
    }

    double n = norm(dst);
    divide(dst, n, dst);
}
Ejemplo n.º 7
0
Mat1b Helper::skeleton(const Mat1b &binaryImage, const int size) {

	Mat1b img = binaryImage.clone();
	Mat skel(img.size(), CV_8UC1, Scalar(0));
	Mat temp;
	Mat eroded;

	Mat element = getStructuringElement(MORPH_CROSS, cv::Size(size, size));

	bool done;
	do {
		erode(img, eroded, element);
		dilate(eroded, temp, element); // temp = open(img)
		subtract(img, temp, temp);
		bitwise_or(skel, temp, skel);
		eroded.copyTo(img);

		done = (countNonZero(img) == 0);
	} while (!done);

	return skel;
}
Ejemplo n.º 8
0
void imadjust(const Mat1b& src, Mat1b& dst, int tol = 1, Vec2i in = Vec2i(0, 255), Vec2i out = Vec2i(0, 255))
{
    // src : input CV_8UC1 image
    // dst : output CV_8UC1 imge
    // tol : tolerance, from 0 to 100.
    // in  : src image bounds
    // out : dst image buonds

    dst = src.clone();

    tol = max(0, min(100, tol));

    if (tol > 0)
    {
        // Compute in and out limits

        // Histogram
        vector<int> hist(256, 0);
        for (int r = 0; r < src.rows; ++r) {
            for (int c = 0; c < src.cols; ++c) {
                hist[src(r,c)]++;
            }
        }

        // Cumulative histogram
        vector<int> cum = hist;
        for (int i = 1; i < hist.size(); ++i) {
            cum[i] = cum[i - 1] + hist[i];
        }

        // Compute bounds
        int total = src.rows * src.cols;
        int low_bound = total * tol / 100;
        int upp_bound = total * (100-tol) / 100;
        in[0] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), low_bound));
        in[1] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), upp_bound));

    }

    // Stretching
    float scale = float(out[1] - out[0]) / float(in[1] - in[0]);
    for (int r = 0; r < dst.rows; ++r)
    {
        for (int c = 0; c < dst.cols; ++c)
        {
            int vs = max(src(r, c) - in[0], 0);
            int vd = min(int(vs * scale + 0.5f) + out[0], out[1]);
            dst(r, c) = saturate_cast<uchar>(vd);
        }
    }
}
        /// @see FeatureExtractor::do_extract()
        // TODO scale the contour parts / histogram parts maybe
        virtual return_error_code::return_error_code do_extract( const Mat3r& original_image, const Mat1b& saliency_map, const Mat1b& saliency_mask, const vector<Contour>& contours, Vec1r& o_features) const {
            using namespace cv;
            assert( original_image.size() == saliency_map.size() && "original_image and saliency map must have same dimensions");
            return_error_code::return_error_code ret_contour, ret_histogram;
            o_features.clear();

            Vec1r contour_features, histogram_features;
            ret_contour   = _contour_extractor->extract( original_image, saliency_map, saliency_mask, contours, contour_features);
            ret_histogram = _histogram_extractor->extract( original_image, saliency_map, saliency_mask, contours, histogram_features);
            
            o_features.reserve( contour_features.size() + histogram_features.size());
            o_features.insert(o_features.end(), contour_features.begin(), contour_features.end());
            o_features.insert(o_features.end(), histogram_features.begin(), histogram_features.end());
            return ret_contour;
        }
Ejemplo n.º 10
0
void DrawLines(Mat1b &input, Mat1i &outputL, Mat1i &outputR, int trash)//рисует все линии вес которых больше чем trash
// для тестирования
{
  Mat1b draw_mat;
  input.copyTo(draw_mat);
  
  draw_mat = draw_mat * 10;
  for (int x = 0; x < outputR.cols; ++x)
    for (int s = 0; s < outputR.rows; ++s)
	    if (outputR(s, x) > trash)
		    line(draw_mat, Point(x, 0), Point(x + s, outputR.rows - 1),  255, 1);
  for (int x = 0; x < outputL.cols; ++x)
    for (int s = 0; s < outputL.rows; ++s)
      if (outputL(s, x) > trash)
		    line(draw_mat, Point(x, 0), Point(x - s, outputL.rows - 1),  255, 1);
  imshow ("lines_more_the_tresh", draw_mat);
  //cout << draw_mat.cols << " " << draw_mat.rows << endl;
  //cvWaitKey(0);
}
Ejemplo n.º 11
0
void test_fht_vertical_r(Mat1b &input,  Mat1i &output)
{
  int st = 1;
  while (st < input.rows)
    st *= 2;
  Mat1b myInput(st, input.cols);
  myInput = 0;
  Mat roi(myInput, Rect(0,0,input.cols,input.rows));
  input.copyTo(roi);

  output = Mat1i(st, myInput.cols);

  for (int i = 0; i < myInput.cols; ++i)
  {
    for (int i1 = 0; i1 < st; ++i1)
    {
      output(i1, i) = test_fht_vertical_line(myInput, Point(i, 0), Point(i + i1 + 1, st));
    }
  }
}
Ejemplo n.º 12
0
void xyVision::GetTarget::imadjust(const Mat1b& src, Mat1b& dst, int tol, Vec2i in, Vec2i out)
{
	dst = src.clone();
	tol = max(0, min(100, tol));
	if (tol > 0)
	{
		// Compute in and out limits
		// Histogram
		vector<int> hist(256, 0);
		for (int r = 0; r < src.rows; ++r) {
			for (int c = 0; c < src.cols; ++c) {
				hist[src(r,c)]++;
			}
		}
		// Cumulative histogram
		vector<int> cum = hist;
		for (int i = 1; i < (int)hist.size(); ++i) {
			cum[i] = cum[i - 1] + hist[i];
		}
		// Compute bounds
		int total = src.rows * src.cols;
		int low_bound = total * tol / 100;
		int upp_bound = total * (100-tol) / 100;
		in[0] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), low_bound));
		in[1] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), upp_bound));
	}
	// Stretching
	float scale = float(out[1] - out[0]) / float(in[1] - in[0]);
	for (int r = 0; r < dst.rows; ++r)
	{
		for (int c = 0; c < dst.cols; ++c)
		{
			int vs = max(src(r, c) - in[0], 0);
			int vd = min(int(vs * scale + 0.5f) + out[0], out[1]);
			dst(r, c) = saturate_cast<uchar>(vd);
		}
	}
}
void AdaptiveManifoldFilterN::computeEta(Mat& teta, Mat1b& cluster, vector<Mat>& etaDst)
{
    CV_DbgAssert(teta.size() == srcSize && cluster.size() == srcSize);

    Mat1f tetaMasked = Mat1f::zeros(srcSize);
    teta.copyTo(tetaMasked, cluster);
    
    float sigma_s = (float)(sigma_s_ / getResizeRatio());

    Mat1f tetaMaskedBlur;
    downsample(tetaMasked, tetaMaskedBlur);
    h_filter(tetaMaskedBlur, tetaMaskedBlur, sigma_s);

    Mat mul;
    etaDst.resize(jointCnNum);
    for (int i = 0; i < jointCnNum; i++)
    {
        multiply(tetaMasked, jointCn[i], mul);
        downsample(mul, etaDst[i]);
        h_filter(etaDst[i], etaDst[i], sigma_s);
        divide(etaDst[i], tetaMaskedBlur, etaDst[i]);
    }
}
Ejemplo n.º 14
0
Mat1b Helper::fillPerspectiva(const Mat1b &imgROI, const Rect &roi, const Size &tamanho, const uchar _default) {
	Mat1b perspectiva = Mat1b(tamanho, uchar(_default));
	imgROI.copyTo(perspectiva(roi));
	return perspectiva;
}
Ejemplo n.º 15
0
vector<vector<vector<Point> > > Nieto::vp(const Mat1b &inBinaryFrameFiltrado, Mat3b &inVanishingPointImage, const Rect &roi, const int maxNumLines, int houghThresholdInicial, int houghStep, double houghMinLineLength, double houghMaxLineGap) {

	double tempoInicio = static_cast<double>(getTickCount());

	// pega somente a regi�o de interesse
	Mat1b mascaraRoi = Mat1b(inBinaryFrameFiltrado.size(), uchar(0));
	mascaraRoi(Rect(roi.x, roi.y, roi.width, roi.height)).setTo(255);
	Mat1b binaryFrameFiltradoMasked;
	cv::bitwise_and(inBinaryFrameFiltrado, mascaraRoi, binaryFrameFiltradoMasked);
	if (display) cvtColor(inBinaryFrameFiltrado, inVanishingPointImage, CV_GRAY2BGR);

	// Hough
	vector<vector<Point> > lineSegments;
	vector<Point> aux;
	vector<Vec4i> lines;
	int houghThreshold = houghThresholdInicial;

	cv::HoughLinesP(binaryFrameFiltradoMasked, lines, 1, CV_PI / 180, houghThreshold, houghMinLineLength, houghMaxLineGap);
	while (lines.size() > maxNumLines) {
		lines.clear();
		houghThreshold += houghStep;
		cv::HoughLinesP(binaryFrameFiltradoMasked, lines, 1, CV_PI / 180, houghThreshold, houghMinLineLength, houghMaxLineGap);
	}
	for (size_t i = 0; i<lines.size(); i++)	{
		Point pt1, pt2;
		pt1.x = lines[i][0];
		pt1.y = lines[i][1];
		pt2.x = lines[i][2];
		pt2.y = lines[i][3];

		// modificado
		int dx = abs(pt2.x - pt1.x);
		int dy = abs(pt2.y - pt1.y);
		if (1.0*dx / 3.0 > dy) {
			if (display) line(inVanishingPointImage, pt1, pt2, CV_RGB(0, 0, 255), 1);
			// continue;
		} else{
			if (display) line(inVanishingPointImage, pt1, pt2, CV_RGB(0, 255, 0), 1);
		}

		// Store into vector of pairs of Points for msac
		aux.clear();
		aux.push_back(pt1);
		aux.push_back(pt2);
		lineSegments.push_back(aux);
	}

	// Multiple vanishing points
	vector<Mat> vps;			// vector of vps: vps[vpNum], with vpNum=0...numDetectedVps
	vector<vector<int> > CS;	// index of Consensus Set for all vps: CS[vpNum] is a vector containing indexes of lineSegments belonging to Consensus Set of vp numVp
	vector<int> numInliers;
	vector<vector<vector<Point> > > lineSegmentsClusters;
	MSAC msac;
	bool msac_verbose = false;
	int numVps = 1;

	msac.init(MODE_NIETO, inBinaryFrameFiltrado.size(), msac_verbose);
	msac.multipleVPEstimation(lineSegments, lineSegmentsClusters, numInliers, vps, numVps); // Call msac function for multiple vanishing point estimation
	msac.drawCS(inVanishingPointImage, lineSegmentsClusters, vps); // Draw line segments according to their cluster

	// sa�das
	this->houghLines = lineSegmentsClusters;
	this->vanishingPoint = vps;

	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- nieto.vp: " << tempoExecutando << " ms" << endl;
	if (display) imshow("Vanishing Point", inVanishingPointImage);

	return { lineSegments };
}
Ejemplo n.º 16
0
void Nieto::featureL_IPM(const Mat1b &inGrayFrameFiltradoRoi, IPM * _ipm, map<string, double> &outMeans0, map<string, double> &outCovs0, map<string, double> &outWeights0) {

	double tempoInicio = static_cast<double>(getTickCount());

	Size imgSize = inGrayFrameFiltradoRoi.size();

	// Imagens que ser�o constru�das
	Mat1b _binaryMarkings = Mat1b(imgSize, uchar(0));
	Mat1b _binaryPavement = Mat1b(imgSize, uchar(0));
	Mat1b binaryMarkings = Mat1b(imgSize, uchar(0)); // NA IPM
	Mat1b binaryPavement = Mat1b(imgSize, uchar(0)); // NA IPM

	// pega o threshold inicial que separa as duas gaussianas (Pavement < thres < LaneMarkings)
	// Nieto: A reasonable threshold that separates these two components is the standard deviation
	Scalar meanFiltro, stddevFiltro;
	cv::meanStdDev(inGrayFrameFiltradoRoi, meanFiltro, stddevFiltro);

	// seta as m�scaras dos elementos que pertecem a mabas as classes
	_binaryMarkings = inGrayFrameFiltradoRoi >  stddevFiltro[0];
	_binaryPavement = inGrayFrameFiltradoRoi <= stddevFiltro[0];

	_ipm->applyHomography(_binaryMarkings, binaryMarkings, INTER_NEAREST);
	_ipm->applyHomography(_binaryPavement, binaryPavement, INTER_NEAREST);


	map<string, Scalar> meansScalar, stddevScalar;
	// ************************************** PAVEMENT
	cv::meanStdDev(inGrayFrameFiltradoRoi, meansScalar["pavement"], stddevScalar["pavement"], _binaryPavement);
	outMeans0["pavement"] = getMean(inGrayFrameFiltradoRoi, _binaryPavement);
	outCovs0["pavement"] = getVariance(inGrayFrameFiltradoRoi.clone(), outMeans0["pavement"], _binaryPavement);

	// ************************************** LANE MARKINGS
	cv::meanStdDev(inGrayFrameFiltradoRoi, meansScalar["markings"], stddevScalar["markings"], _binaryMarkings);
	outMeans0["markings"] = getMean(inGrayFrameFiltradoRoi, _binaryMarkings);
	outCovs0["markings"] = getVariance(inGrayFrameFiltradoRoi.clone(), outMeans0["markings"], _binaryMarkings);

	// ************************************** OBJECTS
	outMeans0["objects"] = outMeans0["pavement"];
	outCovs0["objects"] = outCovs0["pavement"];

	// ************************************** UNKNOWN
	outMeans0["unknown"] = 255.0 / 2.0;
	outCovs0["unknown"] = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));

	// calcula os pesos => propor��o de cada classe
	double nPavements = countNonZero(binaryPavement);
	double nMarkings = countNonZero(binaryMarkings);
	double nTotal = nPavements + nMarkings;
	double nUnknown = nTotal * 0.05;
	nTotal += nUnknown;
	outWeights0["pavement"] = (nPavements / 2) / nTotal;
	outWeights0["objects"] = outWeights0["pavement"];
	outWeights0["markings"] = nMarkings / nTotal;
	outWeights0["unknown"] = nUnknown / nTotal;

	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- nieto.featureL: " << tempoExecutando << " ms" << endl;

	// if (config.display)
	// imshow("L - binaryMarkings", binaryMarkings);
	// imshow("L - binaryPavement", binaryPavement);


	/*
	if (display) {
	Mat1b imgResultNietoMasks = Mat1b(Size(grayFrameRoiIPM.cols, grayFrameRoiIPM.rows * 4), uchar(0));

	grayPavement.copyTo(imgResultNietoMasks(Rect(0, 0, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)));
	grayMarkings.copyTo(imgResultNietoMasks(Rect(0, grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)));
	grayObjects.copyTo(imgResultNietoMasks(Rect(0, 2 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)));

	imgResultNietoMasks(Rect(0, 3 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)).setTo(85, binaryPavement);
	imgResultNietoMasks(Rect(0, 3 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)).setTo(170, binaryObjects);
	imgResultNietoMasks(Rect(0, 3 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)).setTo(255, binaryMarkings);
	imgResultNietoMasks(Rect(0, 3 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)).setTo(0, binaryUnknown);

	imshow("Nieto - Mascaras", imgResultNietoMasks);

	// imshow("binaryIpmInvMask", binaryIpmInvMask);

	// descomente para visualizar o que foi calculado para o Pavement
	// imshow("grayPavement", grayPavement);
	// imshow("binaryPavement", binaryPavement);
	// cout << "p.mean: " << means0["pavement"] << ", p.covs: " << covs0["pavement"] << endl;

	// descomente para visualizar o que foi calculado para os Lane Markings
	// imshow("grayMarkings", grayMarkings);
	// imshow("binaryMarkings", binaryMarkings);
	// cout << "lm.mean: " << means0["markings"] << ", lm.covs: " << covs0["markings"] << endl;

	// descomente para visualizar o que foi calculado para os Objects
	// imshow("grayObjects", grayObjects);
	// imshow("binaryObjects", binaryObjects);
	// cout << "obj.mean: " << means0["objects"] << ", obj.covs: " << covs0["objects"] << endl;

	// descomente para visualizar o que foi calculado para o Unknown
	// imshow("grayUnknown", grayUnknown);
	// imshow("binaryUnknown", binaryUnknown);
	// cout << "unk.mean: " << means0["unknown"] << ", unk.covs: " << covs0["unknown"] << endl;

	// descomente para imprimir o peso inicial
	// cout << "w[pav]: " << outWeights0["pavement"] << endl;
	// cout << "w[lnm]: " << outWeights0["markings"] << endl;
	// cout << "w[obj]: " << outWeights0["objects"] << endl;
	// cout << "w[unk]: " << outWeights0["unknown"] << endl;
	}
	*/
}
int main(int argc, char** argv)
{
   if(argc != 2){
	cout<<"Provide input image";
        return 0;
   }

    // Read image
    Mat3b img = imread(argv[1]);

    // Binarize image. Text is white, background is black
    Mat1b bin;
    cvtColor(img, bin, COLOR_BGR2GRAY);
    bin = bin < 200;

    // Rotate the image according to the found angle
    Mat1b rotated;
    bin.copyTo(rotated);

   // Mat M = getRotationMatrix2D(box.center, box.angle, 1.0);    //warpAffine(bin, rotated, M, bin.size());

    // Compute horizontal projections
    Mat1f horProj;
    reduce(rotated, horProj, 1, CV_REDUCE_AVG);

    // Remove noise in histogram. White bins identify space lines, black bins identify text lines
    float th = 0;
    Mat1b hist = horProj <= th;

    // Get mean coordinate of white pixels groups
    vector<int> ycoords;
    int y = 0;
    int count = 0;
    bool isSpace = false;
    for (int i = 0; i < rotated.rows; ++i)
    {
        if (!isSpace)
        {
            if (hist(i))
            {
                isSpace = true;
                count = 1;
                y = i;
            }
        }
        else
        {
            if (!hist(i))
            {
                isSpace = false;
                ycoords.push_back(y / count);
            }
            else
            {
                y += i;
                count++;
            }
        }
    }
    // Draw line as final result
    Mat3b result;
    cvtColor(rotated, result, COLOR_GRAY2BGR);

if(ycoords.size()>0){
    for (int i = 0; i < ycoords.size()-1; i++)
    {
	Rect rect1;
	rect1.x = 0;
	rect1.y = ycoords[i];
	rect1.width = result.size().width;
	rect1.height = ycoords[i+1]-ycoords[i];
        if(rect1.height > 30){
		Mat Image1 = result(rect1);
		imshow("Display Image", Image1); 
		string name = "";
		std::stringstream ss; 
		ss << i;
		name = "Image"+ss.str()+".jpg";
		imwrite(name,Image1);
		waitKey(0);
        }
	if(i == ycoords.size()-2){
		Rect rect1;
		rect1.x = 0;
		rect1.y = ycoords[i+1];
		rect1.width = result.size().width;
		rect1.height = result.size().height-ycoords[i+1];
		if(rect1.height > 30){
			Mat Image1 = result(rect1);
			imshow("Display Image", Image1); 
			string name = "";
			std::stringstream ss; 
			ss << i+1;
			name = "Image"+ss.str()+".jpg";
			imwrite(name,Image1);
			waitKey(0);
		}	
	}
    }
}
else
   cout<<"No coordinates formed";
   return 0;
}
Ejemplo n.º 18
0
void Nieto::ExpectationMaximizationOpenCV(const Mat1b &inGrayFrameRoi, int maxIters, map<string, double> &_means0, map<string, double> &_covs0, map<string, double> &_weights0) {

	double tempoInicio = static_cast<double>(getTickCount());
	const int nClusters = 4; // 4 classes => {pavement, markings, objects, unknown}
	const bool aplicaResize = true;

	EM em = EM(nClusters, EM::COV_MAT_DIAGONAL);

	Mat1b grayFrameRoiClone = inGrayFrameRoi.clone();
	Mat1b trainGrayFrameRoiClone = inGrayFrameRoi.clone();
	if (aplicaResize) resize(grayFrameRoiClone, trainGrayFrameRoiClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d samples = trainGrayFrameRoiClone.reshape(1, trainGrayFrameRoiClone.rows * trainGrayFrameRoiClone.cols);

	// formata o _means0
	Mat1d means0 = Mat1d(nClusters, 1, CV_64FC1);
	means0.at<double>(0) = _means0["pavement"];
	means0.at<double>(1) = _means0["markings"];
	means0.at<double>(2) = _means0["objects"];
	means0.at<double>(3) = 255.0 / 2.0;

	// formata o _covs0
	vector<Mat> covs0 = {
		Mat1d(1, 1, _covs0["pavement"]),
		Mat1d(1, 1, _covs0["markings"]),
		Mat1d(1, 1, _covs0["objects"]),
		Mat1d(1, 1, ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3)))
	};

	// formata o _weights0
	// Mat1d weights0 = *(Mat1f(nClusters, 1, CV_64FC1) << 0.75, 0.10, 0.10, 0.05);
	Mat1d weights0 = *(Mat1f(nClusters, 1, CV_64FC1) <<
		_weights0["pavement"],
		_weights0["markings"],
		_weights0["objects"],
		_weights0["unknown"]
		);

	// cout << means0 << endl;

	em.set("maxIters", maxIters);
	em.trainE(samples, means0, covs0, weights0);

	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- em opencv (1 feature): " << tempoExecutando << " ms" << endl;
	if (display) {
		// predict
		Mat1b predictedImage = Mat1b(grayFrameRoiClone.size(), uchar(0));
		for (int j = 0; j < predictedImage.rows; ++j) {
			unsigned char *ptRowSrc = grayFrameRoiClone.ptr<uchar>(j);
			unsigned char *ptRowDst = predictedImage.ptr<uchar>(j);
			for (int i = 0; i < predictedImage.cols; ++i) {
				Vec2d emPredicted = em.predict(ptRowSrc[i]);
				switch ((int)emPredicted[1]) {
				case 0: ptRowDst[i] = 160; break;
				case 1: ptRowDst[i] = 255; break;
				case 2: ptRowDst[i] = 80; break;
				case 3: ptRowDst[i] = 0; break;
				}
			}
		}
		imshow("EM OpenCV - 1 Feature", predictedImage);
	}
}
Ejemplo n.º 19
0
void Nieto::ExpectationMaximizationOpenCV2Features(const Mat1b &imageI, const Mat1b &imageL,
	map<string, double> &i_means0, map<string, double> &i_covs0, map<string, double> &i_weights0,
	map<string, double> &l_means0, map<string, double> &l_covs0, map<string, double> &l_weights0, int maxIters) {

	double tempoInicio = static_cast<double>(getTickCount());
	const int nFeatures = 2; // 2 features => {I, L}
	const int nClusters = 4; // 4 classes => {pavement, markings, objects, unknown}
	const bool aplicaResize = true;

	// samples feature I
	Mat1b I_imageClone = imageI.clone();
	Mat1b I_trainImageClone = imageI.clone();
	if (aplicaResize) resize(I_imageClone, I_trainImageClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d I_samples = I_trainImageClone.reshape(1, I_trainImageClone.rows * I_trainImageClone.cols);

	// samples feature L
	Mat1b L_imageClone = imageL.clone();
	Mat1b L_trainImageClone = imageL.clone();
	if (aplicaResize) resize(L_imageClone, L_trainImageClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d L_samples = L_trainImageClone.reshape(1, L_trainImageClone.rows * L_trainImageClone.cols);

	// junta as amostras (uma em cada linha)
	Mat1d samplesArray[] = { I_samples, L_samples };
	Mat1d samples;
	cv::hconcat(samplesArray, 2, samples);
	
	// formata o _means0
	Mat1d means0 = Mat1d(nClusters, nFeatures, CV_64FC1);
	means0.at<double>(0, 0) = i_means0["pavement"];
	means0.at<double>(1, 0) = i_means0["markings"];
	means0.at<double>(2, 0) = i_means0["objects"];
	means0.at<double>(3, 0) = 255.0 / 2.0;
	means0.at<double>(0, 1) = l_means0["pavement"];
	means0.at<double>(1, 1) = l_means0["markings"];
	means0.at<double>(2, 1) = l_means0["objects"];
	means0.at<double>(3, 1) = 255.0 / 2.0;

	// formata o _covs0
	Mat1d covs0_pavement = Mat1d(Size(nFeatures, nFeatures), double(0));
	covs0_pavement.at<double>(0, 0) = i_covs0["pavement"];
	covs0_pavement.at<double>(1, 1) = l_covs0["pavement"];
	Mat1d covs0_markings = Mat1d(Size(nFeatures, nFeatures), double(0));;
	covs0_markings.at<double>(0, 0) = i_covs0["markings"];
	covs0_markings.at<double>(1, 1) = l_covs0["markings"];
	Mat1d covs0_objects = Mat1d(Size(nFeatures, nFeatures), double(0));;
	covs0_objects.at<double>(0, 0) = i_covs0["objects"];
	covs0_objects.at<double>(1, 1) = l_covs0["objects"];
	Mat1d covs0_unknown = Mat1d(Size(nFeatures, nFeatures), double(0));;
	covs0_unknown.at<double>(0, 0) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));
	covs0_unknown.at<double>(1, 1) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));
	vector<Mat> covs0 = {
		covs0_pavement,
		covs0_markings,
		covs0_objects,
		covs0_unknown
	};

	// formata o _weights0
	Mat1d weights0 = Mat1d(nClusters, 1, CV_64FC1);
	double total_i = i_weights0["pavement"] + i_weights0["markings"] + i_weights0["objects"] + i_weights0["unknown"];
	double total_l = l_weights0["pavement"] + l_weights0["markings"] + l_weights0["objects"] + l_weights0["unknown"];
	double total_weights = total_i + total_l;
	weights0.at<double>(0, 0) = (i_weights0["pavement"] + l_weights0["pavement"]) / total_weights;
	weights0.at<double>(1, 0) = (i_weights0["markings"] + l_weights0["markings"]) / total_weights;
	weights0.at<double>(2, 0) = (i_weights0["objects"] + l_weights0["objects"]) / total_weights;
	weights0.at<double>(3, 0) = (i_weights0["unknown"] + l_weights0["unknown"]) / total_weights;

	// cout << means0 << endl;

	// condi��es do EM
	// dims => samples.cols
	// if (!(&means0) || (!means0.empty() && means0.rows == nClusters && means0.cols == samples.cols && means0.channels() == 1)) cout << "means - ok!" << endl;

	EM em = EM(nClusters, EM::COV_MAT_DIAGONAL);
	em.set("maxIters", maxIters);
	em.trainE(samples, means0, covs0, weights0);
	
	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- em opencv (2 features): " << tempoExecutando << " ms" << endl;
	if (display) {
		// predict
		Mat1b predictedImage = Mat1b(I_imageClone.size(), uchar(0));
		for (int j = 0; j < predictedImage.rows; ++j) {
			unsigned char *ptRowI = I_imageClone.ptr<uchar>(j);
			unsigned char *ptRowL = L_imageClone.ptr<uchar>(j);
			unsigned char *ptRowDst = predictedImage.ptr<uchar>(j);
			for (int i = 0; i < predictedImage.cols; ++i) {
				
				Mat1d elementPredict = Mat1d(Size(2, 1), CV_64FC1);
				elementPredict.at<double>(0) = ptRowL[i];
				elementPredict.at<double>(1) = ptRowI[i];
				
				Vec2d emPredicted = em.predict(elementPredict);
				switch ((int)emPredicted[1]) {
				case 0: ptRowDst[i] = 160; break;
				case 1: ptRowDst[i] = 255; break;
				case 2: ptRowDst[i] = 80; break;
				case 3: ptRowDst[i] = 0; break;
				}
			}
		}
		imshow("EM OpenCV - 2 Features", predictedImage);
	}
}
Ejemplo n.º 20
0
void Nieto::ExpectationMaximizationArmadillo2Features(const Mat1b &imageI, const Mat1b &imageL, 
	map<string, double> &i_means0, map<string, double> &i_covs0, map<string, double> &i_weights0, 
	map<string, double> &l_means0, map<string, double> &l_covs0, map<string, double> &l_weights0, int maxIters) {

	double tempoInicio = static_cast<double>(getTickCount());
	const int nClusters = 4; // 4 classes => {pavement, markings, objects, unknown}
	bool aplicaResize = true;
	
	// samples feature I
	Mat1b I_imageClone = imageI.clone();
	Mat1b I_trainImageClone = imageI.clone();
	if (aplicaResize) resize(I_imageClone, I_trainImageClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d I_samples = I_trainImageClone.reshape(1, I_trainImageClone.rows * I_trainImageClone.cols);
	arma::mat I_armaSamples(reinterpret_cast<double*>(I_samples.data), I_samples.rows, I_samples.cols);
	
	// samples feature L
	Mat1b L_imageClone = imageL.clone();
	Mat1b L_trainImageClone = imageL.clone();
	if (aplicaResize) resize(L_imageClone, L_trainImageClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d L_samples = L_trainImageClone.reshape(1, L_trainImageClone.rows * L_trainImageClone.cols);
	arma::mat L_armaSamples(reinterpret_cast<double*>(L_samples.data), L_samples.rows, L_samples.cols);
	
	// junta as amostras (uma em cada linha)
	arma::mat armaSamples = arma::join_rows(I_armaSamples, L_armaSamples);
	// cout << "size armaSamples: " << arma::size(armaSamples.t()) << endl;

	// formata o _means0
	arma::mat means0(2, nClusters);
	means0.at(0, 0) = i_means0["pavement"];
	means0.at(0, 1) = i_means0["markings"];
	means0.at(0, 2) = i_means0["objects"];
	means0.at(0, 3) = 255.0 / 2.0;
	means0.at(1, 0) = l_means0["pavement"];
	means0.at(1, 1) = l_means0["markings"];
	means0.at(1, 2) = l_means0["objects"];
	means0.at(1, 3) = 255.0 / 2.0;
	// cout << "size means0: " << arma::size(means0) << endl;

	// formata o _covs0
	arma::mat covs0(2, nClusters);
	covs0.at(0, 0) = i_covs0["pavement"];
	covs0.at(0, 1) = i_covs0["markings"];
	covs0.at(0, 2) = i_covs0["objects"];
	covs0.at(0, 3) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));
	covs0.at(1, 0) = l_covs0["pavement"];
	covs0.at(1, 1) = l_covs0["markings"];
	covs0.at(1, 2) = l_covs0["objects"];
	covs0.at(1, 3) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));
	// cout << "size covs0: " << arma::size(covs0) << endl;

	// formata o _weights0
	arma::mat weights0(1, nClusters);/*
	double total_i = i_weights0["pavement"] + i_weights0["markings"] + i_weights0["objects"] + i_weights0["unknown"];
	double total_l = l_weights0["pavement"] + l_weights0["markings"] + l_weights0["objects"] + l_weights0["unknown"];
	double total_weights = total_i + total_l;
	weights0.at(0, 0) = (i_weights0["pavement"] + l_weights0["pavement"]) / total_weights;
	weights0.at(0, 1) = (i_weights0["markings"] + l_weights0["markings"]) / total_weights;
	weights0.at(0, 2) = (i_weights0["objects"] + l_weights0["objects"]) / total_weights;
	weights0.at(0, 3) = (i_weights0["unknown"] + l_weights0["unknown"]) / total_weights;
	*/

	double total_i = i_weights0["pavement"] + i_weights0["objects"] + i_weights0["unknown"] + l_weights0["markings"];
	weights0.at(0, 0) = i_weights0["pavement"] / total_i;
	weights0.at(0, 1) = l_weights0["markings"] / total_i;
	weights0.at(0, 2) = i_weights0["objects"] / total_i;
	weights0.at(0, 3) = i_weights0["unknown"] / total_i;
	// cout << "size weights0: " << arma::size(weights0) << endl;
	weights0.at(0, 0) = trunc(weights0.at(0, 0) * 1000) / 1000;
	weights0.at(0, 1) = trunc(weights0.at(0, 1) * 1000) / 1000;
	weights0.at(0, 2) = trunc(weights0.at(0, 2) * 1000) / 1000;
	weights0.at(0, 3) = trunc(weights0.at(0, 3) * 1000) / 1000;

	double diff = 1 - (weights0.at(0, 0) + weights0.at(0, 1) + weights0.at(0, 2) + weights0.at(0, 3));
	weights0.at(0, 3) += diff;

	// if (!(size(means0) != size(covs0))) cout << "1 - ok!" << endl;
	// if (!(weights0.n_cols != means0.n_cols)) cout << "2 - ok!" << endl;
	// if (!(weights0.n_rows != 1)) cout << "3 - ok!" << endl;

	arma::gmm_diag em;
	em.set_params(means0, covs0, weights0);
	em.means.print("means a: ");
	em.dcovs.print("dcovs a: ");
	em.hefts.print("hefts a: ");
	em.learn(armaSamples.t(), nClusters, arma::eucl_dist, arma::keep_existing, 0, 1000, 1e-10, false);
	em.means.print("means b: ");
	em.dcovs.print("dcovs b: ");
	em.hefts.print("hefts b: ");
	
	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- em armadillo (2 features): " << tempoExecutando << " ms" << endl;
	if (display) {
		// predict
		Mat1b predictedImage = Mat1b(imageI.size(), uchar(0));
		for (int j = 0; j < predictedImage.rows; ++j) {
			unsigned char *ptRowI = I_imageClone.ptr<uchar>(j);
			unsigned char *ptRowL = L_imageClone.ptr<uchar>(j);
			unsigned char *ptRowDst = predictedImage.ptr<uchar>(j);
			for (int i = 0; i < predictedImage.cols; ++i) {
				arma::vec v(2, 1);
				v << ptRowI[i] << ptRowL[i];
				int emPredicted = em.assign(v, arma::eucl_dist);
				switch (emPredicted) {
				case 0: ptRowDst[i] = 160; break;
				case 1: ptRowDst[i] = 255; break;
				case 2: ptRowDst[i] = 0; break;
				case 3: ptRowDst[i] = 0; break;
				}
			}
		}
		imshow("EM Armadillo - 2 Features", predictedImage);
	}
}
Ejemplo n.º 21
0
void run_kotsu( const char* path )
{
  Mat1b img;
  try { 
    img = imread( path, IMREAD_GRAYSCALE ); }
  catch (...) {
    return; }
  if (img.empty())
    return;

  //resize?if (img.rows())

  cout << path << endl;

#if 0
  blur( img, img, Size(3,3) );
#endif

  imshow("kotsu", img);

  int hist[256]={0};
#if 1
  for (int y=0; y<img.rows; y++)
  {
    for (int x=0; x<img.cols; x++)
    {
      hist[ img[y][x] ]++;
    }
  }
#endif

#if 0
  for (int y=1; y<img.rows-1; y++)
  {
    for (int x=1; x<img.cols-1; x++)
    {
      if (    img[y][x] >= img[y][x-1] && img[y][x] >= img[y][x+1]   // soft local maximum by x OR
      ||      img[y][x] >= img[y-1][x] && img[y][x] >= img[y+1][x]   // soft local maximum by y
      )
        hist[ img[y][x] ]++;
      else
      if (    img[y][x] <= img[y][x-1] && img[y][x] <= img[y][x+1]   // soft local maximum by x OR
      ||      img[y][x] <= img[y-1][x] && img[y][x] <= img[y+1][x]   // soft local maximum by y
      )
        hist[ img[y][x] ]++;
    }
  }
#endif

#if 0
  for (int y=1; y<img.rows-1; y++)
  {
    for (int x=1; x<img.cols-1; x++)
    {
      int eps = 0;
      if (    img[y][x] >= img[y][x-1]-eps && img[y][x] >= img[y][x+1]-eps   // soft local maximum by x AND
      &&      img[y][x] >= img[y-1][x]-eps && img[y][x] >= img[y+1][x]-eps   // soft local maximum by y
      )
        hist[ img[y][x] ]++;
      else
      if (    img[y][x] <= img[y][x-1]+eps && img[y][x] <= img[y][x+1]+eps   // soft local minimum by x AND
      &&      img[y][x] <= img[y-1][x]+eps && img[y][x] <= img[y+1][x]+eps   // soft local minimum by y
      )
        hist[ img[y][x] ]++;
    }
  }
#endif

  KOtsu kotsu( hist, sizeof(hist)/sizeof(hist[0]), 255 );

  kotsu.print();

  Mat1b bin_img;
  int thr = (int)threshold( img, bin_img, 0, 255, THRESH_OTSU );
  cout << "threshold( img, bin_img, 0, 255, THRESH_OTSU ) =>" << thr << endl;

  for (int ttt = thr-10; ttt < thr+20; ttt ++)
  {
    double ww = kotsu._weight(ttt, 256);
    double ls = kotsu._sigma_sq(0, ttt );
    double rs = kotsu._sigma_sq(ttt, 256 );
    double res_otsu = (1-ww) * ls + ww * rs;
    cout << "res_otsu(" << ttt << ") =>" << res_otsu << endl;
  }

  int kEscape = 27;
  int kPlus = 43;
  int kEquality = 61; // "=" has same button with "+"
  int kMinus = 45;

  int key=0;
  int classes = 6;
  while (key!=kEscape)
  {
    drawKOtsu( kotsu, classes, thr );
    drawBinarized( kotsu, classes, img );
    key = waitKey(0);
    cout << key << endl;
    if (key == kPlus || key == kEquality)
      classes = std::min( classes+1, int(kotsu.otsu.size())-1 );
    if (key == kMinus)
      classes = std::max( classes-1, 1 );
  }


}
Ejemplo n.º 22
0
int CLI(int argc, char** argv) {
  //  for (int argi = 0; argi < argc; ++argi)
  //    printf("argv[%i]:'%s'\n", argi, argv[argi]);
  if (argc < 3) // [exename] + 2 args
    return CLI_help(argc, argv);
  // detect order
  int order = -1;
  string order_str (argv[1]);
  if (order_str == "thin")
    order = THIN;
  else {
    printf("Unknown order '%s'\n", order_str.c_str());
    return CLI_help(argc, argv);
  }
  // check implementation
  string implementation_name (argv[2]);
  int first_file_idx = 2;
  if (order != VIDEO_COMPARER && order != BENCHMARK) {
    first_file_idx = 3;
    if (!VoronoiThinner::is_implementation_valid(implementation_name)) {
      printf("Unknown implementation '%s'\n", implementation_name.c_str());
      return CLI_help(argc, argv);
    }
  }
  if (argc < first_file_idx + 1) // [exename] [order] [file(s)]
    return CLI_help(argc, argv);

  VoronoiThinner thinner;
  // load files
  vector<Mat1b> files;
  for (int argi = first_file_idx; argi < argc; ++argi) {
    Mat1b file = imread(argv[argi], CV_LOAD_IMAGE_GRAYSCALE);
    if (file.empty())
      printf("Could not load file '%s'\n", argv[argi]);
    else
      files.push_back(file);
  } // end loop argi

  // process
  if (order == THIN) {
    for (unsigned int file_idx = 0; file_idx < files.size(); ++file_idx) {
      //Timer timer;
      bool ok = thinner.thin(files[file_idx], implementation_name, true);
      if (!ok) {
        printf("Failed thinning with implementation '%s'\n", implementation_name.c_str());
        continue;
      }
      //timer.printTime(implementation_name.c_str());
      // write file
      ostringstream out; out << "out_" << file_idx << ".png";
      imwrite(out.str(), thinner.get_skeleton());
      printf("Written file '%s'\n", out.str().c_str());
      // show res
      imshow("query", files[file_idx]);
      imshow(implementation_name, thinner.get_skeleton());
      waitKey(0);
    } // end loop file_idx
  } // end if (order == THIN)

  return 0;
} // end CLI()
Ejemplo n.º 23
0
void test_bcv( const char* path, const char *txt_path )
{
  Mat1b img;
  try { 
    img = imread( path, IMREAD_GRAYSCALE ); }
  catch (...) {
    return; }
  if (img.empty())
    return;

  //resize?if (img.rows())

  cout << path << endl;

#if 0
  blur( img, img, Size(3,3) );
#endif

//  imshow("kotsu", img);

  int hist[256]={0};
#if 1
  for (int y=0; y<img.rows; y++)
  {
    for (int x=0; x<img.cols; x++)
    {
      hist[ img[y][x] ]++;
    }
  }
#endif

  KOtsu kotsu( hist, sizeof(hist)/sizeof(hist[0]), 255 );

  ifstream cin(txt_path);
  int n;
  cin >> n;
  vector<int> arr(n + 1);
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  arr[n] = 256;
  int beg = 0;

  double mu = 0;
  double ans = 0;

  for (int i = 0; i < n + 1; ++i)
  {
    int a = beg;
    int b = arr[i];
    
    double w = 1.0 *(kotsu.sum[b] - kotsu.sum[a]) / (img.cols * img.rows);
    if (w == 0)
      continue;
    double mu_ = (kotsu.sum_x[b] - kotsu.sum_x[a]) / (w * img.cols * img.rows);
    mu += mu_ * w;
    ans += w * mu_ * mu_;
    beg = arr[i];
  }
  
  cout << "Kotsu bcv for input file with " << n << "cuts  result = " << ans - mu * mu << endl; 


}
Ejemplo n.º 24
0
void Nieto::featureI(const Mat1b &inBinaryFrameFiltradoRoiIPM, const Mat1b &grayFrameRoiIPM, map<string, double> &outMeans0, map<string, double> &outCovs0, map<string, double> &outWeights0, bool aplicarSobel) {
	
	double tempoInicio = static_cast<double>(getTickCount());
	
	Size imgSize = inBinaryFrameFiltradoRoiIPM.size();
	Mat1b binaryFrameFiltradoRoiIPMDilated;
	if (aplicarSobel) {
		// aplica o Sobel
		Mat1b grayFrameRoiIPMSobel, grayFrameRoiIPMSobelX, grayFrameRoiIPMSobelY;
		Sobel(grayFrameRoiIPM, grayFrameRoiIPMSobelX, CV_8U, 1, 0);
		Sobel(grayFrameRoiIPM, grayFrameRoiIPMSobelY, CV_8U, 0, 1);
		addWeighted(grayFrameRoiIPMSobelX, 0.5, grayFrameRoiIPMSobelY, 0.5, 0, grayFrameRoiIPMSobel);
		Mat1b binaryFrameRoiIPMSobel;
		threshold(grayFrameRoiIPMSobel, binaryFrameRoiIPMSobel, 20, 255, THRESH_BINARY);
		imshow("grayFrameRoiIPMSobel", binaryFrameRoiIPMSobel);

		// aplica um close para remover as sujeiras
		erode(binaryFrameRoiIPMSobel, binaryFrameRoiIPMSobel, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)), Point(-1, -1));
		dilate(binaryFrameRoiIPMSobel, binaryFrameRoiIPMSobel, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)), Point(-1, -1));

		// dilata para formar a m�scara
		int morph_size = 5;
		Mat1b dilate_kernel = getStructuringElement(MORPH_ELLIPSE, Size(1 * morph_size + 1, 1 * morph_size + 1), Point(morph_size, morph_size));
		dilate(binaryFrameRoiIPMSobel, binaryFrameFiltradoRoiIPMDilated, dilate_kernel, Point(-1, -1), 3);
		imshow("binaryFrameFiltradoRoiIPMDilated", binaryFrameFiltradoRoiIPMDilated);
	} else {
		// aplica o dilate no filtro do nieto na IPM
		int morph_size = 5;
		Mat1b dilate_kernel = getStructuringElement(MORPH_ELLIPSE, Size(1 * morph_size + 1, 1 * morph_size + 1), Point(morph_size, morph_size));
		dilate(inBinaryFrameFiltradoRoiIPM, binaryFrameFiltradoRoiIPMDilated, dilate_kernel, Point(-1, -1), 3);
	}

	// Imagens que ser�o constru�das
	// - gray{i} = grayMasked
	// - binary{i} = bin�rio da gray{i}
	Mat1b grayPavement = Mat1b(imgSize, uchar(0));
	Mat1b binaryPavement = Mat1b(imgSize, uchar(0));
	Mat1b grayMarkings = Mat1b(imgSize, uchar(0));
	Mat1b binaryMarkings = Mat1b(imgSize, uchar(0));
	Mat1b grayObjects = Mat1b(imgSize, uchar(0));
	Mat1b binaryObjects = Mat1b(imgSize, uchar(0));
	Mat1b grayUnknown = Mat1b(imgSize, uchar(0));
	Mat1b binaryUnknown = Mat1b(imgSize, uchar(0));

	// valores iniciais das gaussianas
	map<string, Scalar> meansScalar, stddevScalar, weightsScalar;

	// somente o que � imagem, na IPM, ficar� branco
	Mat1b binaryIpmInvMask = Mat1b(imgSize);
	cv::bitwise_not(grayFrameRoiIPM == 0, binaryIpmInvMask);

	// ************************************** PAVEMENT
	// calcula as m�scaras
	cv::bitwise_not(binaryFrameFiltradoRoiIPMDilated, binaryPavement);
	cv::bitwise_and(binaryIpmInvMask, binaryPavement, binaryPavement);
	cv::bitwise_and(grayFrameRoiIPM, binaryPavement, grayPavement);
	// calcula a m�dia e covari�ncia do pavimento
	cv::meanStdDev(grayFrameRoiIPM, meansScalar["pavement"], stddevScalar["pavement"], binaryPavement);
	outMeans0["pavement"] = meansScalar["pavement"][0];
	outCovs0["pavement"] = stddevScalar["pavement"][0] * stddevScalar["pavement"][0];
	
	// ************************************** LANE MARKINGS
	// calcula as m�scaras
	grayFrameRoiIPM.copyTo(grayMarkings);
	double thresMarkings = outMeans0["pavement"] + 3 * stddevScalar["pavement"][0];
	binaryMarkings = grayMarkings > thresMarkings;
	cv::bitwise_and(grayFrameRoiIPM, binaryMarkings, grayMarkings);
	// calcula a m�dia e covari�ncia dos lane markings
	cv::meanStdDev(grayFrameRoiIPM, meansScalar["markings"], stddevScalar["markings"], binaryMarkings);
	outMeans0["markings"] = meansScalar["markings"][0];
	outCovs0["markings"] = stddevScalar["markings"][0] * stddevScalar["markings"][0];
		
	// ************************************** OBJECTS
	// calcula as m�scaras
	grayFrameRoiIPM.copyTo(grayObjects);
	double thresObjects = outMeans0["pavement"] - 3 * stddevScalar["pavement"][0];
	binaryObjects = grayObjects < thresObjects;
	cv::bitwise_and(binaryIpmInvMask, binaryObjects, binaryObjects);
	cv::bitwise_and(grayFrameRoiIPM, binaryObjects, grayObjects);
	// calcula a m�dia e covari�ncia dos objetos
	cv::meanStdDev(grayFrameRoiIPM, meansScalar["objects"], stddevScalar["objects"], binaryObjects);
	outMeans0["objects"] = meansScalar["objects"][0];
	outCovs0["objects"] = stddevScalar["objects"][0] * stddevScalar["objects"][0];
		
	// ************************************** UNKNOWN
	// calcula as m�scaras
	cv::bitwise_or(binaryUnknown, binaryPavement, binaryUnknown);
	cv::bitwise_or(binaryUnknown, binaryObjects, binaryUnknown);
	cv::bitwise_or(binaryUnknown, binaryMarkings, binaryUnknown);
	cv::bitwise_not(binaryUnknown, binaryUnknown, binaryIpmInvMask);
	cv::bitwise_and(grayFrameRoiIPM, binaryUnknown, grayUnknown);
	// calcula a m�dia e covari�ncia dos desconhecidos
	cv::meanStdDev(grayFrameRoiIPM, meansScalar["unknown"], stddevScalar["unknown"], binaryUnknown);
	outMeans0["unknown"] = meansScalar["unknown"][0];
	outCovs0["unknown"] = stddevScalar["unknown"][0] * stddevScalar["unknown"][0];

	// calcula os pesos iniciais
	double nPavements = countNonZero(binaryPavement);
	double nMarkings = countNonZero(binaryMarkings);
	double nObjects = countNonZero(binaryObjects);
	double nUnknown = countNonZero(binaryUnknown);
	double nTotal = (nPavements + nMarkings + nObjects + nUnknown);
	outWeights0["pavement"] = nPavements / nTotal;
	outWeights0["markings"] = nMarkings / nTotal;
	outWeights0["objects"] = nObjects / nTotal;
	outWeights0["unknown"] = nUnknown / nTotal;

	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- nieto.featureI: " << tempoExecutando << " ms" << endl;
	if (display) {
		Mat1b imgResultNietoMasks = Mat1b(Size(grayFrameRoiIPM.cols, grayFrameRoiIPM.rows * 4), uchar(0));

		grayPavement.copyTo(imgResultNietoMasks(Rect(0, 0, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)));
		grayMarkings.copyTo(imgResultNietoMasks(Rect(0, grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)));
		grayObjects.copyTo(imgResultNietoMasks(Rect(0, 2 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)));

		imgResultNietoMasks(Rect(0, 3 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)).setTo(85, binaryPavement);
		imgResultNietoMasks(Rect(0, 3 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)).setTo(170, binaryObjects);
		imgResultNietoMasks(Rect(0, 3 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)).setTo(255, binaryMarkings);
		imgResultNietoMasks(Rect(0, 3 * grayFrameRoiIPM.rows, grayFrameRoiIPM.cols, grayFrameRoiIPM.rows)).setTo(0, binaryUnknown);

		imshow("Nieto - Mascaras", imgResultNietoMasks);

		// imshow("binaryIpmInvMask", binaryIpmInvMask);

		// descomente para visualizar o que foi calculado para o Pavement
		// imshow("grayPavement", grayPavement);
		// imshow("binaryPavement", binaryPavement);
		// cout << "p.mean: " << means0["pavement"] << ", p.covs: " << covs0["pavement"] << endl;

		// descomente para visualizar o que foi calculado para os Lane Markings
		// imshow("grayMarkings", grayMarkings);
		// imshow("binaryMarkings", binaryMarkings);
		// cout << "lm.mean: " << means0["markings"] << ", lm.covs: " << covs0["markings"] << endl;

		// descomente para visualizar o que foi calculado para os Objects
		// imshow("grayObjects", grayObjects);
		// imshow("binaryObjects", binaryObjects);
		// cout << "obj.mean: " << means0["objects"] << ", obj.covs: " << covs0["objects"] << endl;

		// descomente para visualizar o que foi calculado para o Unknown
		// imshow("grayUnknown", grayUnknown);
		// imshow("binaryUnknown", binaryUnknown);
		// cout << "unk.mean: " << means0["unknown"] << ", unk.covs: " << covs0["unknown"] << endl;

		// descomente para imprimir o peso inicial
		// cout << "w[pav]: " << outWeights0["pavement"] << endl;
		// cout << "w[lnm]: " << outWeights0["markings"] << endl;
		// cout << "w[obj]: " << outWeights0["objects"] << endl;
		// cout << "w[unk]: " << outWeights0["unknown"] << endl;
	}
}
void AdaptiveManifoldFilterN::buildManifoldsAndPerformFiltering(vector<Mat>& eta, Mat1b& cluster, int treeLevel)
{
    CV_DbgAssert((int)eta.size() == jointCnNum);

    //splatting
    Size etaSize = eta[0].size();
    CV_DbgAssert(etaSize == srcSize || etaSize == smallSize);

    if (etaSize == srcSize)
    {
        compute_w_k(eta, w_k, sigma_r_over_sqrt_2, treeLevel);
        etaFull = eta;
        downsample(eta, eta);
    }
    else
    {
        upsample(eta, etaFull);
        compute_w_k(etaFull, w_k, sigma_r_over_sqrt_2, treeLevel);
    }
    
    //blurring
    Psi_splat_small.resize(srcCnNum);
    for (int si = 0; si < srcCnNum; si++)
    {
        Mat tmp;
        multiply(srcCn[si], w_k, tmp);
        downsample(tmp, Psi_splat_small[si]);
    }
    downsample(w_k, Psi_splat_0_small);

    vector<Mat>& Psi_splat_small_blur = Psi_splat_small;
    Mat& Psi_splat_0_small_blur = Psi_splat_0_small;

    float rf_ss = (float)(sigma_s_ / getResizeRatio());
    float rf_sr = (float)(sigma_r_over_sqrt_2);
    RFFilterPass(eta, Psi_splat_small, Psi_splat_0_small, Psi_splat_small_blur, Psi_splat_0_small_blur, rf_ss, rf_sr);

    //slicing
    {
        Mat tmp;
        for (int i = 0; i < srcCnNum; i++)
        {
            upsample(Psi_splat_small_blur[i], tmp);
            multiply(tmp, w_k, tmp);
            add(sum_w_ki_Psi_blur_[i], tmp, sum_w_ki_Psi_blur_[i]);
        }
        upsample(Psi_splat_0_small_blur, tmp);
        multiply(tmp, w_k, tmp);
        add(sum_w_ki_Psi_blur_0_, tmp, sum_w_ki_Psi_blur_0_);
    }

    //build new manifolds
    if (treeLevel < curTreeHeight)
    {
        Mat1b cluster_minus, cluster_plus;

        computeClusters(cluster, cluster_minus, cluster_plus);

        vector<Mat> eta_minus(jointCnNum), eta_plus(jointCnNum);
        {
            Mat1f teta = 1.0 - w_k;
            computeEta(teta, cluster_minus, eta_minus);
            computeEta(teta, cluster_plus, eta_plus);
        }

        //free memory to continue deep recursion
        eta.clear();
        cluster.release();

        buildManifoldsAndPerformFiltering(eta_minus, cluster_minus, treeLevel + 1);
        buildManifoldsAndPerformFiltering(eta_plus, cluster_plus, treeLevel + 1);
    }
}
Ejemplo n.º 26
0
Mat1d Helper::from8Uto64F(const Mat1b &from) {
	Mat1d out;
	from.convertTo(out, CV_64F);
	return out;
}
Ejemplo n.º 27
0
Mat1b Helper::morphErode(const Mat1b &from, int size) {
	Mat1b saida = Mat1b(from.size(), uchar(0));
	Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(size, size));
	erode(from, saida, kernel);
	return saida;
}
Ejemplo n.º 28
0
void Nieto::ExpectationMaximizationArmadillo(const Mat1b &inGrayFrameRoi, int maxIters, map<string, double> &_means0, map<string, double> &_covs0, map<string, double> &_weights0) {

	double tempoInicio = static_cast<double>(getTickCount());
	const int nClusters = 4; // 4 classes => {pavement, markings, objects, unknown}

	Mat1b grayFrameRoiClone = inGrayFrameRoi.clone();
	Mat1b trainGrayFrameRoiClone = inGrayFrameRoi.clone();
	resize(grayFrameRoiClone, trainGrayFrameRoiClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d samples = trainGrayFrameRoiClone.reshape(1, trainGrayFrameRoiClone.rows * trainGrayFrameRoiClone.cols);
	arma::mat armaSamples(reinterpret_cast<double*>(samples.data), samples.rows, samples.cols);

	// cout << "size armaSamples: " << arma::size(armaSamples) << endl;

	// formata o _means0
	arma::mat means0(1, nClusters);
	means0.at(0, 0) = _means0["pavement"];
	means0.at(0, 1) = _means0["markings"];
	means0.at(0, 2) = _means0["objects"];
	means0.at(0, 3) = 255.0 / 2.0;

	// cout << "size means0: " << arma::size(means0) << endl;

	// formata o _covs0
	arma::mat covs0(1, nClusters);
	covs0.at(0, 0) = _covs0["pavement"];
	covs0.at(0, 1) = _covs0["markings"];
	covs0.at(0, 2) = _covs0["objects"];
	covs0.at(0, 3) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));

	// cout << "size covs0: " << arma::size(covs0) << endl;

	// formata o _weights0
	arma::mat weights0(1, nClusters);
	weights0.at(0, 0) = _weights0["pavement"];
	weights0.at(0, 1) = _weights0["markings"];
	weights0.at(0, 2) = _weights0["objects"];
	weights0.at(0, 3) = _weights0["unknown"];

	// cout << "size weights0: " << arma::size(weights0) << endl;

	// if (!(size(means0) != size(covs0))) cout << "1 - ok!" << endl;
	// if (!(weights0.n_cols != means0.n_cols)) cout << "2 - ok!" << endl;
	// if (!(weights0.n_rows != 1)) cout << "3 - ok!" << endl;

	arma::gmm_diag em;
	em.set_params(means0, covs0, weights0);
	em.learn(armaSamples.t(), nClusters, arma::eucl_dist, arma::keep_existing, 0, maxIters, 1e-10, false);

	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- em armadillo (1 feature): " << tempoExecutando << " ms" << endl;
	if (display) {
		// predict
		Mat1b predictedImage = Mat1b(grayFrameRoiClone.size(), uchar(0));
		for (int j = 0; j < predictedImage.rows; ++j) {
			unsigned char *ptRowSrc = grayFrameRoiClone.ptr<uchar>(j);
			unsigned char *ptRowDst = predictedImage.ptr<uchar>(j);
			for (int i = 0; i < predictedImage.cols; ++i) {
				arma::vec v;
				v << ptRowSrc[i];
				int emPredicted = em.assign(v, arma::eucl_dist);
				switch (emPredicted) {
				case 0: ptRowDst[i] = 160; break;
				case 1: ptRowDst[i] = 255; break;
				case 2: ptRowDst[i] = 80; break;
				case 3: ptRowDst[i] = 0; break;
				}
			}
		}
		imshow("EM Armadillo - 1 Feature", predictedImage);
	}
}