Hướng dẫn random crop image python opencv - hình ảnh cắt ngẫu nhiên python opencv

Tôi đang cố gắng chụp ảnh từ một video và tạo ra một đoạn ngẫu nhiên 64 x 64 x 3 của nó (chiều rộng 64, chiều cao 64, 3 cho các kênh màu).

Show

Đây là những gì tôi có cho đến nay:

def process_video(video_name):
    # load video using cv2
    video_cap = cv2.VideoCapture(video_name)
    if video_cap.isOpened():
        ret, frame = video_cap.read()
    else:
        ret = False
    # while there's another frame
    i = 0
    while ret:
        ret, frame = video_cap.read()
        if i % 10 == 0:
            # save several images from frame to local directory
        i += 1
    video_cap.release()

Tôi muốn lấy một phần nhỏ của khung (64 x 64 x 3) và lưu nó dưới dạng tệp .jpg, vì vậy tôi gặp rắc rối với phần nhận xét cuối cùng. Bất kỳ đề xuất cho làm thế nào để đi về điều này?

Thanks!

hỏi ngày 16 tháng 2 năm 2017 lúc 1:06Feb 16, 2017 at 1:06

Hướng dẫn random crop image python opencv - hình ảnh cắt ngẫu nhiên python opencv

1

Để có được một vụ mùa ngẫu nhiên của hình ảnh của bạn, bạn chỉ nên lấy mẫu vị trí X và Y và sau đó chọn phần đó của ma trận như @Max đã giải thích:

import numpy as np

def get_random_crop(image, crop_height, crop_width):

    max_x = image.shape[1] - crop_width
    max_y = image.shape[0] - crop_height

    x = np.random.randint(0, max_x)
    y = np.random.randint(0, max_y)

    crop = image[y: y + crop_height, x: x + crop_width]

    return crop



example_image = np.random.randint(0, 256, (1024, 1024, 3))
random_crop = get_random_crop(example_image, 64, 64)

Đã trả lời ngày 30 tháng 3 năm 2019 lúc 23:14Mar 30, 2019 at 23:14

Tedtedted

12.4K7 Huy hiệu vàng58 Huy hiệu bạc100 Huy hiệu Đồng7 gold badges58 silver badges100 bronze badges

Đối với c, r, chiều rộng, chiều cao

img = img[r:r+height,c:c+width] sẽ nhận được một đoạn từ cột C có chiều rộng mong muốn và từ hàng r có chiều cao mong muốn.

Hướng dẫn random crop image python opencv - hình ảnh cắt ngẫu nhiên python opencv

Đã trả lời ngày 16 tháng 2 năm 2017 lúc 9:38Feb 16, 2017 at 9:38

Hướng dẫn random crop image python opencv - hình ảnh cắt ngẫu nhiên python opencv

Max Walczakmax WalczakMax Walczak

4232 huy hiệu bạc11 huy hiệu đồng2 silver badges11 bronze badges

