8 Replies Latest reply on Dec 31, 2011 1:11 AM by c.pfaffenbichler

    Automatic Detection of Scale Bar/Insert New Scale Bar

    olszta77

      Two quick questions.  I have never scripted much, but I have an idea which would help my workflow professionally. 

       

      The first questions is whether there are any professional services which I might be able to contract in order to have a script developed? 

       

      The second question is in regards to whether my scripting idea would even be possible in Photoshop. 

       

      I do research using electron microscopes, and as such in every micrograph I collect there is a scale bar.  Unfortunately, the scale bars are not aesthetically pleasing, and so I bring them into Photoshop.  Typically I will rescale, cut, paste, etc to use the images to tell the story about the data.  One of the things I always do is draw a line trace over the scale bar on the image (typically with my own font and applied stroke) and then adjust it (whether I want to double the scale, etc) to make it look more professional.  On all of my micrographs there is a black information bar with white lettering and a white scale bar (see attached).  Is there any way to write a script that would automatically read the region where the scale bar is located and then draw a new layer with a line the same size (with the line properties desired) using a Fourier transform or some other mathematical algorithm?  If that is possible, the next step would be to read the scale bar size and do a similar function with inserted text.

       

      Scale bar example.png

      Thanks,

      Matt

        • 1. Re: Automatic Detection of Scale Bar/Insert New Scale Bar
          c.pfaffenbichler Level 9

          The first questions is whether there are any professional services which I might be able to contract in order to have a script developed?

          As far as I know some of the regulars in this Forum offer their services professionaly but you could also ask over at

          http://ps-scripts.com/bb/

          • 2. Re: Automatic Detection of Scale Bar/Insert New Scale Bar
            c.pfaffenbichler Level 9

            Have been approached by anyone of the pros yet?

             

            If not it might be possible to get the size of the white rectangle from a Work Path.

            Are the images all grayscale and is the whole bar always the same size and position?

            • 3. Re: Automatic Detection of Scale Bar/Insert New Scale Bar
              olszta77 Level 1

              No, you are the only response I have gotten thusfar.

               

              I have never used Work Paths before, so I am not sure.

               

              The images are grayscale, and the scale bar is always in the same position,

              but not the same size (width wise).  I take images at many differetn

              magnifications, so each image could vary in the length of the scale bar.

              Hence, each time I want to make a custom scale bar and cut off that black

              banner at the bottom, I need to make a new layer by drawing a line matching

              the scale.

               

              Thanks

              Matt

              • 4. Re: Automatic Detection of Scale Bar/Insert New Scale Bar
                Michael L Hale Level 5

                Two things come to mind.

                 

                Some microscopes write the scale and other info to the image's metadata so you may not need to use the existing scale bar to determine the scale.

                 

                Creating a scale marker is a built-in feature of Photoshop Extended. You can find it under the Analysis menu. You will have to set the scale but Photoshop makes is easy because it makes the measure tool active while the dialog is open.

                • 5. Re: Automatic Detection of Scale Bar/Insert New Scale Bar
                  olszta77 Level 1

                  Michael,

                     Yes, most microscope companies do have information in the tiff file, but

                  the way our microscope works is that a separate text file is made with each

                  .tiff file.  It is the text file that contains all of the pertinent data

                  (such as scale bar length in pixels).  While I could write something to

                  utilize this, it would mean I would always have to know the location of the

                  text file.  Unfortunately, this probably wouldn't work well.

                   

                  I do like the function of inserting a scale bar, and I can see that being

                  useful.

                   

                  Thanks

                  Matt

                  • 6. Re: Automatic Detection of Scale Bar/Insert New Scale Bar
                    c.pfaffenbichler Level 9

                    If the bar only varies in width, this might help to create a layer of the size of the bar:

                    // 2011; use it at your own risk;
                    #target photoshop
                    if (app.documents.length > 0) {
                    var myDocument = app.activeDocument;
                    // set to pixels;
                    var originalRulerUnits = app.preferences.rulerUnits;
                    app.preferences.rulerUnits = Units.PIXELS;
                    //
                    myDocument.selection.load(myDocument.channels[0], SelectionType.REPLACE);
                    myDocument.selection.select([[0,960], [975,960], [975,992], [0,990]], SelectionType.INTERSECT, 0, false);
                    myDocument.selection.makeWorkPath(1);
                    var theBar = solidColorLayer(128,128,128);
                    theBar.name = "scalebar";
                    // reset;
                    app.preferences.rulerUnits = originalRulerUnits;
                    };
                    ////// function to create a solid color layer //////
                    function solidColorLayer (theRed, theGreen, theBlue) {
                    // =======================================================
                    var idMk = charIDToTypeID( "Mk  " );
                        var desc3 = new ActionDescriptor();
                        var idnull = charIDToTypeID( "null" );
                            var ref1 = new ActionReference();
                            var idcontentLayer = stringIDToTypeID( "contentLayer" );
                            ref1.putClass( idcontentLayer );
                        desc3.putReference( idnull, ref1 );
                        var idUsng = charIDToTypeID( "Usng" );
                            var desc4 = new ActionDescriptor();
                            var idType = charIDToTypeID( "Type" );
                                var desc5 = new ActionDescriptor();
                                var idClr = charIDToTypeID( "Clr " );
                                    var desc6 = new ActionDescriptor();
                                    var idRd = charIDToTypeID( "Rd  " );
                                    desc6.putDouble( idRd, theRed );
                                    var idGrn = charIDToTypeID( "Grn " );
                                    desc6.putDouble( idGrn,theGreen );
                                    var idBl = charIDToTypeID( "Bl  " );
                                    desc6.putDouble( idBl, theBlue );
                                var idRGBC = charIDToTypeID( "RGBC" );
                                desc5.putObject( idClr, idRGBC, desc6 );
                            var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
                            desc4.putObject( idType, idsolidColorLayer, desc5 );
                        var idcontentLayer = stringIDToTypeID( "contentLayer" );
                        desc3.putObject( idUsng, idcontentLayer, desc4 );
                    executeAction( idMk, desc3, DialogModes.NO );
                    return app.activeDocument.activeLayer
                    };
                    
                    • 7. Re: Automatic Detection of Scale Bar/Insert New Scale Bar
                      olszta77 Level 1

                      OK, I was reading through the script you wrote, and I was curious how it

                      detects the exsiting scale bar?  You are correct in that the length is the

                      only thing that changes.  The width stays the same.

                       

                      Thanks

                      Matt

                      • 8. Re: Automatic Detection of Scale Bar/Insert New Scale Bar
                        c.pfaffenbichler Level 9

                        The Script loads channels[0] as a selection (that’s the gray channel in a grayscale image) and intersects it with a rectangular selection as set in this line:

                        myDocument.selection.select([[0,960], [975,960], [975,992], [0,990]], SelectionType.INTERSECT, 0, false); 

                        If the whole thing at the bottom is always in the same position that should cover the white rectangle as it stretches from the left edge to a couple of pixels before the text.

                        So there is actually no real detection going on and it works only if the scale bar is at the expected position.

                         

                        The resulting selection is then used to create a work path and this to create Solid Color Layer with a Vector Mask.