Hello,
I'm using imhist, and I would like to assign a default value to each pixel whose intensity appears less than a fixed number of times in the image. In other words, I would like a function whose input is an image and the output is its histogram and the concerned pixels (their location) for each bin of the histogram. I could write a script doing that, but I'm looking for a direct method, as optimized as possible.
Thanks for your help
Sylvain

1 Kommentar

Adam
Adam am 22 Aug. 2017
Well, I'm pretty sure there isn't a single inbuilt function that will do it all for you so you'll have to write some code. It depends what you consider a 'direct' method vs whatever you think the alternative is.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Jan
Jan am 22 Aug. 2017

0 Stimmen

The question is not clear. What are " concerned pixels (their location)" exactly?
I assume you want to start with:
[counts, binLocations] = imhist(I); % Or imhist(I, n) with a suitng n?
And now you want set all binLocations with counts<limit to a certain value? How would the script you have mentioned look like?

3 Kommentare

Sylvain Hauser
Sylvain Hauser am 22 Aug. 2017
I was looking for a Matlab built-in function or a very short script which gives me, for each bin of the histogram, the positions (x,y), of the pixels belonging to this bin. I don't want to modify the histogram, but the image itself. For now, I wrote a script which is not ideal, since it has a for loop in order to access to each bin of the histogram. But apparently, there is not necessarily a better way to do.
Adam
Adam am 22 Aug. 2017
Well, you haven't shown your script so who knows if there is a better way or not until you do?!
Sorry, here is my script, f being the grayscale image and F a factor to determine the threshold:
function [f] = imhist2(f,F)
bl=linspace(0,1,256);
bl1=zeros(size(bl));
bl1(2:end)=(bl(1:end-1)+bl(2:end))/2;
bl2=ones(size(bl));
bl2(1:end-1)=(bl(1:end-1)+bl(2:end))/2;
th=F*max(imhist(f));
cn=zeros(size(bl));
for i=1:length(bl)
ind=find(f>bl1(i)&f<bl2(i));
cn(i)=length(ind);
if cn(i)<th
f(ind)=mean2(f);
end
end
end

Melden Sie sich an, um zu kommentieren.

Jan
Jan am 22 Aug. 2017
Bearbeitet: Jan am 22 Aug. 2017

0 Stimmen

Try this:
Edge = linspace(0,1,256);
[N, Edge, Bin] = histcounts(f, Edge);
V = (N < (F * max(N)));
Mask = V(Bin);
f(Mask) = mean(f(:));
histcounts determines the frequency of values. I've used the simpler linspace(0,1,256) here, because the intention of your bl1 and bl2 is not clear to me. You can adjust the wanted edges as you want. Then a mask is created, which is TRUE for all values, which appear less than F times the most frequent value.
Image Analyst
Image Analyst am 22 Aug. 2017

0 Stimmen

Then simply threshold it and assign the new value you want.
pixelsToReplace = cfImage < someThresholdValue;
cfImage (pixelsToReplace) = desiredValue;

Gefragt:

am 22 Aug. 2017

Beantwortet:

am 22 Aug. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by