opencv2實現單張圖片的路線路牌檢測_電腦視覺大作業2

來源:互聯網
上載者:User

標籤:opencv2   電腦視覺   

有好多代碼沒有用

linefiner.h

#if !defined LINEF#define LINEF#include<cmath>#include <opencv2/core/core.hpp>#include <opencv2/imgproc/imgproc.hpp>#define PI 3.1415926class LineFinder {  private:  // original image  cv::Mat img;  // vector containing the end points   // of the detected lines  std::vector<cv::Vec4i> lines;  // accumulator resolution parameters  double deltaRho;  double deltaTheta;  // minimum number of votes that a line   // must receive before being considered  int minVote;  // min length for a line  double minLength;  // max allowed gap along the line  double maxGap;  public:  // Default accumulator resolution is 1 pixel by 1 degree  // no gap, no mimimum length  LineFinder() : deltaRho(1), deltaTheta(PI/180), minVote(10), minLength(0.), maxGap(0.) {}  // Set the resolution of the accumulator  void setAccResolution(double dRho, double dTheta) {  deltaRho= dRho;  deltaTheta= dTheta;  }  // Set the minimum number of votes  void setMinVote(int minv) {  minVote= minv;  }  // Set line length and gap  void setLineLengthAndGap(double length, double gap) {  minLength= length;  maxGap= gap;  }  // Apply probabilistic Hough Transform  std::vector<cv::Vec4i> findLines(cv::Mat& binary) {  lines.clear();  cv::HoughLinesP(binary,lines,deltaRho,deltaTheta,minVote, minLength, maxGap);  return lines;  }  // Draw the detected lines on an image  void drawDetectedLines(cv::Mat &image, cv::Scalar color=cv::Scalar(255,0,0)) {  // Draw the lines  std::vector<cv::Vec4i>::const_iterator it2= lines.begin();  while (it2!=lines.end()) {  cv::Point pt1((*it2)[0],(*it2)[1]);          cv::Point pt2((*it2)[2],(*it2)[3]);  double slope = fabs(double((*it2)[1]-(*it2)[3])/((*it2)[0]-(*it2)[2]));   // double slope = fabs (((double)(lines[1].y-lines[0].y))/((double)(lines[1].x-lines[0].x)));//求直線在座標系中的斜率//double length=sqrt((line[1].y-line[0].y)*(line[1].y-line[0].y)+(line[1].x-line[0].x)*(line[1].x-line[0].x));////線段的長度//if((slope>0.3)&&(slope<1000)&&(length>50)&&(length<500))//{//line[0].y= line[0].y+ROI_rect_src.y;//line[1].y =line[1].y+ROI_rect_src.y;//cvLine(frame, line[0], line[1], CV_RGB(255,0,0), 3, CV_AA, 0 );//}if((slope>0.5)&&(slope<2)){  cv::line( image, pt1, pt2, color,3,8,0);}  ++it2;  }  }  // Eliminates lines that do not have an orientation equals to  // the ones specified in the input matrix of orientations  // At least the given percentage of pixels on the line must   // be within plus or minus delta of the corresponding orientation  //std::vector<cv::Vec4i> removeLinesOfInconsistentOrientations( // const cv::Mat &orientations, double percentage, double delta) {//  std::vector<cv::Vec4i>::iterator it= lines.begin();//  // check all lines//  while (it!=lines.end()) {//  // end points//  int x1= (*it)[0];//  int y1= (*it)[1];//  int x2= (*it)[2];//  int y2= (*it)[3]; //  //  // line orientation + 90o to get the parallel line//  double ori1= atan2(static_cast<double>(y1-y2),static_cast<double>(x1-x2))+PI/2;//  if (ori1>PI) ori1= ori1-2*PI;//  double ori2= atan2(static_cast<double>(y2-y1),static_cast<double>(x2-x1))+PI/2;//  if (ori2>PI) ori2= ori2-2*PI;//  // for all points on the line//  cv::LineIterator lit(orientations,cv::Point(x1,y1),cv::Point(x2,y2));//  int i,count=0;//  for(i = 0, count=0; i < lit.count; i++, ++lit) { //  float ori= *(reinterpret_cast<float *>(*lit));//  // is line orientation similar to gradient orientation ?//  if (std::min(fabs(ori-ori1),fabs(ori-ori2))<delta)//  count++;//  }//  double consistency= count/static_cast<double>(i);//  // set to zero lines of inconsistent orientation//  if (consistency < percentage) { //  (*it)[0]=(*it)[1]=(*it)[2]=(*it)[3]=0;//  }//  ++it;//  }//  return lines;  //}};#endif






