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

该博客介绍了如何在图像处理中检测直线,并对其倾斜角度进行校正。接着,通过扫描二值图像的每一行,当检测到全白像素的行时,会画出对应的直线。最后,探讨了如何绘制这些直线的延长线,以增强图像的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)  //只检测
import numpy as np import matplotlib.pyplot as plt # 苯甲酸数据 time_ben = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20] temp_ben = [15.731, 15.808, 15.893, 15.944, 15.975, 15.993, 16.005, 16.015, 16.022, 16.029, 16.034, 16.038, 16.043, 16.044, 16.048, 16.050, 16.052, 16.054, 16.056, 16.059, 16.111, 16.469, 16.767, 17.048, 17.224, 17.331, 17.404, 17.469, 17.512, 17.546, 17.576, 17.601, 17.619, 17.635, 17.646, 17.654, 17.661, 17.667, 17.671, 17.675] # 计算平均温度对应时间 coeff_post_ben = np.polyfit(time_ben[-20:], temp_ben[-20:], 1) t_avg_ben = (16.830 - coeff_post_ben[1]) / coeff_post_ben[0] # 提取10min和16min温度 T_10 = temp_ben[time_ben.index(10)] # 16.059℃ T_16 = temp_ben[time_ben.index(16)] # 17.601℃ # 插值数据曲线在t_avg处的温度 T_data_avg = np.interp(t_avg_ben, time_ben, temp_ben) # 绘图 plt.figure(figsize=(12, 7)) plt.plot(time_ben, temp_ben, 'k-o', markersize=8, markerfacecolor='none', markeredgewidth=1.5, linewidth=1.5, label='苯甲酸') # 绘制参考线 plt.axvline(t_avg_ben, color='purple', linestyle='--', linewidth=2, label=f'校正时间 ({t_avg_ben:.1f} min)') plt.hlines(T_10, xmin=10, xmax=t_avg_ben, colors='red', linestyles=':', linewidth=1.5) plt.hlines(T_16, xmin=16, xmax=t_avg_ben, colors='green', linestyles=':', linewidth=1.5) plt.hlines(T_10, xmin=0, xmax=10, colors='red', linestyles=':', linewidth=1.5) plt.hlines(T_16, xmin=0, xmax=16, colors='green', linestyles=':', linewidth=1.5) plt.hlines(T_data_avg, xmin=0, xmax=t_avg_ben, colors='blue', linestyles=':', linewidth=1.5) # 标注关键点 plt.plot(t_avg_ben, T_10, 'ro', markersize=8, markerfacecolor='none', markeredgewidth=1.5) plt.plot(t_avg_ben, T_16, 'go', markersize=8, markerfacecolor='none', markeredgewidth=1.5) plt.plot(t_avg_ben, T_data_avg, 'bo', markersize=8, markerfacecolor='none', markeredgewidth=1.5) plt.annotate(f'T={T_10}℃', (0, T_10), (5,5), textcoords='offset points', color='red', fontsize=10) plt.annotate(f'T={T_16}℃', (0, T_16), (5,5), textcoords='offset points', color='green', fontsize=10) plt.annotate(f'T={T_data_avg:.2f}℃', (0, T_data_avg), (5,5), textcoords='offset points', color='blue', fontsize=10) # 图表美化 plt.xlabel('时间 (分钟)', fontsize=12) plt.ylabel('温度 ()', fontsize=12) plt.title('苯甲酸温度校正图(校正温度: 16.830℃)', fontsize=14) plt.legend(loc='lower right') plt.grid(linestyle='--', alpha=0.6) plt.tight_layout() plt.show() 根据要求修改代码:请分别制作苯甲酸和萘的温度矫正图(需要的坐标轴的),要求如下:将各数据点设置为空心,外缘线用黑色,把数据用线连接起来,苯甲酸的平均温度是16.830,萘的平均温度是17.232,然后在平均温度处做一条竖直线,然后在10min和16min的点往平均温度的那条竖直线做温度趋势延长线(拟合好一点)交于两个点标注出来,同时往纵轴做一条水平虚线段,交纵轴于两个点,数据的曲线交平均温度的那条竖直线与一个点,这个点也要往纵轴做水平虚线段,交纵轴于一点标注出来。
03-18
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值