1
$\begingroup$

I have a folder of multi images and all of them are treated in the same way. Now I can only import them one by one and plot data one by one.But I want to import them once a time and plot the data in two graphs(liquid and air ) instead of multi graphs. For example, this is one frame within the folder:test.jpg

I do the following operations on a single picture:

smallSize = 42;
threshold = 0.159;

img = ImageResize[Import["test.jpg"], 1000];
img = ImageTrim[img, {{.05, .05}, {.98, .98}}, DataRange -> {{0, 1}, {0, 1}}];


img1 = DeleteSmallComponents[Binarize[img, threshold], smallSize];
img1 = DeleteSmallComponents[ColorNegate[img1], smallSize];
img1 = ColorNegate[Binarize[img1]];
markers = DeleteSmallComponents[MaxDetect[GaussianFilter[img1, 3], Padding -> 1], smallSize];
img1 = Closing[GeodesicDilation[markers, img1], DiskMatrix[3]];
img2 = MorphologicalComponents[img1] // Colorize;
img3 = WatershedComponents[img1, Method -> {"MinimumSaliency", 2.0}] //
Colorize;

img4 = Opening[ColorNegate[img1], DiskMatrix[3]];
img5 = MorphologicalComponents[img4] // Colorize;
img6 = WatershedComponents[img4, Method -> {"MinimumSaliency", 1.0}] //
Colorize;
Show[GraphicsGrid[{{img2, img5}}, ImageSize -> 1000, Spacings -> {0, 0}]]

