2017-09-12 30 views
0

所以我使用opencv修改1通道圖像上的像素值。 我創建使用Opencv fillPoly()不適用於灰度(單通道)圖像

curr = np.zeros((660,512, 1)) 

然後執行該代碼的空白圖像:其中每個區看起來像

for r in regions: 
    cv2.fillPoly(curr, r, [190]) 

[[[363 588] 
    [304 593] 
    [323 652] 
    [377 654]]] 

我知道這些代碼是至少有些工作,因爲當我使用imshow()時,區域根據需要填充。不過,我想重新訪問修改後的像素值,並得到了[0]我試着寫了整個IMG TI的臨時文件,如下所示:

for elt in curr: 
    f2.write(str(elt) + '\n') 

但是,該文件只是看起來像

[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 
[ 0.] 

我哪裏錯了?爲什麼我不能重新訪問我寫給圖片的190年代?

回答

1

工作得很好。

curr = np.zeros((660,512, 1),dtype = np.uint8) 

regions = np.array([[[363,588],[304,593],[323,652],[377,654]]]) 

for r in regions: 
    cv2.fillPoly(curr, [regions[0]], (190)) 

# find minimum value, maximum value and their location index in the image 
minVal,maxVal,minLoc,maxLoc = cv2.minMaxLoc(curr) 

print(maxVal, maxLoc) 
1

也許你過寫作或重新初始化圖像(CURR)某處代碼,這是一個使用cv2.imwrite保存文件的代碼。

curr = np.zeros((660,512, 1)) 
regions = np.random.uniform(1, 200, size=(1, 5, 2)) 
regions = regions.astype(np.int32, copy=False) 
for r in regions: 
    cv2.fillPoly(curr, [r], [190]) 

while(1): 
    cv2.imshow('Terry Martin', curr) 
    k= cv2.waitKey(1) & 0xFF 
    if k == 27: 
     cv2.imwrite('FillPloy.jpg', curr) 
     break 

cv2.destroyAllWindows() 

OpenCV的窗口: enter image description here

輸出圖像: enter image description here

+0

正如我所說的,顯示在一個窗口中的圖像;它看起來很好。但是我希望能夠查看文件,並找到最大值。 作爲替代方法,我嘗試將圖像作爲png寫入文件,然後使用imread打開保存的文件。現在我得到[190,190,190]作爲圖像中的最大值。這正是我所期望的灰度圖像嗎?我以爲因爲圖像形狀是(660,512,1),訪問單個像素會給我一個奇異的值。但訪問img [x] [y]總是返回一個3元素的數組。 –