Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用opencv获取扫描图像的边缘坐标?_Python_Opencv - Fatal编程技术网

Python 如何使用opencv获取扫描图像的边缘坐标?

Python 如何使用opencv获取扫描图像的边缘坐标?,python,opencv,Python,Opencv,我有一个图像,是扫描硬拷贝文件的结果,如下所示: 如您所见,每个角落都有一个空格和四个矩形 我需要找到矩形每条边的坐标,这样我就可以裁剪它了 我使用opencv,如何使用opencv 如何使用opencv获取扫描图像的边缘坐标 这是我想要达到的结果 如果我有那个坐标,我可以用我从互联网上得到的代码来裁剪它: # USAGE # python transform_example.py --image images/example_01.png --coords "[(73, 239), (35

我有一个图像,是扫描硬拷贝文件的结果,如下所示:

如您所见,每个角落都有一个空格和四个矩形

我需要找到矩形每条边的坐标,这样我就可以裁剪它了

我使用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)

使用此代码,您可以根据需要直接裁剪图像

import cv2
import numpy as np

new_image = cv2.imread('test.jpg', 0)

rows, cols = new_image.shape
vertical_histrogram = np.zeros(cols)
horizontal_histrogram = np.zeros(rows)
for col in range(cols):  # create vertical histrogram for each lines
    for row in range(rows):
        if new_image[row, col] == 0:
            vertical_histrogram[col] += 1

for row in range(rows):  # create horizontal histrogram for each lines
    for col in range(cols):
        if new_image[row, col] == 0:
            horizontal_histrogram[row] += 1

for row in range(rows - 1, -1, -1):
    if horizontal_histrogram[row] > 0:
        top = row;
        break

for row in range(0, rows):
    if horizontal_histrogram[row] > 0:
        bottom = row;
        break

for col in range(0, cols):
    if vertical_histrogram[col] > 0:
        left = col;
        break

for col in range(cols - 1, -1, -1):
    if vertical_histrogram[col] > 0:
        right = col;
        break

new_image = new_image[bottom :top , left :right ]

cv2.imwrite('output.jpg', new_image)

使用此代码,您可以根据需要直接裁剪图像

import cv2
import numpy as np

new_image = cv2.imread('test.jpg', 0)

rows, cols = new_image.shape
vertical_histrogram = np.zeros(cols)
horizontal_histrogram = np.zeros(rows)
for col in range(cols):  # create vertical histrogram for each lines
    for row in range(rows):
        if new_image[row, col] == 0:
            vertical_histrogram[col] += 1

for row in range(rows):  # create horizontal histrogram for each lines
    for col in range(cols):
        if new_image[row, col] == 0:
            horizontal_histrogram[row] += 1

for row in range(rows - 1, -1, -1):
    if horizontal_histrogram[row] > 0:
        top = row;
        break

for row in range(0, rows):
    if horizontal_histrogram[row] > 0:
        bottom = row;
        break

for col in range(0, cols):
    if vertical_histrogram[col] > 0:
        left = col;
        break

for col in range(cols - 1, -1, -1):
    if vertical_histrogram[col] > 0:
        right = col;
        break

new_image = new_image[bottom :top , left :right ]

cv2.imwrite('output.jpg', new_image)

,希望这会有所帮助。首先找到轮廓,然后按比例过滤。这是我的结果:,希望这会有所帮助。首先找到轮廓,然后按比例过滤。这是我的结果:谢谢,伙计,这很有效,非常感谢,但不幸的是。如果图像不对齐,它就不能工作汉克斯,它可以工作,非常感谢,但不幸的是。如果图像没有对齐,它将无法工作