Yes, if you can do it while it is a weapon
There are really two questions here. The first is whether an improvised weapon counts as a weapon. That is easily answered by the rules on improvised weapons (emphases mine):
Often, an improvised weapon is similar to an actual weapon and can be treated as such. For example, a table leg is akin to a club. At the DM's option, a character proficient with a weapon can use a similar object as if it were that weapon and use his or her proficiency bonus.
These rules make it clear that while an improvised weapon is not a weapon in an existential sense, it can be treated as one in a functional sense. Thus, for the purpose of other rules that interact with weapons, including the magic weapon spell, improvised weapons will count as weapons.
The second question is more difficult - when is an improvised weapon a weapon? Are all the table legs in the world sitting there as weapons, or do they become weapons only when someone breaks them off to use in a fight?1 This 'timing' question, when does an object become a weapon, is irrelevant to a combat: what matters is that the table leg is a weapon at the moment of an attack, and whether it is one before or after is inconsequential. The timing is, however, essential to interact with the spell, because the spellcasting rules require that something be a legitimate target at the time the spell is cast.
Here the RAW don't help us out. Consider a situation in which two hostile parties have agreed to parlay at a neutral site with no weapons permitted for those in attendance, but I visit the spot first to enchant an improvised weapon. Can I cast magic weapon (duration: 1 hour) on the table leg before it is removed from the table? Can I cast the spell while I or my ally is wielding the table leg but before combat has started or before my foe is in sight?
Although the improvised weapon rules themselves don't help here, a tweet from Jeremy Crawford2 provides some perspective on RAI:
An improvised weapon is, indeed, a weapon, but only the moment it's used as such. A chair/shield/etc isn't a weapon otherwise.
If a DM accepts this unofficial guidance, then the magic weapon spell can be used on an improvised weapon, but only at the "moment" it is used as a weapon, since outside this moment it would not count as one. This still leaves some latitude for DM interpretation of what a "moment" is, though. Given the 'simultaneous nature' of a combat round, I personally would be inclined to permit casting on anything the caster or an ally had in hand while they were in the initiative order of a combat round. A more conservative DM might restrict casting to during the attack sequence itself, which would be hard to pull off on something wielded by an ally, given that the spell's casting time is a bonus action and casting can thus only be performed on the caster's own turn.
1 If a table leg was alone in the forest, and no one was there to wield it, would it be a weapon?
2 There are different schools of thought on Crawford's unofficial tweets. I tend to ignore them when they are unrelated or contradictory to clear RAW. In cases such as this, when RAW is unclear, I think they can be helpful in understanding RAI.