i need to add an image as background?

here is my code
close all; clear all;
P = @(x,y,c,w,a) a.*exp(-((x-repmat(c(1),size(x,1),size(x,2))).^2 + (y-repmat(c(2),size(y,1),size(y,2))).^2)./w); A=[6 8 10]; W = 1; % Width (constant here, can vary as a vector if you like) C = [[18 15 5];[10 10 10]]; % Peak centers x = linspace(0,20,150); % Define ‘x’ vector
[X,Y] = meshgrid(x); pks = zeros(size(X)); % Preallocate ‘pks’ for k1 = 1:size(C,2) % Loop through ‘C’ (centers) vector pks = pks + P(X,Y,C(:,k1),W,A(k1)) ; end
A=meshz(X,Y,pks); view(2);
grid on;
colorbar
i have this code and i need to add an image as a background how can i do it?

3 Comments

As a background to what? What is the foreground exactly? Are you building up some kind of 2D image? If so, doesn't it have values for every location in the image, and if so, then where would the "background" be?
find attach the image that i need to put as background

Sign in to comment.

 Accepted Answer

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
close all;
clear all;
P = @(x,y,c,w,a) a.*exp(-((x-repmat(c(1),size(x,1),size(x,2))).^2 + (y-repmat(c(2),size(y,1),size(y,2))).^2)./w);
A=[6 8 10];
W = 1; % Width (constant here, can vary as a vector if you like)
C = [[18 15 5];[10 10 10]]; % Peak centers
x = linspace(0,20,150); % Define ‘x’ vector
[X,Y] = meshgrid(x);
pks = zeros(size(X)); % Preallocate ‘pks’
for k1 = 1:size(C,2) % Loop through ‘C’ (centers) vector
pks = pks + P(X,Y,C(:,k1),W,A(k1)) ;
end
subplot(1,3,1);
imshow(pks);
grid on;
colorbar
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Read in background image.
backgroundImage = imread('cameraman.tif');
% Make same size as pks.
backgroundImage = double(imresize(backgroundImage, size(pks)));
% Normalize
backgroundImage = backgroundImage * max(pks(:)) / max(backgroundImage(:));
subplot(1,3,2);
imshow(backgroundImage, []);
axis on;
compositeImage = backgroundImage; % Initialize.
% Get location of circles.
circleLocations = pks > 0.001;
% Now add circles.
compositeImage(circleLocations) = pks(circleLocations);
subplot(1,3, 3);
imshow(compositeImage, []);
axis on;

13 Comments

