making a rectangle with one mouse click
    5 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
I work in matlab with a code that was already made by somebody else. In this code you can choose to form a rectangle area with the code ginput, so in my image I click one two spots to form a rectangle and then the code goes further:
if PickRegion==1
    fprintf('Pick two corners to indicate the region for tracking \n')
    figure(1);
    [xreg yreg] = ginput(2); %2 points
    if (length(xreg) < 2)
      return%break
  end
    rect=[min(xreg)   min(yreg)   abs(xreg(2)-xreg(1))    abs(yreg(2)-yreg(1))]  %rect:[left low xsize ysize]
end
Now I would like to form the same size rectangle everytime but be able to choose where to place it, so that in different images the size of the rectangle is always the same. So that with one mouse click I can choose for example the upper right corner and then a fixed size rectangle is formed. How should I go about making this code, can anybody help?
Thanks in advance !
0 Commenti
Risposta accettata
  Image Analyst
      
      
 il 8 Apr 2014
        I suggest you use rbbox() for creating the first box. Then use ginput(1) to specify the upper left corners of the subsequent boxes.
3 Commenti
  Joseph Cheng
      
 il 8 Apr 2014
				from the comments in the code you can see what is happening.
[xreg yreg] = ginput(2); %2 points
    if (length(xreg) < 2)
      return%break
  end
    rect=[min(xreg)   min(yreg)   abs(xreg(2)-xreg(1))    abs(yreg(2)-yreg(1))]
so you know this slice of code does 2 points and defines a rectangle. so how would you change it to make it 1?
[xreg yreg] = ginput(1); %1 point
    if (length(xreg) < 1)  %check to see if point is selected.
      return%break  
  end
now you'll have to redefine the rectangle as you are only specifying 1 corner. The rectangle is defined in rect by
rect = [x,  y,  width, height];
where x = x axis pixel location for lower left corner
      y = y axis pixel location for lower left corner
      width = width of rectangle
      height = height of rectangle.
since you are specifying the box dimensions those would be static or defined elsewhere. then depending on which corner you want the click to define you'll have to calculate what the coordinate would be for the lower left corner's x and y.
  Image Analyst
      
      
 il 9 Apr 2014
				For rbbox, look at thiis function where the user clicks and drags out a box and then it calculates the x1,x2,y1,and y2 coordinates and finally crops the image (which you don't have to do if you don't want to):
function [croppedImage, xCoords, yCoords] = CropImage(rgbImage)
  k = waitforbuttonpress;
  point1 = get(gca,'CurrentPoint');    % button down detected
  finalRect = rbbox;                   % return figure units
  point2 = get(gca,'CurrentPoint');    % button up detected
  point1 = point1(1,1:2);              % extract x and y
  point2 = point2(1,1:2);
  p1 = min(point1,point2);             % calculate locations
  offset = abs(point1-point2);         % and dimensions
  xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
  yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
  x1 = round(xCoords(1));
  x2 = round(xCoords(2));
  y1 = round(yCoords(5));
  y2 = round(yCoords(3));
  hold on
  axis manual
  plot(xCoords, yCoords); % redraw in dataspace units  
  drawnow;
  croppedImage = rgbImage(y1:y2,x1:x2,:);
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Image Processing Toolbox in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!