image splitting: what is the logic behind the following code? I need explanation.

1 visualizzazione (ultimi 30 giorni)
v = imread('embedded.jpg');
[s1 s2 s3] = size(v);
i = 1;
j = 1;
in = 0;
cond = true;
while (cond)
img = v(i:i+(s1/4)-1, j:j+(s2/4)-1,:);
j = j+(s2/4);
if j > s2
i = i+(s1/4);
j = 1;
if i > s1
cond = false;
end
end
imshow(img);
imwrite(img, strcat('img', num2str(in), '.jpg'));
in = in + 1;
end

Risposte (1)

yanqi liu
yanqi liu il 11 Dic 2021
yes,sir,it use to cut image top-left in to 4*4=16 block area,such as
clc;
clear all;
close all;
v = imread('football.jpg');
[s1 s2 s3] = size(v);
i = 1;
j = 1;
in = 0;
cond = true;
figure(1); imshow(v, []);
while (cond)
img = v(i:i+(s1/4)-1, j:j+(s2/4)-1,:);
% display now rect
bbox=[i j s1/4 s2/4];
figure(1); hold on; rectangle('position', bbox, 'EdgeColor', 'g', 'LineWidth', 2)
j = j+(s2/4);
if j > s2
i = i+(s1/4);
j = 1;
if i > s1
cond = false;
end
end
%imshow(img);
imwrite(img, strcat('img', num2str(in), '.jpg'));
in = in + 1;
end
  2 Commenti
Image Analyst
Image Analyst il 11 Dic 2021
Modificato: Image Analyst il 11 Dic 2021
You said
bbox=[i j s1/4 s2/4];
However the form is [xLeft, yTop, width, height].
But s1 is the number of rows, not columns, and s2 is the number of columns, not rows:
[s1 s2 s3] = size(v); % [rows, columns, numberOfColorChannels]
so you should have
bbox=[i, j, s2/4, s1/4];
That's why your green rectangles don't fit an integer number of times in the image.
Also it's not good to use i and j for things. Not only because they're often used for the imaginary variable, but also because they're deceptive and not clearly named. Since you're using i for the first index, it should be called row, and j should be called column. Good names can avoid mistakes. (I know that some of the poor name choices were the original poster's.)
The whole while loop and if block to check if i and j went outside the image could be done vectorized in a few lines of code using linspace(). Like
startingRows = round(linspace(1, s1+1, 4));
startingCols = round(linspace(1, s2+1, 4));
etc. Now the indexes will never be outside the image and you don't have to check for that.
yanqi liu
yanqi liu il 11 Dic 2021
Modificato: yanqi liu il 11 Dic 2021
yes,sir,if use grid,can get the total split,if the row number equal to column number,may be split equal.
but not the origin code,in fact the origin code is use on top left
of course,if we modify it
clc;
clear all;
close all;
I = imread('football.jpg');
sz = size(I);
th = 5;
[x, y] = meshgrid(linspace(1,sz(2),th), linspace(1,sz(1),th));
z = ones(size(x));
figure;
imshow(I, []);
hold on; axis off;
mesh(x, y, z, 'FaceColor', 'none', ...
'EdgeColor', 'g', 'LineWidth', 2, ...
'Marker', 'o', 'MarkerFaceColor', 'k', ...
'MarkerSize', 3);
% use for imwrite
for i = 1 : length(x)-1
for j = 1 : length(y)-1
bbox = round([x(i,j) y(i,j) diff(x(1,1:2)) diff(y(1:2,1))]);
imi = imcrop(I, bbox);
imwrite(imi, sprintf('%02d-%02d.png', i, j));
end
end

Accedi per commentare.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by