2013-05-12 260 views
1

我正嘗試在Opencv中使用RGB圖像。從圖像我只想保留紅色像素,其餘我想設置爲白色。我不確定如何在opencv中執行此邏輯。圖像被讀作Mat。更改Mat中的像素值opencv

我寫了下面的代碼,但它不工作。

Mat image; 
for(i to rows) 
for(j to col) 
{ 
b=input[image.step * j + i] 
g=input[image.step * j + i + 1] 
r=input[image.step * j + i + 2] 
if(r == 255 && g&b == 0) 
{ 
image.at<Vec3f>(j,i)=img.at<Vec3F>(j,i) 
} 

else image.push_back(0); 

這是代碼我寫

我相信它的不正確,但我無法做到這一點。我能否獲得一些幫助

回答

3

您只想保留那些純粹是紅色的像素,即紅色是255和綠色,藍色是零。基本上,你想改變不滿足該條件的像素:

if(~(red == 255 and green == 0 and blue == 0)) 
    red = green = blue = 255 

下面是在Python代碼:

img = cv2.imread(filename) 
rows , cols , layers = img.shape 

for i in range(rows): 
    for j in range(cols): 
     if(~(img[i][j][2] == 255 and img[i][j][0] == 0 and img[i][j][1] == 0)): 
      img[i][j][0] = img[i][j][1] = img[i][j][2] = 255