Skip to main content
added 1203 characters in body
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327

Getting the image using copy and paste also provides the image with options that differ by version. In 11.3 the options are {ColorSpace -> "RGB", Interleaving -> True}, whereas in 13.3.1 the options are {ColorSpace -> "RGB", ImageResolution -> {96., 96.}, ImageSize -> {353.438, Automatic}, Interleaving -> True}. This and the added alpha channel causes problems with the ImageTrim function and everything following thatit. It explains the issue Kvothe reported in the comments below.

Getting the image using copy and paste also provides the image with options that differ by version. In 11.3 the options are {ColorSpace -> "RGB", Interleaving -> True}, whereas in 13.3.1 the options are {ColorSpace -> "RGB", ImageResolution -> {96., 96.}, ImageSize -> {353.438, Automatic}, Interleaving -> True}. This causes problems with the ImageTrim function and everything following that. It explains the issue Kvothe reported in the comments below.

Getting the image using copy and paste also provides the image with options that differ by version. In 11.3 the options are {ColorSpace -> "RGB", Interleaving -> True}, whereas in 13.3.1 the options are {ColorSpace -> "RGB", ImageResolution -> {96., 96.}, ImageSize -> {353.438, Automatic}, Interleaving -> True}. This and the added alpha channel causes problems with the ImageTrim function and everything following it. It explains the issue Kvothe reported in the comments below.

added 1203 characters in body
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327

Another update

Not sure when this was introduced, but copying the demo figure provided above from your browser window to Mathematica using copy and paste results in each pixel being defined by 4 values instead of usual three RGB values. I suppose the fourth is an alpha channel used for opacity. This is not the case if you Import using the image link.

Getting the image using copy and paste also provides the image with options that differ by version. In 11.3 the options are {ColorSpace -> "RGB", Interleaving -> True}, whereas in 13.3.1 the options are {ColorSpace -> "RGB", ImageResolution -> {96., 96.}, ImageSize -> {353.438, Automatic}, Interleaving -> True}. This causes problems with the ImageTrim function and everything following that. It explains the issue Kvothe reported in the comments below.

The workaround is either importing the image using its link or getting rid of the alpha layer and the ImageResolution option using something like:

im2 = Image[ImageData[im][[All, All, ;; 3]], 
            {ColorSpace -> "RGB", Interleaving -> True}
      ]

where im is assigned to the copy-and-pasted figure.

For me, this makes it work again.

Another update

Not sure when this was introduced, but copying the demo figure provided above from your browser window to Mathematica using copy and paste results in each pixel being defined by 4 values instead of usual three RGB values. I suppose the fourth is an alpha channel used for opacity. This is not the case if you Import using the image link.

Getting the image using copy and paste also provides the image with options that differ by version. In 11.3 the options are {ColorSpace -> "RGB", Interleaving -> True}, whereas in 13.3.1 the options are {ColorSpace -> "RGB", ImageResolution -> {96., 96.}, ImageSize -> {353.438, Automatic}, Interleaving -> True}. This causes problems with the ImageTrim function and everything following that. It explains the issue Kvothe reported in the comments below.

The workaround is either importing the image using its link or getting rid of the alpha layer and the ImageResolution option using something like:

im2 = Image[ImageData[im][[All, All, ;; 3]], 
            {ColorSpace -> "RGB", Interleaving -> True}
      ]

where im is assigned to the copy-and-pasted figure.

For me, this makes it work again.

added 659 characters in body
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327

UPDATE: Added a few workarounds for bugs / features introduced in Mathematica v10 or later.

Main issue involved LocatorPane whose Appearance option no longer correctly handles more than one Graphics object [CASE:4984027]. This bug was introduced somewhere after v11.3 and is present in v.13.1. Additional issues concerned the scaling of the graphics used in the various panels, which was incorrect. Perhaps a different default was introduced somewhere after v9 where the code worked as intended.



Original text

findMarkers[img_, pt_, thres_, minArea_] :=
  ComponentMeasurements[
    MorphologicalComponents[
     ColorNegate[
      Binarize[
       ImageCorrelate[
        imgImageCorrelate[img,
        ImageTrim[img, pt],
        NormalizedSquaredEuclideanDistance
        ], thresthres]
       ]
      ],
     ], {"Centroid", "Area"},
    #2 > minArea &
    ][[All, 2, 1]];

