How to divide/split RGB image into two parts and second part GRAY and concatenate them in a single image

7 views (last 30 days)
  1. First divide/split RGB image into to halves
  2. Second part should be GRAY
  3. Save as a new image
With complete coding

Answers (1)

DGM
DGM on 22 Mar 2022
Edited: DGM on 22 Mar 2022
Dead post, but answering these things is my hobby now. There are many ways to do the same thing, but let's start simple.
Working on only half of the image
Consider a literal interpretation of the process description. We split the image into two images, reduce one half to gray, replicate, and concatenate.
%% split the image, process, concatenate
inpict = imread('peppers.png');
% split image
halfw = ceil(size(inpict,2)/2);
lhspict = inpict(:,1:halfw,:);
rhspict = inpict(:,halfw+1:end,:);
% desaturate and replicate RHS
rhspict = repmat(rgb2gray(rhspict),[1 1 3]);
% concatenatate
outpict = [lhspict rhspict];
imshow(outpict)
Of course, this could be simplified and the output will be identical:
%% operate on half the image without splitting
inpict = imread('peppers.png');
halfw = ceil(size(inpict,2)/2);
outpict = inpict;
outpict(:,halfw+1:end,:) = repmat(rgb2gray(outpict(:,halfw+1:end,:)),[1 1 3]);
imshow(outpict)
Working directly on saturation/chroma
You could choose to operate directly on saturation or chroma information. This may seem unnecessary, but if you're already using another color model as part of your workflow, this might make sense. Consider the case of LAB:
%% operate on saturation/chroma information directly (LAB)
inpict = imread('peppers.png');
% create map of saturation factor
s = size(inpict);
halfw = ceil(s(2)/2);
satmap = ones(s(1:2));
satmap(:,halfw+1:end) = 0;
% convert to some cylindrical model
labpict = rgb2lab(inpict);
labpict(:,:,2) = satmap.*labpict(:,:,2);
labpict(:,:,3) = satmap.*labpict(:,:,3);
outpict = lab2rgb(labpict);
imshow(outpict)
Using basic image composition
You could also simply create a desaturated copy and then compose the two copies using a basic image composition method. Like the above method, this relies on the use of simple multiplication with a map.
%% use basic image composition techniques
inpict = imread('peppers.png');
% create map of saturation factor
s = size(inpict);
halfw = ceil(s(2)/2);
satmap = ones(s(1:2));
satmap(:,halfw+1:end) = 0;
% compose the image
desaturated = rgb2gray(inpict);
outpict = im2double(inpict).*satmap + im2double(desaturated).*(1-satmap);
outpict = im2uint8(outpict);
imshow(outpict)
Of course, it's not necessary that the map is hard-edged. It can have any sort of smooth transition. The composition process remains the same. This makes this approach much more flexible than the basic splitting method.
%% do the same thing with a continuous transition
inpict = imread('peppers.png');
% create map of saturation factor
easew = 100;
padw = (size(inpict,2)-easew)/2;
x = linspace(-pi/2,pi/2,easew);
satmap = [ones(1,ceil(padw)) 1-(1+sin(x))/2 zeros(1,floor(padw))];
satmap = repmat(satmap,[s(1) 1]);
% compose the image
desaturated = rgb2gray(inpict);
outpict = im2double(inpict).*satmap + im2double(desaturated).*(1-satmap);
outpict = im2uint8(outpict);
imshow(outpict)
Using MIMT tools
MIMT (on the File Exchange) offers some tools which make basic image generation and composition much easier. Consider replicating the above composition:
%% use MIMT tools instead
inpict = imread('peppers.png');
% create map of saturation factor
easew = 0.2; % normalized WRT image width
s = size(inpict);
satmap = lingrad(s(1:2),[0 0.5-easew/2; 0 0.5+easew/2],[255; 0],'cosine');
% compose the original with a desaturated copy
outpict = replacepixels(inpict,mono(inpict,'y'),satmap); % using luma
imshow(outpict)
These tools offer further flexibility without significant complication to the user:
%% same thing, but different
inpict = imread('peppers.png');
% create map of saturation factor
s = size(inpict);
satmap = radgrad(s(1:2),[0.5 0.5],0.4,[255; 0],'cosine'); % radial instead of linear
% compose the original with a desaturated copy
outpict = replacepixels(inpict,mono(inpict,'llch'),satmap); % L in LCHab instead
imshow(outpict)
Regarding the grayscale half
You may have noticed that the grayscale representations provided by L in LAB is different than that provided by rgb2gray(). L* is generally going to be a more accurate representation of brightness than the (Rec 601) luma that rgb2gray() returns, but luma is much faster to calculate.
If you're wondering what the original example image used for desaturating the RHS of the image, it's L in HSL. Compared to L* or Y, L (HSL) isn't as good of a brightness representation, but HSL is simple and fast. MATLAB and IPT do not have HSL tools, though L can be calculated simply without purpose-built tools:
mn = min(inpict,[],3);
mx = max(inpict,[],3);
L = (mn+mx)/2; % L in HSL
MIMT does have HSL tools, so you could simply use mono() to just get L, or you could use rgb2hsl()/hsl2rgb() to do the full conversion.
L = mono(inpict,'l'); % L in HSL

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!