2016-10-23 3 views
2

이미지의 녹색 색상 만 빨간색으로 변환하고 싶습니다. 나는 아래의 코드를 작성했지만 작동하지 않습니다 제대로이미지의 한 색상을 다른 색상으로 변경

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'); 

답변

2

귀하의 코드 스왑 적색 채널과 녹색 색상 채널.

rgbImage2 = cat(3, greenChannel, redChannel, blueChannel);
녹색 대신 녹색을 넣고 (스왑 채널) 빨간색을 녹색으로 바꾸십시오. 녹색과 적색 변하지 녹색 채널을 유지하고, (원본) 바꾸기 위해

사용 다음

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'); 

enter image description here

:
rgbImage2 = cat(3, greenChannel, greenChannel, blueChannel);

결과 peppers.png 이미지 원본 이미지 :
enter image description here

+0

재미있게, 그 사진은 수정되지 않은 보인다! –

1

로템 (Rotem)이 말했듯이, 당신은 단지 빨강과 초록색 채널을 교환하고 있습니다. 스와핑은 녹색 색상 대신 전체 이미지에 영향을줍니다.

녹색 색상을 다른 색상으로 변경하려면 먼저 녹색 색상을 분할해야합니다. Matlab 문서 자체에서 examples 쌍을 찾을 수 있습니다.

녹색 색상을 분할하고 변경하려고했는데, 아래 코드가 이미지에서 작동하지 않을 수도 있지만 괜찮은 결과를 얻을 수 있습니다.

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') 
붉은 색

녹색 색상 :

img

관련 문제