1

Basically what I'm trying to do is using predefined ROI's to crop and image into multiple new images.

Longer is that I have a map of brain, with sections of it defined. using that I define many ROI's from it in MATLAB using imfreehand or roipoly. From there I have stained slides of these sections. I want to use the ROI's that I defined from the map to crop the image of the real brain into many new images.

Just having trouble finding something that uses the ROI's as the cropping area and not just some rectangle.

If I need to explain a bit more let me know.

Gbru
  • 97
  • 1
  • 8
  • If you mean cropping more general shape, not only rectangle, try to use `inpolygon` – Adiel Nov 21 '13 at 22:36
  • In summary: you want to take the `imfreehand` result and return a smaller image (bounding box of the ROI) with image values inside the ROI and 0's outside? – nkjt Nov 22 '13 at 10:44

1 Answers1

1

Simple example of what I think you want using imfreehand:

I = imread('pout.tif');
imshow(I);
h = imfreehand; % now pick ROI

BW = createMask(h); % get BW mask for that ROI
pos = getPosition(h); % get position for that ROI

% define bounding box
x1 =  round(min(pos(:,2)));
y1 =  round(min(pos(:,1)));
x2 =  round(max(pos(:,2)));
y2 =  round(max(pos(:,1)));

I2 = I.*uint8(BW); % apply mask to image
I2 = I2(x1:x2,y1:y2);

figure;
subplot(1,2,1);
imshow(I);
subplot(1,2,2);
imshow(I2);

If you have the ROI's already saved in some way, and don't want to run imfreehand again, all you really need is to calculate BW (a mask with ones within the ROI and zeros elsewhere) and the bounding box (to crop tight around the ROI).

nkjt
  • 7,825
  • 9
  • 22
  • 28