I have a original image (on the left) and I want to decrease object (horizontal shape thing) contrast to simulate the reference image (on the right). I have tried all smoothing methods but all of them will impact image contrast (say background contrast). Is there any way I can build a model then subtract it from the original image?
In another word, is there any way we can add opacity to the image on the left to simulate the image on the right? The object mask has been provided and it is the same size as original image.
Any suggestion will be appreciated.
You reduce the contrast by multiplying the masked area in the input image:
I = imread('https://i.sstatic.net/QML1p.png'); %I = imread('1.png'); %Read image
M = imbinarize(imread('https://i.sstatic.net/nBqeS.png')); %imbinarize(imread('2.png')); %Read mask, and convert to binary image.
J = I;
J(M) = J(M)*.5; %Multiply the "horizontal shape thing" by 0.5 - reducing contrast.
figure;imshow(J);
The above solution is kind of setting the opacity of the "shape thing".
I tried to make it a bit smoother, but result is still very different.
I = imread('https://i.sstatic.net/QML1p.png');
%I = imread('1.png'); %Read image
M = imbinarize(imread('https://i.sstatic.net/nBqeS.png'));
%M = imbinarize(imread('2.png')); %Read mask, and convert to binary image.
M = imdilate(M, strel('disk', 20)); %Make mask thicker.
%Crate blurred "shape thing"
T = zeros(size(I));
T(M) = double(I(M)); %"shape thing" with some margins, and black surrounding.
T = T/255; %Change pixels range from [0, 255] to [0, 1].
T = imgaussfilt(double(T), 5); %Blur (2-D Gaussian filtering).
%Create blurred mask:
M = imgaussfilt(double(M), 20); %Blur the mask (2-D Gaussian filtering).
M = max(M, 0.5);
J = T.*M + im2double(I).*(1-M); %Weighed average.
J = imgaussfilt(J, 1);
J = im2uint8(J);
figure;imshow(J);
Try to play with it a little...
Another try:
Code:
I = imread('https://i.sstatic.net/QML1p.png');
M = imbinarize(imread('https://i.sstatic.net/nBqeS.png'));
M = imdilate(M, strel('disk', 20)); %Make mask thicker.
%Crate blurred "shape thing"
T = zeros(size(I));
T(M) = double(I(M)); %"shape thing" with some margins, and black surrounding.
T = T/255; %Change pixels range from [0, 255] to [0, 1].
T = imgaussfilt(double(T), 5); %Blur (2-D Gaussian filtering).
T = T*1.5; %Scale T for changing opaquely
%Create blurred mask:
M = imgaussfilt(double(M), 20); %Blur the mask (2-D Gaussian filtering).
M = max(M, 0.5);
J = T.*M + im2double(I).*(1-M); %Weighed average.
J = imgaussfilt(J, 1);
%Reduce the relative contrast between "shape thing" and background by averaging J and 0.5
J = (J + 0.5)/2;
J = min(J, 1);
%Add gaussian noise (so result be closer to reference).
J = imnoise(J, 'gaussian', 0, 0.005);
J = im2uint8(J);
figure;imshow(J);