The answer by @A.fm. isn't wrong, but it also is unduly optimistic.
In my experience, in real life, people are more likely to lie when they are under oath than when they are not under oath, and law enforcement officers tend to be particularly good at lying on the stand because they testify frequently in court cases.
In almost all places in the United States (and most foreign jurisdictions), in a pure battle of credibility in the eyes of a judge or jury between a law enforcement officer and an individual citizen charged with a crime, the law enforcement officer's account is going to be found to be more credible (beyond a reasonable doubt) about 90% of the time or more, unless you have a majority-minority jury or an outlier extremely liberal judge or the law enforcement officer has a personal involvement in the case (e.g. it involves a family member of the officer). The likelihood that it will be resolved one way or the other does depend significantly on race and social class, however.
The credibility imbalance is still great and favors law enforcement, but not as extreme, when it is between an unrelated third-party witness and a law enforcement officer.
In the absence of hard evidence or a law enforcement insider witness willing to testify in your favor, it is almost impossible to win a credibility contest in a case like the ones you suggest.
One important step an attorney can take, however, is to seek discovery on any prior instance in which the testifying officer has been disciplined for untruthfulness or had his testimony in court found to be untruthful. This will usually be fruitless, but levels the playing field to closer to 50-50 if you get lucky and received such evidence. Such a request also provides a means of collateral attack on a conviction if the law enforcement officer has a history of untruthfulness that wasn't disclosed by the prosecution after such a request is made by a defense lawyer.
So, what does protect you?
Mostly the desire of the bosses of law enforcement officers (who are ultimately politicians, mostly local politicians in the United States) to see the law enforced in a non-corrupt manner and the fear of a law enforcement officer that he or she might be found to have lied using physical evidence unknown to him at the time (like a secret recording of an interaction).
Law enforcement officers usually don't have much to gain from lying (although this equation changes a law when police department can receive assets seized in civil forfeitures that they are involved in) and usually they want people who are "bad guys" to be in jail to protect "good guys" although they aren't always very concerned about the means by which they achieve these ends. Of course, "usually" is a weasel word here and there are many exceptions that crop up in real life (e.g. when police have engaged in misconduct and want to protect themselves from the consequences of that misconduct).
The other obvious solution (so common that in certain eras of U.S. history there were travel guides targeted at African-American motorists to help they carry out this approach) is to avoid places with police who have historically been corrupt. The United States has a uniquely bottom heavy law enforcement structure. Something on the order of 95% of law enforcement officers are employed by local governments or are otherwise tightly constricted geographically (e.g. rangers in national or state parks). And, even the small number of state law enforcement officers are heavily concentrated doing traffic enforcement on major state and federal highways. Similarly, lots of federal law enforcement agencies are broken up into geographic divisions some of which are known to be more corrupt than others (e.g. there is more corruption in the border patrol on parts of the border with Mexico than on most of the border with Canada).
So, if you want to avoid the risk of encountering bad cops, stay away from places that are known to have bad cops.
The solution may seem like a "cop out" (sorry, pun intended), but it is actually a pretty unique feature of the American law enforcement system. Most countries (e.g. the U.K., South Korea, Russia, Japan, Spain, Mexico, France) have a much more centralized law enforcement bureaucracy, which is fine when the people are the top are scrupulously non-corrupt, but which also makes it much easier for the rot of corruption to become geographically widespread and hence unavoidable from the point of view of an individual citizen. In contrast, in the U.S., even in the most corrupt of times (e.g. the Prohibition era), there are almost always many jurisdictions where law enforcement is not corrupt and corrupting the entirely system is much more difficult than in most countries.