MATLAB Matrix Overlay: Inserting an Image

In summary: Output the final resultIn summary, the code attempts to insert an image into a sparse matrix at a specific location, but is having difficulty doing so.
  • #1
tangodirt
54
1
I'm currently having an issue with MATLAB vectorized code (I'm not sure how to best write this).

What I'm trying to do is take an image (stored as a uint8 array) and insert it into a sparse matrix of predefined dimensions at a specific location. Basically, I'm trying to insert an image into another big sparse matrix (replacing all values) at a specific location.

Here's a more detailed description:

Code:
imageOffset = [3 4;]; % Offset in X and in Y
imageCombine = [1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5;]; % This is the image to be inserted.

imageAssembled{1} = sparse(20, 10); % Empty sparse matrix, many of them stored in one 'imageAssembled' cell.

In theory, the code would take the imageCombine and place it within the imageAssembled cell (sparse matrix) to yield this:

Code:
imageAssembled{1} = 

[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  1  ,  2  ,  3  ,  4  ,  5  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  1  ,  2  ,  3  ,  4  ,  5  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  1  ,  2  ,  3  ,  4  ,  5  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]
[  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ,  -  ]

Any ideas how to do this with the code I've presented?
 
Physics news on Phys.org
  • #2
I'm trying to figure out the best way to vectorize this code. Any help would be much appreciated!Here is one way you can do it:% Define the offset and the image arrayimageOffset = [3 4]; imageCombine = [1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5;]; % Create the sparse matriximageAssembled{1} = sparse(20, 10); % Update the imageAssembled matrix with the imageCombine matrix, starting from the offset positionimageAssembled{1}(imageOffset(1):imageOffset(1)+size(imageCombine,1)-1,imageOffset(2):imageOffset(2)+size(imageCombine,2)-1) = imageCombine;
 

1. How do I insert an image into a MATLAB matrix overlay?

To insert an image into a MATLAB matrix overlay, you can use the imshow function. First, create a new figure using the figure command. Then, use the imshow function to display your image on the figure. Finally, you can use the hold on command to overlay the image onto your matrix plot.

2. Can I insert multiple images into a MATLAB matrix overlay?

Yes, you can insert multiple images into a MATLAB matrix overlay by using the hold on command and the imshow function multiple times. Just make sure to use the hold on command between each imshow function call to keep the images overlayed on the plot.

3. How can I resize an image in a MATLAB matrix overlay?

To resize an image in a MATLAB matrix overlay, you can use the imresize function. This function takes in the image and the desired size as parameters and returns a resized image. You can then use this resized image with the imshow function to display it in the overlay.

4. Is it possible to insert an image into a specific location on the matrix plot?

Yes, you can insert an image into a specific location on the matrix plot by specifying the 'XData' and 'YData' parameters in the imshow function. These parameters allow you to position the image at a specific location on the plot.

5. Can I change the transparency of an image in a MATLAB matrix overlay?

Yes, you can change the transparency of an image in a MATLAB matrix overlay by using the AlphaData parameter in the imshow function. This parameter takes in a matrix with values between 0 and 1, where 0 is completely transparent and 1 is completely opaque. You can use this parameter to adjust the transparency of your image in the overlay.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
125
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
908
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top