How to combine two images in specific position for binary masking?

4 views (last 30 days)
Hello, I am trying to produce some binary masks. The masks I need should be size of 1024X796 pixels. I will at first divide it in four quadrant, then 8X8 blocks etc. Now to produce this, I tried to put the quadrant blocks incribed in the corner of the whole image. I used imresize and then montage to produce the masks with four images. I used get(CData) for exact pixel size of the image, but I am getting 928X720 pixels images. How can I get the same size image for the masks I need. I also tried with ROI, but could not solve it. I have provided the code and images here.
clc
clear all
close all
%get quadrant images
q1 = imread('C:\Users\sharm\Desktop\center figuring block masks\desired images\q1.jpg');
q2 = imread('C:\Users\sharm\Desktop\center figuring block masks\desired images\q2.jpg');
q3 = imread('C:\Users\sharm\Desktop\center figuring block masks\desired images\q3.jpg');
q4 = imread('C:\Users\sharm\Desktop\center figuring block masks\desired images\q4.jpg');
%resize the images into half
k1 = imresize(q1,0.5);
k2 = imresize(q2,0.5);
k3 = imresize(q3,0.5);
k4 = imresize(q4,0.5);
%imshow(k1);
%defining original image and resizing into half
Image = zeros(796,1024);
%figure;imshow(Image);
% I = imfuse(Image,k1);
% figure;imshow(I);
% h = images.roi.Rectangle(gca,'Position',[512,0,512,398],'StripeColor','r');
% mask = createMask(h);
% I = Image(mask);
% figure;imshow(I);
I = imresize(Image,0.5);
imshow(I);
%using montage to plot images side by side and writing them
figure; m1 = montage({I,k1,I,I});
m1=get(m1,'CData');
imwrite(m1,'m1.jpg');
figure; m2 = montage({I,k2,I,I});
m2=get(m2,'CData');
imwrite(m2,'m2.jpg');
figure; m3 = montage({I,k3,I,I});
m3=get(m3,'CData');
imwrite(m3,'m3.jpg');
figure; m4 = montage({I,k4,I,I});
m4=get(m4,'CData');
imwrite(m4,'m4.jpg');
  1 Comment
DGM
DGM on 27 Aug 2021
montage() is a tool for viewing images. It's not intended for compositing images. Just the same as with imshow(), the result is dependent on the figure size and is subject to nearest-neighbor interpolation. It's tantamount to taking a screenshot. It's one step up from taking a picture of the screen with a cell phone.
Lemme take a look at this and see what the options are.

Sign in to comment.

Accepted Answer

DGM
DGM on 27 Aug 2021
Edited: DGM on 27 Aug 2021
The images you're importing aren't the expected image height. If you divide by two, they won't tile correctly. If you do what montage() is doing, they'll be padded and won't line up correctly. I'm assuming you want the quadrant blocks to be correctly aligned on a 4x4 grid.
% these are 768x1024, not 796x1024
q1 = imread('q1.jpg');
q2 = imread('q2.jpg');
q3 = imread('q3.jpg');
q4 = imread('q4.jpg');
% in order to fit these in the grid,
% they need to be resized asymmetrically
k1 = imresize(q1,[398 512]);
k2 = imresize(q2,[398 512]);
k3 = imresize(q3,[398 512]);
k4 = imresize(q4,[398 512]);
% why not just define it the right size?
I = zeros(398,512,class(q1));
Since these images are now the same size, the answer is simple. Just edge-concatenate them. For example:
m1 = [I k1; I I];
imwrite(m1,'m1.jpg');
imshow(m1);
% etc
Of course, this is all based on the expectation that the image should be 796x1024. I'm not sure why that particular size instead of 768x1024. I have to ask that you truly intend that difference, as your images are 768x1024, and 796 is not integer-divisible by 8 or 32 (i.e. 4x8), whereas 768 is. If 796 was a typo and the image is supposed to be 768px high, then the explicit resizing isn't necessary.
  2 Comments
DGM
DGM on 27 Aug 2021
q1 = imread('q1.jpg');
q2 = imread('q2.jpg');
q3 = imread('q3.jpg');
q4 = imread('q4.jpg');
k1 = imresize(q1,0.5);
k2 = imresize(q2,0.5);
k3 = imresize(q3,0.5);
k4 = imresize(q4,0.5);
I = zeros(size(q1),class(q1));
m1 = [I k1; I I];
imwrite(m1,'m1.jpg');
m2 = [I k2; I I];
imwrite(m2,'m2.jpg');
m3 = [I k3; I I];
imwrite(m3,'m3.jpg');
m4 = [I k4; I I];
imwrite(m4,'m4.jpg');

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Model Editing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!