【发布时间】:2018-05-09 20:00:53
【问题描述】:
我有一张图片,它是扫描硬拷贝文件的结果,如下所示:
如您所见,每个角落都有一个空格和四个矩形。
我需要找到矩形每个边缘的坐标,所以我可以裁剪它。
我用opencv,用opencv怎么做?
如何用opencv获取扫描图像的边缘坐标?
如果我有那个坐标,那么我可以用我从互联网上得到的这段代码来裁剪它:
# USAGE
# python transform_example.py --image images/example_01.png --coords "[(73, 239), (356, 117), (475, 265), (187, 443)]"
# python transform_example.py --image images/example_02.png --coords "[(101, 185), (393, 151), (479, 323), (187, 441)]"
# python transform_example.py --image images/example_03.png --coords "[(63, 242), (291, 110), (361, 252), (78, 386)]"
# import the necessary packages
from pyimagesearch.transform import four_point_transform
import numpy as np
import argparse
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
ap.add_argument("-c", "--coords",
help = "comma seperated list of source points")
args = vars(ap.parse_args())
# load the image and grab the source coordinates (i.e. the list of
# of (x, y) points)
# NOTE: using the 'eval' function is bad form, but for this example
# let's just roll with it -- in future posts I'll show you how to
# automatically determine the coordinates without pre-supplying them
image = cv2.imread(args["image"])
pts = np.array(eval(args["coords"]), dtype = "float32")
# apply the four point tranform to obtain a "birds eye view" of
# the image
warped = four_point_transform(image, pts)
# show the original and warped images
cv2.imshow("Original", image)
cv2.imshow("Warped", warped)
cv2.waitKey(0)
【问题讨论】:
-
stackoverflow.com/questions/46486078/… ,希望这会有所帮助。
-
先找到轮廓,然后按比例过滤。这是我的结果:i.stack.imgur.com/S39gP.png