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

조회 수: 2 (최근 30일)
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

답변 (1개)

yanqi liu
yanqi liu 2021년 12월 11일
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
Image Analyst
Image Analyst 2021년 12월 11일
편집: Image Analyst 2021년 12월 11일
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 2021년 12월 11일
편집: yanqi liu 2021년 12월 11일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by