Histogram from masked area of grayscale picture

조회 수: 4 (최근 30일)
Anders Jensen
Anders Jensen 2019년 6월 18일
편집: Steven Lord 2019년 6월 18일
Hello!
I want to make a histogram from masked area of a grayscale picture. I've tried this:
maskedImage = bsxfun(@times, grayImage, cast(mask, 'like', grayImage));
histogram(maskedImage(mask), 256, 'EdgeColor', 'none')
But it keeps saying: "Array indices must be positive integers or logical values". There are no negative integers and the mask only contains 0 and 1.
Hope you can help! :-)

채택된 답변

Steven Lord
Steven Lord 2019년 6월 18일
편집: Steven Lord 2019년 6월 18일
When performing indexing, there is a difference between a logical array and a double array containing only 0 and 1. The former works and performs logical indexing, the latter does not work and throws an error because there's no such thing as element 0, row 0, column 0, page 0, etc. of an array in MATLAB.
x = 1:10
L = mod(x, 2) == 0
D = double(L)
onlyEvens1 = x(L) % Works
onlyEvens2 = x(D) % Errors
If mask is a double array containing just 0's and 1's you'll need to convert it into a logical array to use it for indexing. The easiest way to do this is to call the logical function on it.
L2 = logical(D)
isequal(L, L2) % True
onlyEvens3 = x(L2)

추가 답변 (2개)

Image Analyst
Image Analyst 2019년 6월 18일
Attach a .mat file containing your grayImage and mask.
Alternatively, just do
histogram(grayImage(mask), 256, 'EdgeColor', 'none')

Anders Jensen
Anders Jensen 2019년 6월 18일
Thanks for your answer! It still says "Array indices must be positive integers or logical values".
I have attached the .mat file and the grayscale image :-)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by