correct operation of the matlab imrect function

2 次查看(过去 30 天)
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 ?

采纳的回答

Alex Taylor
Alex Taylor 2013-8-7
If you need to use imrect, then you will need to use functions like floor/ceil/round to move from a continuous representation of the rectangle position to integral values that correspond to 1 based subscripts in MATLAB.
One thing that you should be aware of is that in the example you provided in which a rectangle occupies the entire width/height of an image, the intrinsic coordinate system is defined as follows:
imagesc(reshape(1:9,3,3)); axis on
Note that the UL corner of the image falls at 0.5,0.5, and the center of the first pixel is located at integral value 1,1. So, in your example, the position being returned by imrect is actually correct, the full extent of the rectangle for a 128x128 image is from 0.5,0.5 to 128.5,128.5, and that is what the position rectangle you provided:
[x,y,w,h] = [0.5,0.5,128,128]
Is describing.

更多回答(1 个)

Alex Taylor
Alex Taylor 2013-7-31
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 个评论
Devraj Mandal
Devraj Mandal 2013-7-31
Thanks for replying!
Please check I have used indeed the imcrop tool but what I really wanted along with it is to get the starting coordinates of my cropped image as well as its width and height with reference to the original image.
So I used the imrect function but while doing so I encountered the problem as described in the question above.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by