Contour not rendered for constant ZData

79 views (last 30 days)
Hi,
Can anyone help and point me to write direction, I am trying to get the contour of 190 grayscale images but getting error. Code and error shown below.
Code:
srcFile=dir('C:\Users\muhammad\Desktop\newexperiment\*.jpg')
for i=1:length(srcFile)
filename=strcat('C:\Users\muhammad\Desktop\newexperiment\',srcFile(i).name);
I=imread(filename);
C=imcontour(I,3);
path=strcat('C:\Users\muhammad\Desktop\newexperiment\',srcFile(i).name);
imwrite(C,path);
end
Error:
Warning: Contour not rendered for constant ZData
> In contour (line 51)
In imcontour (line 46)
In ContImages (line 8)
Error using imwrite (line 427)
Expected DATA to be nonempty.
Error in ContImages (line 10)
imwrite(C,path);

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jul 2020
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.)

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!