Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 将图像中的一种颜色更改为另一种颜色_Matlab_Image Processing - Fatal编程技术网

Matlab 将图像中的一种颜色更改为另一种颜色

Matlab 将图像中的一种颜色更改为另一种颜色,matlab,image-processing,Matlab,Image Processing,我只想将图像中的绿色转换为红色。我已经写了下面的代码,但它不能正常工作 rgbImage = imread('image.jpg'); [rows columns numberOfColorBands] = size(rgbImage); imshow(rgbImage, []); title('Original Color Image'); redChannel = rgbImage(:, :, 1); greenChannel = rgbImage(:,

我只想将图像中的绿色转换为红色。我已经写了下面的代码,但它不能正常工作

   rgbImage = imread('image.jpg');
   [rows columns numberOfColorBands] = size(rgbImage);
   imshow(rgbImage, []);
   title('Original Color Image');
   redChannel = rgbImage(:, :, 1);
   greenChannel = rgbImage(:, :, 2);
   blueChannel = rgbImage(:, :, 3);
   rgbImage2 = cat(3, greenChannel, redChannel, blueChannel);
   imshow(rgbImage2, []);
   title('Green is now red');  

您的代码交换红色通道和绿色通道

rgbImage2=cat(3,绿色通道,红色通道,蓝色通道)
将绿色替换为红色,将红色替换为绿色(交换通道)

为了保持绿色通道不变,并将(原始)红色替换为绿色,请使用以下命令:
rgbImage2=cat(3,绿色通道,绿色通道,蓝色通道)

peppers.png
image的结果:

rgbImage = imread('peppers.png');
[rows columns numberOfColorBands] = size(rgbImage);
imshow(rgbImage, []);
title('Original Color Image');
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
rgbImage2 = cat(3, greenChannel, greenChannel, blueChannel);
imshow(rgbImage2, []);
title('Green is now red');

原始图像:

正如Rotem所说,您只是在交换红色和绿色通道。将影响切换到整个图像,而不仅仅是绿色

人们必须先把绿色分割出来,才能把绿色变成其他颜色。您可以在Matlab文档本身中找到两个

我已经尝试过分割和更改绿色,下面的代码可能对您的图像不起作用,但我想可能会得到不错的结果

 rgbImage = imread('peppers.png');
 figure, imshow(rgbImage);
 title('Original Image');

 redChannel = rgbImage(:, :, 1);
 greenChannel = rgbImage(:, :, 2);
 blueChannel = rgbImage(:, :, 3);

 %% Now lets take the difference of each the channels. 
 % these subtracted images will be used to mask the segmented area.
 % If you are curious, plot them and see how they look!! 
 red_subtract_grn = redChannel-greenChannel;
 red_subtract_blue = redChannel-blueChannel;
 grn_subtract_blue = greenChannel - blueChannel;
 red_add_grn = double(redChannel)+double(greenChannel)+double(blueChannel);

 %% Lets segment the green color by filtering/thresholding technique,
 % we need to choose the index number according to rgbImage, one should tweak a bit to get better results. (These
 % numbers worked well for 'peppers.jpg' image.I have used indexing since its 
 % very neat and faster, alternatively you can use find() also).
 try_mask = ones(size(rgbImage(:,:,1))); %Initialize mask image.
 try_mask(red_subtract_blue < 7) = 0;    %remove background
 try_mask = medfilt2(try_mask,[4,4]);    %Filter unwanted scattered pixels.
 try_mask(red_subtract_grn > 40) = 0;
 try_mask(red_add_grn > 500) = 0;
 try_mask(grn_subtract_blue < 20) = 0;
 try_mask(blueChannel > 80) = 0;
 try_mask = medfilt2(try_mask,[8,8]);

 %% Lets apply mask to remove green and blue pixels such that only red color will appear on the masked region.
 greenChannel(try_mask > 0) = 0;
 blueChannel(try_mask > 0) = 0;
 rgbImage2 = cat(3, redChannel, greenChannel, blueChannel);

 figure, imshow(rgbImage2);
 title('After changing green to red')
rgbImage=imread('peppers.png');
图,imshow(rgbImage);
标题(“原始图像”);
红色通道=rgbImage(:,:,1);
绿色通道=RGB图像(:,:,2);
blueChannel=rgbImage(:,:,3);
%%现在让我们看看每个通道的差异。
%这些减去的图像将用于遮罩分割区域。
%如果你好奇的话,画出来看看它们是什么样子!!
红色通道减去绿色通道;
红色减去蓝色=红色通道蓝色通道;
grn_减去_蓝色=绿色通道-蓝色通道;
红色通道+绿色通道+蓝色通道;
%%让我们通过过滤/阈值技术分割绿色,
%我们需要根据rgbImage选择索引号,应该稍微调整一下以获得更好的结果。(这些
%“peppers.jpg”图片的数字效果很好。我从发布以来就一直使用索引
%非常简洁和快速,或者您也可以使用find()。
try_mask=one(大小(rgbImage(:,:,1));%初始化遮罩图像。
尝试遮罩(红色减去蓝色<7)=0;%删除背景
try_mask=medfilt2(try_mask[4,4]);%过滤不需要的散乱像素。
尝试遮罩(红色减去40)=0;
尝试遮罩(红色加总>500)=0;
尝试遮罩(grn减去蓝色<20)=0;
尝试屏蔽(blueChannel>80)=0;
try_mask=medfilt2(try_mask[8,8]);
%%让我们应用遮罩来移除绿色和蓝色像素,以便遮罩区域上只显示红色。
绿色通道(try_mask>0)=0;
蓝色通道(try_mask>0)=0;
rgbImage2=cat(3,红色通道,绿色通道,蓝色通道);
图,imshow(rgbImage2);
标题(“将绿色更改为红色后”)
绿色到红色:


有趣的是,那张照片看起来没有修改过!