检测直线(Line)并按倾斜角度进行校正、画线、画延长线

1、检测直线(Line)并按倾斜角度进行校正

int main(int argc, char** argv)
{
	double degree;
	double sum=0.0;
    int m=0;
    const char* filename = "c:\\2.jpg";
    IplImage* src = cvLoadImage( filename, 0 );
    IplImage* dst;
    IplImage* color_dst;
    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* lines = 0;
    int i;
    if( !src )
	{
        return -1;
	}
    dst = cvCreateImage( cvGetSize(src), 8, 1 );
    color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
    cvCanny( src, dst, 50, 200, 3 );
    cvCvtColor( dst, color_dst, CV_GRAY2BGR );

    lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 200, 50, 10);
	double k;

    for (i = 0; i < lines->total/*/20*/; i++)
    {
        CvPoint* line = (CvPoint*)cvGetSeqElem(lines, i);
		if (fabs(line[0].x-line[1].x)>src->width/10 &&  fabs(line[0].y-line[1].y)<4)  //只检测近似水平的线,并且长度不能太短
		{
		   cvLine(color_dst, line[0], line[1], CV_RGB(255, 0, 0), 3, CV_AA, 0);
			
	       double k=(line[0].y-line[1].y)/(line[0].x-line[1].x+0.000001);//不加0.000001 会变成曲线,斜率可能为0,即e.x-s.x可能为0
		   degree=atan(k)*180/3.1415926;
		   sum+=degree;
		   m++;
		  // break;
		}

    }
	cvSaveImage("c:\\hualine19.jpg",color_dst);
	
	//新加的根据角度进行调整
	
	degree=sum/m;

	int center[2]={0,0};	
	
	center[0]=src->width/2;//这两句可以设置旋转中心的坐标
	center[1]=src->height/2;
//	cvSaveImage("c:\\srccopy000.jpg",src);
	
	myRotate(src,0,0, /*- */degree,center);//对原图像按倾斜角度进行旋转
		

	cvSaveImage("c:\\line.jpg",src);
return 0;
}


2、根据二值图像扫描每一行,如果一行全是白点,就画线

// Line.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"


int main(int argc, char* argv[])
{
	IplImage * picture , * gray, *dst;
	picture=cvLoadImage("pi3.jpg",1);
	gray = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);
	cvCvtColor(picture,gray,CV_BGR2GRAY);
	dst = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);

	
	int w=picture->width;
	int h=picture->height;   
	
	cvThreshold(gray,dst,220,255,CV_THRESH_BINARY);	
	cvSaveImage("dst.jpg",dst);
	
	int i,j;
	
	int flag1,flag2,flag3;
	flag1=0;
	flag2=0;
	flag3=1;

	int LineBytes=(w*8+31)/32*4;
	int m=0;
	uchar *pdata=(uchar *)dst->imageData;
	for(j=0;j<h;j++)
	{
		for(i=0;i<w;i++)
		{	
			if( flag1 == 0 && flag2 ==0)
			{
				if(pdata[LineBytes*j +i] == 0)
				{				
					if (m%2==0 )
					{
						cvLine(picture,cvPoint(0,j-1),cvPoint(w,j-1),cvScalar(0,0,0),1);
						m++;
					}
					//cvSaveImage("pi1.jpg",picture);
					flag2=1;
					flag1=1;
					
					break;
					
				}
			}
			else
			{
				if (pdata[LineBytes*j +i] == 255)
				{
					flag2=0;
				//	m++;
				}
				else
				{
					flag2=1;
					break;
				}
			}
		}
		
		if (flag1 ==1 && flag2 ==0)
		{
			flag1 = 0;
			flag2 = 0;
			//m++;
			if (m%2)
			{
				cvLine(picture,cvPoint(0,j+1),cvPoint(w,j+1),cvScalar(0,0,255),1);
				//m++;

			}
			j=j+4;
			
		}
	}	
	
	cvSaveImage("pi2.jpg",picture);
	
	return 0;
}


3、画直线的延长线

 

// Line.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

void OnDrawDotline(CvPoint s, CvPoint e,IplImage *workimg)
{
	CvPoint pa,pb;
	
	double k=(s.y-e.y)/(s.x-e.x+0.000001);//不加0.000001 会变成曲线,斜率可能为0,即e.x-s.x可能为0
	
	double h=workimg->height,w=workimg->width;	
	
	pa.x=w;
	pa.y=s.y+k*(w-s.x);	
	
	cvLine(workimg,e,pa,CV_RGB(0,255,255), 2, CV_AA, 0 );	//向右画线
	
	
	pb.y=e.y-k*e.x;
	pb.x=0;
	
	cvLine(workimg,pb,s,CV_RGB(0,0,255), 2, CV_AA, 0 );	//向左画线

}


int main(int argc, char* argv[])
{
	IplImage * picture , * gray, *dst;
	picture=cvLoadImage("3.jpg",1);
	gray = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);
	cvCvtColor(picture,gray,CV_BGR2GRAY);
	dst = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);

	
	int w=picture->width;
	int h=picture->height;   
	
	cvThreshold(gray,dst,220,255,CV_THRESH_BINARY);	
	cvSaveImage("dst.jpg",dst);
	
	cvLine(picture,cvPoint(20,50),cvPoint(60,40),CV_RGB(255,0,0),3);

	OnDrawDotline(cvPoint(20,50), cvPoint(60,40),picture);

	cvSaveImage("pi2.jpg",picture);
	
	return 0;
}


©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页