opencv 简单的检测直线

//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);  
} 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大米粥哥哥

感谢认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值