Hi John,
It is my understanding that you are able to create an image masked outside of a circular region, but would like to use a rectangular region instead. The key line to change in your code is the creation of the mask. Instead of finding all of the pixels outside of the circle, you are looking for all of the pixels that are to the left of the box, or the right of the box, or above, or below. This translates quite easily into a logical statement. See the code below for an example of a rectangularly-masked image:
inputImage = imread('cameraman.tif');
[rNum, cNum, ~] = size(inputImage);
centerX = ceil(cNum/2);
centerY = ceil(rNum/2);
windowWidth = 125;
windowHeight = 75;
[yy, xx] = ndgrid((1:rNum)-centerY, (1:cNum)-centerX);
mask = xx < -windowWidth/2 | xx > windowWidth/2 | ...
yy < -windowHeight/2 | yy > windowHeight/2;
maskedImage = inputImage;
maskedImage(mask) = 128;
imshow(maskedImage)
I hope this helps.
-Cam
Best Answer