roi mask and working on the masked image

조회 수: 4(최근 30일)
Malini Bakthavatchalam
Malini Bakthavatchalam 2020년 6월 26일
댓글: Walter Roberson 2020년 7월 7일
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.

답변(1개)

Walter Roberson
Walter Roberson 2020년 6월 26일
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
Walter Roberson
Walter Roberson 2020년 7월 7일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by