Two undercover cops who witnessed a daytime street-corner murder in Bedford-Stuyvesant yesterday shot and killed the gunman when he took aim at them, police said.

The two Brooklyn narcotics cops narrowly missed being the gunman’s second and third victims after the suspect brazenly pulled his .38-caliber Rossi handgun and pointed it at the pair.

One of the cops was able to squeeze off two rounds, hitting the perp, Carlos Lopez, 19, in the chest and shoulder before Lopez could fire.

A videotape from a security camera at 685 Gates Ave. – played for reporters by Police Commissioner Ray Kelly – shows a man walking up to the lobby doors, turning, extending his arm and firing five rounds before running away.

The tape does not show the cops shooting Lopez.

The shooting happened around 1:30 p.m. when the two undercovers were driving their minivan west on Gates Avenue.

The cops told investigators they heard several shots ring out and saw Lopez still pumping bullets into his victim, James Hodge, 21.

Lopez was running away toward Marcus Garvey Boulevard when he turned and aimed his revolver at the officers.

That’s when one cop fired. Lopez stumbled about 80 feet before he collapsed and died.

An emotionally charged crowd of neighborhood residents who gathered before the body claimed police had shot the wrong man.

Later, an angry mob broke down an apartment door of a man who they said was the real killer.

Lopez’s mother, Carolin, sobbed as she defended her slain son. “He was an innocent bystander. The real killer is on the run,” she said.