Image mask - apart from rectangle in the center
4 次查看(过去 30 天)
显示 更早的评论
Hi!
I'm using this code (from the internet) to mask everything outside of the circle. I'm struggeling to adapt this code to replace the circle with a rectangle. Any suggestions?
maskGabor = (img);
[rNum, cNum, ~] = size(maskGabor);
% Define coordiantes and radius
[xx, yy] = ndgrid((1:rNum)-centerY, (1:cNum)-centerX); % generate grid with binary mask
mask = (xx.^2 + yy.^2)>radius^2; % define area outside of cirlce that will be masked
maskGabor(mask) = color; % color is gray
Many thanks, jk
0 个评论
采纳的回答
Cam Salzberger
2015-7-29
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:
% Acquire image
inputImage = imread('cameraman.tif');
% Determine image properties
[rNum, cNum, ~] = size(inputImage);
centerX = ceil(cNum/2);
centerY = ceil(rNum/2);
% Define parameters of the rectangle
windowWidth = 125;
windowHeight = 75;
% Create logical mask
[yy, xx] = ndgrid((1:rNum)-centerY, (1:cNum)-centerX);
mask = xx < -windowWidth/2 | xx > windowWidth/2 | ...
yy < -windowHeight/2 | yy > windowHeight/2;
% Mask image and show it
maskedImage = inputImage;
maskedImage(mask) = 128;
imshow(maskedImage)
I hope this helps.
-Cam
0 个评论
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!