ChatGPT最近挺火的,据说还能写程序,感到有些惊讶。于是在使用ChatGPT有一周左右后,分享一下用它写程序的效果如何。
感觉不错的,能清晰介绍原理,然后写示例程序,还给出了示例的运行结果。
上面是一个简答的例子,要求程序详细一些
当要求程序详细一些时,它会写得很详细些,这个不错的;
但是在解释原理时,公式显示乱码了,这点有待完善。
对于特定领域的知识(ONNX模型转换),它也是知道的,能根据需求,写出示例程序。
但是呢,上面的程序会报错:
ValueError: index 10016 is out of bounds for array with size 10000
import cv2
import numpy as np
# 读取图像并转换为灰度图像
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 对图像进行二值化处理
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 找到图像中的轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 对每个轮廓拟合多条直线
for cnt in contours:
# 近似轮廓
epsilon = 0.01 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
# 计算轮廓的凸包
hull = cv2.convexHull(approx, returnPoints=False)
# 如果轮廓点的数量不足 5 个,则不做拟合
if len(approx) < 5:
continue
# 寻找轮廓的顶点
defects = cv2.convexityDefects(approx, hull)
vertices = []
for i in range(defects.shape[0]):
s, e, f, d = defects[i, 0]
start = tuple(approx[s][0])
end = tuple(approx[e][0])
far = tuple(approx[f][0])
vertices.append(far)
# 对轮廓进行拟合
lines = cv2.fitLine(np.array(vertices), cv2.DIST_L2, 0, 0.01, 0.01)
# 绘制拟合直线
x0, y0 = int(lines[2] - lines[0] * 100), int(lines[3] - lines[1] * 100)
x1, y1 = int(lines[2] + lines[0] * 100), int(lines[3] + lines[1] * 100)
cv2.line(img, (x0, y0), (x1, y1), (0, 255, 0), 2)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
# 读取图片
img = cv2.imread('example.png')
# 转为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化处理
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 对轮廓点集合按照横坐标排序
contour_points = contours[0]
contour_points = contour_points[np.argsort(contour_points[:, :, 0])]
# 分离轮廓点集合
mid_x = (contour_points[0][0][0] + contour_points[-1][0][0]) // 2
left_points = contour_points[0:mid_x, :, :]
right_points = contour_points[mid_x:, :, :]
# 拟合左侧直线
left_vx, left_vy, left_x, left_y = cv2.fitLine(left_points, cv2.DIST_L2, 0, 0.01, 0.01)
# 拟合右侧直线
right_vx, right_vy, right_x, right_y = cv2.fitLine(right_points, cv2.DIST_L2, 0, 0.01, 0.01)
# 计算直线的截距和斜率
left_k = left_vy / left_vx
left_b = left_y[0][0] - left_k * left_x[0][0]
right_k = right_vy / right_vx
right_b = right_y[0][0] - right_k * right_x[0][0]
# 画出拟合的直线
height, width, _ = img.shape
left_x0 = 0
left_y0 = int(left_k * left_x0 + left_b)
left_x1 = width
left_y1 = int(left_k * left_x1 + left_b)
right_x0 = 0
right_y0 = int(right_k * right_x0 + right_b)
right_x1 = width
right_y1 = int(right_k * right_x1 + right_b)
cv2.line(img, (left_x0, left_y0), (left_x1, left_y1
import cv2
import numpy as np
# 读入图像
img = cv2.imread("example.jpg")
# 灰度化和边缘检测
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# 获取轮廓
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 对每个轮廓进行拟合
for contour in contours:
# 拟合直线
[vx, vy, x, y] = cv2.fitLine(contour, cv2.DIST_L2, 0, 0.01, 0.01)
slope = vy / vx
y_intercept = y - slope * x
x1 = 0
y1 = int(x1 * slope + y_intercept)
x2 = img.shape[1] - 1
y2 = int(x2 * slope + y_intercept)
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
[vx, vy, x, y] = cv2.fitLine(contour, cv2.DIST_L2, 0, 0.01, 0.01)
slope = vy / vx
y_intercept = y - slope * x
x1 = 0
y1 = int(x1 * slope + y_intercept)
x2 = img.shape[1] - 1
y2 = int(x2 * slope + y_intercept)
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
# 显示结果
cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
# 读入图片,转为灰度图
img = cv2.imread("image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Canny 边缘检测
edges = cv2.Canny(gray, 50, 150)
# 寻找轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 画出轮廓
contour_img = np.zeros_like(img)
cv2.drawContours(contour_img, contours, -1, (0, 255, 0), 3)
# 对每个轮廓拟合两条直线
line_img = np.zeros_like(img)
for cnt in contours:
# 拟合第一条直线
[vx, vy, x, y] = cv2.fitLine(cnt, cv2.DIST_L2, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((img.shape[1] - x) * vy / vx) + y)
cv2.line(line_img, (img.shape[1]-1, righty), (0, lefty), (255, 0, 0), 2)
# 拟合第二条直线
[vx, vy, x, y] = cv2.fitLine(cnt, cv2.DIST_L2, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((img.shape[1] - x) * vy / vx) + y)
cv2.line(line_img, (img.shape[1]-1, righty), (0, lefty), (0, 0, 255), 2)
# 显示结果
cv2.imshow("Contour", contour_img)
cv2.imshow("Lines", line_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
同样的问题,实现的方式有多种;在编写程序需求时,明确一下思路,它写出来的代码会更符合我们所想的。
import cv2
import numpy as np
# 读取图片并进行灰度化处理
img = cv2.imread('test.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 对灰度图像进行边缘检测,如使用 Canny 算法
edges = cv2.Canny(gray, 50, 150)
# 找到所有轮廓,并选择一个想要拟合的轮廓
contours, hierarchy = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
# 对轮廓进行逼近,得到轮廓的拟合线段
epsilon = 0.1 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
# 计算轮廓的重心坐标
M = cv2.moments(cnt)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
# 将轮廓点集合分为左右两部分
left_cnt = []
right_cnt = []
for i in range(len(cnt)):
if cnt[i][0][0] < cx:
left_cnt.append(cnt[i])
else:
right_cnt.append(cnt[i])
left_cnt = np.array(left_cnt)
right_cnt = np.array(right_cnt)
# 对左右两部分的点分别进行拟合直线
[vx_l, vy_l, x_l, y_l] = cv2.fitLine(left_cnt, cv2.DIST_L2, 0, 0.01, 0.01)
[vx_r, vy_r, x_r, y_r] = cv2.fitLine(right_cnt, cv2.DIST_L2, 0, 0.01, 0.01)
# 得到拟合直线的参数,可以使用直线的一般式或截距式表示
k_l = vy_l / vx_l
b_l = y_l - k_l * x_l
k_r = vy_r / vx_r
b_r =
这个代码没写完,又被中断了;感觉写稍微长一些的代码,就容易出现中断的情况。
对于简单的运算,感觉不错的,能清晰介绍原理,然后写示例程序,还给出了示例的运行结果。
在示例程序中,要求程序详细一些时,它会写得很详细些,这个不错的。
对于一些特定领域的知识,它也是知道的,能根据描述需求,写出示例程序。
有些复杂一些的运算,它写出的程序会报错;这个需要我们检测和修正。
写稍微长一些的代码,就容易出现中断的情况,代码还没写完呢。
总体感觉挺挺强的,适合我们去了解一个新的知识(原理和思路),然后参考它的示例程序。
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/