用另一种颜色Matlab替换图像中的某种颜色 [英] Replace a certain color in an image with another color Matlab

查看:3238
本文介绍了用另一种颜色Matlab替换图像中的某种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中使用 imshow 打开了一个图像,我希望用新颜色替换每个像素的颜色(140,50,61) (150,57,80)。如果有人可以请告诉我如何做到这一点。

I have an image that I opened in Matlab using imshow and I want to replace the color of every pixel with value (140,50,61) with a new color (150,57,80). If anyone could please advise how I can do this.

推荐答案

假设 A 作为输入图像数据,这可能是一种方法 -

Assuming A to be the input image data, this could be one approach -

%// 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))






样品运行 -


Sample run -

>> 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

这篇关于用另一种颜色Matlab替换图像中的某种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