How to add offset to image?
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (1)
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:
- Switch to RGB; or
- 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()
Kategorien
Mehr zu Image Filtering finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

