2017-04-10 3 views
0

私は画像315x581を持っています。私は28x28で左上から右下に切り取りたいので、各28x28イメージをフォルダに保存する必要があります。 私は、y1 = 0からy2 = 28、x1 = 0からx2 = 28までの1つのイメージをトリミングすることができました。openCV Python3.5を使用して、画像を左から右、上から下に小さなサイズにトリミングできますか

最初の問題は、この小さな画像を保存するためにcv2.imwrite( "cropped.jpg"、cropped)を使用しましたが、上の行があれば保存しません。

第2の問題は、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)

+0

この問題にアプローチする方法についての答えを参照してください:D –

答えて

0

、私のコードです。次のコードは、あなたを助ける:

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() 

マイ結果:

オリジナル画像:

enter image description here

トリミングされた画像の一部:

トリミング画像1

enter image description here

トリミング画像2

enter image description here

トリミング画像3

enter image description here

+0

はそうありがとう多く。このコードはうまく動作しますが、それは私が探していたものではありませんでした。画像をカットするために別のロジックを使用する必要があることがわかります。私は "click_and_crop"コードをトリミングし、切り取った画像を特定のラベルでフォルダに保存しました。しかし、もう一度ご協力いただきありがとうございます。 – Shittel

+0

@Shittel何をお探しですか?私を修正してください! –

関連する問題