I'm sure Jim will be able to give a better explanation, and there are probably a lot of exceptions, but if the original word starts with a p, m, b or any other sound that has your lips together, you use the IM-.
I'm not sure about IN- vs. UN- though. I'm trying to think of other words that start with a vowel to find one that uses UN, but a lot of the vowels do seem to use IN-.
ability/inability; effective/ineffective; opportune/inopportune