A good question. The reason Jesus was careful about people completely understanding exactly who he was earlier in his ministry is because he was very well aware of the need for precise timing of his death. He had to die in Jerusalem, at the Passover, in a particular year that fulfilled Daniel 9:24-25. Once Jesus declared openly that he was God-in-the-flesh, his eventual fate of being killed was sealed. Jesus is the Passover lamb, and the fulfillment of the foreshadow in the Passover itself, as his death, which put his blood on the wooden cross is the antitype to the blood of the Passover lambs which saved the Israelite firstborn from death as well. The timing was prophesied very precisely.
If Jesus were to be too public with some of his miracles, and especially with his claims about himself, he would have been killed too early. For this reason, Jesus held back. As he said several times in the Gospel of John, “My time has not yet come.” This “time” was the time of his death, burial and resurrection in Jerusalem at the Passover in AD 30. Then, in John 13:1, when Jesus entered Jerusalem the week of his death, John tells us that his time had come. That Jesus was being careful about too publicly revealing his being Messiah and God is found in all four gospels.
You are very perceptive to notice the exception of the man called Legion in Luke. Why would Jesus treat him differently? The reason is that Legion was not a Jew and he lived in a Greek, rather than Jewish area. There was little danger of letting out who Jesus is among the Gentiles, but much risk in doing the same thing among the Jews. This explains the difference. I hope this helps.