Clear Filters
Clear Filters

Hi, How to segment(divide) an image into four halves of "different dimensions" automatically as shown in the attached image using MATLAB?

1 view (last 30 days)
As shown in the attached image, the 4 images in a 2×2 grid are of different dimensions. I want to extract them automatically into four different subplots. Now in this case all(4) images are not equally spaced still can we extract them automatically(not using hard coding) in Matlab? If they are equally spaced(same dimension) then it's easy to extract them "automatically", but I found difficulty in extracting them when these images are not equally spaced in a 2*2 grid. Need help in this regard. Thanks in advance.

Accepted Answer

DGM
DGM on 17 Dec 2022
Edited: DGM on 17 Dec 2022
Here's one idea.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1234097/cars.jpg');
graypict = rgb2gray(inpict);
% filter the image with a wide difference filter
% this emphasizes long grid-aligned edges
fk = repmat([1; -1],[1 30]);
filtered = abs(imfilter(im2double(graypict),fk,'replicate'));
[~,rowidx] = max(mean(filtered,2)); % find the peak index
imshow(filtered,[])
% do the same thing with the filter rotated 90 deg
filtered = abs(imfilter(im2double(graypict),fk.','replicate'));
[~,colidx] = max(mean(filtered,1)); % find the peak index
imshow(filtered,[])
% disassemble the image using the peak locations
imgpile = cell(2,2);
imgpile{1,1} = inpict(1:rowidx,1:colidx,:);
imgpile{1,2} = inpict(1:rowidx,colidx+1:end,:);
imgpile{2,1} = inpict(rowidx+1:end,1:colidx,:);
imgpile{2,2} = inpict(rowidx+1:end,colidx+1:end,:);
% show the result
montage(imgpile,'size',[2 2])
Of course, this will have problems if the edges between subimages are weak or if there are more dominant grid-aligned edges in the composite image.
  3 Comments
DGM
DGM on 17 Dec 2022
That's not really considerably different. You're just finding two edges at once. I'm not so sure that it would have any significant advantage. I guess you could.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1234097/cars.jpg');
graypict = rgb2gray(inpict);
% construct the filter (center overlaps, but eh)
s = 30;
fk0 = repmat([1; -1],[1 s]);
fk = zeros(s);
fk(s/2+[0 1],:) = fk0;
fk(:,s/2+[0 1]) = fk0.';
% filter the image with a wide difference filter
% this emphasizes long grid-aligned edges
filtered = abs(imfilter(im2double(graypict),fk,'replicate'));
[~,rowidx] = max(mean(filtered,2)); % find the peak index
[~,colidx] = max(mean(filtered,1)); % find the peak index
imshow(filtered,[])
% disassemble the image using the peak locations
imgpile = cell(2,2);
imgpile{1,1} = inpict(1:rowidx,1:colidx,:);
imgpile{1,2} = inpict(1:rowidx,colidx+1:end,:);
imgpile{2,1} = inpict(rowidx+1:end,1:colidx,:);
imgpile{2,2} = inpict(rowidx+1:end,colidx+1:end,:);
% show the result
montage(imgpile,'size',[2 2])

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!