有好多代码没有用
linefiner.h
#if !defined LINEF
#define LINEF
#include
#include
#include
#define PI 3.1415926
class LineFinder {
private:
// original image
cv::Mat img;
// vector containing the end points
// of the detected lines
std::vector 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 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::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 removeLinesOfInconsistentOrientations(
// const cv::Mat &orientations, double percentage, double delta) {
// std::vector::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(y1-y2),static_cast(x1-x2))+PI/2;
// if (ori1>PI) ori1= ori1-2*PI;
// double ori2= atan2(static_cast(y2-y1),static_cast(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(*lit));
// // is line orientation similar to gradient orientation ?
// if (std::min(fabs(ori-ori1),fabs(ori-ori2))(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
#include
#include
//#include
#include
#include "linefinder.h"
//#include "edgedetector.h"
using namespace cv;
using namespace std;
#define PI 3.1415926
int main()
{
// Read input image
Mat 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 image
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);
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 contours
cv::namedWindow("Canny Contours");
cv::imshow("Canny Contours",contoursInv);
// Create LineFinder instance
LineFinder ld;
// Set probabilistic Hough parameters
ld.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 lines
vector 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 circles;
HoughCircles(imgGry, circles, CV_HOUGH_GRADIENT,
2, // accumulator resolution (size of the image / 2)
50, // minimum distance between two circles
200, // Canny high threshold
100, // minimum number of votes
25, 50); // min and max radius
cout << "Circles: " << circles.size() << endl;
// Draw the circles
//image= imread("chariot.jpg",0);
vector::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);
注释掉就好了。
只是一张图片,具有特殊性,不知道其他图片什么结果。