gpt4 book ai didi

python - Opencv和python获取正确的文档构建并绘制图像

转载 作者:行者123 更新时间:2023-12-02 16:51:16 26 4
gpt4 key购买 nike

将opencv与python一起使用的是文档conture enter image description here的输出

我正在使用opencv_python-4.2.0.34,左下角不像其他角那样锋利。如何使它正确? (自动编辑左下角以使其看起来清晰,因为其他情况会出现)

这是绘制轮廓的代码:

img = cv2.imread(imagePath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
invGamma = 1.0 / 0.3
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")

# apply gamma correction using the lookup table
gray = cv2.LUT(gray, table)

ret, thresh1 = cv2.threshold(gray, 80, 255, cv2.THRESH_BINARY)

contours, hierachy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# except:
# print('exception occurred')

def biggestRectangle(conts):
biggest = None
max_area = 0
indexReturn = -1
for index in range(len(conts)):
i = conts[index]
area = cv2.contourArea(i)
if area > 100:
peri = cv2.arcLength(i, True)
approx = cv2.approxPolyDP(i, 0.1 * peri, True)
if area > max_area and len(approx) == 4:
biggest = approx
max_area = area
indexReturn = index
return indexReturn

indexReturn = biggestRectangle(contours)
hull = cv2.convexHull(contours[indexReturn])

print(indexReturn)
print(contours[indexReturn])
print(hull)

# copyImg = img.copy()

cv2.imwrite(os.path.join('path_to_save', imageName),
cv2.drawContours(img, [hull], 0, (0, 255, 0), 3))

如何使其正确。我尝试了另一种方法:
x, y, w, h = cv2.boundingRect(hull)

cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
newImageName = imageName.split('.')
newImageName = newImageName[0] + '_rect.' + newImageName[1]
print(newImageName)
cv2.imwrite(os.path.join('path_to_save', newImageName), img)

但这给了我这个(蓝色轮廓):
enter image description here

这也是错误的。如何纠正?

最佳答案

因为您知道您感兴趣的轮廓是一张纸,所以也许您可以使用更系统的方法,使用OpenCV的cv2.minAreaRect(link)找到最大的旋转矩形。它将返回最紧密地包围轮廓的矩形的中心,大小和旋转。由于您的图像背景非常困难,因此在抓取轮廓之前,我不得不使用OpenCV's morphologyEx function进行一些处理,但是此代码使我看到了更好的输出(如下所示)。

# first, do some processing
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (27, 27))
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (13, 13))
thresh1 = cv2.morphologyEx(thresh1, cv2.MORPH_ERODE, kernel1)
thresh1 = cv2.morphologyEx(thresh1, cv2.MORPH_DILATE, kernel2)

# find all contours
contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
# Find the largest ('max' using the key of the contour area)
biggest_contour = max(contours, key=cv2.contourArea)

# Calculate the minimum enclosing rectangle: format ((x, y), (length, width), angle)
rectangle = cv2.minAreaRect(biggest_contour)

# conver the minAreaRect output to points
pts = cv2.boxPoints(rectangle)
# contours must be of shape (n, 1, 2) and dtype integer
pts = pts.reshape(-1, 1, 2)
pts = pts.astype(int)

# draw contours and display
cv2.drawContours(img, [pts], -1, (255, 0, 0), 2)
cv2.imshow('img', img)
k = cv2.waitKey(0)

输出:
enter image description here

关于python - Opencv和python获取正确的文档构建并绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61681264/

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