A) One of your images is constant value, such as if it is all black or all white.
B) You are reading in JPEG images. However, the overwhelming majority of .jpg files are RGB files even if they look gray. True grayscale .jpg files are possible but they are mostly only encountered as technical proofs that they can exist, or as part of "compression corpuses"... and occasionally they do show up in "real life" as the output of scientific devices (but rarely from cameras.) I would recommend that you account for the possibility that the image is not truly grayscale
if ndims(I) > 2; I = rgb2gray(I); end
C)
C=imcontour(I,3);
The output of imcontour() is a contour matrix, not an image with the contours drawn in it. Contour matrices describe the contours, rather than being images. See https://www.mathworks.com/help/matlab/ref/contour.html#mw_6bc1813f-47cf-4c44-a454-f937ea210ab6 . The result will be a 2 x something double precision matrix. You can technically imwrite() that, but you will not get anything useful as the result: it would be a 2 x whatever image that is nearly all white.
You can use tools such as https://www.mathworks.com/matlabcentral/fileexchange/43162-c2xyz-contour-matrix-to-coordinates or https://www.mathworks.com/matlabcentral/fileexchange/38863-extract-contour-data-from-contour-matrix-c to extract coordinates from the contour matrix into an easier form, and you can then use Computer Vision toolbox insertShape() to draw the lines into an array that you can imwrite() .
Or you can let the contour draw to the display and getframe() -- less trouble, but it does require that a display be present (not suitable for batch processing.)