Skip navigation
Currently Being Moderated

Squaring up layered objects from photos?

Oct 1, 2011 9:23 PM

I frequently work with photos of display sample boards in our store that I take photos of and then edit in CS5 to delete the background, square up the image, match the color and lighting to the original, etc. I use the select tool to go around the board, then cut it and paste it into a layer, I then fill in the base layer with white (ctrl-delete).  I then use the ruler tool to rotate one side of the board in the image so that it is straight/horizontal. I then pull in guide lines to the vertical and horizontal sides and then I use the transform function with the ctrl key and the guides to get the rest of the edges vertical/horizontal and square.  Is there some automated way of getting the layered rectangle to square up on all corners? Hope this is making sense and thanks for any help or ideas.

 
Replies
  • JJMack
    5,969 posts
    Jan 9, 2006
    Currently Being Moderated
    Oct 2, 2011 3:01 PM   in reply to doane2u

    doane2u wrote:

     

    Is there some automated way of getting the layered rectangle to square up on all corners?

    There is a basic problem with the above question.  A layer can have any shape.  All layers bounds are square with respect to the canvas. You can transform a layers boundaries to canvas size however that will not change the layers shape other then distortion. A circle may become oval a cutout person may become fat, thin, tall or short etc.

     

     

    You need to use straiten. Or Rotate arbitrary

     
    |
    Mark as:
  • Noel Carboni
    23,455 posts
    Dec 23, 2006
    Currently Being Moderated
    Oct 2, 2011 6:01 PM   in reply to doane2u

    One way to achieve some accuracy (but without much automation) might be to use the ruler to "measure" what should be a horizontal line, then note the angle.  Now use Edit - Free Transform and type in the angle.

     

    -Noel

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 2, 2011 10:48 PM   in reply to JJMack

    If the OP would use the Pen Tool instead of whatever they mean by »select tool« it would be easy to get the fout points’ coordinates.

    But I’m afraid the math to calculate the necessary perspectival transformation to achieve a square from that is not clear to me, possibly someone over at the Photoshop Scripting Forum has an idea.

     
    |
    Mark as:
  • Noel Carboni
    23,455 posts
    Dec 23, 2006
    Currently Being Moderated
    Oct 3, 2011 12:19 AM   in reply to doane2u

    doane2u wrote:

     

    Hi, Noel: In CS5 you can use the ruler and make a line along one edge of an object and then click the straighten button to make it horizontal, but that doesn't fix the other three sides that may be off because of the distortions in the photo.

     

    Straighten turns your entire canvas, while Transform will turn an individual selected element, but am I to understand the more difficult problem you're trying to solve is that you're trying to "undistort" complex shapes?

     

    Where are these shapes coming from?

     

    Could you use a Perspective Crop on a complex shape in a separate document to get the job done?  Only thing is the Crop Tool set to Perspective tends to change aspect ratios if you move a lot of corners.

     

    -Noel

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2011 12:52 AM   in reply to Noel Carboni

    Dang, the Pespectival Crop could be Scripted rather neatly.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2011 1:34 AM   in reply to c.pfaffenbichler

    doane2u, if you want to give it a try, paste the following text into a new file in ExtendScript Toolkit (part of Photoshop’s installation, /Applications/Utilities/Adobe Utilities-CS5/ExtendScript Toolkit CS5) and save it as a jsx-file into Photoshop’s Presets/Scripts-folder.

    After restarting Photoshop the Script should be available under File > Scripts and can be assigned a Keyboard Shortcut directly, recorded into an Action, be used in a Configurator-Panel or started from ExtendScript Toolkit directly.

     

    Edit: The Script uses the last Path in the Paths Panel as basis for the crop, it needs to have only one subPathItems and that must have four points.

     

    // perspectival crop according to the last path’s four points;
    // 2011, use it at your own risk;
    #target photoshop;
    if (app.documents.length > 0 && app.activeDocument.pathItems.length > 0) {
    var myDocument = app.activeDocument;
    // set to pixels;
    var originalRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.POINTS;
    // get pathpoints;
    var thePath = myDocument.pathItems[myDocument.pathItems.length - 1];
    if (thePath.subPathItems.length == 1 && thePath.subPathItems[0].pathPoints.length == 4) {
    var point1 = thePath.subPathItems[0].pathPoints[0].anchor;
    var point2 = thePath.subPathItems[0].pathPoints[1].anchor;
    var point3 = thePath.subPathItems[0].pathPoints[2].anchor;
    var point4 = thePath.subPathItems[0].pathPoints[3].anchor;
    // duplicate;
    var theCopy = myDocument.duplicate("copy", true);
    // crop;
    perspectivalCrop (point1, point2, point3, point4);
    if (theCopy.width > theCopy.height) {var theSize = theCopy.height}
    else {var theSize = theCopy.width};
    theCopy.resizeImage(theSize, theSize);
    theCopy.layers[0].duplicate(myDocument, ElementPlacement.PLACEATBEGINNING);
    theCopy.close(SaveOptions.DONOTSAVECHANGES);
    }
    else {alert ("last path does not fit")};
    // reset;
    app.preferences.rulerUnits = originalRulerUnits;
    }
    else {alert ("please select a document with a four point path as the last one")};
    ////// crop perspectivally //////
    function perspectivalCrop (point1, point2, point3, point4) {
    // =======================================================
    var idCrop = charIDToTypeID( "Crop" );
        var desc3 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
            var desc4 = new ActionDescriptor();
            var idCntr = charIDToTypeID( "Cntr" );
                var desc5 = new ActionDescriptor();
                var idHrzn = charIDToTypeID( "Hrzn" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc5.putUnitDouble( idHrzn, idRlt, 240.000000 );
                var idVrtc = charIDToTypeID( "Vrtc" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc5.putUnitDouble( idVrtc, idRlt, 240.000000 );
            var idPnt = charIDToTypeID( "Pnt " );
            desc4.putObject( idCntr, idPnt, desc5 );
            var idquadCornerzero = stringIDToTypeID( "quadCorner0" );
                var desc6 = new ActionDescriptor();
                var idHrzn = charIDToTypeID( "Hrzn" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc6.putUnitDouble( idHrzn, idRlt, point1[0] );
                var idVrtc = charIDToTypeID( "Vrtc" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc6.putUnitDouble( idVrtc, idRlt, point1[1] );
            var idOfst = charIDToTypeID( "Ofst" );
            desc4.putObject( idquadCornerzero, idOfst, desc6 );
            var idquadCornerone = stringIDToTypeID( "quadCorner1" );
                var desc7 = new ActionDescriptor();
                var idHrzn = charIDToTypeID( "Hrzn" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc7.putUnitDouble( idHrzn, idRlt, point2[0] );
                var idVrtc = charIDToTypeID( "Vrtc" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc7.putUnitDouble( idVrtc, idRlt, point2[1] );
            var idOfst = charIDToTypeID( "Ofst" );
            desc4.putObject( idquadCornerone, idOfst, desc7 );
            var idquadCornertwo = stringIDToTypeID( "quadCorner2" );
                var desc8 = new ActionDescriptor();
                var idHrzn = charIDToTypeID( "Hrzn" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc8.putUnitDouble( idHrzn, idRlt, point3[0] );
                var idVrtc = charIDToTypeID( "Vrtc" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc8.putUnitDouble( idVrtc, idRlt, point3[1] );
            var idOfst = charIDToTypeID( "Ofst" );
            desc4.putObject( idquadCornertwo, idOfst, desc8 );
            var idquadCornerthree = stringIDToTypeID( "quadCorner3" );
                var desc9 = new ActionDescriptor();
                var idHrzn = charIDToTypeID( "Hrzn" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc9.putUnitDouble( idHrzn, idRlt, point4[0] );
                var idVrtc = charIDToTypeID( "Vrtc" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc9.putUnitDouble( idVrtc, idRlt, point4[1] );
            var idOfst = charIDToTypeID( "Ofst" );
            desc4.putObject( idquadCornerthree, idOfst, desc9 );
        var idquadrilateral = stringIDToTypeID( "quadrilateral" );
        desc3.putObject( idT, idquadrilateral, desc4 );
        var idWdth = charIDToTypeID( "Wdth" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc3.putUnitDouble( idWdth, idPxl, 0.000000 );
        var idHght = charIDToTypeID( "Hght" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc3.putUnitDouble( idHght, idPxl, 0.000000 );
        var idRslt = charIDToTypeID( "Rslt" );
        var idRsl = charIDToTypeID( "#Rsl" );
        desc3.putUnitDouble( idRslt, idRsl, 0.000000 );
    executeAction( idCrop, desc3, DialogModes.NO );
    };
    
     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2011 8:45 AM   in reply to doane2u

    »In CS5 I use the polygon lasso tool (L) to go around the edges of the panel, and I cut it out and paste it on a new layer.«

    Like I mentioned, if you want to use the Script you’ll need to use the Pen Tool instead and create a four-point-path – and the current Script creates a square, but one could include a dialog to enter the correct width/height-relation.

     
    |
    Mark as:
  • Noel Carboni
    23,455 posts
    Dec 23, 2006
    Currently Being Moderated
    Oct 3, 2011 8:53 AM   in reply to doane2u

    I believe I've already given you the answer.

     

    Instead of lassoing the image, try surrounding it with the Crop Tool, then selecting the [  ] Perspective option, then moving the corner points to the corners of the object.  Complete the crop and the edges will be squared.  Now select all and copy/paste.

     

    For example:

     

    1.  Starting, say, with this perspective-distorted image of a print on the wall:

     

    PrintOnWall.jpg

     

     

    2.  Crop as follows:

     

    PerspectiveCrop.jpg

     

     

    3.  Complete the crop and voila, it's squared-up:

     

    SquaredUp.jpg

     

     

    Only thing is that, as I mentioned above, if you move a lot of corners sometimes the Perspective Crop tool can leave the image a little too tall or too wide, and you might have to scale it in one dimension to make it look just right.

     

    -Noel

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2011 9:56 AM   in reply to doane2u

    Can't believe no one has said View > Show > Grid! It's a big help for this sort of thing.

     

    I find it easiest to use the Perspective thing in Free Transform while holding the Shift key, to constrain the movement to horizontal or vertical, first for one direction, then the other. If the vertical or horizontal scaling gwets thrown off, drag one of the middle handles.

     

    Noels-Pic.jpg

     
    |
    Mark as:
  • JJMack
    5,969 posts
    Jan 9, 2006
    Currently Being Moderated
    Oct 3, 2011 3:18 PM   in reply to Semaphoric

    IMO Noel suggestion is best. If your poto is always of a rectangle product board the perspective crop tool is what you want to use not perspective free transform grid or no grid. Yout photo could be way off square using the perspective crop tool would be easy and results would be good  I did a quick search for a high resolution sample image using google images. Found this image http://interaction.dundee.ac.uk/~mvanis/wordpress/wp-content/uploads/2 011/04/THIS-ONE.jpg  here are two screen captures in Photoshop Using Crop tool (best viewed in new tab or window)

    pct1.jpg

     

    Crop

    pct2.jpg

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 4, 2011 12:17 AM   in reply to doane2u

    It works but it creates a perfect square on a new layer. Is there some way of having it just square up the shape with out making it square with equal sides?

    Delete the lines

    if (theCopy.width > theCopy.height) {var theSize = theCopy.height}
    else {var theSize = theCopy.width};
    theCopy.resizeImage(theSize, theSize);
    

    or make them inactive by entering two slashes (//) at the start of each line.

     

    I work on a Mac, so I have no idea why you are not permitted from saving the jsx-file into the Scripts-Folder.

    You can also start it from ExtendScript Toolkit, by the way – just hit the Play-button icon in the Script’s window.

     

    Edit: But the Script actually offers no real advantage over Noel’s recommendation on the Crop Tool.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 4, 2011 1:08 AM   in reply to doane2u

    Do you have multiple versions of photoshop installed because the path for the cs5 should be:

    C:\Program Files (x86)\Adobe\Adobe Utilities - CS5\ExtendScript Toolkit CS5

     

     

    You probably need to right click on the ExtendScript Toolkit.exe and choose Run as Administrator in order to save the script to the photoshop cs5 scripts folder, then

    the script will be listed under File>Scripts.

     

     

    Untitled-1.png

     

    Also there should be a shortcut to the ExtendScript Toolkit.exe under the Start Menu>All Programs>Adobe and right click on that shortcut and choose Run As Administrator.

     

     

     

    MTSTUNER

     
    |
    Mark as:
  • JJMack
    5,969 posts
    Jan 9, 2006
    Currently Being Moderated
    Oct 4, 2011 4:09 AM   in reply to doane2u

    On Win 7 64bit system there are two versions of Photoshop installed Scripts cab ne installed any where on your system and run using browse.

     

    When creatinf the path for the perspective crop script be sure ro creath the path starting with the top left corner then top right bottom right bottom left back to top left. If not the result will seem strange. For example top left botton left bottom right top right then top left CCW. reseultccwpc.jpg

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points