Select ROI (circle and square) in matlab to approximate filter

I am working on Matlab and I want to make an interactive choice, like you do when using the roipoly function, but I want to select a circle or square. I'm already looking for funcion to select the area of ​​interest (ROI) that you select when using roipoly, but using a circle or square, but I can't find anything.

Any tips?

I have already tried using ginput.

[X, Y]= ginput(2) xmin=min(X) xmax=max(X) ymin=min(Y) ymax=max(Y) 

In this code, I define the angle of the square (the user defines two points using ginput). But when I check the image points, they are wrong. I think this is due to the size of the figure, which does not coincide with the plot.

The best way to choose the ROI I want is to use a function similar to the roipoly function, but for a circle and for a square instead of a polygon. And with this type of function, I can only select the points inside the image using ginput. I should enter an error message if the user selects any point outside the shape (the problem is that they do not match, the point I can select is larger than the image size).

+4
source share
2 answers

There are two questions here:

1) What is wrong with the GINPUT code as well as 2) How to write roiCircle or roiSquare

In response to (1) nothing happens; this code behaves as it should:

 imgData = randn(100); imagesc(imgData ); [X, Y]= ginput(2) xmin=min(X); xmax=max(X); ymin=min(Y); ymax=max(Y); squareX = [xmin xmin xmax xmax xmin]; squareY = [ymin ymax ymax ymin ymin]; hold on; plot(squareX,squareY); %plot the correct square hold off; 

You can use IMCROP to get data:

 width = xmax - xmin; height = ymax - ymin; imgSelect = imcrop(imgData,[xmin,ymin,width,height]); figure; imagesc(imgSelect); 

Regarding (2) (spelling roiCircle or roiSquare), so that they are well updated, as roiPoly does, they will require a significant (though not insurmountable) amount of MATLAB GUI programming. This is tolerable, but not trivial.

+2
source

I managed to implement the choice of an interactive area (in my case, a circle) using the following technique:

  • Get the first point using the built-in ginput (1):

     [X1, Y1] = ginput(1); xp = [X1 Y1]; 
  • Create a handle for the circle:

     h = plot(X1, Y1, 'r'); 
  • Install the custom MouseMove event handler to select the second point:

     set(gcf, 'WindowButtonMotionFcn', {@mousemove, h, xp}); 
  • Wait for the user to click when the handler works with its magic:

     k = waitforbuttonpress; 
  • Finally, disable the handler:

     set(gcf, 'WindowButtonMotionFcn', ''); 

The event handler is as follows:

 function mousemove(object, eventdata, h, xp) cp = get(gca, 'CurrentPoint'); r = norm([cp(1,1) - bp(1) cp(1,2) - bp(2)]); theta = 0:.1:2*pi; xc = r*cos(theta)+bp(1); yc = r*sin(theta)+bp(2); set(h, 'XData', xc); set(h, 'YData', yc); end 

Et voila. This works well, and r displayed by the calling function, so you can use it.

+1
source

All Articles