opencv条码(5)边缘检测

代码其实可以很简洁

 1 #include <cv.h>
 2 #include <highgui.h>
 3 
 4 using namespace cv;
 5 
 6 int main(){
 7     // Read input image
 8     cv::Mat image= cv::imread("C:/testdir/barcode5.jpg", 0);  //0表示按黑白图读入
 9     if(!image.data) return 0;
10     Mat contours;
11     Canny(image, contours, 125, 350);
12     imshow("result", contours);
13     cv::waitKey();
14     return 0;
15 }

 不过为了看起来效果更好一点,可以将黑白对调再显示:

 1 #include <cv.h>
 2 #include <highgui.h>
 3 #include <iostream>
 4 #include <vector>
 5 
 6 using namespace std;
 7 using namespace cv;
 8 
 9 #define PI 3.1415926
10 
11 int main(){
12     Mat image=imread("C:/testdir/barcode0.jpg", 0);
13     Mat contours;
14     Canny(image, contours, 125, 350);       //对于image做边缘检测,结果处理在contours上
15     //--------inverted representation--------//
16     Mat contoursInv;
17     threshold(contours, contoursInv, 128, 255, THRESH_BINARY_INV);//将contours黑白对换,结果处理在contoursInv上
18     //---------------------------------------//
19     imshow("sdfsdf", contoursInv);
20     waitKey(0);
21     return 0;
22 }

 用霍夫变换检测线段

 1 #include <cv.h>
 2 #include <highgui.h>
 3 #include <iostream>
 4 #include <vector>
 5 
 6 using namespace std;
 7 using namespace cv;
 8 
 9 #define PI 3.1415926
10 
11 class LineFinder{
12 private:
13     Mat img;
14     vector<Vec4i>lines;
15     double deltaRho;
16     double deltaTheta;
17     int minVote;
18     double minLength;
19     double maxGap;
20 public:
21     LineFinder():deltaRho(1), deltaTheta(PI/180), minVote(10), minLength(0.), maxGap(0.){}
22 
23     void setAccResolution(double dRho, double dTheta){
24         deltaRho=dRho;
25         deltaTheta=dTheta;
26     }
27     void setMinVote(int minv){
28         minVote=minv;
29     }
30     void setLineLengthAndGap(double length, double gap){
31         minLength=length;
32         maxGap=gap;
33     }
34     vector<Vec4i>findLines(Mat& binary){
35         lines.clear();
36         HoughLinesP(binary, lines, deltaRho, deltaTheta, minVote, minLength, maxGap);
37         return lines;
38     }
39     void drawDetectedLines(Mat &image, Scalar color=Scalar(255, 255, 255)){
40         vector<Vec4i>::const_iterator it2=lines.begin();
41         while(it2!=lines.end()){
42             Point pt1((*it2)[0], (*it2)[1]);
43             Point pt2((*it2)[2], (*it2)[3]);
44             line(image, pt1, pt2, color);
45             ++it2;
46         }
47     }
48 };
49 
50 int main(){
51     Mat image=imread("C:/testdir/barcode0.jpg", 0);
52     Mat contours;
53     Canny(image, contours, 125, 350);       //对于image做边缘检测,结果处理在contours上
54 
55     LineFinder finder;
56     finder.setLineLengthAndGap(100, 20);
57     finder.setMinVote(80);
58 
59     vector<Vec4i>lines=finder.findLines(contours);
60     finder.drawDetectedLines(image);
61     namedWindow("Detected Lines with HoughP");
62     imshow("Detected Lines with HoughP", image);
63 
64 
65   //  imshow("abracadabra", result);
66 
67     waitKey(0);
68     return 0;
69 }

 

posted @ 2013-05-01 21:56  ChrisZZ  阅读(965)  评论(0编辑  收藏  举报