How to add rectangles and noise to my image without changing background?

1 view (last 30 days)
I have an image with background like below:
Now without changing the ripples on the above image, I need to have two rectangles (like shown in below figure) with noise on the above image.
I tried by using following code but the background changes. But backgound should not change (the ripples should always be present). How can I do that?
clc;
clearvars;
close all;
x=zeros(224,224);
x(10:30,10:30)=randn(21,21);
h=real(ifft2(x));
figure
pcolor(h);
shading flat;
colormap winter;
colorbar;
I = double(h);
m =2*abs(randn(90-10+1,60-10+1)+22);
I(10:90, 10:60) = m; %first rectangle with noise
m1 =3*abs(randn(110-95+1,60-10+1)+19);
I(95:110, 10:60) = m1; %second rectangle with noise
figure
pcolor(I);
shading flat;
colormap winter;
colorbar;
If possible, also how to add noise in the background (ripples should be there like shown in first figure)?

Accepted Answer

Walter Roberson
Walter Roberson on 10 Oct 2020
The range of your first figure is much much less than the range of your second figure, and you are letting the color interpolation range be chosen automatically.
clc;
clearvars;
close all;
x=zeros(224,224);
x(10:30,10:30)=randn(21,21);
h=real(ifft2(x));
figure
pcolor(h);
shading flat;
colormap winter;
colorbar;
cax = caxis
cax = 1×2
-0.0012 0.0011
min(h(:)), max(h(:))
ans = -0.0012
ans = 0.0011
I = double(h);
m =2*abs(randn(90-10+1,60-10+1)+22);
I(10:90, 10:60) = m; %first rectangle with noise
m1 =3*abs(randn(110-95+1,60-10+1)+19);
I(95:110, 10:60) = m1; %second rectangle with noise
figure
pcolor(I);
shading flat;
colormap winter;
colorbar;
caxis(cax)
min(I(:)), max(I(:))
ans = -0.0012
ans = 66.2414
You can see that if you pin the color interpolation to the same range as the original that the original ripples show up the same, but that the new rectangles (which has dynamic range about 30000 times higher) are completely washed out.
If you want to accommodate both scales, you are going to need to convert to RGB.
I suggest you look in the File Exchange for "freezeColors". Apply it after the first plot and that will convert the plot to RGB, which will then not be affected by drawing on top of it. (You would need to draw the rectangles separately rather than just using pcolor())
  2 Comments
kumara dommeti
kumara dommeti on 10 Oct 2020
Edited: kumara dommeti on 10 Oct 2020
I need only intensity images not RGB images. Is it possible to keep both scales without converting into RGB? Also the intensity values should be highest for small rectangle, medium for background and low for large rectangle (like shown in below figure).
Walter Roberson
Walter Roberson on 10 Oct 2020
Your image with ripples has a data range of about +/- 0.0012 . Suppose that you were comfortable with representing that with 5 colors, then each color bin would be a data width of about 0.00025 which is 1/4000 .
Your maximum intensity is not fixed, but is probably on the order of 66 1/4 . For the moment we can neglect the negative component in the overall range because 66 1/4 is an approximation anyhow. In order to represent 66 1/4 data range with color bins that are about 1/4000 each, that is going to need about 265000 color bins.
Can your graphics system actually handle a colormap with over a quarter million entries?
Perhaps you would be willing to squeeze the data range slightly on the upper end to get the number of bins down to 2^18 (data span 65.536 when the bins are 1/4000 each). Does your graphics system support 2^18 simultaneous colors?
Not so many releases ago, the maximum MATLAB colormap size for MS Windows was 256, which is the limit for MS Windows processing the color mapping itself; to go beyond that in a later release, MATLAB had to do the data to RGB conversion itself and send the final rendered colors to the graphics system. I do not know the current limit on colormap sizes for MS Windows; my memory was that when the limit was increased beyond 256, that the new limit was 65536, which is 4 times too small for your purposes. With a limit of 65536 and a data range of about 65.536 each bin would have to be about 1/1000 data units, which would be at most three colors for your ripples that have a data span less than 0.00025 .
You could always experiment on your system with
colormap(winter(2^18))
caxis([-.0012 65.5])

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!