使用 ChatGPT 计算图片中包含的三角形的个数

简介: 使用 ChatGPT 计算图片中包含的三角形的个数

输入图片:

Prompt:

please count how many triangles are there in this picture


生成的代码:


import cv2

import numpy as np

# Load the image

image_path = '/mnt/data/三角形.png'

img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

# Define a threshold to make the image binary

_, thresh = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)

# Find contours

contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Define a function to calculate slope of line

def line_slope(p1, p2):

return (float(p2[1] - p1[1])) / (float(p2[0] - p1[0])) if p2[0] != p1[0] else np.inf

# Check if three points can form a triangle (none of the slopes should be the same)

def can_form_triangle(p1, p2, p3):

slopes = {line_slope(p1, p2), line_slope(p2, p3), line_slope(p1, p3)}

return len(slopes) == 3

# We will find intersections by extending lines along the contours.

# Since we are dealing with straight lines in this image, we only need to find the end points of these lines.

# We can assume each contour segment corresponds to a line in the figure.

line_endpoints = []

for cnt in contours:

for i in range(len(cnt)):

line_endpoints.append(tuple(cnt[i][0]))

# Since the image might have duplicate points for line endpoints, we need to deduplicate them.

unique_endpoints = list(set(line_endpoints))

# Function to calculate unique combinations of points (combinatorial approach)

def unique_combinations(lst, n):

if n == 0:

return [[]]

l = []

for i in range(len(lst)):

m = lst[i]

remLst = lst[i + 1:]

for p in unique_combinations(remLst, n-1):

l.append([m]+p)

return l

# Find all unique combinations of 3 points from the unique endpoints

triangle_points = unique_combinations(unique_endpoints, 3)

# Count how many unique sets of points can form a triangle

triangle_count = sum(1 for points in triangle_points if can_form_triangle(*points))

triangle_count


这段代码的主要目的是从一张包含简单图形(如三角形)的图片中,识别并计算可以由直线端点组成的独特三角形数量。现在,我们将详细解析每一行代码及其功能:


import cv2

import numpy as np

这两行代码负责导入所需的库。cv2OpenCV 库的常用别名,这是一个强大的计算机视觉处理库。numpy 是 Python 中用于大规模数值计算的核心库,通常缩写为 np


image_path = '/mnt/data/三角形.png'

定义变量 image_path,存储图像文件的路径。

img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)


使用 cv2.imread() 函数读取指定路径的图像。这里使用 cv2.IMREAD_GRAYSCALE 参数,意味着图像将被转换为灰度图,即图像将仅包含灰度强度信息。


_, thresh = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)

此行代码应用阈值处理,将灰度图转换为二值图像。cv2.threshold() 函数的参数 150 是阈值,255 是超过阈值后赋予的新值。cv2.THRESH_BINARY 指定了阈值化的类型,即低于 150 的像素点被设置为 0(黑色),高于 150 的点被设置为 255(白色)。


contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

这行代码使用 cv2.findContours() 函数检测二值图像中的轮廓。cv2.RETR_EXTERNAL 表示只检测最外层轮廓。cv2.CHAIN_APPROX_SIMPLE 是轮廓近似方法,它仅保存轮廓线段的端点,有助于减少存储空间。


def line_slope(p1, p2):

return (float(p2[1] - p1[1])) / (float(p2[0] - p1[0])) if p2[0] != p1[0] else np.inf

定义函数 line_slope,计算两点之间线段的斜率。如果两点的 x 坐标相同,函数返回无穷大(表示垂直线)。否则,斜率由 y 坐标差除以 x 坐标差计算得出。


def can_form_triangle(p1, p2, p3):

slopes = {line_slope(p1, p2), line_slope(p2, p3), line_slope(p1, p3)}

return len(slopes) == 3

定义函数 can_form_triangle,该函数接受三个点,如果它们的斜率各不相同(即没有两点共线),则可以构成三角形。通过检查斜率集合的长度是否为 3 来判断。


line_endpoints = []

for cnt in contours:

for i in range(len(cnt)):

line_endpoints.append(tuple(cnt[i][0]))

这段代码遍历每个轮廓中的点,并将它们作为线的端点添加到 line_endpoints 列表中。轮廓点由坐标列表组成,每个坐标被转换为元组格式。

unique_endpoints = list(set(line_endpoints))


使用 Python 的 set 类型删除 line_endpoints 列表中的重复点,然后转换回列表 unique_endpoints


