roi mask and working on the masked image

4 views (last 30 days)
Hi, I am working on a project, where i have to work with only the foreground, not the background, so iused color threshold, image segment app nothing worked, so finally i made a transparent background in alpha layer and stored the image usng imwrite. Now for my foreground analysis, i used the saved image but it calculates including transparent backgroun. I tried to use the imfreehand roi , i am able to make the roi, but cannot use the masked ROI for my further analysis. Even i freehand roi is not exactly masking the foreground, there are some features that missing. so i used this below mentioned code
img = double(imread('alphaimg1.png'))/255;
imshow(img);
ROI = imfreehand;
img_mask = ROI(ROI ~=1)
Maximg = max(img_mask); % trying to find the max value of the roi image
Minimg = min(img_mask); % trying to find the max value of the roi image
hist(img_mask,0:0.01:1)
medMask = median(img_mask);
imshow(medMask)
It shows the following error
img_mask =
imfreehand with properties:
Deletable: 1
Error using max
Invalid data type. First argument must be numeric or logical.
Error in alphaimgtrial1 (line 54)
Maximg = max(img_mask); % trying to find the max value of the roi image
using this ROI, i have find median pixel value of the image and try to split the histogram like dark pixels less than median value set to median value and light pixels higher than median value set to median value.
How do I proceed? I am attaching an alpha layer transparent image.

Answers (1)

Walter Roberson
Walter Roberson on 26 Jun 2020
img = im2double( imread('alphaimg1.png'));
imshow(img);
ROI = imfreehand;
img_mask = ROI(ROI ~=1);
roi_img = img(img_mask);
Maximg = max(roi_img);
Minimg = min(roi_img);
hist(roi_img, 0:0.01:1);
MedMask = median(roi_img);
imshow(MedMask) does not make sense to do, as the median will be a scalar. Perhaps you are thinking of median filtering on the image, such as medfilt2(). In that case, you have a problem with the masked pixels. One approach is:
dimg = img; %must be double or single for this to work
dimg(~img_mask) = nan;
medimg = cast( nlfilt(dimg, [3 3], @(B) median(B(:), 'omitnan')), class(img) );
h = imshow(medimg);
h.AlphData = double(img_mask); %0.0 for transparent, 1.0 for opaque
  28 Comments
Malini Bakthavatchalam
Malini Bakthavatchalam on 7 Jul 2020
@Walter Roberson: Sir, Now i spoke to my professor he suggested me like for example we have 3 color planes and three axis, can we get the histogram of x axis with the white point as the median? Like in my image x axis has the color range from red to green somewer there will be a middle white point. so that white point should be set as median. And he wants to look at the image and histogram of the axis...
Walter Roberson
Walter Roberson on 7 Jul 2020
Like in my image x axis has the color range from red to green somewer there will be a middle white point.
No, that is not true that there is a white point in the middle.
The green part starts with no blue component.
The red part has no blue component.
If there were hypothetically a white point in the middle, then because white is equal parts red, green, and blue, then you have two possibilities:
  1. That the "white point" you refer to is a point with red, green, and blue components all 0, which would normally be called black, but you are calling it white because it has equal red, blue, and green (which is to say, none at all of any of them.) This is contrary to the common meaning of white.
  2. Or... through some process, the transition from green to the hypothetical white point in the middle adds blue, and the transition from the hypothetical white point in the middle to red subtracts blue again. This would be... odd... but not impossible. It would have to be justified.

Sign in to comment.

Categories

Find more on MATLAB Support Package for USB Webcams 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!