我有一张图
.
我想这样做:
做一些操作后,我应该能够重新组合图像以获得最终结果。我的代码是这样的:
clc;
clear all;
close all;
tic
I = imread('ChanVese.jpg');
I = imresize(I, [128 128]);
Img = I;
I = double(I(:, :, 1));
figure();
imshow(Img);
% // As there are three figures
crop_pos = zeros(3, 4);
new_image = zeros(size(I));
c = cell(1, 3);
for i=1:3
% // Sub-divide the image
h = imrect(gca);
% // To make the rect function bounded within the image size
addNewPositionCallback(h, @(p) title(mat2str(p, 3)));
fcn = makeConstrainToRectFcn('imrect', get(gca, 'XLim'), get(gca, 'YLim'));
setPositionConstraintFcn(h, fcn);
crop_area = wait(h)
crop_pos(i, :) = (crop_area);
% // Cropped is the new cropped image on which we will do our operation
cropped = (imcrop(Img, crop_area));
c{i} = cropped;
% // Do operation on the image
%***************************
% Code to be written
%***************************
% // Insert the part-image back into the image
new_image(crop_pos(i, 2):crop_pos(i, 4), crop_pos(i,1):crop_pos(i, 3)) = c{i};
end
imagesc(new_image, [0 255]),colormap(gray);axis on
toc
我的问题在于 imrect 函数:我将尝试举一个例子。即使我选择大小为 [128x128] 的整个图像,我也会得到crop_pos 的输出为
[x,y,w,h] = [0.5, 0.5, 128, 128]
而实际上应该是
[x, y, w, h] = [1, 1, 128, 128];
有时宽度和高度也以浮点数给出。为什么会这样?我相信 MATLAB 将图像处理为矩阵,并将它们转换为离散组件。所以所有的值都应该是整数。
我怎么解决这个问题?