only masking part of image help
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
DGM
2021-11-28
编辑:DGM
2021-11-28
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 个)
Image Analyst
2021-11-28
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 个评论
DGM
2021-11-28
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
2021-11-28
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modify Image Colors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!