correct operation of the matlab imrect function
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I have an image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149168/image.jpeg)
What I wanted to do was this .
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149170/image.jpeg)
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 ?
0 Commenti
Risposta accettata
Alex Taylor
il 7 Ago 2013
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.
0 Commenti
Più risposte (1)
Alex Taylor
il 31 Lug 2013
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.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!