only masking part of image help

3 views (last 30 days)
Is there a way to apply an image mask on only part of the image?
When I use data tips, I was able to find the dimensions of where exactly I want my mask to appy (x and y values) and I tried to do something like this
frame(frame(755:933,206:388)<threshold)=0
but I recieved an error saying that the numbers were out of bound.

Accepted Answer

DGM
DGM on 28 Nov 2021
Edited: DGM on 28 Nov 2021
There's at least one thing going on here. This operation
frame(755:933,206:388)<threshold
will return a 179x183 logical image, which is smaller than the source image. This is going to cause an indexing problem -- not necessarily an error, but it won't be addressing the same part of the image. You're doing two indexing operations here. One based on the ROI location, and one based on the logical comparison. You need to keep track of both. This is a simple way:
% a simple image
A = imread('cameraman.tif');
% coordinates of ROI
x = [50 206];
y = [50 100];
threshold = 180;
Aroi = A(y(1):y(2),x(1):x(2)); % extract ROI
Aroi(Aroi<threshold) = 0; % operate on it
A(y(1):y(2),x(1):x(2)) = Aroi; % insert it back into the image
imshow(A)
That said, the above issue shouldn't be causing an out-of-bounds error since the ROI is smaller than the parent image. If you're getting an error like that, you might want to make sure that your subscripts are in the right order (y,x instead of x,y).
  1 Comment
Nara Sixty Five
Nara Sixty Five on 28 Nov 2021
Thanks so much for the help. I was using the order x,y rather than y,x like you mentioned!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 28 Nov 2021
An alternative way is:
frame = imread('concordorthophoto.png');% Read in sample image.
threshold = 110; % Whatever you want.
mask = frame < threshold; % Get binary image.
% Assign binary image in the region to the original image in the same region.
frame(755:933, 206:388) = 255 * mask(755:933, 206:388);
imshow(frame); % Display image with binary portion in left side.
axis('on', 'image'); % Show tick labels for rows and columns.
Note the binarized part on the left side of this image between rows 755 and 933, and columns 206 to 388.
  2 Comments
DGM
DGM on 28 Nov 2021
Instead of binarizing the ROI
frame(755:933, 206:388) = 255 * mask(755:933, 206:388);
just need to set the dark fraction of the region to black
frame(755:933, 206:388) = frame(755:933, 206:388) .* uint8(~mask(755:933, 206:388));
Image Analyst
Image Analyst on 28 Nov 2021
Ah, you're right @DGM (I didn't read closely enough). Thanks for the correction.

Sign in to comment.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!