我正在嘗試保存在所有感興趣區域上添加了白色蒙版的影像。但由于某種原因,它不會保存最終影像,也不會回傳任何錯誤訊息。如何使用蒙版保存影像?
import cv2
import numpy as np
image = cv2.imread('C:/Users/Desktop/testim.png')
gray_scale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Set threshold level
Dark = 10
coords = np.column_stack(np.where(gray_scale < Dark))
print("xy:\n", coords)
mask = gray_scale < Dark
# Color the pixels in the mask
image[mask] = (255, 255, 255)
cv2.imshow('mask', image)
cv2.waitKey()
#save new image with the added mask to directory
if not cv2.imwrite(r'./mask.png', image):
raise Exception("Could not write image")
uj5u.com熱心網友回復:
我認為這與程式中的幾個錯別字有關。修復它們后,一切都很好。
import cv2
import numpy as np
image = cv2.imread('/content/test.png')
gray_scale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Set threshold level
Dark = 10
coords = np.column_stack(np.where(gray_scale < Dark))
# print("xy:\n", coords)
mask = gray_scale < Dark
# Color the pixels in the mask
image[mask] = (255, 255, 255)
# cv2.imshow('mask', image)
cv2.waitKey()
if not cv2.imwrite(r'./mask.png', image):
raise Exception("Could not write image")
處理前的影像
處理后的影像
uj5u.com熱心網友回復:
.在路徑中代表當前作業目錄。
這可能與包含腳本的目錄不同。
使用檢查 CWD
print("CWD:", os.getcwd())
在那里你會找到你的影像檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442581.html
