I am trying to detect various objects containing colored markers, so a red blue green marker identifies object A, and a red blue red marker identifies object B. My problem is I can't use template matching cause objects can be rotated, currently I am thinking about check for each color then find the object by checking the distance between colors but it seems inefficient, so my question is there a better way to do this?
Here's a short article I wrote about tracking colored objects. This just might be what you're looking for.
You might want to try doing local color histograms, and use the color ratios as the identifiers.
For a robust and somewhat CPU-efficient approach, I'd suggest you first transforming the image using cv::transform, so that nonzero values of the output image channels correspond to blobs with colors you'd like to track. For example,
b g r bias r' [ -1 -1 1 -15 ] b' [ 1 -1 -1 -15 ]
is a transformation matrix that will assign nonzero value for very red pixels to the first output channel, and for very blue pixels to the second output channel.
Then, you can run cv::findContours on the output channels, one by one, to find favorably colored blobs. Then, by iterating over pairs, 3-tuples, etc. of blobs and running some geometrical checks on them (for example, if your marker is composed of a blue, red and green circle, you should check that the three blobs are "circular" enough is shape and lie close to each other to consider them a marker and not just noise) the markers can be located.
To track them, instead of re-running the above algorithm every frame, you can apply some kind of CAMShift on meanShift-based tracking after running cv::transform. In case of CAMShifting, it should be checked every frame if the object which is being tracked is still the marker, if a robust tracker is sought for.
The raw position (and maybe rotation) values from the tracking will generally be somewhat noisy. For example, if you have fluorescent lights in the room and use a red-blue marker, the tracking may "flicker" a bit. To compensate, a Kalman filter or an extended Kalman filter is useful, but introduces a lot of parameters to be adjusted / guesstimated.