SetAttributes[getMarkers, HoldRest];
getMarkers[img_, resMarkers_] := 
 DynamicModule[
   {
    pt = {ImageDimensions[img]/4, ImageDimensions[img]/2},
    axisDefinePane, defineMarkerPane, findMarkerPane, editMarkersPane,
   
   finalResultPane, xAxisBegin, xAxisEnd, yAxisBegin, yAxisEnd, 
    myMarkers, myTransform, 
    xoy = {{1/2, 1/8} ImageDimensions[img], 
           {1/8, 1/8} ImageDimensions[img], ImageDimensions[
          img], {1/8, 1/2} ImageDimensions[img]}},
  axisDefinePane },=
   Grid[
  axisDefinePane = {
   Grid[{  {
      LocatorPane[
       Dynamic[xoy],
       Dynamic[
        Show[
         img,
         Graphics[{Line[xoy]}],
         ImageSize -> ImageDimensions[img]
         ]
        ],
       Appearance -> 
 {Graphics[{Red, AbsolutePointSize[5],      {
         Graphics[{AbsolutePointSize[5], Red, Point[{0, 0}]}], 
         Graphics[{GreenAbsolutePointSize[5], AbsolutePointSize[5]Green, Point[{0, 0}]}], 
         Graphics[{BlueAbsolutePointSize[5], AbsolutePointSize[5]Blue, Point[{0, 0}]}]
         }
       ]
      },
     {Row[{"x(1): ", 
        InputField[Dynamic[xAxisBegin], Number, FieldSize -> Tiny], 
        " x(2): ", 
        InputField[Dynamic[xAxisEnd], Number, 
         FieldSize -> Tiny]}]},
     {Row[{"y(1): ", 
        InputField[Dynamic[yAxisBegin], Number, FieldSize -> Tiny], 
        " y(2): ", 
        InputField[Dynamic[yAxisEnd], Number, FieldSize 
 -> Tiny]}]}
     }
  FieldSize -> Tiny]}]}}];
  
  defineMarkerPane =
   LocatorPane[
    Dynamic[pt],
    Dynamic[
     Show[
      img,
      Graphics[
       {
        EdgeForm[Black], FaceForm[], Rectangle @@ pt
        }],
      ImageSize ]-> ImageDimensions[img]
      ]
     ], 
     Appearance -> Graphics[{Red, AbsolutePointSize[5], Point[{0Style["\[FilledSmallCircle]", 0}]}]Red]
    ];
  
  findMarkerPane =
   Manipulate[
     Show[
       img, 
       Graphics[{Red, 
       Circle[#, 5] & /@ (myMarkers = findMarkers[img, pt, t, 1.05])}],
     ImageSize -> ImageDimensions[img]
     ],
     {{t, 0.2, "Threshold"}, 0, 1},
     TrackedSymbols -> {t},
     ControlPlacement -> Bottom
    ];
  
  editMarkersPane =
   LocatorPane[Dynamic[LocatorPane[
 myMarkers]   Dynamic[myMarkers],
    img, 
     Appearance -> 
     Graphics[{Red, Circle[{0, 0}, 1]}, ImageSize -> 10],
     LocatorAutoCreate -> True
    ];
  
  finalResultPane = 
   Dynamic[myTransform = Dynamic[
     FindGeometricTransform[
 myTransform =  
     FindGeometricTransform[{{xAxisEnd, yAxisBegin}, {xAxisBegin, yAxisBegin}, 
          yAxisBegin}, {xAxisBegin, yAxisEnd}
        }, xoy
     ][[2]]xoy][[2]] // Quiet; 
    ListLinePlot[resMarkers = myTransform /@ Sort[myMarkers],
      
     Frame -> True, Mesh -> All]All, ImageSize -> ImageDimensions[img]],
    TrackedSymbols -> {myMarkers, xoy, xAxisEnd, yAxisBegin, 
      xAxisBegin, yAxisBegin, xAxisBegin, yAxisEnd}
    ];
  
  TabView[
    {
     "Define axes" -> axisDefinePane, 
     "Define marker" -> defineMarkerPane, 
     "Find Markers" -> findMarkerPane, 
     "Edit Markers" -> editMarkersPane, 
     "Results" -> finalResultPane
    }
   ]
  ]
findMarkers[img_, pt_, thres_, minArea_] :=
  ComponentMeasurements[
    MorphologicalComponents[
     ColorNegate[
      Binarize[
       ImageCorrelate[
        img,
        ImageTrim[img, pt],
        NormalizedSquaredEuclideanDistance
        ], thres
       ]
      ]
     ], {"Centroid", "Area"}, #2 > minArea &
    ][[All, 2, 1]];

SetAttributes[getMarkers, HoldRest];
getMarkers[img_, resMarkers_] := 
 DynamicModule[
   {
    pt = {ImageDimensions[img]/4, ImageDimensions[img]/2},
    axisDefinePane, defineMarkerPane, findMarkerPane, editMarkersPane,
     finalResultPane, xAxisBegin, xAxisEnd, yAxisBegin, yAxisEnd, 
    myMarkers, myTransform, 
    xoy = {{1/2, 1/8} ImageDimensions[img], 
           {1/8, 1/8} ImageDimensions[img], 
           {1/8, 1/2} ImageDimensions[img]}
   },
  
  axisDefinePane =
   Grid[{{
      LocatorPane[
       Dynamic[xoy],
       Dynamic[
        Show[
         img,
         Graphics[{Line[xoy]}]
         ]
        ],
       Appearance -> {Graphics[{Red, AbsolutePointSize[5], 
           Point[{0, 0}]}], 
         Graphics[{Green, AbsolutePointSize[5], Point[{0, 0}]}], 
         Graphics[{Blue, AbsolutePointSize[5], Point[{0, 0}]}]}
       ]},
     {Row[{"x(1): ", 
        InputField[Dynamic[xAxisBegin], Number, FieldSize -> Tiny], 
        " x(2): ", 
        InputField[Dynamic[xAxisEnd], Number, FieldSize -> Tiny]}]},
     {Row[{"y(1): ", 
        InputField[Dynamic[yAxisBegin], Number, FieldSize -> Tiny], 
        " y(2): ", 
        InputField[Dynamic[yAxisEnd], Number, FieldSize -> Tiny]}]}
     }
    ];
  
  defineMarkerPane =
   LocatorPane[
    Dynamic[pt],
    Dynamic[
     Show[
      img,
      Graphics[
       {
        EdgeForm[Black], FaceForm[], Rectangle @@ pt
        }
       ]
      ]
     ], 
     Appearance -> Graphics[{Red, AbsolutePointSize[5], Point[{0, 0}]}]
    ];
  
  findMarkerPane =
   Manipulate[
     Show[
       img, 
       Graphics[{Red,Circle[#, 5] & /@ (myMarkers = findMarkers[img, pt, t, 1.05])}]
     ],
     {{t, 0.2, "Threshold"}, 0, 1},
     TrackedSymbols -> {t},
     ControlPlacement -> Bottom
  ];
  
  editMarkersPane =
   LocatorPane[Dynamic[ myMarkers], img, 
     Appearance -> Graphics[{Red, Circle[{0, 0}, 1]}, ImageSize -> 10],
     LocatorAutoCreate -> True
   ];
  
  finalResultPane = 
   Dynamic[myTransform = 
     FindGeometricTransform[
        {{xAxisEnd, yAxisBegin}, {xAxisBegin, yAxisBegin}, 
         {xAxisBegin, yAxisEnd}
        }, xoy
     ][[2]] // Quiet; 
    ListLinePlot[resMarkers = myTransform /@ Sort[myMarkers],
          Frame -> True, Mesh -> All], 
    TrackedSymbols -> {myMarkers, xoy, xAxisEnd, yAxisBegin, 
      xAxisBegin, yAxisBegin, xAxisBegin, yAxisEnd}];
  
  TabView[
    {
     "Define axes" -> axisDefinePane, 
     "Define marker" -> defineMarkerPane, 
     "Find Markers" -> findMarkerPane, 
     "Edit Markers" -> editMarkersPane, 
     "Results" -> finalResultPane
    }
  ]
]

UPDATE: Added a few workarounds for bugs / features introduced in Mathematica v10 or later.

Main issue involved LocatorPane whose Appearance option no longer correctly handles more than one Graphics object [CASE:4984027]. This bug was introduced somewhere after v11.3 and is present in v.13.1. Additional issues concerned the scaling of the graphics used in the various panels, which was incorrect. Perhaps a different default was introduced somewhere after v9 where the code worked as intended.



Original text

findMarkers[img_, pt_, thres_, minArea_] :=
  ComponentMeasurements[
    MorphologicalComponents[
     ColorNegate[
      Binarize[
       ImageCorrelate[img, ImageTrim[img, pt],
        NormalizedSquaredEuclideanDistance
        ], thres]
      ]
     ],
    {"Centroid", "Area"},
    #2 > minArea &
    ][[All, 2, 1]];

SetAttributes[getMarkers, HoldRest];
getMarkers[img_, resMarkers_] :=
 DynamicModule[
  {
   pt = {ImageDimensions[img]/4, ImageDimensions[img]/2},
   axisDefinePane, defineMarkerPane, findMarkerPane, editMarkersPane,  
   finalResultPane, xAxisBegin, xAxisEnd, yAxisBegin, yAxisEnd, 
   myMarkers, myTransform, 
   xoy = {{1/2, 1/8} ImageDimensions[img], {1/8, 1/8} ImageDimensions[
       img], {1/8, 1/2} ImageDimensions[img]}},
  axisDefinePane =
   Grid[
    {
     {
      LocatorPane[
       Dynamic[xoy],
       Dynamic[
        Show[
         img,
         Graphics[{Line[xoy]}],
         ImageSize -> ImageDimensions[img]
         ]
        ],
       Appearance -> 
        {
         Graphics[{AbsolutePointSize[5], Red, Point[{0, 0}]}],
         Graphics[{AbsolutePointSize[5], Green, Point[{0, 0}]}],
         Graphics[{AbsolutePointSize[5], Blue, Point[{0, 0}]}]
         }
       ]
      },
     {Row[{"x(1): ", 
        InputField[Dynamic[xAxisBegin], Number, FieldSize -> Tiny], 
        " x(2): ", 
        InputField[Dynamic[xAxisEnd], Number, 
         FieldSize -> Tiny]}]}, {Row[{"y(1): ", 
        InputField[Dynamic[yAxisBegin], Number, FieldSize -> Tiny], 
        " y(2): ", 
        InputField[Dynamic[yAxisEnd], Number,  
         FieldSize -> Tiny]}]}}];
  
  defineMarkerPane =
   LocatorPane[
    Dynamic[pt],
    Dynamic[
     Show[
      img,
      Graphics[{EdgeForm[Black], FaceForm[], Rectangle @@ pt}],
      ImageSize -> ImageDimensions[img]
      ]
     ],
    Appearance -> Style["\[FilledSmallCircle]", Red]
    ];
  
  findMarkerPane =
   Manipulate[
    Show[
     img,
     Graphics[{Red, 
       Circle[#, 5] & /@ (myMarkers = findMarkers[img, pt, t, 1.05])}],
     ImageSize -> ImageDimensions[img]
     ],
    {{t, 0.2, "Threshold"}, 0, 1},
    TrackedSymbols -> {t},
    ControlPlacement -> Bottom
    ];
  
  editMarkersPane =
   LocatorPane[
    Dynamic[myMarkers],
    img,
    Appearance -> 
     Graphics[{Red, Circle[{0, 0}, 1]}, ImageSize -> 10],
    LocatorAutoCreate -> True
    ];
  
  finalResultPane =
   Dynamic[
    myTransform =  
     FindGeometricTransform[{{xAxisEnd, yAxisBegin}, {xAxisBegin, 
          yAxisBegin}, {xAxisBegin, yAxisEnd}}, xoy][[2]] // Quiet;
    ListLinePlot[resMarkers = myTransform /@ Sort[myMarkers],  
     Frame -> True, Mesh -> All, ImageSize -> ImageDimensions[img]],
    TrackedSymbols -> {myMarkers, xoy, xAxisEnd, yAxisBegin, 
      xAxisBegin, yAxisBegin, xAxisBegin, yAxisEnd}
    ];
  
  TabView[
   {
    "Define axes" -> axisDefinePane,
    "Define marker" -> defineMarkerPane,
    "Find Markers" -> findMarkerPane,
    "Edit Markers" -> editMarkersPane,
    "Results" -> finalResultPane
    }
   ]
  ]
added 60 characters in body
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327
Loading
Bounty Ended with 50 reputation awarded by bobthechemist
added 152 characters in body
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327
Loading
added 290 characters in body
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327
Loading
added 119 characters in body
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327
Loading
added 5070 characters in body
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327
Loading
Source Link
Sjoerd C. de Vries
  • 66.1k
  • 15
  • 189
  • 327
Loading