4

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Cắt một hình ảnh là một trong những hoạt động hình ảnh cơ bản nhất mà chúng tôi thực hiện trong các dự án của chúng tôi. Trong bài viết này, W sẽ thảo luận về cách cắt hình ảnh bằng OpenCV trong Python.

    Hướng dẫn random crop image python opencv - hình ảnh cắt ngẫu nhiên python opencv

    Thực hiện từng bước

    Đối với điều này, chúng tôi sẽ chụp ảnh hiển thị bên dưới. & NBSP;

    Bước 1: Đọc hình ảnhWhen we load an image in OpenCV using cv2.imread(), we store it as a Numpy n-dimensional array.

    Phương thức cv2.ImRead () tải một hình ảnh từ tệp được chỉ định. Nếu hình ảnh không thể được đọc (vì tệp bị thiếu, các quyền không đúng, định dạng không được hỗ trợ hoặc không hợp lệ) thì phương thức này sẽ trả về một ma trận trống.Python program to read the image

    Python3

    Lưu ý: Khi chúng tôi tải một hình ảnh trong openCV bằng CV2.Imread (), chúng tôi lưu trữ nó dưới dạng mảng N chiều numpy.

    Ví dụ: Chương trình Python để đọc hình ảnh

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    2
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    3
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    4
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    5

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    6
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    7
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    8

    import cv2

    image.shape
    2

    Output: 

    Hướng dẫn random crop image python opencv - hình ảnh cắt ngẫu nhiên python opencv

    img = cv2.imread(import numpy as np def get_random_crop(image, crop_height, crop_width): max_x = image.shape[1] - crop_width max_y = image.shape[0] - crop_height x = np.random.randint(0, max_x) y = np.random.randint(0, max_y) crop = image[y: y + crop_height, x: x + crop_width] return crop example_image = np.random.randint(0, 256, (1024, 1024, 3)) random_crop = get_random_crop(example_image, 64, 64) 0import numpy as np def get_random_crop(image, crop_height, crop_width): max_x = image.shape[1] - crop_width max_y = image.shape[0] - crop_height x = np.random.randint(0, max_x) y = np.random.randint(0, max_y) crop = image[y: y + crop_height, x: x + crop_width] return crop example_image = np.random.randint(0, 256, (1024, 1024, 3)) random_crop = get_random_crop(example_image, 64, 64) 1

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    9
    image.shape
    0
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    1img‘ as ‘numpy.ndarray‘. Now, we simply apply array slicing to our NumPy array and produce our cropped image, So we have to find the dimensions of the image. For this we will use the image.shape attribute.

    Syntax:

    image.shape

    Bước 2: Nhận kích thước hình ảnh

    Chúng ta có thể thấy loại ‘img‘ như numpy.ndarray. Bây giờ, chúng tôi chỉ cần áp dụng cắt mảng vào mảng numpy của chúng tôi và tạo ra hình ảnh bị cắt của chúng tôi, vì vậy chúng tôi phải tìm kích thước của hình ảnh. Đối với điều này, chúng tôi sẽ sử dụng thuộc tính Image.Shape. Python code to find the dimensions of the image,

    Python3

    Lưu ý: Khi chúng tôi tải một hình ảnh trong openCV bằng CV2.Imread (), chúng tôi lưu trữ nó dưới dạng mảng N chiều numpy.

    Ví dụ: Chương trình Python để đọc hình ảnh

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    2
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    3
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    4
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    5

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    2
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    3
    image[rows,columns]
    6
    image[rows,columns]
    7

    Output: 

    Hướng dẫn random crop image python opencv - hình ảnh cắt ngẫu nhiên python opencv

    import cv2

    img = cv2.imread(import numpy as np def get_random_crop(image, crop_height, crop_width): max_x = image.shape[1] - crop_width max_y = image.shape[0] - crop_height x = np.random.randint(0, max_x) y = np.random.randint(0, max_y) crop = image[y: y + crop_height, x: x + crop_width] return crop example_image = np.random.randint(0, 256, (1024, 1024, 3)) random_crop = get_random_crop(example_image, 64, 64) 0import numpy as np def get_random_crop(image, crop_height, crop_width): max_x = image.shape[1] - crop_width max_y = image.shape[0] - crop_height x = np.random.randint(0, max_x) y = np.random.randint(0, max_y) crop = image[y: y + crop_height, x: x + crop_width] return crop example_image = np.random.randint(0, 256, (1024, 1024, 3)) random_crop = get_random_crop(example_image, 64, 64) 1

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    9
    image.shape
    0
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    1

    Bước 2: Nhận kích thước hình ảnh

    image[rows,columns]

    Chúng ta có thể thấy loại ‘img‘ như numpy.ndarray. Bây giờ, chúng tôi chỉ cần áp dụng cắt mảng vào mảng numpy của chúng tôi và tạo ra hình ảnh bị cắt của chúng tôi, vì vậy chúng tôi phải tìm kích thước của hình ảnh. Đối với điều này, chúng tôi sẽ sử dụng thuộc tính Image.Shape.

    1. trong đó hình ảnh là hình ảnh đầu vào
    2. Ví dụ: Mã python để tìm kích thước của hình ảnh,

    Example:

    Python3

    Lưu ý: Khi chúng tôi tải một hình ảnh trong openCV bằng CV2.Imread (), chúng tôi lưu trữ nó dưới dạng mảng N chiều numpy.

    Ví dụ: Chương trình Python để đọc hình ảnh

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    2
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    3
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    4
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    5

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    2
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    3
    image[rows,columns]
    6
    image[rows,columns]
    7

    import cv2

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    6cv25
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    8

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    6cv28cv29

    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    9
    image.shape
    0
    import numpy as np
    
    def get_random_crop(image, crop_height, crop_width):
    
        max_x = image.shape[1] - crop_width
        max_y = image.shape[0] - crop_height
    
        x = np.random.randint(0, max_x)
        y = np.random.randint(0, max_y)
    
        crop = image[y: y + crop_height, x: x + crop_width]
    
        return crop
    
    
    
    example_image = np.random.randint(0, 256, (1024, 1024, 3))
    random_crop = get_random_crop(example_image, 64, 64)
    
    
    1

    image.shape
    2

    Output: 

    Hướng dẫn random crop image python opencv - hình ảnh cắt ngẫu nhiên python opencv


    Làm cách nào để cắt một hình ảnh trong OpenCV Python?

    Không có chức năng cụ thể để cắt xén bằng cách sử dụng openCV, cắt mảng numpy là những gì công việc. Mỗi hình ảnh được đọc trong, được lưu trữ trong một mảng 2D (cho mỗi kênh màu). Chỉ cần chỉ định chiều cao và chiều rộng (tính bằng pixel) của khu vực được cắt. Và nó đã xong!NumPy array slicing is what does the job. Every image that is read in, gets stored in a 2D array (for each color channel). Simply specify the height and width (in pixels) of the area to be cropped. And it's done!

    Làm cách nào để cắt nhiều hình ảnh cùng một lúc trong Python?

    Bạn có thể thay đổi kích thước nhiều hình ảnh trong Python với thư viện PIL tuyệt vời và một trợ giúp nhỏ của thư viện HĐH (hệ điều hành).Bằng cách sử dụng hàm Os.ListDir (), bạn có thể đọc tất cả các tên tệp trong một thư mục. Sau đó, tất cả những gì bạn phải làm là tạo một vòng lặp để mở, thay đổi kích thước và lưu mỗi hình ảnh trong thư mục.By using os. listdir() function you can read all the file names in a directory. After that, all you have to do is to create a for loop to open, resize and save each image in the directory.

    Làm cách nào để cắt một phần hình ảnh trong Python?

    Phương pháp pil.image.crop () được sử dụng để cắt một phần hình chữ nhật của bất kỳ hình ảnh nào. Image. crop() method is used to crop a rectangular portion of any image.

    Cây trồng ngẫu nhiên là gì?

    RandomCrop (chiều cao, chiều rộng, hạt giống = không có, ** kwargs) một lớp tiền xử lý có hình ảnh ngẫu nhiên trong quá trình đào tạo.Trong quá trình đào tạo, lớp này sẽ chọn ngẫu nhiên một vị trí để cắt hình ảnh xuống kích thước mục tiêu.Lớp sẽ cắt tất cả các hình ảnh trong cùng một lô đến cùng một vị trí cắt xén.A preprocessing layer which randomly crops images during training. During training, this layer will randomly choose a location to crop images down to a target size. The layer will crop all the images in the same batch to the same cropping location.