我有一個圖像,我用Matlab打開使用imshow
,我想用新顏色(150)替換每個像素的顏色值(140,50,61) ,57,80)。如果有人可以請告知我如何做到這一點。用另一種顏色替換圖像中的某種顏色Matlab
1
A
回答
2
假設A
是輸入的圖像數據,這可能是一個辦法 -
%// Initialize vectors for old and new pixels tuplets
oldval = [140,50,61]
newval = [150,57,80]
%// Reshape the input array to a 2D array, so that each column would
%// reprsent one pixel color information.
B = reshape(permute(A,[3 1 2]),3,[])
%// Find out which columns match up with the oldval [3x1] values
matches = all(bsxfun(@eq,B,oldval(:)),1)
%// OR matches = matches = ismember(B',oldval(:)','rows')
%// Replace all those columns with the replicated versions of oldval
B(:,matches) = repmat(newval(:),1,sum(matches))
%// Reshape the 2D array back to the same size as input array
out = reshape(permute(B,[3 2 1]),size(A))
採樣運行 -
>> A
A(:,:,1) =
140 140 140
40 140 140
A(:,:,2) =
50 20 50
50 50 50
A(:,:,3) =
61 65 61
61 61 61
>> out
out(:,:,1) =
150 140 150
40 150 150
out(:,:,2) =
57 20 57
50 57 57
out(:,:,3) =
80 65 80
61 80 80
1
bsxfun
是我會解決它的辦法。但是,如果您不熟悉它,則可以從圖像中提取每個通道,爲每個通道使用三個邏輯模板,然後使用logical
AND進行組合。做和會發現你的圖像中的像素,尋找特定的RGB三重。
因此,我們相應地設置每個通道的輸出並重構圖像以產生輸出。
因此,鑑於你的輸入圖像A
,一個可以這樣做:
red = A(:,:,1); green = A(:,:,2); blue = A(:,:,3);
mred = red == 140; mgreen = green == 50; mblue = blue == 61;
final_mask = mred & mgreen & mblue;
red(final_mask) = 150; green(final_mask) = 57; blue(final_mask) = 80;
out = cat(3, red, green, blue);
相關問題
- 1. 用另一種顏色替換圖像中的某個顏色範圍Matlab
- 2. 用另一種顏色替換圖像中的特定顏色
- 3. 用CSS3中的圖像中的另一種顏色替換顏色
- 4. 用另一種顏色替換uiimage中的顏色
- 5. 在NSImage中替換另一種顏色
- 6. 使用PHP替換圖像中的另一種顏色
- 7. 背景顏色50%一種顏色和50%另一種顏色
- 8. 將某些像素RGB顏色到另一種顏色
- 9. 從一種特定顏色到另一種顏色的研究生matlab繪圖
- 10. 從一種顏色到另一種顏色的插值
- 11. 用另一種顏色更改所有像素的顏色
- 12. 從一種顏色到另一種顏色到百分比
- 13. 如何將一種顏色變成另一種顏色?
- 14. 確定一種顏色「圖像中單個顏色的多少」
- 15. 在UIView中將背景色從一種顏色淡出到另一種顏色
- 16. 一半的信是一種顏色,另一半另一種顏色
- 17. 將ShapeDrawable中的單色(黑色)替換爲Android中的另一種顏色
- 18. 將自動文本顏色替換爲選中的某種顯式顏色
- 19. 直接從高亮顏色切換顏色到另一種顏色
- 20. 需要更快的方法來替換基於另一種顏色的圖像中的顏色
- 21. 如何將一種顏色替換爲eps文件的每個對象中的另一種顏色?
- 22. 如何讓圖像中的某種顏色變成黑色?
- 23. 顏色的另一種顏色的JTable中選擇行
- 24. 另一種方法將背景顏色淡入另一種顏色
- 25. 在UIImage(或其衍生產品)中,如何將一種顏色替換爲另一種顏色?
- 26. ggplot:一種顏色/
- 27. 通過用戶輸入從一種顏色變爲另一種顏色
- 28. 您可以在位圖圖像中將一種顏色更改爲另一種顏色嗎?
- 29. JAVA:使用ColorModel替換另一種顏色
- 30. 使用CSS給一個黑色的圖標另一種顏色
你能告訴我怎麼用替換'OLDVAL(:)'?舊的顏色是RGB(140,50,61) – Tak 2015-02-11 04:30:07
@shepherd檢查編輯? – Divakar 2015-02-11 04:30:35
謝謝:)。是否可以寫一些解釋你寫的代碼行的註釋? :) – Tak 2015-02-11 04:32:25