main.cpp

#include <iostream>#include <vector>#include <opencv2/core/core.hpp>//#include <opencv2/imageproc/imageproc.hpp>#include <opencv2/highgui/highgui.hpp>#include "linefinder.h"//#include "edgedetector.h"using namespace cv;using namespace std;#define PI 3.1415926int main(){// Read input imageMat image= imread("1.jpg",1);if (!image.data)return 0; //CvRect ROI_rect_src;  //矩形框的位移和大小 //ROI_rect_src.x =0;//方形的最左角的x-座標//ROI_rect_src.y =0;//方形的最上或者最下角的y-座標//ROI_rect_src.width =image.size().width;//寬//ROI_rect_src.height =3000;//高    // Display the imageMat mf1(image.size(),image.type());      medianBlur(image,mf1,3); //Mat image;//cvtColor(image,image,CV_BGR2GRAY);GaussianBlur(image,image,Size(5,5),1.5);namedWindow("Original Image");imshow("Original Image",image);Mat img=image(Rect(0.4*image.cols,0.58*image.rows,0.4*image.cols,0.3*image.rows));Mat contours;Canny(img,contours,80,100);cv::Mat contoursInv;cv::threshold(contours,contoursInv,128,255,cv::THRESH_BINARY_INV);    // Display the image of contourscv::namedWindow("Canny Contours");cv::imshow("Canny Contours",contoursInv);// Create LineFinder instanceLineFinder ld;// Set probabilistic Hough parametersld.setLineLengthAndGap(80,30);ld.setMinVote(30);//Mat img=image(Rect(0.2*contours.cols,0.6*contours.rows,0.5*contours.cols,0.25*contours.rows));// Detect linesvector<Vec4i> li= ld.findLines(contours);ld.drawDetectedLines(img);//ld.removeLinesOfInconsistentOrientations(img,0.4,0.1);namedWindow(" HoughP");imshow(" HoughP",img);namedWindow("Detected Lines with HoughP");imshow("Detected Lines with HoughP",image);Mat imgGry;cvtColor(image,imgGry,CV_BGR2GRAY);GaussianBlur(imgGry,imgGry,Size(5,5),1.5);vector<Vec3f> circles;HoughCircles(imgGry, circles, CV_HOUGH_GRADIENT, 2,   // accumulator resolution (size of the image / 2) 50,  // minimum distance between two circles200, // Canny high threshold 100, // minimum number of votes 25, 50); // min and max radiuscout << "Circles: " << circles.size() << endl;// Draw the circles//image= imread("chariot.jpg",0);vector<Vec3f>::const_iterator itc= circles.begin();while (itc!=circles.end()) {  circle(image,   Point((*itc)[0], (*itc)[1]), // circle centre  (*itc)[2], // circle radius  Scalar(255), // color   2); // thickness  ++itc;}namedWindow("Detected Circles");imshow("Detected Circles",image);waitKey();return 0;}


結果:


把左邊兩條線變為一條:

Mat mf1(image.size(),image.type());      medianBlur(image,mf1,3); //Mat image;//cvtColor(image,image,CV_BGR2GRAY);GaussianBlur(image,image,Size(5,5),1.5);
注釋掉就好了。


只是一張圖片,具有特殊性,不知道其他圖片什麼結果。


opencv2實現單張圖片的路線路牌檢測_電腦視覺大作業2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.