I am trying to crop images according to their masked images (image masking) but i am running into 3 problems:
If I run it on Jupyter notebook, it successfully crops 1 image. If I add even 1 more image, it gives list index out of range
error.
I have tried several different methods to download the cropped images saved in the Dataset_cropped list to a folder in both jupyter notebook and google colab. But the target folder remains empty in both.
When I run this code on Google Colab, I am unable to display the cropped image on the screen using plt.imshow(img_org)
command. It generated the error TypeError: Image data of dtype object cannot be converted to float
at first. Now it is not producing any result and not giving an error either. -This command works fine on jupyter notebook.
Please let me know if you can help in this regard. Thanks!
The code I am using is:
import cv2
import glob
import os
import matplotlib.pyplot as plt
Dataset_cropped = []
org_list = sorted(glob.glob('\content\drive\MyDrive\original*'))
msk_list = sorted(glob.glob('\content\drive\MyDrive\mask*'))
for i in range(len(org_list)):
img_org = cv2.imread(org_list[i])
img_mask = cv2.imread(msk_list[i])
img_org = cv2.resize(img_org, (400,400), interpolation = cv2.INTER_AREA)
img_mask = cv2.resize(img_mask, (400,400), interpolation = cv2.INTER_AREA)
for h in range(len(img_mask)):
for w in range(len(img_mask)):
if img_mask[h][w][0]==0:
for i in range (3):
img_org[h][w][i] = 255
else:
continue
#plt.imshow(img_org)
dataset_cropped.append(img_org)
continue
plt.imshow(img_org)
Some of the methods I have tried to download the saved images:
Using these inside the loop:
img = cv2.imread(img_org) cv2.imwrite("\content\drive\MyDrive\cropped\CH.png", img)
Using these outside the loop:
image = np.asarray(TBB_CROPPED)
count=0
i=0
while i < len(TBB_CROPPED):
cv2.imwrite('\content\drive\MyDrive\cropped\CH%d'+str(count)+'.png',image)
i = i+1
count = count + 1
Christoph Rackwitz is right: the slashes are pretty much the problem. Try changing them from backward to forward when you save the file:
cv2.imwrite("/content/drive/MyDrive/cropped/CH.png", img)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.