簡體   English   中英

為什么我的旋轉圖像無法正確保存?

[英]Why aren't my rotated images being saved properly?

我需要圍繞150張不同數量的圖像旋轉,以便可以使用邊界框正確注釋我的數據。 問題是由於某種原因,在運行我的代碼並輸入一些值之后,圖像文件沒有被旋轉保存。 如何以旋轉方式保存這些圖像,以便當我重新打開已旋轉的圖像文件時?

鑒於我已經完全刪除了文件並以相同的名稱保存了一個新文件,因此應該正確覆蓋該文件。

import os
from skimage import io, transform
import cv2
from scipy import ndimage
import scipy.misc

folder_name = r'C:\Users\admin\Desktop\Pedro_Database\Setup_Data\test'
with os.scandir(folder_name) as folder:
    for file in folder:
        if (file.name.endswith('.bmp')):
            path = folder_name + '/' + file.name
            img = cv2.imread(path)
            rotate = 360
            rotated_img = img
            while(rotate != 0):
                cv2.imshow('image',rotated_img)
                cv2.waitKey(0)
                cv2.destroyAllWindows()
                rotate = int(input("Rotate By: "))
                rotated_img = ndimage.rotate(img, rotate, reshape=False)
            #os.remove(path)
            cv2.imwrite(path, rotated_img)
            print(file.name + " saved")

在兩張照片上運行此代碼並正常終止后,我重新打開圖像文件,它們100%不變。

我不習慣使用opencv,但我認為問題出在這一行

cv2.imwrite(path, rotated_img)

必須將其放置在while循環中,以便當圖像通過rotated_img = ndimage.rotate(img, rotate, reshape=False) ,將被寫入(保存)。 如果不是,那么您的rotated_img將保持不變,當退出while循環時, rotate = 0

另外,如果要保存所有旋轉的圖像,則應考慮更改path ,例如: path = path + str(i) ,其中i增加1。

i = 0
while(rotate != 0):
    cv2.imshow('image',rotated_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    rotate = int(input("Rotate By: "))
    rotated_img = ndimage.rotate(img, rotate, reshape=False)
    i += 1
    path_of_rotated_image = path + str(i)
    cv2.imwrite(path_of_rotated_image, rotated_img)
    print(file.name + " saved") # if you want to print

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM