[Python]Manipulating histogram values

asked 2018-03-15 23:25:48 -0500

Hi! Well, i'm fairly new to Python but i have some coding background on some other languages like C. I could have gone for C++ but i wanted to learn Python so here i am, treading on unknown territory and i hope you guys can help me : )

The thing is, i've got an image:


And i was able to extract (most of) its background, giving me my region of interest:


Now, i wan't to access the HSV values of the pixels in my RoI, which i did using:

hist_H = cv2.calcHist([img],[0],mask,[256],[0,256]) #For the H channel

And so on for the S and V channels (altough i'm just gonna use the H and S).

I need to get the average or predominant value of each specific channel, but using NumPy's mean/median functions compute the number of pixels, and not the value of those pixels (which are between 0 and 256). If i use the mean/median directly on the channel (say, the H channel) then it gives me the mean/median of all pixel values, but unfortunately it will compute the black pixels from my mask :/

I need this data so I can determine HSV ranges for ripe/unripe fruits and the like.

Does anyone have any idea of what could be done in this case?

edit retag flag offensive close merge delete


"I need this data so I can determine HSV ranges for ripe/unripe fruits" -- hmm, you already got a nice mask, and the histogram. isn't that all you need for a comparison ?

imho, it's much better, to compare histograms for this (longer features, more precision), than hsv ranges.

you could go as far as training an SVM on histograms of ripe/unripe apples, and let it predict() the outcome furtheron.

berak gravatar imageberak ( 2018-03-17 10:38:31 -0500 )edit

Hi, Well, i didn't think about using the whole array of values from the histogram to train something. I thought it would be way easier to train something based on one variable, but i can understand how better it would be if i could train it to see the whole picture (hehe). I will try finding some sources on the subject of SVMs. Thanks!

Navarrox gravatar imageNavarrox ( 2018-03-18 14:04:57 -0500 )edit