Selecting best range of values from histogram curve

2024/9/29 7:22:11

Scenario :

I am trying to track two different colored objects. At the beginning, user is prompted to hold the first colored object (say, may be a RED) at a particular position in front of camera (marked on screen by a rectangle) and press any key, then my program takes that portion of frame (ROI) and analyze the color in it, to find what color to track. Similarly for second object also. Then as usual, use cv.inRange function in HSV color plane and track the object.

What is done :

I took the ROI of object to be tracked, converted it to HSV and checked the Hue histogram. I got two cases as below :

enter image description here enter image description here

( here there is only one major central peak. But in some cases, I get two such peaks, One a bigger peak with some pixel cluster around it, and second peak, smaller than first one, but significant size with small cluster around it also. I don't have an sample image of it now. But it almost look like below (created in paint))

enter image description here

Question :

How can I get best range of hue values from these histograms?

By best range I mean, may be around 80-90% of the pixels in ROI lie in that range.

Or is there any better method than this to track different colored objects ?

Answer

If I understand right, the only thing you need here is to find a maximum in a graph, where the maximum is not necessarily the highest peak, but the area with largest density.

Here's a very simple not too scientific but fast O(n) approach. Run the histogram trough a low pass filter. E.g. a moving average. The length of your average can be let's say 20. In that case the 10th value of your new modified histogram would be:

mh10 = (h1 + h2 + ... + h20) / 20

where h1, h2... are values from your histogram. The next value:

mh11 = (h2 + h3 + ... + h21) / 20

which can be calculated much easier using the previously calculated mh10, by dropping it's first component and adding a new one to the end:

mh11 = mh10 - h1/20 + h21/20

Your only problem is how you handle numbers at the edge of your histogram. You could shrink your moving average's length to the length available, or you could add values before and after what you already have. But either way, you couldn't handle peaks right at the edge.

And finally, when you have this modified histogram, just get the maximum. This works, because now every value in your histogram contains not only himself but it's neighbors as well.

A more sophisticated approach is to weight your average for example with a Gaussian curve. But that's not linear any more. It would be O(k*n), where k is the length of your average which is also the length of the Gaussian.

https://en.xdnf.cn/q/71242.html

Related Q&A

dash_bootstrap_components installed succesfully but no recognised

I have my dash working perfectly. I have installed dash_bootstrap_components to give style to my dash. I wrote pip install dash-bootstrap-components and was perfectly installed. But when I run the app,…

Efficient updates of image plots in Bokeh for interactive visualization

Im trying to create a smooth interactive visualization of different slices of a muldimensional array using Bokeh. The data in the slices changes according to the user interaction and thus has to be upd…

AttributeError: module cv2.cv2 has no attribute TrackerMOSSE_create

As the Dans suggestion, i tried to edit this post Error occurred at setting up MOOSE tracker, I also dont know why this error happened because i installed the Opencv-contrib-python==4.5.1.48.However,af…

Python, Error audio Recording in 16000Hz using Pyaudio

I use Python 2.7.3, Mac OS 10.8.2 and Xcode 4.5.1I am trying to record sound using PyAudio following the instructions in http://people.csail.mit.edu/hubert/pyaudio/and using the program ""&qu…

FastAPI passing json in get request via TestClient

Im try to test the api I wrote with Fastapi. I have the following method in my router : @app.get(/webrecord/check_if_object_exist) async def check_if_object_exist(payload: WebRecord) -> bool:key = g…

Python TDD directory structure

Is there a particular directory structure used for TDD in Python?Tutorials talk about the content of the tests, but not where to place themFrom poking around Python Koans, suspect its something like:/…

Pillow was built without XCB support

Im working on a program that uses ImageGrab in Pillow. I am getting the error mentioned in the title. I notice in the documentation that it says the generic pip install Pillow doesnt come with libxcb. …

Set equal aspect in plot with colorbar

I need to generate a plot with equal aspect in both axis and a colorbar to the right. Ive tried setting aspect=auto, aspect=1, and aspect=equal with no good results. See below for examples and the MWE.…

How to emit dataChanged in PyQt5

The code below breaks on self.emit line. It works fine in PyQt4. How to fix this code so it works in PyQt5?from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QObject, pyqtSignalclass …

django: gettext and coercing to unicode

I have following code in my django application.class Status(object):def __init__(self, id, desc):self.id = idself.desc = descdef __unicode__(self):return self.descSTATUS = Status(0, _(u"Some text&…