My response:
Statements made in a courtroom are privileged and non-actionable. Whether or not the judge will even consider what you're thinking of saying is one matter, but if you say it, there's nothing anyone can do to you for having said it. More than likely, the judge will not accept that as a defense to your ticket because, and despite the fact that the officer may have made derogatory remarks, if you did, in fact, violate the law, then that is all the judge will consider.
My suggestion would be that, if you sincerely feel that some sort of discrimination occurred, that you make a written complaint about the officer's conduct. The police station has such forms. Again, the statements you write are privileged and every citizen has a right to complain about a public servant's actions. The police captain will make a cursory investigation, find that there was no wrongdoing on the officer's part, and that will be the end of it.
Good luck to you.
IAAL