使用 OpenCV 从左到右和从上到下将图像裁剪为较小的尺寸
Crop an image to smaller size from left to right and top to bottom using OpenCV
我有一张 315x581 的图片。我想从左上角到右下角以 28x28 裁剪它,然后我需要将每个 28x28 图像保存在文件夹中。
我可以只裁剪一张图像,从 y1=0 到 y2=28 和 x1=0 到 x2=28。
第一个问题是:我使用 cv2.imwrite("cropped.jpg", cropped) 来保存这个小图像,但它并没有保存它,前提是它在上面的某行上工作。
第二个问题是:如何编写一个代码,它不断地从左到右、从上到下裁剪28x28的图像并保存每个子图像。
我使用了for循环,但我不知道如何完成它。
非常感谢您的帮助。
这是我的代码,
import cv2
import numpy as np
from PIL import Image
import PIL.Image
import os
import gzip
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
#%%
image1LL='C:/Users/Tala/Documents/PythonProjects/Poster-OpenCV-MaskXray/CHNCXR_0001_0_LL.jpg'
mask1LL='C:/Users/Tala/Documents/PythonProjects/Poster-OpenCV-MaskXray/CHNCXR_0001_0_threshLL.jpg'
#finalsSave='C:/Users/Tala/Documents/PythonProjects/Poster-OpenCV-MaskXray/Xray Result'
# load the image
img = cv2.imread(image1LL,0)
mask = cv2.imread(mask1LL,0)
# combine foreground+background
final1LL = cv2.bitwise_and(img,img,mask = mask)
cv2.imshow('final1LL',final1LL)
cv2.waitKey(100)
final1LL.size
final1LL.shape
# Save the image
cv2.imwrite('final1LL.jpg',final1LL)
# crop the image using array slices -- it's a NumPy array
# after all!
y1=0
x1=0
for y2 in range(0,580,28):
for x2 in range(0,314,28):
cropped = final1LL[0:28, 0:28]
cv2.imshow('cropped', cropped)
cv2.waitKey(100)
cv2.imwrite("cropped.jpg", cropped)
您的方法不错,但需要进行一些微调。以下代码将帮助您:
import cv2
filename = 'p1.jpg'
img = cv2.imread(filename, 1)
interval = 100
stride = 100
count = 0
print img.shape
for i in range(0, img.shape[0], interval):
for j in range(0, img.shape[1], interval):
print j
cropped_img = img[j:j + stride, i:i + stride] #--- Notice this part where you have to add the stride as well ---
count += 1
cv2.imwrite('cropped_image_' + str(count) + '_.jpg', cropped_img) #--- Also take note of how you would save all the cropped images by incrementing the count variable ---
cv2.waitKey()
我的结果:
原图:
部分裁剪图片:
裁剪后的图片 1
裁剪后的图片 2
裁剪后的图片 3
如果您在 PyTorch 中使用它作为深度学习框架,那么这项任务将非常简单,并且无需任何其他外部图像处理库(如 OpenCV)即可完成。下面的代码将以 PyTorch 张量的形式将单个图像转换为多个图像的堆栈。如果只想使用图像,则需要删除行 "transforms.ToTensor()" 并使用 matplotlib 将代码中的 "tens" 变量保存为图像。
注意:这里使用尺寸为 32 x 32 x 3 的鸟图像,裁剪图像为 5x5x3,步长 =1。
image = Image.open('bird.png')
tensreal = trans(image)
trans = transforms.Compose([transforms.Resize(32),
transforms.ToTensor(),
])
stride = 1
crop_height = 5
crop_width = 5
img_height = 32
img_width = 32
tens_list = []
for i in range(0,img_width-crop_width,stride):
for j in range(0,img_height-crop_height ,stride):
tens = trans(image)
tens1 = tens[:, j:j+crop_height, i:i+crop_width]
tens_list.append(tens1)
all_tens = torch.stack(tens_list)
print(all_tens.size())
我有一张 315x581 的图片。我想从左上角到右下角以 28x28 裁剪它,然后我需要将每个 28x28 图像保存在文件夹中。 我可以只裁剪一张图像,从 y1=0 到 y2=28 和 x1=0 到 x2=28。
第一个问题是:我使用 cv2.imwrite("cropped.jpg", cropped) 来保存这个小图像,但它并没有保存它,前提是它在上面的某行上工作。
第二个问题是:如何编写一个代码,它不断地从左到右、从上到下裁剪28x28的图像并保存每个子图像。 我使用了for循环,但我不知道如何完成它。 非常感谢您的帮助。
这是我的代码,
import cv2
import numpy as np
from PIL import Image
import PIL.Image
import os
import gzip
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
#%%
image1LL='C:/Users/Tala/Documents/PythonProjects/Poster-OpenCV-MaskXray/CHNCXR_0001_0_LL.jpg'
mask1LL='C:/Users/Tala/Documents/PythonProjects/Poster-OpenCV-MaskXray/CHNCXR_0001_0_threshLL.jpg'
#finalsSave='C:/Users/Tala/Documents/PythonProjects/Poster-OpenCV-MaskXray/Xray Result'
# load the image
img = cv2.imread(image1LL,0)
mask = cv2.imread(mask1LL,0)
# combine foreground+background
final1LL = cv2.bitwise_and(img,img,mask = mask)
cv2.imshow('final1LL',final1LL)
cv2.waitKey(100)
final1LL.size
final1LL.shape
# Save the image
cv2.imwrite('final1LL.jpg',final1LL)
# crop the image using array slices -- it's a NumPy array
# after all!
y1=0
x1=0
for y2 in range(0,580,28):
for x2 in range(0,314,28):
cropped = final1LL[0:28, 0:28]
cv2.imshow('cropped', cropped)
cv2.waitKey(100)
cv2.imwrite("cropped.jpg", cropped)
您的方法不错,但需要进行一些微调。以下代码将帮助您:
import cv2
filename = 'p1.jpg'
img = cv2.imread(filename, 1)
interval = 100
stride = 100
count = 0
print img.shape
for i in range(0, img.shape[0], interval):
for j in range(0, img.shape[1], interval):
print j
cropped_img = img[j:j + stride, i:i + stride] #--- Notice this part where you have to add the stride as well ---
count += 1
cv2.imwrite('cropped_image_' + str(count) + '_.jpg', cropped_img) #--- Also take note of how you would save all the cropped images by incrementing the count variable ---
cv2.waitKey()
我的结果:
原图:
部分裁剪图片:
裁剪后的图片 1
裁剪后的图片 2
裁剪后的图片 3
如果您在 PyTorch 中使用它作为深度学习框架,那么这项任务将非常简单,并且无需任何其他外部图像处理库(如 OpenCV)即可完成。下面的代码将以 PyTorch 张量的形式将单个图像转换为多个图像的堆栈。如果只想使用图像,则需要删除行 "transforms.ToTensor()" 并使用 matplotlib 将代码中的 "tens" 变量保存为图像。
注意:这里使用尺寸为 32 x 32 x 3 的鸟图像,裁剪图像为 5x5x3,步长 =1。
image = Image.open('bird.png')
tensreal = trans(image)
trans = transforms.Compose([transforms.Resize(32),
transforms.ToTensor(),
])
stride = 1
crop_height = 5
crop_width = 5
img_height = 32
img_width = 32
tens_list = []
for i in range(0,img_width-crop_width,stride):
for j in range(0,img_height-crop_height ,stride):
tens = trans(image)
tens1 = tens[:, j:j+crop_height, i:i+crop_width]
tens_list.append(tens1)
all_tens = torch.stack(tens_list)
print(all_tens.size())