Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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;
}
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.º 7
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;
	}
}