1
$\begingroup$

I have several images to be processed with a series of functions. The current script I have only gives me the result for the last image. How can I get all the results for every single images? Thanks in advance!

The script I have now:

NotebookDirectory[]
ClearAll;
files1 = FileNames["*.jpg", NotebookDirectory[]]
nfile = Length [files1]
i = 1
Do[Data = Import[files1[[i]]];
 img = Dilation[Data, DiskMatrix[5]];
 img = Binarize[img, 0.67];
 img = CurvatureFlowFilter[img, 10];
 img = EdgeDetect[img, 10];
 img = RemoveAlphaChannel[ColorNegate@ColorConvert[img, "Grayscale"]];
 pts = PixelValuePositions[img, Black, .2];
 s1 = Select[pts, #[[1]] > 0 &];
 s1 = Select[s1, #[[1]] < 100 &];
 line1 = Fit[s1, {1, x}, x];
 a = Coefficient[line1, x ^1], {i, 1, nfile}]

The images are like enter image description here enter image description here

$\endgroup$
2
  • $\begingroup$ Coding tip: ClearALL is not a command, so the 2nd line in your code does nothing. ClearALL is a function with side effects on the sequence of symbols it receives as arguments. No arguments -- no results. $\endgroup$ – m_goldberg Aug 23 '17 at 23:43
  • 1
    $\begingroup$ What do you mean it only gives you the result for the last image? That a only has the final value? Well, you keep assigning to it. How can it have any other value? $\endgroup$ – Itai Seggev Aug 24 '17 at 6:06
4
$\begingroup$

Don't use Do loops. Instead, Map your operation on a list of filenames:

ClearAll[process]

process[Data_] :=
 Module[{img, pts, s1, line1},
  img = Dilation[Data, DiskMatrix[5]];
  img = Binarize[img, 0.67];
  img = CurvatureFlowFilter[img, 10];
  img = EdgeDetect[img, 10];
  img = RemoveAlphaChannel[ColorNegate@ColorConvert[img, "Grayscale"]];
  pts = PixelValuePositions[img, Black, .2];
  s1 = Select[pts, 100 > #[[1]] > 0 &]; (* Note the double condition *)
  (* Note the slightly more direct method to get the slope *)
  LinearModelFit[s1, {1, x}, x]["BestFitParameters"][[2]]
]

results = process /@ Import /@ FileNames["*.jpg", NotebookDirectory[]]

This will return a list of results, stored in results, which you can access as you wish.

With the two images you provided:

Mathematica graphics

(* Out: {-0.269902, -0.227854} *)
$\endgroup$
2
  • $\begingroup$ Thanks very much for your solution! But I got this after copy and run the answer: "The first argument "C:\Users\Yanyao\Desktop\pic\001.jpg is neither a rectangular array nor an image." It seems process /@ should be followed by a series of images rather than the locations of the images. I tried several times and can't solve it...help appreciated! $\endgroup$ – Yanyao Bao Aug 24 '17 at 1:24
  • $\begingroup$ OK, I found a solution...I added the commands below and copy all the images after 'process /@', then I got all the results I want. I will keep searching more convenient way for this...Thanks again for your help. files = FileNames["*.jpg", NotebookDirectory[]] Import[#] & /@ files $\endgroup$ – Yanyao Bao Aug 24 '17 at 1:41

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