//2.4.0
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <math.h>
using namespace cv;
using namespace std;
void my(Mat &img)
{
//中值滤波
medianBlur(img,img,3);
//边缘检测 上下和左右
Mat bmp = img.clone();
Sobel(img,bmp,CV_8U,0,1);
Sobel(img,img,CV_8U,1,0);
img = img | bmp; //整合到一幅图
bmp.release();
//二值化 otsu
threshold(img, img, 0, 255, CV_THRESH_OTSU);
//threshold(img, img, 30, 255, CV_THRESH_BINARY);
//adaptiveThreshold(img,img,255,ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,51,3);
//膨胀 腐蚀 闭操作 3x3长方形
Mat e = getStructuringElement(MORPH_RECT,Size(3,3));
dilate(img,img,e);
erode(img,img,e);
//霍夫变换
vector<Vec4i> lines;
HoughLinesP(img, lines, 1, CV_PI/90, 50, 50, 5);
//为了画绿线 单通道转换为三通道
cvtColor(img, img, CV_GRAY2BGR);
//画出线段
for( size_t i = 0; i < lines.size(); i++ )
{
//绿线
line( img, Point(lines[i][0],lines[i][1]), Point(lines[i][2],lines[i][3]), Scalar(0,255,0), 1, CV_AA);
}
}
int main()
{
Mat im;
Mat img=imread("2.bmp",0);
im = img.clone();
my(img);
//-------------------------------------
imshow("我的算法",img);
imshow("原图",im);
waitKey(0);
}
opencv 简单的检测直线
最新推荐文章于 2024-08-12 09:02:43 发布