window[list_, {xmin_, xmax_}] := Pick[list, Boole[xmin <= # <= xmax] & /@ list, 1]
data1 = Tally[Flatten[MorphologicalComponents[img5, CornerNeighbors -> False]]];
d = window[data1[[All, 2]], {25, 3050}];
{binsL, countsL} = HistogramList[Sqrt[d], {5}];
dataL = Transpose@{Flatten[{Rest[binsL] + Most[binsL]}/2], 
Flatten[countsL]};
GraphicsGrid[{{img, img5, Histogram[Sqrt[d]]}}, ImageSize -> 1000, Spacings -> {0, 0}]
Sort[data1, #1[[2]] > #2[[2]] &]

window[list_, {xmin_, xmax_}] := 
  Pick[list, Boole[xmin <= # <= xmax] & /@ list, 1]
data2 = Tally[Flatten[MorphologicalComponents[img2, CornerNeighbors -> False]]];
d = window[data2[[All, 2]], {1, 2500}];
{binsA, countsA} = HistogramList[Sqrt[d], {5}];
dataA = Transpose@{Flatten[{Rest[binsA] + Most[binsA]}/2],Flatten[countsA]};
GraphicsGrid[{{img, img2, Histogram[Sqrt[d]]}}, ImageSize -> 1000, Spacings -> {0, 0}]
Sort[data2, #1[[2]] > #2[[2]] &]

plt = ListPlot[{dataL, dataA}, AxesLabel -> {Size, Number}, 
               LabelStyle -> Directive[Black, Bold, {FontSize -> 14}],
               Joined -> True, 
               PlotLabel -> "Liquid-Air Cluster Distribution", 
               LabelStyle -> Directive[Black, Bold, {FontSize -> 14}],
               PlotLegends -> Placed[{Liquid, Air}, {0.25, 0.75}]]

SetOptions[Manipulator, Appearance -> "Labeled"];
Manipulate[
  DeleteSmallComponents[Binarize[img, t], j], {t, 0, 1}, {j, 1, 500, 1}]

After these steps I got a graph. But it is the data from only one frame. I want to plot all data from frames in this folder into two graphs: one is air, the other one is liquid. In other words, multi lines representing liquid and air in a single graph,respectively.Here is a hand-draw to illustrate what I am meaning: enter image description here I have changed the code into following and still have no result, I am a newbie in MMA, really thanks for your suggestions:

SetDirectory[$UserDocumentsDirectory]
files = FileNames["*.JPG", {"E:\\photo\\2mmsingle3ml20s"}];
data = Table[Import[files[[j]]], {j, 1, Length[files]}];
 smallSize = 23;
threshold = 0.271;
i = 1;
Do[
 {
  img = ImageResize[Import[files[[i]]], 1000];
  img = ImageTrim[img, {{0, 0}, {1, 1}}, 
    DataRange -> {{0, 1}, {0, 1}}];


  img1 = DeleteSmallComponents[Binarize[img, threshold], smallSize];
  img1 = DeleteSmallComponents[ColorNegate[img1], smallSize];
  img1 = ColorNegate[Binarize[img1]];
  markers = 
   DeleteSmallComponents[
   MaxDetect[GaussianFilter[img1, 3], Padding -> 1], smallSize];
  img1 = Closing[GeodesicDilation[markers, img1], DiskMatrix[3]]; 
  img2 = MorphologicalComponents[img1] // Colorize;
  img3 = WatershedComponents[img1, 
  Method -> {"MinimumSaliency", 2.0}] // Colorize;
  img4 = Opening[ColorNegate[img1], DiskMatrix[3]];
  img5 = MorphologicalComponents[img4] // Colorize;
  img6 = WatershedComponents[img4, 
  Method -> {"MinimumSaliency", 1.0}] // Colorize;

  (*image processing*)
  window[list_, {xmin_, xmax_}] := 
   Pick[list, Boole[xmin <= # <= xmax] & /@ list, 1]
    data1 = 
   Tally[Flatten[
   MorphologicalComponents[img5, CornerNeighbors -> False]]];
  d = window[data1[[All, 2]], {25, 3050}];
  {binsL, countsL} = HistogramList[Sqrt[d], {5}];
  dataL[i] = 
 Transpose@{Flatten[{Rest[binsL] + Most[binsL]}/2], 
 Flatten[countsL]};}, {i, 1, Length[files]}]
(*liquid data*)
window[list_, {xmin_, xmax_}] := 
Pick[list, Boole[xmin <= # <= xmax] & /@ list, 1]
data2 = Tally[
 Flatten[MorphologicalComponents[img2, CornerNeighbors -> False]]];
d = window[data2[[All, 2]], {1, 2500}];
{binsA, countsA} = HistogramList[Sqrt[d], {5}];
dataA[i] = 
Transpose@{Flatten[{Rest[binsA] + Most[binsA]}/2], Flatten[countsA]};

plots = Table[
ListPlot[dataL[i], Frame -> True, Axes -> {True, False}, 
Filling -> Axis, FrameTicks -> All, PlotRangePadding -> .2, 
PlotRange -> All], {i, 1, Length[files]}]
plots = Table[
ListPlot[dataA[i], Frame -> True, Axes -> {True, False}, 
Filling -> Axis, FrameTicks -> All, PlotRangePadding -> .2, 
PlotRange -> All], {i, 1, Length[files]}]
$\endgroup$
  • 4
    $\begingroup$ Have a look at SetDirectory, FileNames and Map. Basically what you do is turn your whole procedure into a proper function and map it on all the files in a given directory (via setting the current working directory with SetDirectory and selecting the files with FileNames). Check out Module too. $\endgroup$ – Sascha Jul 11 '16 at 6:25
  • $\begingroup$ Somewhat related: (1122) $\endgroup$ – Mr.Wizard Jul 11 '16 at 6:59
  • $\begingroup$ Thanks Sascha, I have tried your suggestion and the notebook has been changed, but as it runs to this line: img2 = Map[MorphologicalComponents[#] // Colorize &, img1]; I got this error:Expecting an image or graphics instead of #1. Since I am new in MMA, thanks for your help. $\endgroup$ – Shuoqi Li Jul 14 '16 at 11:10
  • $\begingroup$ try img2 = Map[Colorize[MorphologicalComponents[#]] &, img1]. looks like your img1 is a single element (considering the definition inside the Do loop). In that case you don't need Map. Just use img2 = Colorize[MorphologicalComponents[img1]]. $\endgroup$ – Sumit Jul 19 '16 at 13:59

Your Answer

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

Browse other questions tagged or ask your own question.