python计算周长代码_Python计算ci的周长

我正在用python编写一个面积计算器,一切看起来都很好,…直到我开始计算一个圆的周长。。。

有人能指点我正确的方向吗?import math

from math import pi

menu = """

Pick a shape(1-3):

1) Square (area)

2) Rectangle (area)

3) Circle (area)

4) Square (perimeter)

5) Rectangle (perimeter)

6) Circle (perimeter)

7) Quit

"""

shape = int(input(menu))

while shape != 7:

if shape == 1:

length = float(input("Length: "))

print( "Area of square = ", length ** 2 )

elif shape == 2:

length = float(input("Length: "))

width = float(input("Width: "))

print( "Area of rectangle = ", length * width )

elif shape == 3:

area = float(input("Radius: "))

circumference = float

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提取图片中的表格数据可以采用以下思路: 1. 图像预处理:将彩色图像转为灰度图像,并进行二值化处理,使表格线条更加明显。 2. 边缘检测:使用Canny算法进行边缘检测,找到表格的边缘。 3. 轮廓检测:使用OpenCV的findContours函数找到边缘的所有轮廓。 4. 轮廓筛选:根据轮廓的周长、面积等特征,筛选出可能是表格的轮廓。 5. 表格校正:使用透视变换将表格矫正为矩形。 6. 单元格分割:根据表格的行列数,将表格分割为单元格。 7. OCR识别:对每个单元格进行OCR文字识别,得到表格数据。 下面是一个简单的Python代码示例,仅供参考: ```python import cv2 import pytesseract # 读取图像 img = cv2.imread('table.jpg') # 图像预处理 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] # 边缘检测 edges = cv2.Canny(thresh, 50, 150, apertureSize=3) # 轮廓检测 contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 轮廓筛选 table_contours = [] for c in contours: x, y, w, h = cv2.boundingRect(c) if w > 50 and h > 50 and abs(w - h) < 30: table_contours.append(c) # 表格校正 if len(table_contours) > 0: rect = cv2.minAreaRect(table_contours[0]) box = cv2.boxPoints(rect) box = box.astype(int) cv2.drawContours(img, [box], 0, (0, 0, 255), 2) warp = cv2.getPerspectiveTransform(box, ((0, 0), (500, 0), (500, 500), (0, 500))) warped = cv2.warpPerspective(img, warp, (500, 500)) # 单元格分割 rows = 10 cols = 10 cell_w = warped.shape[1] // cols cell_h = warped.shape[0] // rows for i in range(rows): for j in range(cols): x1, y1 = j * cell_w, i * cell_h x2, y2 = (j + 1) * cell_w, (i + 1) * cell_h cell = warped[y1:y2, x1:x2] cv2.imwrite(f'cell_{i}_{j}.jpg', cell) # OCR识别 pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' for i in range(rows): for j in range(cols): img_path = f'cell_{i}_{j}.jpg' img = cv2.imread(img_path) text = pytesseract.image_to_string(img) print(f'[{i}, {j}]: {text}') ``` 需要注意的是,该代码示例仅适用于简单的表格,对于复杂的表格可能需要更加复杂的算法和代码来处理。同时,OCR识别的准确率也可能会受到图像质量、字体、字号等因素的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值