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

Nol*_*lik 184 python opencv

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

关于PIL的工作示例

im = Image.open('0.png').convert('L')
im = im.crop((1, 1, 98, 33))
im.save('_0.png')
Run Code Online (Sandbox Code Playgroud)

但是我怎么能在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)
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

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

Fro*_*oyo 433

这很简单.使用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)
Run Code Online (Sandbox Code Playgroud)

  • **记住**x和y被翻转.我错过了这个. (35认同)
  • 这很好,只要知道改变crop_img会改变img.否则,你应该crop_img = img [y:y + h,x:x + w] .copy() (27认同)
  • 嗯......但是我如何将裁剪图像保存为变量呢? (8认同)
  • 或者,如果您已经定义了作物边距,则可以执行`crop_img = img [margin:-margin,margin:-margin] (5认同)
  • @javadba numpy 实现细节。Numpy 使用 row, col 表示法而不是 col, row (2认同)
  • 使用 x, y 来解释这有点令人困惑,实际上最好使用“axis”来解释。在上面的答案中,y实际上代表第一个轴,x代表第二个轴,参考[基础知识](https://docs.scipy.org/doc/numpy/user/quickstart.html#the-basics)。然后我们就可以理解就像访问第一个轴然后访问第二个轴一样。这与 Python、Java 或 C/C++ 中的 2D 数组访问相同。 (2认同)

sam*_*n13 105

我有这个问题,在这里找到另一个答案:复制感兴趣的区域

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

roi = im[y1:y2, x1:x2]
Run Code Online (Sandbox Code Playgroud)

这是一个关于numpy数组索引和切片的综合资源,它可以告诉你更多关于裁剪图像的一部分的事情.图像将作为numpy数组存储在opencv2中.

:)


smt*_*tsp 11

需要注意的是,图像切片也没有创造的一个副本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.
Run Code Online (Sandbox Code Playgroud)

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

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

找出在此之后,我意识到一个评论user1270710提到,但我花了相当长的一段时间去找出(即,调试等).所以,我认为值得一提.


m.h*_*ami 9

此代码将图像从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) 
Run Code Online (Sandbox Code Playgroud)

  • 感谢使用实际上可以理解的变量名称。 (2认同)

Dan*_*rez 5

这是一些更强大的 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
Run Code Online (Sandbox Code Playgroud)


小智 5

具有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
Run Code Online (Sandbox Code Playgroud)


San*_*yal 5

下面是裁剪图像的方法。

image_path:要编辑的图像的路径

coords: x/y 坐标元组 (x1, y1, x2, y2)[在 mspaint 中打开图像并检查视图选项卡中的“标尺”以查看坐标]

saved_location : 保存裁剪图像的路径

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')
Run Code Online (Sandbox Code Playgroud)