Tôi muốn đọc nhiều hình ảnh trên cùng một thư mục bằng OpenCV [Python]. Để làm điều đó, tôi cần sử dụng vòng lặp for
hoặc while
với funcion imread
? Nếu vậy, làm thế nào? làm ơn giúp tôi...
Tôi muốn đưa hình ảnh vào một mảng và sau đó xử lý chúng một lần thông qua một vòng lặp.
Hỏi ngày 27 tháng 10 năm 2015 lúc 13:51Oct 27, 2015 at 13:51
1
import glob
import cv2
images = [cv2.imread[file] for file in glob.glob["path/to/files/*.png"]]
Soufiane s
1871 Huy hiệu vàng4 Huy hiệu bạc15 Huy hiệu đồng1 gold badge4 silver badges15 bronze badges
Đã trả lời ngày 27 tháng 3 năm 2017 lúc 14:48Mar 27, 2017 at 14:48
Tiến sĩ Sokobandr SokobanDr Sokoban
1.5984 Huy hiệu vàng19 Huy hiệu bạc31 Huy hiệu đồng4 gold badges19 silver badges31 bronze badges
2
Điều này sẽ nhận được tất cả các tệp trong một thư mục trong
from os import listdir
from os.path import isfile, join
import numpy
import cv2
mypath='/path/to/folder'
onlyfiles = [ f for f in listdir[mypath] if isfile[join[mypath,f]] ]
images = numpy.empty[len[onlyfiles], dtype=object]
for n in range[0, len[onlyfiles]]:
images[n] = cv2.imread[ join[mypath,onlyfiles[n]] ]
0. Và sau đó nó sẽ đọc tất cả và lưu trữ chúng trong mảng from os import listdir
from os.path import isfile, join
import numpy
import cv2
mypath='/path/to/folder'
onlyfiles = [ f for f in listdir[mypath] if isfile[join[mypath,f]] ]
images = numpy.empty[len[onlyfiles], dtype=object]
for n in range[0, len[onlyfiles]]:
images[n] = cv2.imread[ join[mypath,onlyfiles[n]] ]
1.from os import listdir
from os.path import isfile, join
import numpy
import cv2
mypath='/path/to/folder'
onlyfiles = [ f for f in listdir[mypath] if isfile[join[mypath,f]] ]
images = numpy.empty[len[onlyfiles], dtype=object]
for n in range[0, len[onlyfiles]]:
images[n] = cv2.imread[ join[mypath,onlyfiles[n]] ]
Đã trả lời ngày 27 tháng 10 năm 2015 lúc 15:03Oct 27, 2015 at 15:03
0
import glob
import cv2 as cv
path = glob.glob["/path/to/folder/*.jpg"]
cv_img = []
for img in path:
n = cv.imread[img]
cv_img.append[n]
Đã trả lời ngày 25 tháng 2 năm 2019 lúc 18:50Feb 25, 2019 at 18:50
0
Điều này có hiệu quả thời gian tốt hơn.
def read_img[img_list, img]:
n = cv2.imread[img, 0]
img_list.append[n]
return img_list
path = glob.glob["*.bmp"] #or jpg
list_ = []`
cv_image = [read_img[list_, img] for img in path]
Đã trả lời ngày 31 tháng 7 năm 2019 lúc 4:35Jul 31, 2019 at 4:35
import cv2
from pathlib import Path
path=Path["."]
path=path.glob["*.jpg"]
images=[]`
for imagepath in path.glob["*.jpg"]:
img=cv2.imread[str[imagepath]]
img=cv2.cvtColor[img,cv2.COLOR_BGR2GRAY]
img=cv2.resize[img,[200,200]]
images.append[img]
print[images]
Eric Aya
69.1K35 Huy hiệu vàng176 Huy hiệu bạc247 Huy hiệu đồng35 gold badges176 silver badges247 bronze badges
Đã trả lời ngày 13 tháng 2 năm 2019 lúc 13:07Feb 13, 2019 at 13:07
0
Def Flatten_Images [thư mục]: # Đường dẫn thư mục [Bộ dữ liệu]
images=[] # list contatining all images
for filename in os.listdir[folder]:
print[filename]
img=plt.imread[folder+filename] # reading image [Folder path and image name ]
img=np.array[img] #
img=img.flatten[] # Flatten image
images.append[img] # Appending all images in 'images' list
return[images]
Đã trả lời ngày 3 tháng 7 năm 2020 lúc 18:39Jul 3, 2020 at 18:39
Đây là cách tôi đã làm điều đó mà không sử dụng
from os import listdir
from os.path import isfile, join
import numpy
import cv2
mypath='/path/to/folder'
onlyfiles = [ f for f in listdir[mypath] if isfile[join[mypath,f]] ]
images = numpy.empty[len[onlyfiles], dtype=object]
for n in range[0, len[onlyfiles]]:
images[n] = cv2.imread[ join[mypath,onlyfiles[n]] ]
2, nhưng với việc sử dụng mô -đun from os import listdir
from os.path import isfile, join
import numpy
import cv2
mypath='/path/to/folder'
onlyfiles = [ f for f in listdir[mypath] if isfile[join[mypath,f]] ]
images = numpy.empty[len[onlyfiles], dtype=object]
for n in range[0, len[onlyfiles]]:
images[n] = cv2.imread[ join[mypath,onlyfiles[n]] ]
3 thay vào đó, vì tôi không thể làm cho nó hoạt động với from os import listdir
from os.path import isfile, join
import numpy
import cv2
mypath='/path/to/folder'
onlyfiles = [ f for f in listdir[mypath] if isfile[join[mypath,f]] ]
images = numpy.empty[len[onlyfiles], dtype=object]
for n in range[0, len[onlyfiles]]:
images[n] = cv2.imread[ join[mypath,onlyfiles[n]] ]
2 trên máy tính của mình:# This is to get the names of all the files in the desired directory
# Here I assume that they are all images
original_images = os.listdir['./path/containing/images']
# Here I construct a list of relative path strings for each image
original_images = [f"./path/containing/images/{file_name}" for file_name in original_images]
original_images = [cv2.imread[file] for file in original_images]
Đã trả lời ngày 23 tháng 3 lúc 23:25Mar 23 at 23:25