Image Analyst — This would work as a demo or a FEX tutorial. (I couldn’t figure out how to do it!)
Since the background is around zero, you could also add the two images instead of replacing the background. That would get rid of the black stuff.
I have 150 demos so far. Each is somewhat specific in what it does but sort of general purpose at the same time. The Mathworks wants me to post them but it's just too much work and I feel the "important" ones would get lost in the list. Someday I have plans just to zip them all up and post them as a "grab bag of image processing demos".
For now, I've attached two related demos.
Thanks! I’ll take a look. (I have several relevant to what I do stashed in an ‘Image Analyst’ MATLAB-path directory.)
It would be a slightly tedious but highly educational task for a MathWorks intern to go through them, create an index for them, and provide some sort of ranking to make them easy to search. (The intern would necessarily have to understand their coding and function.) The ‘important’ ones could be given a higher ranking so they would appear higher in the results sequence.
to clarify the idea these circles represent sensors these circles change color when the value of the sensor changes for that i don't want the color of the circles to be gray i attached the image that i need to put it as background
i modified the code
close all; clear all;
P = @(x,y,c,w,a) a*exp(-((x-repmat(c(1),size(x,1))).^2 + (y-repmat(c(2),size(y,1))).^2)./w); % Function to calculate peaks a=[10 15 20]; % set the amplitude of each peak. Ces valeurs seront les valeurs des sensors W = 2; % Width (constant here, can vary as a vector if you like) C = [[5 5 5]; [5 10 15]]; % Peak centers [[x1,x2,x3];[y1 y2 y3]] with this command i can decide wherre to put the peaks on the surface x = linspace(0,20,150); % Define ‘x’ vector [X,Y] = meshgrid(x); pks = zeros(size(X)); % Preallocate ‘pks’ for k1 = 1:size(C,2) % Loop through ‘C’ (centers) vector pks = pks + P(X,Y,C(:,k1),W,a(k1)); end
subplot(1,3,1); pcolor(pks); grid on;
% Enlarge figure to full screen. set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]); % Read in background image. backgroundImage = imread('foot1.jpg'); % Make same size as pks. backgroundImage = double(imresize(backgroundImage, size(pks))); % Normalize backgroundImage = backgroundImage *max(pks(:)) / max(backgroundImage(:)); subplot(1,3,2); imshow(backgroundImage,[]); axis on; compositeImage = backgroundImage; % Initialize. % Get location of circles. circleLocations = pks > 0.001; % Now add circles. compositeImage(circleLocations) = pks(circleLocations); subplot(1,3, 3); imshow(compositeImage, []); axis on; colorbar;
i want the figure in the subplot(1,3,2) to be the background of the subplot(1,3,1) it's not working
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
P = @(x,y,c,w,a) a*exp(-((x-repmat(c(1),size(x,1))).^2 + (y-repmat(c(2),size(y,1))).^2)./w); % Function to calculate peaks
a=[10 15 20]; % set the amplitude of each peak. Ces valeurs seront les valeurs des sensors
W = 2; % Width (constant here, can vary as a vector if you like)
C = [[5 5 5]; [5 10 15]]; % Peak centers [[x1,x2,x3];[y1 y2 y3]] with this command i can decide wherre to put the peaks on the surface
x = linspace(0,20,150); % Define ‘x’ vector
[X,Y] = meshgrid(x);
pks = zeros(size(X)); % Preallocate ‘pks’
for k1 = 1:size(C,2) % Loop through ‘C’ (centers) vector
pks = pks + P(X,Y,C(:,k1),W,a(k1));
end
% Normalize
pks = uint8(255 * mat2gray(pks));
rgbPks = ind2rgb(pks, jet(256));
% Read in background foot image.
backgroundImage = imread('C:\Users\elie\Documents\Temporary\foot1.jpg');
[rowsB, columnsB, numberOfColorChannelsB] = size(backgroundImage)
subplot(1, 3, 2);
imshow(backgroundImage,[]);
axis on;
% Make same size as pks.
if numel(backgroundImage) > numel(rgbPks)
rgbPks = double(imresize(rgbPks, [rowsB, columnsB]));
else
backgroundImage = double(imresize(backgroundImage, size(pks)));
end
% Normalize
backgroundImage = 255 * mat2gray(backgroundImage);
% % Normalize
% backgroundImage = backgroundImage * max(pks(:)) / max(backgroundImage(:));
% Display pks.
subplot(1, 3, 1);
imshow(rgbPks);
axis on;
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Display foot.
subplot(1, 3, 2);
imshow(uint8(backgroundImage),[]);
axis on;
% Get a weighted sum.
weight = 0.997;
weightedSumImage = ((1-weight)*backgroundImage + weight*rgbPks) / weight;
compositeImage = uint8(255*mat2gray(weightedSumImage));
% Display the sum.
subplot(1, 3, 3);
imshow(compositeImage);
axis on;
thank you man you are awesome!!!
You're welcome. Can you officially mark the answer as "Accepted" then? Thanks.
can you explain me briefly the code what you mean by numel....
and these two commands? weight = 0.997; weightedSumImage = ((1-weight)*backgroundImage + weight*rgbPks) / weight;
numel() is the number of pixels in the image. weight says how much wieght to apply to one image. The other line just does the weighted sum. It just says how much of one image shows in comparison to the other image. Not sure how to describe weighted sum. I thought everyone knew what it was. Perhaps you can Google it.
okey thanks for your help !!

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!