How to add offset to image?

16 次查看(过去 30 天)
I have an intensity image (values are around in range of -4*10^(-4)) shown in below.
Now without changing the ripples on the above image, I created two rectangles (like shown in below figure) with noise on the above image.
Now I need to add offset to these images so that the intensity values are positive (like starting from 0 and not from -4*10^(-4)) and ripples should be visible. I tried the following code but the ripples disappear in it.
clc;
clearvars;
close all;
x=zeros(224,224);
x(10:20,10:20)=randn(11,11);
h=real(ifft2(x)); %h=real(ifft2(x))+5; "5" is for adding offset
figure
imagesc(h);
colormap winter;
colorbar;
cax = caxis;
I = 2*h;
m = 2*(randn(90-10+1,60-10+1)-1);
I(10:90, 10:60) = m; %first rectangle with noise
m1 = 3*(randn(110-95+1,60-10+1)+1);
I(95:110, 10:60) = m1; %second rectangle with noise
figure
imagesc(I);
colorbar;
caxis(cax)
Can anyone tell me how to do that?

采纳的回答

Mathieu NOE
Mathieu NOE 2020-10-19
Hi
my 2 cents suggestion.
Main problem is that the first image (h) and your noise amplitude in the second had amplitudes with factor x 1000 , so no chance to see your ripples in the second image
below my code suggestions
x=zeros(224,224);
x(10:20,10:20)=randn(11,11);
h=real(ifft2(x)); %h=real(ifft2(x))+5; "5" is for adding offset
h = h - min(min(h)); % offset = - min(min(h)) , so now h is always positive now
% NB : max(max(h)) = 0.001
max_h = max(max(h));
figure
imagesc(h);
colormap winter;
colorbar;
cax = caxis;
I = h;
m = max_h*(0.75*rand(90-10+1,60-10+1)); % you can play with the "amplitude" factor (here 0.75) to see the effect
I(10:90, 10:60) = m; %first rectangle with noise
m1 = max_h*(rand(110-95+1,60-10+1)+0.75); % you can play with the "offset" factor (here 0.75) to see the effect
I(95:110, 10:60) = m1; %second rectangle with noise
figure
imagesc(I);
colormap winter;
colorbar;
caxis(cax)
  5 个评论
kumara dommeti
kumara dommeti 2020-10-22
编辑:kumara dommeti 2020-10-22
I am unable to see the intensity image (I) with waves when I saved and reopened it. Can you guide me for saving the final image "I" in above code? Also I need to save as 2D (intensity or gray scale) image only. The following code I have used.
imagesc(I);
colorbar
caxis(cax)
f=mat2gray(I,cax);
imwrite(uint8(f),'sample1.png'); %to save
figure
s=imread("sample1.png"); % to reopen and check
imagesc(s);
colorbar

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2020-10-19
clc;
clearvars;
close all;
x=zeros(224,224);
x(10:20,10:20)=randn(11,11);
h=real(ifft2(x)); %h=real(ifft2(x))+5; "5" is for adding offset
figure
imagesc(h);
cmap = winter();
colormap(cmap);
colorbar;
cax = caxis;
I = 2*h;
m = 2*(randn(90-10+1,60-10+1)-1);
m1 = 3*(randn(110-95+1,60-10+1)+1);
offset = min([m(:); m1(:)]);
m = m - offset;
m1 = m1 - offset;
I(10:90, 10:60) = m; %first rectangle with noise
I(95:110, 10:60) = m1; %second rectangle with noise
figure
imagesc(I);
colormap(cmap);
caxis(cax)
colorbar;
You might notice that the result is washed out. You should expect that with what you are doing. You specifically caxis() based upon the color axes that was chosen automatically with the first data range that included only the ripples, so any values that are larger in magnitude than the ripples are going to appear as the last color.
I remind you of our previous discussions in which I pointed out that your ripples only span about 1E-3, but your rectangles span probably more than +8. I discussed with you then that you have two choices:
  1. Switch to RGB; or
  2. Use a colormap with something like 50000 color slots.
Your ripple data span is about 1e-3 (that is, -5e-4 to +5e-4 = 10e-4 = 1e-3). You need to decide how many distinct colors you want in that range. I think you would be unhappy with fewer than 5 colors in that range. If we say 10, then each color slot would be width 1e-3/10 = 1e-4 .
Now decide on a maximum number of colormap entries you want for your finished plot. For example 256 is common. Calculate: -5e-4 + (256-1)*1e-4 = 0.25 . That is then the maximum value you would be able to distinguish with that many colormap entries. You would now scale your rectangles to fit in that range:
N_h_cmap = 10; %number of color entries to allocate for ripples
N_full_cmap = 256;
h_span = max(h(:)) - min(h(:));
h_incr = h_span / N_h_cmap;
all_m = [m(:); m1(:)]);
max_m = max(all_m);
min_m = min(all_m);
c_delta = N_cmap * h_incr / (max_m - min_m);
m = (m - min_m) * c_delta;
m1 = (m1 - min_m) * c_delta;
I(10:90, 10:60) = m; %first rectangle with noise
I(95:110, 10:60) = m1; %second rectangle with noise
imagesc(I);
colormap(winter(N_cmap));
%and do NOT caxis()

类别

Help CenterFile Exchange 中查找有关 Red 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by