How to remove 'zeros 0' from histogram so it will not affect imhistmatchn?

26 ビュー (過去 30 日間)
John
John 2025 年 1 月 22 日 0:14
編集済み: DGM 2025 年 1 月 22 日 1:14
Due to the mask, the histogram contains zeros, which is incorrect. How can I remove the zeros from the histogram, or how can I adjust the histogram function to exclude the zeros? This would ensure that the result will not affect the result of the imhistmatch function.
Thank you.

採用された回答

DGM
DGM 2025 年 1 月 22 日 0:45
編集済み: DGM 2025 年 1 月 22 日 1:14
I'm going to guess at what's going on here. I'm assuming you're doing something like this:
% a single-channel image and a logical mask
inpict = imread('coins.png');
mk = imread('coinmask.png');
montage({inpict,mk})
% apply matting to the image using the mask
outpict = inpict;
outpict(~mk) = 0;
imshow(outpict)
% that makes a nice picture,
% but it's counterproductive for programmatic purposes
imhist(outpict) % the background (0) now dominates
Instead of using the mask to create a visualization with black matting, use the mask to extract the pixels in the ROI.
% same as before
inpict = imread('coins.png');
mk = imread('coinmask.png');
% just address the image to get the ROI content
% the result is just a vector, so there's no point viewing it.
roipix = inpict(mk);
% we're just interested in the intensity distribution in the ROI
% and that's all we get.
imhist(roipix)
If you intend to apply the value transformation to create a new image, then you can simply operate on the vectorized ROI content and then use the mask to insert it back into the original image, the matted image, or any other image of compatible geometry.
% same as before
inpict = imread('coins.png');
mk = imread('coinmask.png');
% some reference image for example
ref = imread('pout.tif');
% reshape the distribution of the ROI pixels
roipix = imhistmatch(inpict(mk),ref);
% shove the altered pixels back into the original image
% (or any other image of the same size)
outpict = inpict;
outpict(mk) = roipix;
imshow(outpict)
This example is working on a 2D grayscale image, but the same concept can be applied to multichannel or volumetric images, assuming that the mask is sized to match the image.
See also:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by