1
$\begingroup$

Here's a snippet of my notebook. When run with the frontend, the code runs fine with no problem at all. However, when I save the notebook as .m file and run it using "Mathkernel -noprompt -script notebook.m," the ImageSubtract command throws the error " Expecting an image or graphics instead of -Image-." Any help is appreciated.

Print["Processing images..."];
atomImageFile = 
  imagesDirectory <> imageTime <> atomImageSuffix <> imageFormat;
noAtomImageFile = 
  imagesDirectory <> imageTime <> noAtomImageSuffix <> imageFormat;
backgroundImageFile = 
  imagesDirectory <> backgroundImageFileName <> imageFormat;
backgroundImage = Image[Import[backgroundImageFile, "PNG"]];
atomImage = Image[Import[atomImageFile, "PNG"]];
noAtomImage = Image[Import[noAtomImageFile, "PNG"]];
ImageSubtract[atomImage, backgroundImage];

Print["Done."];
$\endgroup$
4
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Nov 28 '14 at 17:52
  • 2
    $\begingroup$ Not able to try it now, but please try assigning xx = ImageSubtract[...];.Thanks $\endgroup$ Nov 28 '14 at 18:07
  • $\begingroup$ Still giving me the same error @belisarius $\endgroup$ Nov 30 '14 at 0:55
  • $\begingroup$ What are FullForm[atomImage] and FullForm[noAtomImage]? $\endgroup$
    – bill s
    Oct 1 '15 at 15:19
1
$\begingroup$

The solution to this problem is to convert all images to ImageData for calculation. When the calculation is finished, Export the result image to a file. Here's the code that works.

atomImageFile = 
  imagesDirectory <> imageTime <> atomImageSuffix <> imageFormat;
noAtomImageFile = 
  imagesDirectory <> imageTime <> noAtomImageSuffix <> imageFormat;
backgroundImageFile = 
  imagesDirectory <> backgroundImageFileName <> imageFormat;
backgroundImage = 
  ImageData[
   ColorConvert[Image[Import[backgroundImageFile, "PNG"]], 
    "Grayscale"]];
atomImage = 
  ImageData[
   ColorConvert[Image[Import[atomImageFile, "PNG"]], "Grayscale"]];
subtractImage = atomImage - backgroundImage;
subtractImage = Image[subtractImage];
Export[atomImageFile, subtractImage, "TIFF"];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.