correct operation of the matlab imrect function
Show older comments
I have an image

What I wanted to do was this .

After doing some operation I should be able to recombine the images to get the final result. My code is this
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 3 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
My problem is with the imrect function: I will try to give an example . Even if I select the whole of the image whose size is [128x128], I get an output of crop_pos as
[x,y,w,h] = [0.5,0.5,128,128]
whereas, it actually should be
[x,y,w,h] = [1,1,128,128];
Also sometimes the width and the height are given in floating point . Why is this so ? I believe that matlab handles images as matrixes and doing so converts them into discrete components. So all values should be in integers.
How can I solve this problem ?
Accepted Answer
More Answers (1)
Alex Taylor
on 31 Jul 2013
0 votes
It looks like you are trying to implement a cropping tool. Have you looked at the Image Processing Toolbox function imcrop:
If imcrop isn't what you are looking for, please provide more information about why imcrop doesn't suit your needs as a cropping tool.
1 Comment
Devraj Mandal
on 31 Jul 2013
Categories
Find more on Image Segmentation 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!