1 2 Previous Next 65 Replies Latest reply on Oct 16, 2014 2:59 AM by Monstrik

    Selecting the brightest pixels

    Monstrik

      Hello

      Is it possible to write a script that finds brightest pixel or pixels (if there are more of them at the same brightness) in an active layer and selects them?

       

      if there is an easier way to do that, (without writing a script) please tell me how...

        • 1. Re: Selecting the brightest pixels
          c.pfaffenbichler Level 9

          How would you do it manually?

          • 2. Re: Selecting the brightest pixels
            Monstrik Level 1

            That is the problem, i dont really know. One possible solution, but very very slow, even if it was made as an action would be "blend if sliders". I would push the dark slider to the right until i have no visible pixels left. Then i would do ctrl+click on the layer to select transparency, and do inverse selection twice to see if the selection remains In tact. If it dissapears, then there were no pixels to select. Therefore i would push the slider back through one luminosity step and do the same again by inverting the selection twice.

            If the selection remains, then there is a pixel or more selected. Probably that is how i would know i got the brightest pixel or pixels. Or maybe i could push the white slider to the left and look for pixels dissapearing. Then again, click for transparency, inverse, inverse.

            But that is crazy. I need a quick way of doing it; just boom and there it is - brightest pixel/s selected.

             

            if my poor english is an obtacle to understand what i wrote here, please tell me, i would post screenshots.

            • 3. Re: Selecting the brightest pixels
              c.pfaffenbichler Level 9

              Loading and re-loading etc. the Transparency may be a bit time consuming. (I don’t understand what the double inversion is supposed to achieve.)

              I would recommend storing the luminance in a new file and using a Solid Color Layer set to Blend Mode Lighten (starting with 254/254/254) and check the Histogram.

              As long as all the pixels have the same value no lighter pixel can be underneath, so when the Histogram starts showing more than one spike that would be the indicator and the appropriately adjusted Channel can be used as a Layer Mask.

              • 4. Re: Selecting the brightest pixels
                Monstrik Level 1

                But can this be scripted, and work as quickly as loading any selection?

                • 5. Re: Selecting the brightest pixels
                  c.pfaffenbichler Level 9

                  I think it can be Scripted but will not be as fast as that by a long run.

                  Scripts are just like "smarter" Actions, they only utilise the same functionalities you can use manually, for more intricate needs proper plug-ins may be necessary.

                  • 6. Re: Selecting the brightest pixels
                    Monstrik Level 1

                    So i guess before i ask for any script i must come up with a way of doing it manually that works fast...

                     

                    How long would your offered method take, if it was scripted (Approx)? Is it like 10min, or 10 seconds?

                    • 7. Re: Selecting the brightest pixels
                      c.pfaffenbichler Level 9

                      Dang, the issue may be a lot simpler than I originally thought.

                      The Selection’s Histogram (in Quick Mask Mode) can be used too determine the first value that has more than zero pixels going back from 255, Image > Adjustments > Threshold can then be used to edit the Selection accordingly.

                      Of course this can result in a Selection of literally one pixel.

                      • 8. Re: Selecting the brightest pixels
                        Monstrik Level 1

                        Can you please give me more details on how to do that...for example im not sure what is selection's histogram... As i understand it is a histogram of a selected area? Am i right? even so, i dont understand how to do that.. Please explain

                        • 9. Re: Selecting the brightest pixels
                          c.pfaffenbichler Level 9

                          When you switch to Quick Mask Mode the Selection is stored as a Channel, use that Channel’s Histogram.

                          • 10. Re: Selecting the brightest pixels
                            Monstrik Level 1

                            I hate to be a nuisance but could you help me a little bit more. I still dont understand how to do this...can you explain step by step please... im not getting it... So for example i take a random image, what is the first thing i do?

                            • 11. Re: Selecting the brightest pixels
                              c.pfaffenbichler Level 9

                              Load the luminance, switch to Quick Mask Mode.

                              Then use something like this to determine the brightest value in the Histogram.

                              // check the histogram;

                              var theHist = activeDocument.channels[activeDocument.channels.length - 1].histogram;

                              var theCheck = false;

                              var theIndex = 255;

                              while (theCheck == false) {

                              var thePixels = theHist[theIndex];

                              // if the value has more than one pixels;

                              if (thePixels > 0) {var theCheck = true};

                              theIndex--

                              };

                              alert (theIndex);

                              Then use that with Threshold for example.

                              • 12. Re: Selecting the brightest pixels
                                Monstrik Level 1

                                Please im still stuck.. help.. please look at the screenshots... what am i doing wrong?

                                 

                                luminosity 5.jpg

                                • 13. Re: Selecting the brightest pixels
                                  c.pfaffenbichler Level 9

                                  You are trying to run the Script in ESTK, not Photoshop.

                                  • 14. Re: Selecting the brightest pixels
                                    c.pfaffenbichler Level 9

                                    I omitted the line

                                    #target photoshop

                                    because the code was only a fragment that you need to combine with the other steps anyway (the Selection, Quick Mask Mode, …).

                                    • 15. Re: Selecting the brightest pixels
                                      Monstrik Level 1

                                      Can you please tell me how to run the script in photoshop? . Sorry but i didnt know there is way to run scripts inside photoshop

                                      • 16. Re: Selecting the brightest pixels
                                        c.pfaffenbichler Level 9

                                        You can determine the target application with code ("#target photoshop") or in the Window.

                                        You can also put the jsx file in Photoshop’s Scripts Folder.

                                        estkApplicationSelection.jpg

                                        • 17. Re: Selecting the brightest pixels
                                          Monstrik Level 1

                                          i got it... It worked, but i've faced another problem.... If the image is darker than 128 i get this warning:

                                          No pixels are more than 50% selected

                                          And therefore photoshop doesnt place marching ants


                                          I need photoshop to select any pixel, doesnt matter if it is very bright or pretty dark.  So if i use a very dark image, it doesnt do the job... how could this be fixed?

                                          • 18. Re: Selecting the brightest pixels
                                            c.pfaffenbichler Level 9

                                            So use something other than loading the composite as a Selection and Quick Mask (which does raise the alert but does not affect the operation otherwise).

                                            Image > Calculations for example can be used to create a Channel (using Gray Channel for both Sources and Blending Normal).

                                            • 19. Re: Selecting the brightest pixels
                                              Monstrik Level 1

                                              Please tell me what i'm doing wrong... this time i've faced two problems.. one: the script doesnt place marching ants after i run the script? what did i do wrong? (please have a look at the screenshots). Second problem: it seems like it includes more values than the very brightest pixels or pixel...

                                              script doesnt work B.P 6.jpg

                                              • 20. Re: Selecting the brightest pixels
                                                c.pfaffenbichler Level 9

                                                Why so you use "Merged" and "Layer 0"?

                                                 

                                                I already mentioned that you need to use the value that the code I posted alerts you of in Threshold.

                                                You should create the code for that yourself.

                                                • 21. Re: Selecting the brightest pixels
                                                  c.pfaffenbichler Level 9

                                                  Also you need to add the code to load the Selection from that Channel (and then remove the Channel).

                                                  • 22. Re: Selecting the brightest pixels
                                                    Monstrik Level 1

                                                    I used layer 0 so i could use the blend if slider to check. it doesnt have to be merged or layer 0.


                                                    Would it be possible for you to write a script so it does include this threshold operation, please? Could you please make the script do it all? For example: i have an active layer, i click run the script and i get the marching ants selecting the brightest pixel or pixels?? Is it even possible?

                                                    • 23. Re: Selecting the brightest pixels
                                                      c.pfaffenbichler Level 9

                                                      If this was the first time I tried to help you it would be one thing, but as it is I think you should learn to do more of your Scripts yourself.

                                                      Use ScriptingListener.plugin to record Threshold and have that code take the intended value and apply it to the Channel.

                                                      • 24. Re: Selecting the brightest pixels
                                                        Monstrik Level 1

                                                        I guess you are right. Otherwise i will never learn... I will try

                                                         

                                                        But i believe it will take several months or more until i finish this script. Im a dummy when comes to photoshoping let alone scripting. I'm still learning. And i have learned a lot from this forum. So please gime me time and please help. Let me learn how to script to open a document first. You know how i felt when you said i should do it myself, i felt like Neo in Matrix entering the operator's room

                                                        image.jpg

                                                         

                                                        ok this is what i did... i created an aplha channel using calculations (as you said)... i ran the script you wrote.. and then i used threshold to record its code...do i paste that code into ESTK? And then how do i have this code take the intended value and apply it to the channel... To be honest I dont know what you mean by saying "apply to the channel". I dont even understand how would this have photoshop select brightest pixels?

                                                         

                                                        dont give up on me yet, please help...

                                                         

                                                         

                                                         

                                                        scripting plugin 8.jpg

                                                         

                                                        Edit: you said that i should use threshold to edit the selection the script creates, but the script doesnt create any selection...what i am doing wrong?

                                                        scripting plugin 9.jpg

                                                        • 25. Re: Selecting the brightest pixels
                                                          c.pfaffenbichler Level 9

                                                          I have a question is it possible to make this script use the value that It alerts me of in Threshold?

                                                          Of course.

                                                           

                                                          • You need to select the Channel.

                                                          In the DOM activeDocument.activeChannels can be used for this, but keep in mind that this needs an Array!

                                                          • Then you should put theIndex in the line

                                                          desc33.putInteger( idLvl, 12 );

                                                          instead of the originally recorded value.

                                                          • Then load the Channel as a Selection (check out the Class "Selection" in ESTK’s Object Model Viewer to see how one can load a channel as a Selection).

                                                          • after that you can remove the Channel

                                                          • 26. Re: Selecting the brightest pixels
                                                            c.pfaffenbichler Level 9

                                                            Some of the images you posted are inconvenient to me because of the loading time and because they are displayed downscaled in my browser – please post the screenshots individually and when you talk about code please post it, not just a screenshot of it.

                                                            • 27. Re: Selecting the brightest pixels
                                                              Monstrik Level 1

                                                              Hey Christoph, first of all thank you for helping me out...i appreciate your patience

                                                              im going to continue asking you questions

                                                               

                                                              as i understand you instructed me to select the channel after i run the script your wrote... but i dont know how to do that... i tried to use this object model viewer... but im not even sure how to use it.. as you see (image 1) i tried to find out how to select the channel, but i see no codes..? please guide me

                                                              In the DOM activeDocument.activeChannels can be used for this, but keep in mind that this needs an Array!

                                                              i really dont understand this sentence.. what is DOM? also i looked up ARRAY...[Definition: In math, an array refers to a set of numbers or objects that will follow a specific pattern. An array is an orderly arrangement, often in rows, columns or a matrix] why should i keep this in mind?


                                                              • Then you should put theIndex in the line

                                                               

                                                              you mean i should put this line at the end of this code?

                                                              instead of the originally recorded value.

                                                              I dont understand what is the originally recorded value?

                                                               

                                                              • Then load the Channel as a Selection (check out the Class "Selection" in ESTK’s Object Model Viewer to see how one can load a channel as a Selection)

                                                              Again i looked at the object model viewer.. not sure i understand what to look at? (image 2)

                                                               

                                                              1.jpg

                                                              2.jpg

                                                              • 28. Re: Selecting the brightest pixels
                                                                c.pfaffenbichler Level 9
                                                                i really dont understand this sentence.. what is DOM? also i looked up ARRAY

                                                                DOM stands for Document Object Model and is contrasted against Action Manager code (the one recorded with ScriptingListener.plugin).

                                                                An Array in JavaScript can be a collection of Numbers, Strings, Arrays, … and can be signified by square brackets.

                                                                So to create an Array of the last Channel

                                                                var theArray = [app.activeDocuments.channels[app.activeDocuments.channels.length - 1]];

                                                                 

                                                                I dont understand what is the originally recorded value?

                                                                The value you used in the Threshold operation when you recorded it.

                                                                 

                                                                Again i looked at the object model viewer.. not sure i understand what to look at? (image 2)

                                                                You had looked up

                                                                Selection.load

                                                                What arguments does this take?

                                                                A Channel, SelectionType and a boolean argument.

                                                                (Edit: In the brackets after "activeDocument.selection.load" you need to indicate the channel to load, how the Selection will be combined with a potentially existing Selection and whether it should be inverted.)

                                                                • 29. Re: Selecting the brightest pixels
                                                                  Monstrik Level 1

                                                                  Sorry im still lost... what is....

                                                                  an Array of the last Channel

                                                                  and please tell me what does this code do? and what i should to do with it?

                                                                  var theArray = [app.activeDocuments.channels[app.activeDocuments.channels.length - 1]];

                                                                  and still, please tell me where did you get this from?

                                                                  Selection.load

                                                                  and do i put this code into this one? like this?

                                                                  var theArray = [app.activeDocuments.Selection.load[app.activeDocuments.Selection.load.length - 1]];

                                                                  The only thing i think i understand is that i take this code from the last line of the script you wrote

                                                                  2.jpg

                                                                  and paste it instead of number 12?

                                                                  #target photoshop

                                                                  // check the histogram;

                                                                  var theHist = activeDocument.channels[activeDocument.channels.length - 1].histogram;

                                                                  var theCheck = false;

                                                                  var theIndex = 255;

                                                                  while (theCheck == false) {

                                                                  var thePixels = theHist[theIndex];

                                                                  // if the value has more than one pixels;

                                                                  if (thePixels > 0) {var theCheck = true};

                                                                  theIndex--

                                                                  };

                                                                  alert (theIndex);

                                                                  // =======================================================

                                                                  var idThrs = charIDToTypeID( "Thrs" );

                                                                      var desc33 = new ActionDescriptor();

                                                                      var idLvl = charIDToTypeID( "Lvl " );

                                                                      desc33.putInteger( idLvl, theIndex );

                                                                  executeAction( idThrs, desc33, DialogModes.NO );

                                                                  the script seems to be working.. and it seems it takes the threshold value and uses it to create a channel..which is great...

                                                                   

                                                                  here im not sure what i should do...

                                                                  (Edit: In the brackets after "activeDocument.selection.load" you need to indicate the channel to load, how the Selection will be combined with a potentially existing Selection and whether it should be inverted.)

                                                                  and where did you get this from?

                                                                  activeDocument.selection.load

                                                                  • 30. Re: Selecting the brightest pixels
                                                                    c.pfaffenbichler Level 9

                                                                    and what i should to do with it?

                                                                    You should use it to select that Channel.

                                                                    activeDocument.activeChannels = theArray;

                                                                     

                                                                    var theArray = [app.activeDocuments.Selection.load[app.activeDocuments.Selection.load.length - 1]];

                                                                    That’s nonsense, a method (like load) needs brackets, not square brackets and there is no need to define the Selection as an Array (or call it theArray).


                                                                    Selection does not exist on its own, it exists only with a document.

                                                                    So to load a Channel you need to reference the document:

                                                                    activeDocument.selection.load()

                                                                    And in the brackets note the Channel, the SelectionType and the boolean argument for inverting.

                                                                     

                                                                    If you do not understand the OMV yet I recommend you search for sample Scripts.

                                                                    For example a search on this Forum for "selection.load" pointed to this:

                                                                    Re: Is this possible? crop selection....?

                                                                    • 31. Re: Selecting the brightest pixels
                                                                      c.pfaffenbichler Level 9

                                                                      You should use it to select that Channel.

                                                                      By this I mean "select the Channel", not "load the Channel as a Selection".

                                                                      • 32. Re: Selecting the brightest pixels
                                                                        Monstrik Level 1

                                                                        First can you please tell me what im missing to achieve selection of the brightest pixel or pixels? because im getting confused

                                                                        now, I have this code...

                                                                        #target photoshop

                                                                        // check the histogram;

                                                                        var theHist = activeDocument.channels[activeDocument.channels.length - 1].histogram;

                                                                        var theCheck = false;

                                                                        var theIndex = 255;

                                                                        while (theCheck == false) {

                                                                        var thePixels = theHist[theIndex];

                                                                        // if the value has more than one pixels;

                                                                        if (thePixels > 0) {var theCheck = true};

                                                                        theIndex--

                                                                        };

                                                                        alert (theIndex);

                                                                        // =======================================================

                                                                        var idThrs = charIDToTypeID( "Thrs" );

                                                                            var desc33 = new ActionDescriptor();

                                                                            var idLvl = charIDToTypeID( "Lvl " );

                                                                            desc33.putInteger( idLvl, theIndex );

                                                                        executeAction( idThrs, desc33, DialogModes.NO );

                                                                        activeDocument.activeChannels = theArray;

                                                                        please tell me in order what's left to do?

                                                                        - To select the channel

                                                                        - Load the channel

                                                                        - ...

                                                                        • 33. Re: Selecting the brightest pixels
                                                                          c.pfaffenbichler Level 9

                                                                          You need to select the Channel to which you want to apply Threshold.

                                                                          • 34. Re: Selecting the brightest pixels
                                                                            c.pfaffenbichler Level 9

                                                                            And after applying Threshold you need to load the Channel as a Selection.

                                                                            • 35. Re: Selecting the brightest pixels
                                                                              Monstrik Level 1

                                                                              But i thought that this part of the code has already applied threshold.. no?

                                                                               

                                                                              // =======================================================

                                                                              var idThrs = charIDToTypeID( "Thrs" );

                                                                                  var desc33 = new ActionDescriptor();

                                                                                  var idLvl = charIDToTypeID( "Lvl " );

                                                                                  desc33.putInteger( idLvl, theIndex );

                                                                              executeAction( idThrs, desc33, DialogModes.NO );

                                                                              activeDocument.activeChannels = theArray;

                                                                              • 36. Re: Selecting the brightest pixels
                                                                                c.pfaffenbichler Level 9

                                                                                But to the active Channel/s.

                                                                                Is the last Channel already selected?

                                                                                • 37. Re: Selecting the brightest pixels
                                                                                  Monstrik Level 1

                                                                                  the last channel is selected after i run the script and also before i run the script, because i use calculations command.. so before i run this script

                                                                                  #target photoshop

                                                                                  // check the histogram;

                                                                                  var theHist = activeDocument.channels[activeDocument.channels.length - 1].histogram;

                                                                                  var theCheck = false;

                                                                                  var theIndex = 255;

                                                                                  while (theCheck == false) {

                                                                                  var thePixels = theHist[theIndex];

                                                                                  // if the value has more than one pixels;

                                                                                  if (thePixels > 0) {var theCheck = true};

                                                                                  theIndex--

                                                                                  };

                                                                                  // =======================================================

                                                                                  var idThrs = charIDToTypeID( "Thrs" );

                                                                                      var desc33 = new ActionDescriptor();

                                                                                      var idLvl = charIDToTypeID( "Lvl " );

                                                                                      desc33.putInteger( idLvl, theIndex );

                                                                                  executeAction( idThrs, desc33, DialogModes.NO );

                                                                                  i use calculations and get this

                                                                                  Untitled-1.jpg

                                                                                  then i run the script and get this

                                                                                  Untitled-2.jpg

                                                                                  Please tell me what's left to do? how would you select the brightest pixel/pixels?

                                                                                  • 38. Re: Selecting the brightest pixels
                                                                                    boo radley 2013 Level 3

                                                                                    why don't you just use a "levels" adjustment layer ontop of the layer you need to select the brightest pixels from?

                                                                                     

                                                                                    1. add levels adjustment layer ontop of the layer that you need to select the brightest pixels from

                                                                                     

                                                                                    2. in the levels panel, pull the middle slider far to the right, but not all the way

                                                                                     

                                                                                    3. pull the left slider just a bit to the left

                                                                                     

                                                                                    now your image should be black, except for the bright pixels. You can tweak this to your needs by slightly moving those sliders again

                                                                                     

                                                                                    4. merge the adjutsment layer with the layer below (your brightest pixel layer)

                                                                                     

                                                                                    5. using the magic wand, select anywhere that is dark (black pixels)

                                                                                     

                                                                                    6. invert your selection, and you now have the brightest pixles selected

                                                                                     

                                                                                     

                                                                                    I tried this on your "bubbles" image and it worked fine

                                                                                    • 39. Re: Selecting the brightest pixels
                                                                                      c.pfaffenbichler Level 9

                                                                                      Use

                                                                                      activeDocument.selection.load(/*insert the Channel here*/, SelectionType.REPLACE, false)

                                                                                      1 2 Previous Next