如何使用Python在OpenCV中裁剪图像

时间:2013-03-23 17:00:43

标签: python opencv

如何使用OpenCV裁剪图像,就像我之前在PIL中所做的那样。

关于PIL的工作示例

im = Image.open('0.png').convert('L')
im = im.crop((1, 1, 98, 33))
im.save('_0.png')

但是我怎么能在OpenCV上做到这一点?

这就是我的尝试:

im = cv.imread('0.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
(thresh, im_bw) = cv.threshold(im, 128, 255, cv.THRESH_OTSU)
im = cv.getRectSubPix(im_bw, (98, 33), (1, 1))
cv.imshow('Img', im)
cv.waitKey(0)

但它不起作用。

我认为我错误地使用了getRectSubPix。如果是这种情况,请解释我如何正确使用此功能。

11 个答案:

答案 0 :(得分:367)

这很简单。使用numpy切片。

import cv2
img = cv2.imread("lenna.png")
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

答案 1 :(得分:94)

我有这个问题,并在此处找到了另一个答案:copy region of interest

如果我们将(0,0)视为名为im的图像的左上角,从左到右为x方向,从上到下为y方向。我们将(x1,y1)作为左上顶点,将(x2,y2)作为该图像中矩形区域的右下顶点,然后:

roi = im[y1:y2, x1:x2]

这是numpy array indexing and slicing上的综合资源,它可以告诉您更多关于裁剪图像的一部分的信息。图像将作为numpy数组存储在opencv2中。

:)

答案 2 :(得分:7)

请注意,图像切片不是创建cropped image的副本,而是创建pointerroi的副本。如果要加载这么多图像,使用切片裁剪图像的相关部分,然后追加到列表中,则可能会浪费大量内存。

假设您加载N张图像,每个图像均为>1MP,并且您只需要从左上角开始100x100个区域。

Slicing

X = []
for i in range(N):
    im = imread('image_i')
    X.append(im[0:100,0:100]) # This will keep all N images in the memory. 
                              # Because they are still used.

或者,您可以通过.copy()复制相关部分,因此垃圾收集器将删除im

X = []
for i in range(N):
    im = imread('image_i')
    X.append(im[0:100,0:100].copy()) # This will keep all only the crops in the memory. 
                                     # im's will be deleted by gc.

发现这一点后,我意识到one of the commentsuser1270710提到了这一点,但花了我很多时间才能发现(即调试等)。所以,我认为值得一提。

答案 3 :(得分:3)

此代码将图像从x = 0,y = 0位置裁剪为h = 100,w = 200

import numpy as np
import cv2

image = cv2.imread('download.jpg')
y=0
x=0
h=100
w=200
crop = image[y:y+h, x:x+w]
cv2.imshow('Image', crop)
cv2.waitKey(0) 

答案 4 :(得分:2)

具有opencv复制边框功能的健壮作物:

def imcrop(img, bbox):
   x1, y1, x2, y2 = bbox
   if x1 < 0 or y1 < 0 or x2 > img.shape[1] or y2 > img.shape[0]:
        img, x1, x2, y1, y2 = pad_img_to_fit_bbox(img, x1, x2, y1, y2)
   return img[y1:y2, x1:x2, :]

def pad_img_to_fit_bbox(img, x1, x2, y1, y2):
    img = cv2.copyMakeBorder(img, - min(0, y1), max(y2 - img.shape[0], 0),
                            -min(0, x1), max(x2 - img.shape[1], 0),cv2.BORDER_REPLICATE)
   y2 += -min(0, y1)
   y1 += -min(0, y1)
   x2 += -min(0, x1)
   x1 += -min(0, x1)
   return img, x1, x2, y1, y2

答案 5 :(得分:1)

这里有一些代码用于更强大的imcrop(有点像在matlab中)

def imcrop(img, bbox): 
    x1,y1,x2,y2 = bbox
    if x1 < 0 or y1 < 0 or x2 > img.shape[1] or y2 > img.shape[0]:
        img, x1, x2, y1, y2 = pad_img_to_fit_bbox(img, x1, x2, y1, y2)
    return img[y1:y2, x1:x2, :]

def pad_img_to_fit_bbox(img, x1, x2, y1, y2):
    img = np.pad(img, ((np.abs(np.minimum(0, y1)), np.maximum(y2 - img.shape[0], 0)),
               (np.abs(np.minimum(0, x1)), np.maximum(x2 - img.shape[1], 0)), (0,0)), mode="constant")
    y1 += np.abs(np.minimum(0, y1))
    y2 += np.abs(np.minimum(0, y1))
    x1 += np.abs(np.minimum(0, x1))
    x2 += np.abs(np.minimum(0, x1))
    return img, x1, x2, y1, y2

答案 6 :(得分:1)

或者,您可以使用tensorflow进行裁剪,并使用openCV从图像中创建数组。

import cv2
img = cv2.imread('YOURIMAGE.png')

现在img是一个(imageheight,imagewidth,3)形状数组。使用tensorflow裁剪数组:

import tensorflow as tf
offset_height=0
offset_width=0
target_height=500
target_width=500
x = tf.image.crop_to_bounding_box(
    img, offset_height, offset_width, target_height, target_width
)

使用tf.keras重新组装图像,因此我们可以对其进行查看:

tf.keras.preprocessing.image.array_to_img(
    x, data_format=None, scale=True, dtype=None
)

这会打印出笔记本中的图片(在Google Colab中经过测试)。


整个代码在一起:

import cv2
img = cv2.imread('YOURIMAGE.png')

import tensorflow as tf
offset_height=0
offset_width=0
target_height=500
target_width=500
x = tf.image.crop_to_bounding_box(
    img, offset_height, offset_width, target_height, target_width
)

tf.keras.preprocessing.image.array_to_img(
    x, data_format=None, scale=True, dtype=None
)

答案 7 :(得分:0)

下面是裁剪图像的方法。

image_path:要编辑的图像的路径

坐标: x / y坐标(x1,y1,x2,y2)的元组[在 mspaint,然后在视图选项卡中检查“ ruler”以查看坐标]

保存位置:保存裁剪图像的路径

from PIL import Image
    def crop(image_path, coords, saved_location:
        image_obj = Image.open("Path of the image to be cropped")
            cropped_image = image_obj.crop(coords)
            cropped_image.save(saved_location)
            cropped_image.show()


if __name__ == '__main__':
    image = "image.jpg"
    crop(image, (100, 210, 710,380 ), 'cropped.jpg')

答案 8 :(得分:0)

为您提供方便,这是我使用的代码:

w, h = image.shape
top=10
right=50
down=15
left=80
croped_image = image[top:((w-down)+top), right:((h-left)+right)]
plt.imshow(croped_image, cmap="gray")
plt.show()

答案 9 :(得分:0)

通过使用此功能,您可以轻松裁剪图像

def cropImage(Image, XY: tuple, WH: tuple, returnGrayscale=False):
    # Extract the x,y and w,h values
    (x, y) = XY
    (w, h) = WH
    # Crop Image with numpy splitting
    crop = Image[y:y + h, x:x + w]
    # Check if returnGrayscale Var is true if is then convert image to grayscale
    if returnGrayscale:
        crop = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
    # Return cropped image
    return crop

希望对您有所帮助

答案 10 :(得分:0)

在代码下方裁剪或感兴趣区域(ROI)用于面部使用

import cv2 
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
image=cv2.imread("ronaldo.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
     cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2) 
     roi_image = gray[y:y+h, x:x+w]
cv2.imshow("crop/region of interset image",roi_image) 
cv2.waitKey(0)
cv2.destroyAllWindows()

check for reference