void houghTransform(cv::Mat img, float* &alpha)
{

	// rotate for hough detection
	img = rotateImg(img, 90);
	// canny edge detection
	cv::Mat imgMap;
	cannyDetector(img, imgMap); // retrives a binary image with contours of orange spots
	cvtColor(imgMap, mapDraw, CV_GRAY2BGR); //image for drawing

	// detect line ROI1
	cv::Rect roi1 = cv::Rect(0, 160, 120, 320);
	cv::rectangle(mapDraw, cv::Point(roi1.x,roi1.y), cv::Point(roi1.x+roi1.width,roi1.y+roi1.height), cv::Scalar( 0, 55, 255 ), +1, 4 );
	std::vector<cv::Vec4i> lines;
	HoughLinesP(imgMap(roi1), lines, 1, CV_PI/180, 50, 50, 20 );
	int p1x, p1y, p2x, p2y; // line start and final point

	if (lines.size() > 0) 
	{
		lineParam(roi1, lines, p1x, p1y, p2x, p2y);
		*alpha =  atan((float)(p2y-p1y)/(p2x-p1x));
		cv::line(mapDraw, cv::Point (p1x,p1y), cv::Point(p2x,p2y), cv::Scalar(0, 255, 0), 2, 8); //draw line
	}
	else *alpha = 999;
	*alpha =  atan((float)(p2y-p1y)/(p2x-p1x));

	//result visualization
	mapDraw = rotateImg(mapDraw, -90);
	cv::imshow("hough", mapDraw);
	
}
a
void lineDetector(cv::Mat imgSrc, float &alpha, float &lineEnd)
{

	// rotate image needed for hough detection
	imgSrc = rotateImg(imgSrc, 90);
	// detection of orange contours
	cv::Mat imgMapColor, imgMapMorpho, imgDraw;
	imgDraw  = cv::Mat::zeros( imgSrc.size(), imgSrc.type() );
	int h_min, h_max, m_e, m_d;
	h_min = 0; h_max = 10; // orange min and max hue
	m_e = 5; m_d = 15; // erode and dilate morphological kernels
	ml::findColoredContour(imgSrc, imgMapColor, imgMapMorpho, h_min, h_max, m_e, m_d);

	// parameters of line
	int roi_k; // roi counter (from 0 to 3)
	std::vector<std::vector<int> > linePoints; // point coordinates of the segemented line
	std::vector<float> lineAngles; // anlges of the segemented line
	
	// find line in ROI
	roi_k = 0; 	std::cout<<std::endl<<"Start line detector"<<std::endl;
	while(roi_k < 4)
	{
		lineROI(roi_k, imgMapMorpho, imgDraw, linePoints, lineAngles);
		roi_k++;
	}
	if(linePoints.size() > 0) // print line found
	{
		std::cout<<"Printing lines angles ending points... "<<std::endl;
		for(int i = 0; i < linePoints.size(); i++){
			std::cout<<"Line "<<i<<": "<<linePoints[i][2]<<" "<<linePoints[i][3]<<" "<<std::endl;
			std::cout<<"Angle "<<i<<": "<<lineAngles[i]<<std::endl;
		}
		alpha = lineAngles[0];
		lineEnd = linePoints[linePoints.size() - 1][2];
	}
	else
	{
	std::cout<<"No line found"<<std::endl;
		alpha = 999;
		lineEnd = 999;
	}

	//result visualization
	imgDraw = rotateImg(imgDraw, -90);
	//cv::imshow("source", imgSrc);
	cv::imshow("hough", imgDraw); cv::waitKey(10);
	
}
예제 #3
0
cv::Mat getSyntheticImage(const Mat& image) {
  int rand_type = rand();
  Mat result = image.clone();

  if (rand_type % 2 == 0) {
    int ran_x = rand() % 5 - 2;
    int ran_y = rand() % 5 - 2;

    result = translateImg(result, ran_x, ran_y);
  }
  else if (rand_type % 2 != 0) {
    float angle = float(rand() % 15 - 7);

    result = rotateImg(result, angle);
  }
  
  return result;
}
예제 #4
0
int _tmain(int argc, _TCHAR* argv[])
{
    srand((int)time(0));



    ////大凹点配置
    //int minR = 25;
    //int maxR = 60;

    ////小凹点 水渍配置
    //int minR = 15;
    //int maxR = 30;

    ////划痕配置
    //int minR = 30;
    //int maxR = 160;

    //指纹配置
    int minR = 60;
    int maxR = 200;

    int deflection = 50;//中心点随机偏离距离

    //cv::Mat src = cv::imread("A9划痕凹点_x3.jpg");
    //cv::Point center = cv::Point(962, 4394);//大凹点
    //string path = "samples\\大凹点A_";
    //cv::Point center = cv::Point(1398, 4473);//小凹点
    //string path = "samples\\小凹点A_";
    //cv::Point center = cv::Point(882, 6244);
    //string path = "samples\\水渍A4_";
    //cv::Point center = cv::Point(1677, 3400);
    //string path = "samples\\划痕A2_";
    //cv::Point center = cv::Point(2110, 9000);//小凹点
    //string path = "samples\\釉面A2_";



    //cv::Mat src = cv::imread("D9划痕凹点_x3.jpg");
    //cv::Point center = cv::Point(852, 5638);//大凹点
    //string path = "samples\\大凹点D_";
    //cv::Point center = cv::Point(1284, 5708);//小凹点
    //string path = "samples\\小凹点D_";
    //cv::Point center = cv::Point(1832, 9057);//小凹点
    //string path = "samples\\划痕D2_";
    //cv::Point center = cv::Point(992, 10057);//小凹点
    //string path = "samples\\釉面D2_";



    //cv::Mat src = cv::imread("A14杂质_x3.jpg");
    //cv::Point center = cv::Point(1300, 2050);
    //string path = "samples\\指纹A2_";
    //cv::Point center = cv::Point(2130, 7226);
    //string path = "samples\\杂质A_";


    //cv::Mat src = cv::imread("D14杂质_x3.jpg");
    //cv::Point center = cv::Point(1200, 9400);
    //string path = "samples\\指纹D2_";
    //cv::Point center = cv::Point(2029, 7711);
    //string path = "samples\\杂质D_";


    //cv::Mat src = cv::imread("A31崩角_x3.jpg");
    //cv::Point center = cv::Point(2600,7900);
    //string path = "samples\\水纹A4_";


    cv::Mat src = cv::imread("D31崩角_x3.jpg");
    cv::Point center = cv::Point(1800, 3100);
    string path = "samples\\水纹D4_";




    int r = 400;
    cv::Mat ROI = src(cv::Rect(center.x - r, center.y - r, r + r, r + r));




    for (size_t i = 1; i <= 10; i++)
    {
        double angle = random(361);
        cv::Mat rotateMat = cv::getRotationMatrix2D(cv::Point(r, r), angle, 1);
        cv::Mat rotateImg;
        cv::warpAffine(ROI, rotateImg, rotateMat, ROI.size());

        int offsetX = random(deflection) - deflection/2;
        int offsetY = random(deflection) - deflection/2;

        int range = abs(random(maxR - minR)) + minR;
        cv::Mat ROI2 = rotateImg(cv::Rect(r - offsetX - range, r - offsetY - range, range + range, range + range));

        char num[4];
        sprintf(num, "%03d", i);
        stringstream ss;
        ss << path << num << ".jpg";
        cv::imwrite(ss.str(), ROI2);

        //cv::imshow("1", ROI2);
        //cv::waitKey(0);
    }

    return 0;
}
예제 #5
0
int main(int argc, char** argv)
{
  int height ,width ,step ,channels;
  int same, lighter;
  float thresh;  
  uchar *dataB, *dataG, *dataR, *dataGray, *dataD;
  uchar b1, g1, r1, b2, g2, r2;
  int w = 3;
  int th = 50;
  int idx1, idx2;

  cv::Mat img = cv::imread(argv[1]);
  height = img.rows;
  width = img.cols;

  cv::namedWindow("Image0", cv::WINDOW_NORMAL);
  cv::Mat textImg(1000, 1200, CV_8UC1, cv::Scalar(255));
  cv::putText(textImg, "Original Image:", cv::Point(400, 500), cv::FONT_HERSHEY_SIMPLEX, 2, cv::Scalar(0, 0, 0));
  //cv::imshow("Image0", textImg);
  //cv::waitKey();
  //cv::imshow("Image0", img);
  //cv::waitKey();

  textImg.setTo(cv::Scalar(255, 255, 255));
  cv::putText(textImg, "Next: Apply SUSAN algorithm to detect edge and cross.", cv::Point(200, 500), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  cv::putText(textImg, "Press any key to continue...", cv::Point(400, 600), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  //cv::imshow("Image0", textImg);
  //cv::waitKey();

  std::vector<cv::Mat> imgChannels;
  cv::split(img, imgChannels);
  cv::Mat dstSusan(height, width, CV_8UC1, cv::Scalar(0));
  cv::Mat grayImg(height, width, CV_8UC1, cv::Scalar(0));
    
  step = imgChannels[0].step[0];
  dataB = imgChannels[0].data;
  dataG = imgChannels[1].data;
  dataR = imgChannels[2].data;
  dataGray = grayImg.data;
  dataD= dstSusan.data;

  for (int x = w; x < width-w; x++)
    {
      for (int y = w; y < height-w; y++)
	{
	  same = 0;
	  idx1 = x + y * step;
	  b1 = dataB[idx1];
	  g1 = dataG[idx1];
	  r1 = dataR[idx1];
	  for (int u = 0; u < w+1; u++)
	    {
	      for (int v = 0; v < w+1; v++)
		{
		  if (u + v == 0)
		    {
		      continue;
		    }
		  idx2 = (x+u) + (y+v) * step;
		  b2 = dataB[idx2];
		  g2 = dataG[idx2];
		  r2 = dataR[idx2];
		  if (calc_dist(b1, g1, r1, b1, g2, r2) < th)
		    {
		      same += 1;
		    }

		  idx2 = (x-u) + (y+v) * step;
		  b2 = dataB[idx2];
		  g2 = dataG[idx2];
		  r2 = dataR[idx2];

		  if (u != 0 && calc_dist(b1, g1, r1, b1, g2, r2) < th)
		    {
		      same += 1;
		    }

		  idx2 = (x+u) + (y-v) * step;
		  b2 = dataB[idx2];
		  g2 = dataG[idx2];
		  r2 = dataR[idx2];
		  if (v != 0 && calc_dist(b1, g1, r1, b1, g2, r2) < th)
		    {
		      same += 1;
		    }

		  idx2 = (x-u) + (y-v) * step;
		  b2 = dataB[idx2];
		  g2 = dataG[idx2];
		  r2 = dataR[idx2];
		  if (u != 0 && v != 0 && calc_dist(b1, g1, r1, b1, g2, r2) < th)
		    {
		      same += 1;
		    }
		}
	    }
	  dataD[idx1] = uchar(255.0 * float(same) / ((2*w+1) * (2*w+1) - 1));
	  if (dataD[idx1] < 128)
	    {
	      dataD[idx1] = 255;
	    }
	  else
	    {
	      dataD[idx1] = 0;
	    }
	}
    }

  //cv::imshow("Image0", dstSusan);
  cv::imwrite("outimg_1.jpg", dstSusan);
  textImg.setTo(cv::Scalar(255, 255, 255));
  cv::putText(textImg, "Next: Apply Hough algorithm to detect lines.", cv::Point(300, 500), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  cv::putText(textImg, "Press any key to continue...", cv::Point(400, 600), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  //cv::waitKey();
  //cv::imshow("Image0", textImg);
  //cv::waitKey();

  //Hough line detection
  std::vector<cv::Vec4i> lines;
  HoughLinesP(dstSusan, lines, 1, CV_PI/180, 80, 500, 20);

  double thetaSum = 0.0;
  int thetaNum = 0;
  double theta;
  for(size_t i = 0; i < lines.size(); i++)
    {  
      cv::Vec4i l = lines[i];
      cv::line(img, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(186,88,255), 1, CV_AA);
      if (l[0] == l[2])
	{
	  theta = CV_PI / 2;
	}
      else
	{
	  theta = std::atan(-double(l[3]-l[1]) / (l[2] - l[0]));
	}
      if (theta >= -CV_PI / 4 && theta <= CV_PI / 4)
	{
	  thetaSum += theta;
	  thetaNum += 1;
	}
    }

  theta = -thetaSum / thetaNum * 180 / CV_PI;
  
  //cv::imshow("Image0", img);
  cv::imwrite("outimg_2.jpg", img);
  //cv::waitKey();
  textImg.setTo(cv::Scalar(255, 255, 255));
  std::ostringstream textStr;
  textStr << "Find " << lines.size() << " lines.";
  cv::putText(textImg, textStr.str(), cv::Point(500, 400), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  textStr.str(std::string());
  textStr.clear();
  textStr << "Rotating angle is " << theta << " degree.";
  cv::putText(textImg, textStr.str(), cv::Point(350, 500), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  cv::putText(textImg, "Next: Rotating the image.", cv::Point(400, 600), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  cv::putText(textImg, "Press any key to continue...", cv::Point(400, 700), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  //cv::imshow("Image0", textImg);
  //cv::waitKey();

  img.release();
  img = cv::imread(argv[1]);
  imgChannels[0].release();
  imgChannels[1].release();
  imgChannels[2].release();
  imgChannels.clear();
  
  cv::Mat rotateImg(height, width, CV_8UC3);
  cv::Point2f center;
  center.x = float(width / 2.0 + 0.5);
  center.y = float(height / 2.0 + 0.5);
  cv::Mat affineMat = getRotationMatrix2D(center, theta, 1);
  cv::warpAffine(img,rotateImg, affineMat, cv::Size(width, height), CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS);

  //cv::imshow("Image0", rotateImg);
  cv::imwrite("outimg_3.jpg", rotateImg);
  //cv::waitKey();
  textImg.setTo(cv::Scalar(255, 255, 255));
  cv::putText(textImg, "Next: Transform the image to gray scale.", cv::Point(300, 500), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  cv::putText(textImg, "Press any key to continue...", cv::Point(400, 600), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  //cv::imshow("Image0", textImg);
  //cv::waitKey();

  cv::split(rotateImg, imgChannels);
  dataB = imgChannels[0].data;
  dataG = imgChannels[1].data;
  dataR = imgChannels[2].data;
  step = imgChannels[0].step[0];
  //imgChannels[2].setTo(cv::Scalar(0));
  for (int x = 0; x < rotateImg.cols; x++)
    {
      for (int y = 0; y < rotateImg.rows; y++)
	{
	  int idx = x + y * step;
	  if (dataB[idx] < dataG[idx] && dataB[idx] < dataR[idx])
	    {
	      dataG[idx] = dataB[idx];
	      dataR[idx] = dataB[idx];
	    }
	  if (dataG[idx] < dataB[idx] && dataG[idx] < dataR[idx])
	    {
	      dataB[idx] = dataG[idx];
	      dataR[idx] = dataG[idx];
	    }
	  if (dataR[idx] < dataB[idx] && dataR[idx] < dataG[idx])
	    {
	      dataB[idx] = dataR[idx];
	      dataG[idx] = dataR[idx];
	    }
	}
    }
  cv::Mat filterRedImg(rotateImg.rows, rotateImg.cols, CV_8UC3, cv::Scalar::all(255));
  cv::merge(imgChannels, filterRedImg);

  cv::cvtColor(filterRedImg, grayImg, CV_BGR2GRAY);

  //cv::imshow("Image0", grayImg);
  cv::imwrite("outimg_4.jpg", grayImg);
  //cv::waitKey();
  textImg.setTo(cv::Scalar(255, 255, 255));
  cv::putText(textImg, "Next: Clean the noise.", cv::Point(450, 500), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  cv::putText(textImg, "Press any key to continue...", cv::Point(400, 600), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  //cv::imshow("Image0", textImg);
  //cv::waitKey();

  step = grayImg.step[0];
  for (int x = 0; x < width; x++)
    {
      for (int y = 0; y < height; y++)
	{
	  int idx = x + y * step;
	  if (grayImg.data[idx] > 100)
	  //if(!is_gray(dataB[idx], dataG[idx], dataR[idx]))
	    {
	      grayImg.data[idx] = 255;
	    }
	}
    }

  //cv::imshow("Image0", grayImg);
  cv::imwrite("outimg_5.jpg", grayImg);
  //cv::waitKey();
  textImg.setTo(cv::Scalar(255, 255, 255));
  cv::putText(textImg, "Next: Digitizing the curves.", cv::Point(400, 500), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  cv::putText(textImg, "Press any key to continue...", cv::Point(400, 600), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  //cv::imshow("Image0", textImg);
  //cv::waitKey();

  cv::Mat newImg(height, width, CV_8UC3, cv::Scalar::all(255));
  
  SegmentFactory segFactory = SegmentFactory(0);
  std::vector<Segment *> segments;

  segFactory.makeSegments(grayImg, segments);

  std::vector<Segment *>::iterator itr;
  for (itr = segments.begin(); itr != segments.end(); itr++)
    {
      Segment *seg = *itr;
      std::vector<SegmentLine *>::iterator itr_l;
      for (itr_l = seg->m_lines.begin(); itr_l != seg->m_lines.end(); itr_l++)
	{
	  SegmentLine *line = *itr_l;
	  cv::line(newImg, cv::Point(line->m_x1, line->m_y1), cv::Point(line->m_x2, line->m_y2), cv::Scalar(186,88,255), 1, CV_AA);
	  std::cout << line->m_x1 << ", " << line->m_y1 << ", " << line->m_x2 << ", " << line->m_y2 << std::endl;
	}
    }

  //cv::imshow("Image0", newImg);
  cv::imwrite("outimg_6.jpg", newImg);
  //cv::waitKey();
  textImg.setTo(cv::Scalar(255, 255, 255));
  cv::putText(textImg, "Done.", cv::Point(550, 500), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255));
  //cv::imshow("Image0", textImg);
  //cv::waitKey();

  return 0;  
}