OpenCV python 裁剪图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41858998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:44:35  来源:igfitidea点击:

OpenCV python cropping image

pythonimageopencv

提问by hory

I've created black image, than I drew a red rectangle into this image. Afterwards I cropped this image and drew a another rectangle into the croppedimage using the command. cv2.rectangle(crop,(50,50),(150,150),(0,0,255),3)

我创建了黑色图像,然后在该图像中绘制了一个红色矩形。之后我裁剪了这个图像并使用命令在裁剪后的图像中绘制了另一个矩形。cv2.rectangle(crop,(50,50),(150,150),(0,0,255),3)

Why does this second rectangle appears in the original image when I show it at the end? I expected to see just the first rectangle.

当我在最后显示它时,为什么第二个矩形会出现在原始图像中?我希望只看到第一个矩形。

import cv2
import numpy as np

#create image
image = np.zeros((400,400,3), np.uint8)

#draw rectangle into original image
cv2.rectangle(image,(100,100),(300,300),(0,0,255),3)

#crop image
crop = image[100:300,100:300]

#draw rectangle into cropped image
cv2.rectangle(crop,(50,50),(150,150),(0,0,255),3)
cv2.imshow('Result', image)
cv2.waitKey()    

cv2.destroyAllWindows()

回答by Martin Valgur

crop = image[100:300,100:300]creates a viewon the original image instead of a new object. Modifying that view will modify the underlying original image. See http://scipy-cookbook.readthedocs.io/items/ViewsVsCopies.htmlfor more details.

crop = image[100:300,100:300]在原始图像而不是新对象上创建视图。修改该视图将修改底层原始图像。有关更多详细信息,请参阅http://scipy-cookbook.readthedocs.io/items/ViewsVsCopies.html

You can resolve this issue by creating a copy when cropping: crop = image[100:300,100:300].copy().

您可以通过在裁剪时创建副本来解决此问题: crop = image[100:300,100:300].copy()

Note: image[100:300,100:300]parameters are y: y+h, x: x+w not x: x+w, y: y+h

注:image[100:300,100:300]参数为y: y+h, x: x+w not x: x+w, y: y+h

回答by Filomeno E. Plaza III

if you want to save the cropped image, just add this code:

如果要保存裁剪后的图像,只需添加以下代码:

cv2.imwrite("Cropped.jpg", roi)

after cv2.imshow("Cropped", roi)

I hope this helps.

我希望这有帮助。

回答by Md. Hanif Ali Sohag

you can easily crop the image in python by using

您可以使用以下命令轻松裁剪python中的图像

roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]

In order to get the two points you can call cv2.setMouseCallback("image", mouse_crop). The function is something like this

为了得到这两个点,你可以调用cv2.setMouseCallback("image", mouse_crop)。功能是这样的

def mouse_crop(event, x, y, flags, param):
    # grab references to the global variables
    global x_start, y_start, x_end, y_end, cropping

    # if the left mouse button was DOWN, start RECORDING
    # (x, y) coordinates and indicate that cropping is being
    if event == cv2.EVENT_LBUTTONDOWN:
        x_start, y_start, x_end, y_end = x, y, x, y
        cropping = True

    # Mouse is Moving
    elif event == cv2.EVENT_MOUSEMOVE:
        if cropping == True:
            x_end, y_end = x, y

    # if the left mouse button was released
    elif event == cv2.EVENT_LBUTTONUP:
        # record the ending (x, y) coordinates
        x_end, y_end = x, y
        cropping = False # cropping is finished

        refPoint = [(x_start, y_start), (x_end, y_end)]

        if len(refPoint) == 2: #when two points were found
            roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
            cv2.imshow("Cropped", roi)

You can get details from here : Mouse Click and Cropping using Python

您可以从此处获取详细信息:使用 Python 进行鼠标单击和裁剪