Thresholding an image based on part of a histogram's values

3 ビュー (過去 30 日間)
Teshan Rezel
Teshan Rezel 2020 年 6 月 12 日
コメント済み: Teshan Rezel 2020 年 6 月 18 日
Hi folks,
I'm trying to get the parts of an image that fall between 2 values of its grayscale histogram, the revert the image back to its colour form. So far, I am struggling to get anything but a black image! Any ideas on what I'm doing wrong please? Below is my code.
baseGray = rgb2gray(I);
isoLower = 135;
isoUpper = 155;
lessThan = baseGray < isoLower;
greaterThan = baseGray > isoUpper;
baseGray(lessThan) = 0;
baseGray(greaterThan) = 0;
grayThresh = imbinarize(baseGray);
grayThresh = bwareaopen(grayThresh, 3000);
grayThresh = imfill(grayThresh, 'holes');
grayThresh = bwperim(grayThresh);
grayThresh = imdilate(grayThresh, ones(5));
grayThresh = imerode(grayThresh, ones(3));
grayThresh = imfill(grayThresh, 'holes');
refigure = img.*repmat(uint8(grayThresh),[1 1 3]);
imshow(refigure);

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 13 日
編集済み: Ameer Hamza 2020 年 6 月 13 日
Try something like this
img = im2double(imread('pears.png'));
img_gray = rgb2gray(img);
isoLower = 135/255; % double image, pixel intensity between 0 and 1
isoUpper = 155/255;
mask = (isoLower < img_gray) & (img_gray < isoUpper);
refigure = img;
refigure(~mask(:,:,[1 1 1])) = 0;
imshow(refigure)
  10 件のコメント
Image Analyst
Image Analyst 2020 年 6 月 18 日
Why not just try the Color Thresholder on the Apps tab of the tool ribbon and interactively set your thresholds???
Teshan Rezel
Teshan Rezel 2020 年 6 月 18 日
Hi Image Analyst, I've tried the tool and found it useful to a degree. But my issue is that I will need to analyse several batches of 900 images each, so doing them individually in the app would not be efficient. Furthermore, I need to analyse them in grayscale, which isn't an option on the app as far as I can see. Thanks for your suggestion!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by