We discussed most ways to align objects to make them fit the image and its margins. They all are based on isolated images, the images with a consistent background filled with a solid color, most often white. But what if you don't need to isolate your images, what if they are supposed to stay on the natural background? Does that mean you can't align them automatically?
Not necessarily. As alignment is mostly about making selections around items, if the item is placed on a more or less plain and not too dirty background, you can automatically find its edges and select it. And when you have a selection, you know what to do with it already. So let's concentrate on the methods of edge detection that are available in Photoshop.
One problem that arises immediately when you mention edge detection is that you need to somehow determine which edges belong to the object and which edges are just trash and dust on the background. This is the most important thing if you want to be able to find and select your objects automatically. The second problem is finding an edge of an object that has low contrast with the background, like a white item on a white backdrop. How the hell is Photoshop supposed to find something that is barely visible? And the third problem arises when you have shadows under the objects and you don't want them to be included in your selection. So instead of just one problem of edge detection, you have three. Photoshop needs to detect the object, but not dirt and scratches on the background. It has to do it pretty well even if the object blends in with the background. And it also has somehow to distinguish between the object and its shadow. Sounds like a lot of trouble, right?
Having all that in mind, I have to mention that there are some demands to the images if you want to be able to automatically detect edges. The most important thing is that the background should be as clean as possible. The more trash there is, the bigger the probability that it will be detected as a part of an object. And of course, there should be no other objects in sight or anything that might be detected as an object. Let's see some examples.
This image has a light fixture in the upper right corner, and it will be detected. You'll have to crop it out.
There's also something black that can be seen in the upper left corner, it won't work if you don't crop it out.
Here's a thick piece of wire to the left and some plastic semi-transparent thing to the right, and both will be detected, so images like this have to be retouched first.
Don't think that we're completely powerless and you have to deal with spotless images only to be able to detect edges. That's not true. The background in this image is pretty dirty, but that's not really an obstacle. This amount of dirt is bearable. If I run one of my edge detection actions, you'll see that it manages to catch the outline of an object perfectly.
This earring is hanging on a piece of fishing line, which is quite thin. When something that is not a part of the object is thin, you have pretty high chances that you can detect the edges of the object correctly. I'll run the other edge detecting action. It takes some time for it to work, but it's worth it: in the end, I got a good selection of the object, and the fishing line was omitted.
As you could see, I have three edge detecting actions, yet still, the mechanisms that make them work are a mystery to you. Not for long! I'll reveal all the secrets behind this tricky process in a few minutes. But before that happens let's think about edge detection and how it can be done in Photoshop. There are, in fact, many ways how you can find edges. There's a filter that is called Find Edges, which is located in the Stylize submenu of the Filter menu. There's the Threshold command in the Image menu, Adjustments submenu. As you move the slider, you can see that the image turns black and white, not grayscale, but truly black and white – there are just two colors left. If you're lucky, you can get your object outline in mere seconds.
When you think about ways how you can find an object on some relatively consistent background, you might get the idea about automatic background selection. Because if you manage to select the background, you can invert the selection and there you go, you've got your object. It's true, but we've dealt with that already in the Isolation chapter. As we're not going to isolate things, we should really concentrate on edge detection. No automatic isolation mechanism will let you select this image, it's too low contrast, you can't really see the border between the object and the background. But we still can detect its edge. Let's see how we can do it even in desperate cases like this.
Smart Blur edge detection
If you paid attention to the section of the course where we discussed complicated isolation issues (LINK), you probably remember the notorious headband that seemed impossible to isolate. But as it turned out, there was an appropriate algorithm in Photoshop that allowed us to detect all the edges of the white net blending with the white background. This algorithm is called Smart Blur, it can be found in the Blur submenu of the Filter menu and it doesn't really blur anything if you set it in a correct way. This is the best edge detection algorithm that I know and it's really useful. Its only downside is that it's too slow. It's so slow that it might take almost a minute for it to work on old machines.
Processor and installed RAM seriously matter. When I tested the same image on 2 HZ, 8 GB RAM laptop it took the Smart Blur 45 seconds to work on a particular image when set to High quality, while a more powerful laptop, almost 3 HZ, and 32 GB RAM did it in 9 seconds. I think it should only be used if your computer is really fast, otherwise, it might not be worth it.
Let's start with something simple, like black shoes on a white background. It's not totally white as the object was not isolated, and this is an original hi-res image. If I make it darker with the Levels, you'll see that there's some colored stuff and scratches on the background. I'll copy the image to another layer first and then run the Smart Blur filter. So, as this algorithm is very time-consuming, we'd better use it set to Low Quality, as it's quite enough for what we need it to do. Radius and Threshold values are not as important as it usually happens with Blur filters. Last time I experimented, I was satisfied with Radius 23, Threshold 20. Once again, my image is quite big, it's more than 5000 pixels high, so if you work with images that are much smaller, you might want to do some tweaking with the numbers.
That's it. We've got a white outline of the object on black, and now the only thing that's left is selecting it. It can be done with the Select Color Range command, that can be found in the Select menu. I'll set it to select only black by setting Fuzziness to 0 and clicking on the background, and after applying the effect, I'll invert the selection. That will automatically include all the whites in the image. Now when you have a selection you can use it to align the object however you want, as you've learned in the previous section of the course.
But wait. Is that it? Are you sure this will work on pretty much every
Great. So, I've put all these steps into
Will it always work on every image, no matter how the object blends with the background? Not really. In some cases, the action won't be able to detect the edge correctly. See, it doesn't work properly on the upper part of the tongue, as the tongue doesn't make its way into the selection and gets cut when I crop the image. This is not much of a problem though, as this image is the only one that doesn't get selected correctly. All the other images I've tested the action on were fine.
It's even able to select this earring without selecting the fishing line – all thanks to the Dust & Scratches filter. So unless you work with really badly shot catalogue images, you will be fine. But the fact is that the action is very slow because of the Smart Blur filter. There are other ways you can detect an edge that take less time than that. Let's see what else is out there.
High Pass edge detection
You might be surprised, but the High Pass filter is not only good for the Frequency Separation technique. It can also be used for edge detection. It has a nice advantage over the Smart Blur method, too. The High Pass might not be able to detect edges so well but is usually ignores soft shadows under objects, and sometimes, when you want to select the object, but not its shadow, it can be a great advantage. Let me show you.
I'll run the High Pass filter with Radius 4 on this image. It is now on an average gray background and you can see the outline of the object more or less clearly. It just needs more contrast, which can be easily added by accessing the Levels and dragging the rightmost slider to the left, let's say until it says 150, and the middle slider goes to the right until it says almost 40. Good. Now let's get rid of the gray background by accessing the Levels again and dragging the rightmost slider to the left until it hits 165. It doesn't matter how the original background of the image looked like because we deal with the High Pass average gray background. It is always the same. And that's pretty much it because this outline can be selected with the Select Color Range if you select only white by setting Fuzziness to 0 and clicking on the background. After applying the effect, invert the selection. Now what we got here is a selection of the object and all the trash on the background as well. To prevent this kind of thing from happening, we'll add the Dust & Scratches step in the beginning.
This step is crucial. If the Radius is too big, you'll get your object eaten away. But if it's too small, you won't be able to get rid of trash on the background. Radius 12 seems to work for most of my images, which tend to be a bit dirty. But if the action doesn't really work for you, then Dust & Scratches is the step you'd want to adjust to fix it. Now when everything is ready, let's test this new action on the same set of images. You'll see that it works almost as well as the Smart Blur action, but much faster.
Find Edges edge detection
Find Edges is a filter which can also be used to detect edges. It can be found in the Stylize submenu of the Filter menu. It can't really be set up as it works automatically and silently. Normally I try not to use filters that just do whatever they want, and initially, I've had just two edge detection actions in my pack. But as I started writing about this matter for the course, I somehow managed to invent another method, the method that uses the Find Edges filter. And I can't say it works much worse than the previous two.
What it does it very simple. It applies the Dust & Scratches filter to avoid catching trash on the background, then it uses the Find Edge filter and then the Threshold command, which gives us a black outline of the object. This outline is selected with the Color Range command and that is your object selection which can be used for cropping or any kind of selection-based alignment.
I'll test it on the same images as before and you'll see that it finds the edges pretty efficiently. The notorious white shoes, though, still get cut from the top, but this is the way they are. The rest of the objects get selected properly.
Now let's summarize all the things we've learned about edge detection so far. Automatic edge detection always balances between not finding the object and finding trash on the background as well as the object. A good action is able to find a compromise. To detect the edges of the objects you can use one of the three Photoshop algorithms: Smart Blur, High Pass or Find Edges. These algorithms are filters and they can be found in the respective Photoshop menu. Smart Blur and High Pass can be set up, while the Find Edges works on its own. To avoid detecting edges of small trash, you have to use some filter that will blur the small details without blurring the edge of the object. I mostly use the Dust & Scratches filter, but the Median or Surface Blur might also work as they do a similar job on images. When you've got an outline of your object, you have to turn it black while the background has to be as white as possible, or vice versa. It will ease the selection process, which is done with the Select Color Range command. When you have a selection, you can use it to crop and trim your images or align images in more advanced ways as explained in the respective chapter of the course, the one that is dedicated to selection-based image alignment.
And don't forget about the shop where you can buy actions that will not just detect edges, but also align any objects to make them fit any image size and margins with just one click. A free test version (HERE) is available as well.