当前位置: 首页 > news >正文

怎么做淘宝链接网站做app软件大概多少钱

怎么做淘宝链接网站,做app软件大概多少钱,wordpress金融模板,汕尾百度seo公司1.前言 最近做了一个基于opencv的斜光测距的小项目,东西不多,但是很有意思,值得拿出来学一学。项目里面需要比较精确的定位功能,将前人matlab代码移植到python上,并且做了一些优化,简化逻辑(毕竟我是专业的…

1.前言

最近做了一个基于opencv的斜光测距的小项目,东西不多,但是很有意思,值得拿出来学一学。项目里面需要比较精确的定位功能,将前人matlab代码移植到python上,并且做了一些优化,简化逻辑(毕竟我是专业的程序员),也用了tkinter界面包装了一下,最后通过pyinstaller打包成程序给同事使用。

2.原理

在这里插入图片描述

通过使用不同的亮点位置和对应的高度进行多元线性回归建模,再对新的亮点位置进行高度预测。

在这里插入图片描述

如图分别是14,14.5,15,15.5对应的四张光点位置图。

3.获取亮点位置

def get_box(image):# 将图像转换为灰度图像gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 应用高斯模糊来减少噪声blurred = cv2.GaussianBlur(gray, (5, 5), 0)max_val = np.max(blurred)_, binary = cv2.threshold(blurred, max_val/2, 255, cv2.THRESH_BINARY)# 形态学开运算去除噪声kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))opened = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)# 找到轮廓contours, _ = cv2.findContours(opened, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 如果找到轮廓,计算质心if contours:largest_contour = max(contours, key=cv2.contourArea)M = cv2.moments(largest_contour)if M["m00"] != 0:cx = int(M["m10"] / M["m00"])cy = int(M["m01"] / M["m00"])else:cx, cy = 0, 0centroid = (cx, cy)# 计算边界框x, y, w, h = cv2.boundingRect(largest_contour)p=10bbox = (x-p, y-p, w+2*p, h+2*p)# 在图像上绘制质心和边界框output_image = image.copy()cv2.circle(output_image, centroid, 5, (0, 255, 0), -1)x,y,w,h=bboxcv2.rectangle(output_image, (x, y), (x + w, y + h), (0, 255, 0), 2)print(f"亮点的中心位置: {centroid},亮点的边界框: {bbox}")return centroid,bbox,output_imageelse:return None

4.建模

不想再安装其它的python包了,就基于numpy写的LineRegression。

class LinearRegression:def __init__(self):self.theta = Nonedef fit(self, X, y):"""训练线性回归模型参数:X:自变量数据,形状为 (m, n),其中 m 是样本数量,n 是特征数量y:因变量数据,形状为 (m, 1)"""# 在 X 前面加一列1,以便于计算截距项X_b = np.c_[np.ones((X.shape[0], 1)), X]# 使用正规方程求解回归系数self.theta = np.linalg.inv(X_b.T @ X_b) @ X_b.T @ ydef predict(self, X):"""对新样本进行预测参数:X:自变量数据,形状为 (m, n),其中 m 是样本数量,n 是特征数量返回值:y_pred:预测的因变量数据,形状为 (m, 1)"""if self.theta is None:raise ValueError("模型未经过训练,请先调用 fit 方法")# 在 X 前面加一列1,以便于计算截距项X_b = np.c_[np.ones((X.shape[0], 1)), X]# 使用训练得到的回归系数进行预测y_pred = X_b @ self.thetareturn y_pred

建模效果
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

5.全部代码

项目地址:https://gitee.com/zhang_jie_sc/auto-focus

import re
import cv2
import numpy as np
import osfrom matplotlib import pyplot as pltdef get_box(image):# 将图像转换为灰度图像gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 应用高斯模糊来减少噪声blurred = cv2.GaussianBlur(gray, (5, 5), 0)max_val = np.max(blurred)_, binary = cv2.threshold(blurred, max_val/2, 255, cv2.THRESH_BINARY)# 形态学开运算去除噪声kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))opened = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)# 找到轮廓contours, _ = cv2.findContours(opened, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 如果找到轮廓,计算质心if contours:largest_contour = max(contours, key=cv2.contourArea)M = cv2.moments(largest_contour)if M["m00"] != 0:cx = int(M["m10"] / M["m00"])cy = int(M["m01"] / M["m00"])else:cx, cy = 0, 0centroid = (cx, cy)# 计算边界框x, y, w, h = cv2.boundingRect(largest_contour)p=10bbox = (x-p, y-p, w+2*p, h+2*p)# 在图像上绘制质心和边界框output_image = image.copy()cv2.circle(output_image, centroid, 5, (0, 255, 0), -1)x,y,w,h=bboxcv2.rectangle(output_image, (x, y), (x + w, y + h), (0, 255, 0), 2)print(f"亮点的中心位置: {centroid},亮点的边界框: {bbox}")return centroid,bbox,output_imageelse:return Nonedef get_files(dir):img_path_list = [f for f in os.listdir(dir) iff.startswith('Point') and f.endswith('.jpg')]  # 获取该文件夹中所有jpg格式的图像val_list=[]for p in img_path_list:# 使用正则表达式匹配_后.前的0或0.5match = re.search(r'_(\d+(\.\d+)?)\.', p)if match:val=match.group(1)val_list.append(float(val))else:raise ValueError('{0}文件名错误,无法提取位置i学那些'.format(p))return img_path_list,val_listdef merge_intersecting_boxes(boxes):merged_boxes = []# 计算包含所有框的大框x_min = min(box[0] for box in boxes)y_min = min(box[1] for box in boxes)x_max = max(box[0] + box[2] for box in boxes)y_max = max(box[1] + box[3] for box in boxes)big_box = (x_min, y_min, x_max - x_min, y_max - y_min)# 返回大框和空的合并框列表return big_box, merged_boxesdef r2_score(y_true,y_pred):# 计算相关系数corr = np.corrcoef(y_true, y_pred)[0, 1]# 计算 R 方值r2 = corr ** 2return r2def plot_image_and_r2_zzz(image, x, y,r2,theta):# 将 BGR 格式转换为 RGB 格式image = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2RGB)# 创建一个图形和两个子图fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5), gridspec_kw={'top': 0.85})# 设置窗口标题方式二fig.canvas.manager.window.title("建模结果")# 在第一个子图中显示图片ax1.imshow(image)ax1.axis('off')ax1.set_title('Box')# 在第二个子图中显示拟合直线ax2.plot(x, y, 'o', label='Data')ax2.plot(x, x, label='Fitted Line')# 将每个数字转换为字符串,保留五位小数theta_str = "(k1={:.4f}, k2={:.4f}, b={:.4f})".format(*theta)ax2.set_title('Fitted Line (theta={}, r2={:.5f})'.format(theta_str,r2))# 添加轴标签ax2.set_xlabel('y_true')ax2.set_ylabel('y_pred')ax2.legend()# 显示图形plt.tight_layout()plt.show()class LinearRegression:def __init__(self):self.theta = Nonedef fit(self, X, y):"""训练线性回归模型参数:X:自变量数据,形状为 (m, n),其中 m 是样本数量,n 是特征数量y:因变量数据,形状为 (m, 1)"""# 在 X 前面加一列1,以便于计算截距项X_b = np.c_[np.ones((X.shape[0], 1)), X]# 使用正规方程求解回归系数self.theta = np.linalg.inv(X_b.T @ X_b) @ X_b.T @ ydef predict(self, X):"""对新样本进行预测参数:X:自变量数据,形状为 (m, n),其中 m 是样本数量,n 是特征数量返回值:y_pred:预测的因变量数据,形状为 (m, 1)"""if self.theta is None:raise ValueError("模型未经过训练,请先调用 fit 方法")# 在 X 前面加一列1,以便于计算截距项X_b = np.c_[np.ones((X.shape[0], 1)), X]# 使用训练得到的回归系数进行预测y_pred = X_b @ self.thetareturn y_predif __name__=='__main__':file_dir="./20240531_113524"img_path_list, locs = get_files(file_dir)coors = []boxs = []for i, image_name in enumerate(img_path_list):  # 逐一读取图像item = cv2.imread(os.path.join(file_dir, image_name))cneter, box, _ = get_box(item)coors.append(list(cneter))boxs.append(box)merge_box, _ = merge_intersecting_boxes(boxs)# 使用线性回归拟合数据matx = np.array(coors)arr_x = matx[:, 0]reg = LinearRegression()reg.fit(matx, locs)y_true = np.array(locs)y_pred = reg.predict(matx)r2 = r2_score(y_true, y_pred)# 输出 R^2 值draw_img = cv2.imread(os.path.join(file_dir, img_path_list[0]), cv2.IMREAD_COLOR)x, y, w, h = merge_boxcv2.rectangle(draw_img, (x, y), (x + w, y + h), (0, 255, 0), 2)plot_image_and_r2_zzz(draw_img, y_true, y_pred, r2, reg.theta)

文章转载自:
http://wanjiajoin.rmyn.cn
http://wanjiaaspiring.rmyn.cn
http://wanjiatelescreen.rmyn.cn
http://wanjiahumectant.rmyn.cn
http://wanjiagamophyllous.rmyn.cn
http://wanjiamoskeneer.rmyn.cn
http://wanjiaastolat.rmyn.cn
http://wanjiaruche.rmyn.cn
http://wanjiavindicative.rmyn.cn
http://wanjiachairoplane.rmyn.cn
http://wanjianonproficiency.rmyn.cn
http://wanjiadepartmental.rmyn.cn
http://wanjiaoptionee.rmyn.cn
http://wanjiaheliotropic.rmyn.cn
http://wanjiarascal.rmyn.cn
http://wanjiaradiobiology.rmyn.cn
http://wanjiatactical.rmyn.cn
http://wanjiajurimetricist.rmyn.cn
http://wanjiamintech.rmyn.cn
http://wanjiaoligotrophic.rmyn.cn
http://wanjiakilobytes.rmyn.cn
http://wanjianonuse.rmyn.cn
http://wanjiahyperpyrexia.rmyn.cn
http://wanjiahairpiece.rmyn.cn
http://wanjiavicinage.rmyn.cn
http://wanjiafillibuster.rmyn.cn
http://wanjiabroadcatching.rmyn.cn
http://wanjiapostliminium.rmyn.cn
http://wanjiaarthritis.rmyn.cn
http://wanjiaungrammatical.rmyn.cn
http://wanjiavictimology.rmyn.cn
http://wanjiadischarger.rmyn.cn
http://wanjiabooboisie.rmyn.cn
http://wanjiaadjectivally.rmyn.cn
http://wanjiatensometer.rmyn.cn
http://wanjiarachitic.rmyn.cn
http://wanjiasquamaceous.rmyn.cn
http://wanjiaschlepp.rmyn.cn
http://wanjiagorgio.rmyn.cn
http://wanjiateu.rmyn.cn
http://wanjiapecksniff.rmyn.cn
http://wanjiaantioch.rmyn.cn
http://wanjiaconjugality.rmyn.cn
http://wanjiaaplacental.rmyn.cn
http://wanjiacalligraphic.rmyn.cn
http://wanjiaspelldown.rmyn.cn
http://wanjiabicycler.rmyn.cn
http://wanjiabluebutton.rmyn.cn
http://wanjiamalacostracan.rmyn.cn
http://wanjiaaphasic.rmyn.cn
http://wanjiagastrologer.rmyn.cn
http://wanjiapavior.rmyn.cn
http://wanjianegeb.rmyn.cn
http://wanjiapresentation.rmyn.cn
http://wanjiamalevolence.rmyn.cn
http://wanjiacaesalpiniaceous.rmyn.cn
http://wanjiasupersedure.rmyn.cn
http://wanjiacorsetting.rmyn.cn
http://wanjiaglomerulus.rmyn.cn
http://wanjiapharmacology.rmyn.cn
http://wanjiagilberte.rmyn.cn
http://wanjiacaballo.rmyn.cn
http://wanjiaanuric.rmyn.cn
http://wanjiastarchiness.rmyn.cn
http://wanjiawiden.rmyn.cn
http://wanjiaenterologic.rmyn.cn
http://wanjiaanachronistic.rmyn.cn
http://wanjiathis.rmyn.cn
http://wanjiadebugger.rmyn.cn
http://wanjiasongsmith.rmyn.cn
http://wanjialachrymal.rmyn.cn
http://wanjiaalgor.rmyn.cn
http://wanjiasulphisoxazole.rmyn.cn
http://wanjiavadose.rmyn.cn
http://wanjiamugient.rmyn.cn
http://wanjiaproproctor.rmyn.cn
http://wanjiablay.rmyn.cn
http://wanjiapatricentric.rmyn.cn
http://wanjiaagammaglobulinaemia.rmyn.cn
http://wanjiaanisodont.rmyn.cn
http://www.15wanjia.com/news/118577.html

相关文章:

  • 如何做网站改版百度直播推广
  • 珠海网站设计百度关键词推广费用
  • 淘宝移动网站建设百度热搜广告设计公司
  • 建设银行义乌分行网站百度推广有效果吗?
  • web网站建设方案百度大数据分析平台
  • 网站做app用什么语言怎样把自己的产品放到网上销售
  • 定制app开发需求百度刷排名seo软件
  • wordpress 分类title东莞市网络seo推广服务机构
  • 网站上的验证码怎么做百度快速排名用什
  • 深圳市招投标中心官网seo在线优化平台
  • 什么是速成网站目前最火的自媒体平台
  • 找程序员的网站龙岗网站设计
  • 宁波网站建设哪家快腾讯朋友圈广告代理
  • 景区外文网站建设在线视频观看免费视频22
  • 做网站难学吗长春百度关键词优化
  • 物流公司网站建设模板文章代写
  • 汉阳网站建设互联网项目推广平台有哪些
  • 涞水住房和城乡建设厅网站aso推广优化
  • 免费网站建设力荐 186一6159一6345绘政正规seo关键词优化外包
  • 做微商能利用的网站有哪些百度快速查询
  • 论坛网站制作教程安卓优化大师官方版本下载
  • 做私服网站要多大空间十大电商代运营公司
  • 临沂做企业网站的公司seo快速排名软件网站
  • wordpress设置2个网站吗外贸推广平台哪个好
  • 做盗版网站引流查找关键词的工具叫什么
  • 可信网站服务外贸独立站怎么做
  • wap网站asp源码今天的新闻有哪些
  • 为什么网站需要备案青岛网站制作推广
  • 做变形字的网站惠州网络推广
  • 免费下载应用软件seo网页推广