In the following image, I have filled two regions with two different shades:
How can I fill the remaining part of the image (parts not filled) with the pixel values 0
(black) in MATLAB
?
Thanks.
In the following image, I have filled two regions with two different shades:
How can I fill the remaining part of the image (parts not filled) with the pixel values 0
(black) in MATLAB
?
Thanks.
Assuming this is a follow on from this question, when you take your roi, instead of or in addition to using it to create the image above, you can use it to make an image with a black background. This avoids any issues with having the same values elsewhere in the image (credit to Bee's answer from the previous question):
img = im2double(imread('cameraman.tif'));
imshow(img);
roi = imfreehand(gca);
img2 = img;
img3 = zeros(size(img));
img2(roi.createMask) = val_1;
img3(roi.createMask) = val_1;
% and repeat to add additional roi
Alternatively, you can store the regions as individual BW masks, if you want to use them again later:
imshow(img);
% create masks
roi = imfreehand(gca);
BW = roi.createMask;
roi2 = imfreehand(gca);
BW2 = roi.createMask;
% original image + roi
img2 = img;
img2(BW) = val_1;
img2(BW2) = val_2;
% B&W image
img3 = BW*val_1+BW2*val_2;
Try this to pick points in the two gray shapes and black out all else.
img = imread(myimagefilename);
imshow(img);
% you can skip the following part and set clr1 and clr2 manually
% if you already know the grayscale values in the patches
pts=ginput(2); % <-- pick points within the regions you colored in
clr1=img(pts(1,2),pts(1,1));
clr2=img(pts(2,2),pts(2,1));
img2=img;
img2(find(img~=clr1 & img~=clr2)) = 0;
img2=im2bw(img2,0.2); % <-- 0.2 is the threshold
[xxx idx1]= bwfill(~img2,pts(1,1),pts(1,2),8);
[xxx idx2]= bwfill(~img2,pts(2,1),pts(2,2),8);
idx=setxor(union(idx1,idx2),[1:numel(img)]);
img2 = img;
img2(idx)=0;
imshow(img2)
Feels overly complicated but it works. It uses two steps, first a coarse "filter" and then a more thorough removal using a mask generated by converting the initially filtered image to B&W (which requires a threshold) and then a fill operation to identify pixels within the patches.
Assuming that the values of the pixel values of the two regions are known as val_1
and val_2
, you could do something like so:
val_1
set to 0, the other doing the same with val_2
. This will most likely contain a lot of noise points as well.If the shades are not known beforehand, you could use ginput
as illustrated @TryHard's answer.
You may use the Simple single-seeded region growing
function (or one of its equivalent) from matlab file exchange (below or download). This function will create a logical mask you can use to blacken your image (successive calls to this function for multiple regions)
I = im2double(imread('5Yo8l.png'));
J = segCroissRegion(0.01,I,0,0)
imshow(I+J);
function Phi = segCroissRegion(tolerance,Igray,x,y)
if(x == 0 || y == 0)
imshow(Igray);
[y,x] = ginput(1); %%% beware of the (x,y) mix-up here
end
Phi = false(size(Igray,1),size(Igray,2));
ref = true(size(Igray,1),size(Igray,2));
PhiOld = Phi;
Phi(uint8(x),uint8(y)) = 1;
while(sum(Phi(:)) ~= sum(PhiOld(:)))
PhiOld = Phi;
segm_val = Igray(Phi);
meanSeg = mean(segm_val);
posVoisinsPhi = imdilate(Phi,strel('disk',1,0)) - Phi;
voisins = find(posVoisinsPhi);
valeursVoisins = Igray(voisins);
Phi(voisins(valeursVoisins > meanSeg - tolerance & valeursVoisins < meanSeg + tolerance)) = 1;
end