由两点确定的直线,得到与图边界的交点

//求一直线与图边界的交点
bool get_cross_pt_with_pic(CvPoint input_pt1,CvPoint input_pt2,vector<CvPoint>& cross_pt,IplImage* img)
{
	if (input_pt1.x>=img->width||input_pt1.x<0||input_pt1.y<0||input_pt1.y>=img->height
		||input_pt2.x>=img->width||input_pt2.x<0||input_pt2.y<0||input_pt2.y>=img->height)
	{
		cout << "输入点超出图像范围" << endl;
		return false;
	}
	double k_t;
	k_t=input_pt2.x - input_pt1.x;
	if (k_t==0.)
	{
		cross_pt.push_back(cvPoint(input_pt1.x,0));
		cross_pt.push_back(cvPoint(input_pt1.x,img->height-1));
	}
	else
	{
		double k,b;
		k=((double)input_pt2.y - input_pt1.y) /(input_pt2.x - input_pt1.x);
		b=input_pt1.y - k*input_pt1.x; 
		if (!k)
		{
			cross_pt.push_back(cvPoint(0,b));
			cross_pt.push_back(cvPoint(img->width-1,b));
		}
		else
		{
			int x1,x2,y1,y2;
			x1=-b/k;//y=0
			x2=(img->height-1-b)/k;//y=img->height-1
			y1=b;//x=0
			y2=k*(img->width-1)+b;//x=img->width-1
			if(y1>=0&&y1<img->height)
				cross_pt.push_back(cvPoint(0,y1));
			if(y2>=0&&y2<img->height)
				cross_pt.push_back(cvPoint(img->width-1,y2));
			if (x1>=0&&x1<img->width)
				cross_pt.push_back(cvPoint(x1,0));
			if (x2>=0&&x2<img->width)
				cross_pt.push_back(cvPoint(x2,img->height-1));
		}
	}
	return true;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值