2
$\begingroup$

I am a new mathematica user.

I am using the following code to measure the internal rectangle width in some images. When I run mathematica for $1$ image, it takes a short time. Also, it works and it shows the results for more than one image. However, it takes (EXTREMELY) long time to show the results, especially when the number of picture more than $5$. It took around $25$ minutes to process around $35$ pictures!

One possibility, that my requirement from mathematica is too much, and it is normal to process $35$image/$25$min.

One possibility, that I use mathematica in a wrong way to get the result; I go to Evaluation bar then Evaluate Notebook option.

enter image description here

One possibility, that the code can be modified in a way it will work faster.

One possibility, that this version of mathematica (12.1) has an issue.

PLEASE CHECK THE FOLLOWING:

ClearAll[vDilate, internalRectangles, dataSet]

vDilate = 
  ColorNegate@
    Dilation[MorphologicalBinarize@#, 
     ConstantArray[1, {ImageDimensions[#][[1]], 1}]] &;

internalRectangles[w_ : 1] := 
  Rectangle @@@ 
    Values[ComponentMeasurements[
      SelectComponents[
       vDilate@#, #CaliperWidth > 
          w && #AdjacentBorders == {Bottom, Top} &], "BoundingBox"]] &;

dataSet[w_ : 1] := 
  ComponentMeasurements[
    SelectComponents[
     vDilate@#, #CaliperWidth > 
        w && #AdjacentBorders == {Bottom, Top} &], {"BoundingBox", 
     "CaliperWidth"}, "Dataset"] &;

{img1, img2, img3, img4, img5, img6} = 
 Import /@ {"D:\\SHARGAM\\167716 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) \
(61-65 C) 2 4046 08-03-12  10-06-2020.jpg", 
   "D:\\SHARGAM\\252929 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) (75-80 \
C) 2 4048 09-16-57  11-06-2020.jpg", 
   "D:\\SHARGAM\\232614 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) (70-75 \
C) 2 4051 03-15-54  11-06-2020.jpg", 
   "D:\\SHARGAM\\211778 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) (70-75 \
C) 2 4051 21-05-53  10-06-2020.jpg", 
   "D:\\SHARGAM\\192633 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) (70-75 \
C) 2 4052 15-25-53  10-06-2020.jpg", 
   "D:\\SHARGAM\\172707 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) (65-70 \
C) 2 4050 09-31-56  10-06-2020.jpg"}


ClearAll[dataSet2]
dataSet2 = 
  MaximalBy[#CaliperWidth &]@
    ComponentMeasurements[
     SelectComponents[
      vDilate@#, #AdjacentBorders == {Bottom, Top} &], {"BoundingBox",
       "CaliperWidth"}, "Dataset"] &;

dataSet2 /@ {img1, img2, img3, img4, img5, img6}

Where;

"D:\SHARGAM\167716 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) (61-65 C) 2 \ 4046 08-03-12 10-06-2020.jpg", "D:\SHARGAM\252929 6 SDGM Comp 2 \ HRDH B2 4200 rpm + TB) (75-80 C) 2 4048 09-16-57 11-06-2020.jpg", \ "D:\SHARGAM\232614 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) (70-75 C) 2 \ 4051 03-15-54 11-06-2020.jpg", "D:\SHARGAM\211778 6 SDGM Comp 2 \ HRDH B2 4200 rpm + TB) (70-75 C) 2 4051 21-05-53 10-06-2020.jpg", \ "D:\SHARGAM\192633 6 SDGM Comp 2 HRDH B2 4200 rpm + TB) (70-75 C) 2 \ 4052 15-25-53 10-06-2020.jpg", "D:\SHARGAM\172707 6 SDGM Comp 2 \ HRDH B2 4200 rpm + TB) (65-70 C) 2 4050 09-31-56 10-06-2020.jpg"

are the path of the images to be processed, they look like this;

enter image description here

It took around $3.5-4$ minutes for those $6$ images!


If I need to find the caliperwidth of $1000$ images, it may take $1000$ life-times!


You can see my previous problem, kglr helped a lot. Many thanks to kglr.


AGAIN, I am a new mathematica user. (2 days).


Any help/suggestion to modify the code, or correcting me on how to evaluate the outputs, would be really appreciated. THANKS!

$\endgroup$
  • $\begingroup$ Getting dataSet2 is the slow bit. Through profiling on a fresh kernel MaximalBy[#CaliperWidth &]@ ComponentMeasurements[ SelectComponents[ vDilate@#, #AdjacentBorders == {Bottom, Top} &], {"BoundingBox", "CaliperWidth"}, "Dataset"] &; is where most of your time's going. You could reduce the image resolution with ImageResize[img,512] if you're prepared to sacrifice some precision in the measurements. $\endgroup$ – flinty Jun 13 '20 at 18:02
  • $\begingroup$ Also you could evaluate dataSet2 /@ {img1, img2, img3, img4, img5, img6} in parallel if you have multiple cores with ParallelMap[dataSet2, {img1, img2, img3, img4, img5, img6}] but I don't know if the trial version will allow you to use the Parallel... functions. $\endgroup$ – flinty Jun 13 '20 at 18:12
  • $\begingroup$ @flinty Should I delete dataSet2? or Should I just add ImageResize[img1,512] , ImageResize[img2,512] , ImageResize[img3,512] , ... , ImageResize[img1000,512]? Because without deleting dataSet2, it took time too. Please dear, can you edit it and paste it as an answer? $\endgroup$ – Hussain-Alqatari Jun 13 '20 at 18:12
  • $\begingroup$ Where you Import the images, just change Import to ImageResize[Import[#],512]& $\endgroup$ – flinty Jun 13 '20 at 18:14
  • $\begingroup$ @flinty please dear, is it possible for you to copy the code from the post, edit it, and paste it as an answer? I am poor in mathematica because I am new. Thanks. $\endgroup$ – Hussain-Alqatari Jun 13 '20 at 18:21

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.