• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

pattern recognition in Photoshop?

New Here ,
Jun 09, 2009 Jun 09, 2009

Copy link to clipboard

Copied

Hello All,

I have a challenge facing me and would like to pose a question to this board. (If I've posted it to the wrong board, please excuse me)

To what extent can I use scripting with Photoshop to perform automated pattern recognition?

Here's my Issue: As part of a manufacturing process, I need to measure two distinct, but concentric, roughly circular areas from an image of the cross section of a circular wire.

Area1: Inner core; identified as a solid Grey filled circle.  Small diameter
Area2: Interior; composed of the inner core and other "stuff" (usually many small dark Gray colored hexagonal shapes surrounded by gold). Larger diameter

Both of these areas are concentric and surrounded by a thin outer gold layer.  

I need to calculate the percent area of the inner core which equals (Area1/Area2)*100

I am looking for a script (or guidance on how to approach creating one) that would automatically find the edges of the inner core area and the Interior Area and output these values to a file. I need it to be done in as little user input as possible. (Ex: One click to select Area1, One-Two clicks to adjust accuracy of the selection, One click to select Area2, One-Two clicks to adjust accuracy of the selection, one click to save)

Dificulties:
1) The edges of the circles are not always well defined. Grey color bleeds through to the gold outer barrier due to lighting inconsistencies. (But the edge is intact to about 90% of the circumference ususally)
2) Finding the edges needs to be fairly accurate. At least as accurate as visually tracing them with a pen input device. Therefore Smoothing the selection after using magic wand often looses accuracy (due to #1)

I can get the edges sharp with levels, contrast, etc... Using Magic wand is close, but if the tolerance is too small I get better edges, but It grabs too much in the center of the circle. If the tolerance is too large, I loose the edge accuracy. Oh, and I know (roughly) the diameter of each circle before hand (so I guess I can automate trimming the image to my target areas)

Any suggestions? Do I have to look into specialized pattern recognition software? I hope someone will be able to help me approach this challenge.

Thank you in advance.

Sincerely,

Sean

TOPICS
Actions and scripting

Views

6.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Advisor ,
Jun 10, 2009 Jun 10, 2009

Copy link to clipboard

Copied

It's just about impossible to say with any certainty whether it can be done with scripting in PS without a set of test images available.

Some of us have written scripts that do something similar, but whether or not they would have the precision that you apparently require

is difficult to tell.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 10, 2009 Jun 10, 2009

Copy link to clipboard

Copied

Xbytor2,

Your are right, I should have posted a picture

Here is a similar image I grabbed of the net that shows what I'm trying to do.  The inner core is has an obstruction at the edge that I need photoshop to "intelligently" avoid when using the magic wand.  After using the magic wand, I've tried to smooth the selection to get the selection to follow the hidden edge, but I have to apply so much smoothing that the other sections of the selection that correctly follow the edge move off the edge too much.

Like I said, probably 90+% of the time the edges are easily selectable with the magic wand.  I just don't know how to account for cases like the example I've attached.

Also, In order to automate the removal of undesired selections in the middle of the inner core, is there any way to tell photoshop that when I use the magic wand, to automatically remove any "islands" of selections that are contained within a selection. (Assuming the "island" of selections are smal, I guess I could smooth a little bit after selecting.)

Thanks for your input.

-Sean

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 10, 2009 Jun 10, 2009

Copy link to clipboard

Copied

There is probably a way to remove the isolated islands. Depending on how you are wanding, it may be a simple case of making sure that the 'Contiguous' box is checked.

A technique for removing the shavings on the edge, however, is not quite as simple. I'll let you know if I think of something.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 10, 2009 Jun 10, 2009

Copy link to clipboard

Copied

Thank you for the reply,

The best I could do with the "Islands" is to use "Smooth" after the magic

wand with "Contiguous" checked. (Unchecked, it grabbs areas out side the

circle. No good)

Smoothing by 1-5 pixels seems to get rid of most of the "Islands".

To remove the shavings, can you tell Photoshop to keep a certain "stiffness"

to the selection (magic wand)? Or, Apply a "stiffness" after using the

magic wand? The "stiffness" would limit the selection path from changing

direction too quickly/sharply. Maybe you would have to turn the selection

into a path, then apply this "stiffness" to the path, then convert back to a

selection?

Thanks for thinking about this.

-Sean

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

The best I could do with the "Islands" is to use "Smooth" after the magic

wand with "Contiguous" checked.  (Unchecked, it grabbs areas out side the

circle.  No good)

Expanding and contracting the selection may also be useful.

Maybe you would have to turn the selection

into a path, then apply this "stiffness" to the path, then convert back to a

selection?

This would work. One problem, though, is getting a good/precise conversion from the selection bounds to the path.

After you've seen a point on the path that is too far out of alignment with the previous two points, skip it and check the next point on the path.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

xbytor2 wrote:


Expanding and contracting the selection may also be useful.


Yes, I plan to use the expanding/contracting selection as the fine user control (the extra one-two clicks I mentioned in the original post).  But, only after the script figured out how to find the true edge.

This would work. One problem, though, is getting a good/precise conversion from the selection bounds to the path

After you've seen a point on the path that is too far out of alignment with the previous two points, skip it and check the next point on the path.


Hmm.. I'm a bit confused.  I thought that when you convert a selection to a path, the new path follows the exact location of the selection. (Therefore conversion from the selection bounds to the path should be pixel exact)   What I don't know how to do (if it's even possible) is program Photoshop using scripts to alter each and every pixel on the path using an algorithm similar to what you described.

BTW, when you ".. skip it and check the next point on the path" what path are we talking about (we just rejected a test point on the current path because it was out of alignment).  The new path hasn't even been defined yet.  Even if you used the next point on a tangent to the previous two points as the new path, what's to say that that tangent will meet up with the correct portion of the path?

NetGeek888, has an interesting idea (see my commet to his post)

-Sean

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied


Hmm.. I'm a bit confused.  I thought that when you convert a selection to a path, the new path follows the exact location of the selection. (Therefore conversion from the selection bounds to the path should be pixel exact)

Nope. Wand some part of a photograph and zoom in to %1600. Then make the selection a workpath with various tolerance levels. At 0.5 you get a very good approximation, but converting between bitmap graphics and vector graphics will never be pixel perfect.

What I don't know how to do (if it's even possible) is program Photoshop using scripts to alter each and every pixel on the path using an algorithm similar to what you described.

You can use the Path APIs in PS to describe as many points on a path as you need.

BTW, when you ".. skip it and check the next point on the path" what path are we talking about (we just rejected a test point on the current path because it was out of alignment).  The new path hasn't even been defined yet.  Even if you used the next point on a tangent to the previous two points as the new path, what's to say that that tangent will meet up with the correct portion of the path?

A better way of looking at the this is that you traverse the path. If the next point is way out of whack, delete it and check the next point. If the border defects aren't too large, you eventually find a point that is near enough that you can discard/delete the intermediate points, though you do end up with a small flat part on the circle.

NetGeek888, has an interesting idea (see my commet to his post)

If you are going to have a user tweaking the selection, I'd take the bounds of the initial wand selection, as he suggests, create a circle in those bounds, and let the user adjust the circle's size and location.

You probably need to start coding some of these ideas up and see what's reasonable for your workflow.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

How about this approach...

Using a selection tool (probably Magic Wand), select the inner core. This should get most of the inner circle except for the bright spots. This selection would produce a bounding box around the inner circle. If your shape is indeed a circle, this bounding box should be square. From there, you can derive the radius of the circle, and hence the area. The same approach can be used to find the area of the outer circle.

Minor imperfections of the circle edge (like that shown in your picture) won't affect your area calculation.

Does this give you sufficient accuracy for your pattern recognition task? There might also be a way to impose further constraints using the expected circle diameter information that you have.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

Using a selection tool (probably Magic Wand), select the inner core. This should get most of the inner circle except for the bright spots. This selection would produce a bounding box around the inner circle. If your shape is indeed a circle, this bounding box should be square. From there, you can derive the radius of the circle, and hence the area. The same approach can be used to find the area of the outer circle.

Minor imperfections of the circle edge (like that shown in your picture) won't affect your area calculation.

Does this give you sufficient accuracy for your pattern recognition task? There might also be a way to impose further constraints using the expected circle diameter information that you have.

I like this approach and think it's worth a try (For some extremely odd ball shapes, it won't work. But I believe my shapes are fairly circular.  I've got lots of pictures to test it out on).

But how do I get access to the positional data (XY coordinates/location of each and every pixel in the selection)?  Does Photoshop export this data?  Can I call it from a script?   I've never written a Photoshop script before.  Can you point me in the right direction?

Thank you in advance.

-Sean

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

Using you sample image and NetGeek888's suggestion I made a selection using the quick select tool and ran this bit of code

ellipseSelection = function( b) {
     function cTID(s){return charIDToTypeID(s);}
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putProperty( cTID('Chnl'), cTID('fsel') );
    desc.putReference( cTID('null'), ref );
        var desc1 = new ActionDescriptor();
        desc1.putUnitDouble( cTID('Top '), cTID('#Pxl'), b[1].as('px') );
        desc1.putUnitDouble( cTID('Left'), cTID('#Pxl'), b[0].as('px') );
        desc1.putUnitDouble( cTID('Btom'), cTID('#Pxl'), b[3].as('px')  );
        desc1.putUnitDouble( cTID('Rght'), cTID('#Pxl'), b[2].as('px')  );
    desc.putObject( cTID('T   '), cTID('Elps'), desc1 );
    desc.putBoolean( cTID('AntA'), true );
    executeAction( cTID('setd'), desc, DialogModes.NO );
};
ellipseSelection( activeDocument.selection.bounds );

It seems to work well with the sample image and you could refine or transform the selection for an even closer match.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

Thank you very much,

I'm going to have to start reading up on how to use scripts now that I see

it probably can be done in Photoshop. But I appreciate the head start you

provided.

Sincerely,

-Sean

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

I like this approach and think it's worth a try (For some extremely odd ball

shapes, it won't work. But I believe my shapes are fairly circular. I've

got lots of pictures to test it out on).