def unique_combinations(lst, n):

if n == 0:

return [[]]

l = []

for i in range(len(lst)):

m = lst[i]

remLst = lst[i + 1:]

for p in unique_combinations(remLst, n-1):

l.append

([m]+p)

return l


定义函数 unique_combinations,递归生成列表中元素的所有可能组合。这里特别用于生成所有可能的三个点的组合。

triangle_points = unique_combinations(unique_endpoints, 3)


生成所有唯一端点的三点组合。

triangle_count = sum(1 for points in triangle_points if can_form_triangle(*points))


计算能够形成三角形的点组合数量。这通过迭代每组点并应用 can_form_triangle 函数来检查是否可以形成三角形来实现。


最后,triangle_count 变量存储了可以由检测到的线段端点形成的独特三角形的数量,这个值是脚本的输出。

相关文章
|
1月前
ChatGPT4最新大招!DALL·E 图片可以编辑了!
本教程详细介绍了如何使用 DALL·E 编辑功能来修改图片。它覆盖了从基本的图片选择和编辑到更高级的操作,如添加、移除或更新图片中的元素。教程还解释了如何通过对话框直接输入编辑需求,以及如何在 ChatGPT 手机应用上使用这些工具。
44 0
ChatGPT4最新大招!DALL·E 图片可以编辑了!
|
1月前
|
JavaScript 前端开发 Python
用chatgpt帮你写一段GEE计算森林生物量的代码,你猜结果如何?
用chatgpt帮你写一段GEE计算森林生物量的代码,你猜结果如何?
33 0
|
人工智能
用ChatGPT/midjourney生成创意营销图片素材,产品图、虚拟主播、终端店铺图
第一步,先预设场景,询问应该包含的关键词范围 假设你是一位世界一流水平的设计师,你想要使用AI绘画工具midjourney帮忙设计一款XXX,列举该场景需要用到的关键词范畴与示例。 第二步,按照推荐的关键词填充内容来输入到midjourney中,生成对应的图片。 按照逗号区隔不同描述词,用谷歌助手翻译成英文描述词,输入到midjourney中。
575 0
|
7月前
|
人工智能 新能源 大数据
王坚院士谈ChatGPT:计算是对人工智能最关键的技术
王坚院士谈ChatGPT:计算是对人工智能最关键的技术
72 0
王坚院士谈ChatGPT:计算是对人工智能最关键的技术
|
1月前
|
人工智能 算法 异构计算
ChatGPT一年电费2亿元,AI咋这么费电?
【2月更文挑战第24天】ChatGPT一年电费2亿元,AI咋这么费电?
630 1
ChatGPT一年电费2亿元,AI咋这么费电?
|
1月前
|
人工智能 IDE Linux
chatgpt的ai编程工具
该内容是关于两个chatgpt的ai编程工具的安装和使用说明。Copilot的下载步骤包括在IDE的设置中搜索并安装插件,然后重启IDE并登录GitHub账户。使用时,通过写注释触发建议,用快捷键选择建议。启用或禁用Copilot可通过底部状态图标。另一个工具是Alibaba Cloud AI Coding Assistant (Cosy),同样在IDE的插件市场下载安装后重启。其详细使用方法建议参考官网。
251 0
|
1月前
|
人工智能 机器人 Go
飞书+ChatGPT搭建智能AI助手,无公网ip实现公网访问飞书聊天界面
飞书+ChatGPT搭建智能AI助手,无公网ip实现公网访问飞书聊天界面
556 0
|
12天前
|
人工智能 机器人 API
OpenAI发布新AI模型GPT-4o和桌面版ChatGPT
OpenAI发布新AI模型GPT-4o和桌面版ChatGPT
|
17天前
|
机器学习/深度学习 人工智能 自然语言处理
好书推荐丨AI时代Python量化交易实战:ChatGPT让量化交易插上翅膀
好书推荐丨AI时代Python量化交易实战:ChatGPT让量化交易插上翅膀
24 2
|
1月前
|
人工智能 iOS开发 MacOS
[译][AI OpenAI] 引入 GPT-4o 及更多工具至免费版 ChatGPT 用户
我们推出了最新的旗舰模型 GPT-4o,并为免费版 ChatGPT 用户提供更多功能,包括更快的速度、改进的文本、语音和视觉能力,以及新的桌面应用程序和简化的界面。
[译][AI OpenAI] 引入 GPT-4o 及更多工具至免费版 ChatGPT 用户