This function is located in opencv_imgcodecs
library. It also worths mentioning that you may need to put your object file before libraries in order to link successfully:
g++ -c -I/usr/include/opencv4/opencv -I/usr/include/opencv4 main.cpp
g++ main.o -lopencv_imgcodecs $(OTHER_FLAGS) -o main