答案 0 :(得分:0)
您可以尝试使用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))
寻找角落并不容易。该算法在您提供的图像上效果很好,但是我不知道其他图像的外观以及是否也可以在这些图像上使用。 裁剪图片祝您好运^^
答案 1 :(得分: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)
答案 2 :(得分:0)
也许您可以使用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)
假设您要提取每次形状/位置都不相同的卡片,则该技术可能会派上用场。