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].
Đâ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
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.
Đã trả lời ngày 16 tháng 2 năm 2017 lúc 9:38Feb 16, 2017 at 9:38
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
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.
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]
2import 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]
3import 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]
4import 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]
5import 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]
6import 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]
7import 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]
8import
cv2
image.shape2
Output: :
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]
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]
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]
9image.shape0
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]
2import 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]
3import 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]
4import 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]
5import 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]
2import 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]
3image[rows,columns]6
image[rows,columns]7
Output: :
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]
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]
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]
9image.shape0
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]
1Bướ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.
- trong đó hình ảnh là hình ảnh đầu vào
- 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]
2import 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]
3import 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]
4import 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]
5import 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]
2import 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]
3image[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]
6cv2
5import 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]
8import 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]
6cv2
8cv2
9import 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]
9image.shape0
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]
1image.shape2
Output: :