我需要使用 python 编码裁剪订单。我需要的只是卡。所以我想裁剪边框。怎么做??
这是我使用下面评论中提到的代码得到的输出。
也许您可以使用不同的方法opencv
,在其中创建一个灰色蒙版以仅获取图像的有趣区域。你可以这样做:
#import the necessary packages
import numpy as np
import cv2
#read the image
image = cv2.imread('image.jpg')
#rgb values for grey color in pixels
lower = np.array([80,70,70],dtype='uint8')
upper = np.array([95,85,85],dtype='uint8')
#create a grey mask and then the inverse of that mask
mask = cv2.inRange(image,lower,upper)
mask_inv = cv2.bitwise_not(mask)
output = cv2.bitwise_and(image,image,mask=mask_inv)
# display the result
cv2.imshow('images',np.hstack([output]))
cv2.waitKey(0)
假设您每次都想提取形状/位置不同的卡片,那么该技术可能会派上用场。
您需要做的就是对数组进行切片。首先向切片提供 startY 和 endY 坐标,然后是 startX 和 endX 坐标。而已。您的图像将被裁剪!
使用 Python 和 OpenCV 的步骤:
1)加载图像并将其显示在屏幕上
# import the necessary packages
import cv2
# load the image and show it
image = cv2.imread("cardWithBorder.jpg")
cv2.imshow("original", image)
cv2.waitKey(0)
2)获取图像的尺寸
print image.shape
3)裁剪图像
# crop the image using array slices -- it's a NumPy array
# after all!
cropped = image[70:170, 440:540]
cv2.imshow("cropped", cropped)
cv2.waitKey(0)
4) 将裁剪后的图像保存到磁盘,仅以 PNG 格式(原为 JPG):
cv2.imwrite("thumbnail.png", cropped)
您可以尝试使用 Python 图像库PIL
from PIL import Image
img = Image.open("ImageName.jpg")
area_top = (125,70,444,253)
cropped_img_top = img.crop(area_top)
cropped_img_top.show()
img = Image.open("ImageName.jpg")
area_bottom = (125,306,444,490)
cropped_img_bottom = img.crop(area_bottom)
cropped_img_bottom.show()
如果您想自动编辑多张图片,您可以使用以下程序将所有 .jpg 文件裁剪到与您的 python 文件相同的文件夹中:
import os
from PIL import Image
files = os.listdir()
for image_name in files:
if ".jpg" in image_name:
img = Image.open(image_name)
area_top = (125, 70, 444, 253)
cropped_img_top = img.crop(area_top)
cropped_img_top.save("cropped_top_"+image_name)
area_bottom = (125, 306, 444, 490)
cropped_img_bottom = img.crop(area_bottom)
cropped_img_bottom.save("cropped_bottom_"+image_name)
编辑: 好的,如果你想让它自己找到角落,你可以试试下面的代码。我刚刚实现了一个非常基本的角点查找算法,该算法在您提供的图像上运行良好,但我不知道您的其他图像看起来如何,因此可能存在问题。我也花了很长时间来编码,所以请欣赏使用;)
这是代码:
import os
from PIL import Image
import numpy as np
def convolution2d(matrix, kernel):
m, n = kernel.shape
y, x = matrix.shape
y = y - m + 1
x = x - m + 1
new_matrix = np.zeros((y, x))
for i in range(y):
for j in range(x):
new_matrix[i][j] = np.sum(matrix[i:i + m, j:j + m] * kernel)
return new_matrix
def widen(mask, amount):
return np.array([[mask[0][0]] * amount + [mask[0][1]] * amount] * amount +
[[mask[1][0]] * amount + [mask[1][1]] * amount] * amount)
def too_close(existing, new, max_dist):
for c in existing:
if (c[0] - new[0]) ** 2 + (c[1] - new[1]) ** 2 < max_dist ** 2:
return True
return False
def corner(bw, mask, corner_threshold, offset):
corner_hotmap = convolution2d(bw, mask)
corner_threshold = np.max(corner_hotmap) * corner_threshold
width = len(corner_hotmap)
height = len(corner_hotmap[0])
corners = []
for x in range(width):
for y in range(height):
if corner_hotmap[x][y] > corner_threshold:
if not too_close(corners, [x, y], 10):
corners.append([x + offset, y + offset])
return corners
def get_areas(image, brightness_threshold=100, corner_threshold=0.9, n_pix=4):
width = len(image)
height = len(image[0])
greyscale = np.zeros(shape=(width, height))
for x in range(width):
for y in range(height):
s = sum(image[x][y]) / 3
if s > brightness_threshold:
greyscale[x][y] = 1
else:
greyscale[x][y] = -1
top_left = widen([[-1, -1, ], [-1, 1, ]], n_pix)
bottom_right = widen([[1, -1, ], [-1, -1, ]], n_pix)
corners_topleft = corner(greyscale, top_left, corner_threshold, n_pix)
corners_bottomright = corner(greyscale, bottom_right, corner_threshold, n_pix)
if len(corners_topleft) != len(corners_bottomright):
return []
else:
out = []
for i in range(len(corners_topleft)):
out.append((corners_topleft[i][1], corners_topleft[i][0], corners_bottomright[i][1],
corners_bottomright[i][0]))
return out
files = os.listdir()
for image_name in files:
if ".jpg" in image_name:
img = Image.open(image_name)
width = img.size[0]
height = img.size[1]
image = np.array(Image.open(image_name).getdata()).reshape(height, width, 3)
print("Getting Areas for file {}.".format(image_name))
areas = get_areas(image)
if len(areas)==0:
print("Could not find corners for file {}.".format(image_name))
else:
print("Found {} cards".format(len(areas)))
for i in range(len(areas)):
cropped_img = img.crop(areas[i])
cropped_img.save("cropped_{}_{}".format(i, image_name))
寻找角落并不容易。该算法适用于您提供的图像,但是我不知道其他图像的外观以及这是否也适用于这些图像。祝你裁剪图像好运^^