But how do I get access to the positional data (XY coordinates/location of

each and every pixel in the selection)? Does Photoshop export this data?

Can I call it from a script? I've never written a Photoshop script

before. Can you point me in the right direction?

Thank you in advance.

-Sean

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

But how do I get access to the positional data (XY coordinates/location of

each and every pixel in the selection)?

You don't need to know about each pixel, just the bounds of the selection.

E.g.

var doc = app.activeDocument;

alert(doc.selection.bounds);

There are pdf docs that come with PS on scripting (in general) and a (mostly) complete JS API reference.

ps-scripts.com has a lot of stuff as well.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 15, 2009 Jun 15, 2009

Copy link to clipboard

Copied

LATEST

I know I’m a bit late in the discussion, but I wonder if for the determination of area-relations the histogram wouldn’t be convenient?

If the actual images are similar to the one example but showed the whole diameter of the cable one could apply Dust & Scratches and/or Median to get rid of some of the noise and the apply Hue/Saturation to completely desaturate, then Posterize to 2 and make a Wand Selection of the upper left point of the image, expand and invert that and put a white Solid Color set to 50% Opacity on top.

That would create sharp edged areas of either 255, 128 or 0 in numbers that could be read out from the Histogram.

circles.jpg

All of that is Script-able and manual corrections could easily be applied in a Layer between the image Layer and the Hue/Saturation-Layer.

I don’t know if that would produce sufficiently exact results though.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines