sklearn 读取 raw8 计算光心

 

 

import numpy
import struct
import cv2
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

raw_img = []
with open("20191211T102737.1280X1080.raw8_bggr.vcmpos_0.raw", "rb") as f:
    for i in range(1080):
        line_img = []
        for j in range(1280):
            data = f.read(1)
            # print("data=", data)
            pix = struct.unpack("B", data)
            # print("pix = ", pix)
            # quit()
            line_img.append(pix)
        raw_img.append(line_img)

#    raw_img.append(f.read(1280))

raw_img = numpy.array(raw_img, dtype=numpy.uint8)
print(raw_img.shape)
print(raw_img[0][0])

# bgr_img = cv2.cvtColor(raw_img, cv2.COLOR_BAYER_BG2BGR)
# bgr_img = cv2.cvtColor(raw_img, cv2.COLOR_BAYER_GB2BGR)
# bgr_img = cv2.cvtColor(raw_img, cv2.COLOR_BAYER_GR2BGR)
# bgr_img = cv2.cvtColor(raw_img, cv2.COLOR_BAYER_RG2BGR)
bgr_img = cv2.cvtColor(raw_img, cv2.COLOR_BayerBG2BGR)
# cv2.imshow("bgr_img", bgr_img)
# cv2.waitKey(10000)
# quit()
gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)
# cv2.imshow("gray_img", gray_img)
# cv2.waitKey(10000)
print(gray_img[540])
x = numpy.array(range(0, 1280)).reshape(-1, 1)

plt.plot(x, gray_img[540])
plt.title('Y_value')
plt.xlabel('x')
plt.ylabel('pix')
plt.grid(True)

polynomial = PolynomialFeatures(degree=2)  # 二次多项式
x_transformed = polynomial.fit_transform(x)  # x每个数据对应的多项式系数

poly_linear_model = LinearRegression()  # 创建回归器
poly_linear_model.fit(x_transformed, gray_img[540])

xx_transformed = polynomial.fit_transform(x.reshape(x.shape[0], 1))
yy = poly_linear_model.predict(xx_transformed)
print("coef_ = ", poly_linear_model.coef_)
print("intercept_ = ", poly_linear_model.intercept_)
a = poly_linear_model.coef_[2]
b = poly_linear_model.coef_[1]
c = poly_linear_model.intercept_
center = -(b / (2 * a))
print("center = ", center)

plt.plot(x, yy)

plt.show()

打印内容

coef_ =  [ 0.00000000e+00  4.99277020e-02 -3.71188114e-05]
intercept_ =  121.35914870018544
center =  672.5390720161159

 

sklearn 读取 raw8 计算光心_第1张图片

你可能感兴趣的:(sklearn 读取 raw8 计算光心)