微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在python中倾斜回归线

如何解决在python中倾斜回归线

我想使用线性回归(python)对图像进行倾斜校正。我有一个包含x,y和z轴的tiff文件。我从x和z轴上取了一条线,并尝试通过线性回归来倾斜该线。我的动机是对整个图像进行倾斜校正。

我的代码如下:

import tifffile 
import matplotlib.pyplot as plt
from scipy import stats 
import numpy as np 
import math
from scipy import ndimage

im = tifffile.imread('test_2.tif',key =0)

fig=plt.figure()
ax= plt.axes()
plt.imshow(im)
plt.xlabel('distance[mm]')
plt.ylabel('position[mm]')
plt.show()
print(im)
print(im.shape)

x = im[0,400:1000]
im1=tifffile.imread('test_2.tif',key =2)
fig=plt.figure()
plt.imshow(im1)
plt.xlabel('distance[mm]')
plt.ylabel('position[mm]')
plt.show()
print(im1)
print(im1.shape)

y = im1[0,400:1000]
slope,intercept,r_value,p_value,std_err = stats.linregress(x,y)
print("The slope is as follows: {0}".format(slope))
print("The intercept is as follows: {0}".format(intercept))
#(y= mx+b) where m is slope and b is the intercept 
y1 = (slope* x)+ intercept
Residual = y - y1
Residual_errors = (y - y1)**2
Total_errors = sum(Residual_errors)
print("The Total errors is as follows: {}".format(Total_errors))
Tilt_angle = math.degrees(math.atan(slope))
print("The Tilt angle is as follows: {0}".format(Tilt_angle))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。