Superimpose Image 1 onto bottom right of Image 2

1 visualización (últimos 30 días)
Jeffrey Gifford
Jeffrey Gifford el 13 de Jul. de 2021
Comentada: DGM el 13 de Jul. de 2021
Hello,
I copy and pasted this pseudo code and tried to adjust it to my variables. I have "USI.png" and "FigIWant.png" both of which are saved as png. I am trying to put USI in the bottom right corner of FigIWant (sort of like a legend on a graph). Due to legal reasons, I am not allowed to post the figures I am working with, but I can provide the properties. I can offer a subsitute image if needed.
Figure (3: USl) with properties:
Number: 3
Name: 'Unscaled Color Wheel'
Color: [0.9400 0.9400 0.9400]
Position: [360 198 560 420]
Units: 'pixels'
Figure (6: FigIWant) with properties:
Number: 6
Name: 'Tissue-Velocity Segmented'
Color: [0.9400 0.9400 0.9400]
Position: [360 198 490.8000 370]
Units: 'pixels'
Here is the error msg:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right
side. Error in superimposeexample (line 26)
disp((1:size(obj,1))+rowshift, (1:size(obj,2))+colshift, :) = obj;
% Create the scene
% This example uses a random set of pixels to create a TrueColor image
scene = FigIWant;
% Create the object image
% This example uses a blend of colors from left to right, converted to a TrueColor image
% Use repmat to replicate the pattern in the matrix
% Use the "jet" colormap to specify the color space
obj = USI;
% Display the images created in subplots
hf2 = figure('units','normalized','position',[.2 .2 .6 .6]);
axi1 = subplot(2,3,1);
file1 = uigetfile('FigIWant.png');
axis off
title('Scene')
axi2 = subplot(2,3,4);
obj = uigetfile('USI.png');
axis off
title('Object image')
% Now replace pixels in the scene with the object image
result = scene;
% Define where you want to place the object image in the scene
rowshift = 20;
colshift = 0;
% Perform the actual indexing to replace the scene's pixels with the object
disp((1:size(obj,1))+rowshift, (1:size(obj,2))+colshift, :) = obj;
% Display the results
ax3 = subplot(2,3,[2:3, 5:6]);
iresult = image(result);
axis off
hold on
title(sprintf('Using indexing to overlay images:\nresult is one image object'))

Respuesta aceptada

DGM
DGM el 13 de Jul. de 2021
disp() is a function that takes only one argument, yet you're feeding it indices like it's an array. The assignment will try to overload disp(). No matter the dim3 size of obj, the assignment will fail due to the expansion of the last colon operator. This whole assignment line is messed up. Just pick the right arrays to use and make sure their sizes are correspondent.
  2 comentarios
Jeffrey Gifford
Jeffrey Gifford el 13 de Jul. de 2021
The code I copied from was from a 2014 solution and they used "result()" instead of "disp()", are those different functions or would the same error occur?
How would I produce the superimposed image? If i use disp() that will just give me one of the images I already have.
DGM
DGM el 13 de Jul. de 2021
Bear with me. My network connection is garbage right now and this site simply doesn't work below 300kbps. I can't afford to include any images.
result() isn't a built-in function name; disp() is. How the implicit indexes are expanded will be different. Use a variable name that's appropriate for the conventions established in the code up to that point. In your case, the variable called 'result' is a copy of the image called 'scene'. Operating on a copy prevents modification of the original image.
For example:
A = repmat(imread('cameraman.tif'),[1 1 3]); % I expanded to RGB, uint8
B = im2uint8(rand(50,50,3)); %RGB, double converted to uint8
% both images are the same class and have same # of channels
s = size(B);
A(end-s(1)+1:end,end-s(2)+1:end,:) = B; % lower right corner
imshow(A);
This directly modifies the image A. If you don't want to modify A, you'd make a copy of A and modify it instead. This is exactly why 'result' exists.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Display Image en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by