Image mask - apart from rectangle in the center
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
John Kau
le 27 Juil 2015
Réponse apportée : John Kau
le 29 Juil 2015
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 commentaires
Réponse acceptée
Cam Salzberger
le 29 Juil 2015
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 commentaires
Plus de réponses (1)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!