gpt4 book ai didi

python - 拉直图像中最大的线

转载 作者:太空宇宙 更新时间:2023-11-03 14:36:31 24 4
gpt4 key购买 nike

我在一个托盘上有大量食物图片,托盘是图片中最大的东西,它总是包含最大的一行。 IE。 Input image我不得不用 Photoshop 处理掉托盘上的元素

我想要一个可以对齐和拉直三分球的脚本,如下所示: expected output

这可能使用 python 吗?

最佳答案

这是一个方法

  • 将图像转换为灰度
  • Otsu 获取二值图像的阈值
  • 查找轮廓并使用近似轮廓进行过滤
  • 自上而下 View 的透视变换
  • 旋转图片以获得正确的方向

转为灰度后,我们用Otsu的阈值得到二值图像

现在我们在此图像上找到轮廓并使用 cv2.arcLength()cv2.approxPolyDP() 进行过滤。这个想法是,如果轮廓有大约 4 个角,那么它一定是我们想要的对象。额外的过滤步骤可能是使用 cv2.contourArea() 来确保只使用最大的轮廓。这是检测到的轮廓

从这里我们执行透视变换以获得自上而下的 View

最后,我们根据所需的方向旋转图像。这是结果

import cv2
import numpy as np
import imutils

def perspective_transform(image, corners):
def order_corner_points(corners):
# Separate corners into individual points
# Index 0 - top-right
# 1 - top-left
# 2 - bottom-left
# 3 - bottom-right
corners = [(corner[0][0], corner[0][1]) for corner in corners]
top_r, top_l, bottom_l, bottom_r = corners[0], corners[1], corners[2], corners[3]
return (top_l, top_r, bottom_r, bottom_l)

# Order points in clockwise order
ordered_corners = order_corner_points(corners)
top_l, top_r, bottom_r, bottom_l = ordered_corners

# Determine width of new image which is the max distance between
# (bottom right and bottom left) or (top right and top left) x-coordinates
width_A = np.sqrt(((bottom_r[0] - bottom_l[0]) ** 2) + ((bottom_r[1] - bottom_l[1]) ** 2))
width_B = np.sqrt(((top_r[0] - top_l[0]) ** 2) + ((top_r[1] - top_l[1]) ** 2))
width = max(int(width_A), int(width_B))

# Determine height of new image which is the max distance between
# (top right and bottom right) or (top left and bottom left) y-coordinates
height_A = np.sqrt(((top_r[0] - bottom_r[0]) ** 2) + ((top_r[1] - bottom_r[1]) ** 2))
height_B = np.sqrt(((top_l[0] - bottom_l[0]) ** 2) + ((top_l[1] - bottom_l[1]) ** 2))
height = max(int(height_A), int(height_B))

# Construct new points to obtain top-down view of image in
# top_r, top_l, bottom_l, bottom_r order
dimensions = np.array([[0, 0], [width - 1, 0], [width - 1, height - 1],
[0, height - 1]], dtype = "float32")

# Convert to Numpy format
ordered_corners = np.array(ordered_corners, dtype="float32")

# Find perspective transform matrix
matrix = cv2.getPerspectiveTransform(ordered_corners, dimensions)

# Return the transformed image
return cv2.warpPerspective(image, matrix, (width, height))

image = cv2.imread('1.jpg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.015 * peri, True)
if len(approx) == 4:
cv2.drawContours(image,[c], 0, (36,255,12), 3)
transformed = perspective_transform(original, approx)

rotated = imutils.rotate_bound(transformed, angle=-90)

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.imshow('transformed', transformed)
cv2.imshow('rotated', rotated)
cv2.waitKey()

关于python - 拉直图像中最大的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57862682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com