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

举报
汪子熙 发表于 2024/05/05 21:25:02 2024/05/05
【摘要】 输入图片:Prompt:please count how many triangles are there in this picture生成的代码:import cv2import numpy as np# Load the imageimage_path = '/mnt/data/三角形.png'img = cv2.imread(image_path, cv2.IMREAD_GRAYSC...

输入图片:

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

这两行代码负责导入所需的库。cv2 是 OpenCV 库的常用别名,这是一个强大的计算机视觉处理库。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 变量存储了可以由检测到的线段端点形成的独特三角形的数量,这个值是脚本的输出。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。