我正在嘗試從某個影像的像素中獲取一個顏色通道的所有強度值,然后將其傳輸到 2D 陣列(我在下面稱為 testarray)。但是,當我運行代碼時,對于某個 w 的某個值,h 的所有值似乎都是相等的(也就是資料的“列”都是相同的)。有人可以幫我找出為什么會這樣嗎?我的目標是讓每個像素強度值直接從影像傳輸到特定顏色的測驗陣列。使用的測驗影像
#import packages
import cv2
import numpy as np
import matplotlib.pyplot as plt
#reads image
image = cv2.imread("nighttime.jpg", cv2.IMREAD_COLOR)
#gets height and width vals from image
h, w, c = image.shape
#creates the array which will act as the copy
testarray=[[0]*w]*h
#loop to traverse through image and transfer each value of image to testarray
for i in range(h):
for j in range(w):
testarray[i][j]=image[i][j][1] #[1] is used here in order to attain the pixel intensity value for green color channel
print(image[600][450][1]) #value returned is 27
print(testarray[600][450]) #value returned is 18 (not equal to previous line's value)
uj5u.com熱心網友回復:
我認為這是python串列和numpy陣列/變數之間的沖突(我不確定,希望有人可以深入探討)。我發現的一個修復是在運行 for 回圈之前將 testarray (這是一個 python 串列)轉換為一個 numpy 陣列
#------------------------Your code--------------------------
#creates the array which will act as the copy
testarray=[[0]*w]*h
#loop to traverse through image and transfer each value of image to testarray
for i in range(h):
#-------------------------The fix--------------------------
#creates the array which will act as the copy
testarray=[[0]*w]*h
testarray=np.array(testarray) #<----- The line i added
#loop to traverse through image and transfer each value of image to testarray
for i in range(h):
有了這個,你應該得到這個
27
27
但是,如果您的目標是從影像中傳輸顏色強度(例如,本例中的綠色強度),則更快的方法是使用 np.copy ()。有了這個,你不必使用 for 回圈
#---------------------------------Your code------------------------------
#reads image
image = cv2.imread("nighttime.jpg", cv2.IMREAD_COLOR)
#gets height and width vals from image
h, w, c = image.shape
#creates the array which will act as the copy
testarray=[[0]*w]*h
testarray=np.array(testarray) #<----- The line i added
#loop to traverse through image and transfer each value of image to testarray
for i in range(h):
for j in range(w):
testarray[i][j]=image[i][j][1] #[1] is used here in order to attain the pixel intensity value for green color channel
print(image[600][450][1]) #value returned is 27
print(testarray[600][450]) #value returned is 18 (not equal to previous line's value)
#-----------------------------------With np.copy------------------
#reads image
image = cv2.imread("nighttime.jpg", cv2.IMREAD_COLOR)
testarray=np.copy(image[:,:,1])
print(image[600][450][1]) #value returned is 27
print(testarray[600][450]) #value returned is 27
np.copy() 基于另一個陣列創建一個陣列(影像是一個 numpy 陣列)
為什么是影像[:,:,1]?因為它在 C=1 時選擇了所有元素(C 從 image.shape 指令中獲得),在這種情況下,所有的綠色強度。它根據每一行的每列的第二個元素創建一個副本,類似于你最初制作的 for ,只是 numpy 使它更快。
現在,如果要選擇其他通道強度(如紅色或藍色),則必須更改影像索引。
testarray=np.copy(image[:,:,0]) #---->Blue
testarray=np.copy(image[:,:,1]) #---->Green
testarray=np.copy(image[:,:,2]) #---->Red
編輯#1:
如果您決定采用第一種方法(使用 for 回圈和 testarray=np.array(testarray) 命令),我建議您將 testarray 上的元素轉換為 uint-8
#--------------------------------Old code------------------------------
testarray=[[0]*w]*h
testarray=np.array(testarray) #<----- The line i added
# tic = time.time()
#loop to traverse through image and transfer each value of image to testarray
for i in range(h):
for j in range(w):
testarray[i][j]=image[i][j][1] #[1] is used here in order to attain the pixel intensity value for green color channel
#------------------------adding uint8 transformation----------------
testarray=[[0]*w]*h
testarray=np.array(testarray) #<----- The line i added
# tic = time.time()
#loop to traverse through image and transfer each value of image to testarray
for i in range(h):
for j in range(w):
testarray[i][j]=image[i][j][1] #[1] is used here in order to attain the pixel intensity value for green color channel
testarray=testarray.astype(np.uint8)#<----------------- New line I added
發生的事情是我嘗試了 imshow 指令來驗證副本的圖片和原始圖片是否相同,但是當我在 testarray 上嘗試 imshow 時出現錯誤
src_depth != CV_16F && src_depth != CV_32S in function 'convertToShow'
我到這里結束,發現解決方案是將元素從testarray轉換為 uint-8。這可以通過 testarray=testarray.astype(np.uint8) 指令實作。
之后,我用 testarray 再次嘗試了 imshow 并且它作業正常。
但是,我仍然建議使用 np.copy() 根據另一個陣列創建一個陣列,并且使用 np.copy(),在這種情況下不需要 .astype(np.uint8) 指令
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515206.html
