Is it possible to crop two rectangles out of standard shapes

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
allomorphy
Posts: 8
Joined: 2017-10-29T14:15:04-07:00
Authentication code: 1151

Is it possible to crop two rectangles out of standard shapes

Post by allomorphy » 2018-04-15T13:42:19-07:00

HI,

I have two cartridges that are photographed adjacent to one another as shown in:
https://nofile.io/f/N0M4EZWNadG/1804_tw ... notate.jpg
Is there a way with IM to crop out a rectangle that fits inside the oval in each of the two cartridges as shown here with the desired crops indicated by the black rectangles:
https://nofile.io/f/gpAJcYuY7WB/1804_tw ... angles.jpg

As you see in the example given, there are three lines in the target area, however the solution should be usable with any number of lines or no lines at all by using only the casing of the cartridges as reference. It may be the the two cartridges are not perfectly lined up as shown in the example.

Thank you in advance for any suggestions or solutions!

Mets

snibgo
Posts: 10360
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is it possible to crop two rectangles out of standard shapes

Post by snibgo » 2018-04-15T16:55:46-07:00

If the two cartridges were always in the same location, so the desired rectangles were in constant positions, the task would be simple.

But if the positions can vary, what else can vary? The lighting? The background? The scale of the image?

White cartridges on a white background isn't easy. It's hard to see by eye where the right and bottom edges are.
snibgo's IM pages: im.snibgo.com

allomorphy
Posts: 8
Joined: 2017-10-29T14:15:04-07:00
Authentication code: 1151

Re: Is it possible to crop two rectangles out of standard shapes

Post by allomorphy » 2018-04-16T01:34:08-07:00

Thank you Snigbo, I appreciate your suggestions!

I include the following four images on a dark background, the variables (as I hope are clear to see) are
[1] different lighting (flash and no flash in this example) and
[2] whether the cartridges are touching or there is a gap between them:
https://www.plustransfer.com/download.p ... 8676161a5a
  • A dark background is now a requirement.
  • If you find it necessary to constrain that there presence of a space between, the cartridges or no spacing, then please go ahead and base your solution on the relevant image.
  • I would prefer that the solution will work in varying colour and intensity of the lighting. (As an example the two examples given are with flash and without flash).
  • There is a single constraint on the lighting, however. And that is that shadows or severe heterogeneity in illumination of the cropped out rectangles is not permitted.
  • The cartridges are always of the same size and shape
(If your solution requires a grayscale reference, then this may be provided in the image as well, please advise).

Now in order to provide some guidance, one of the cartridges will have a delineation around the oval to guide the fitting of the cropped area. The other cartridge will not have this delineation. This is illustrated here:
https://www.plustransfer.com/download.p ... e3&email=x
(If this helps, then I can more examples with this delineation).

Please let me know if anything needs to be better defined.

Many thanks for your suggestions.
Mets

snibgo
Posts: 10360
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is it possible to crop two rectangles out of standard shapes

Post by snibgo » 2018-04-16T13:21:04-07:00

Are there always exactly two cartridges? It would be slightly easier with just one.

Don't use JPEG inputs if they can be avoided.

If the photos are taken under controlled conditions, I suggest making a tray or baseboard with protrusions so the cartridges are always in the same place. The baseboard could be plywood or hardboard, with semi-circular dowels or pegs to locate the cartridges. Paint it matte black. Use the same camera and lens for each photo. Mount the camera in a fixed position directly above the baseboard, with the lens axis perpendicular to the baseboard. Ensure consistent lighting between the photos. Image processing is then very simple.


For uncontrolled conditions, I suggest using a matte black background (eg black paper, but black velvet would be better), the two cartridges parallel to each other with a small gap, the long axis of each cartridge parallel to the long axis of the image, as shown on two of your samples. The image processing can then be:

1. Chop the image into two, to isolate each cartridge. Apply the following steps to each cartridge.

2. Find the four edges of the cartridge, eg by contrast-stretch, threshold, canny and hough-lines.

3. The intersections of the edges gives locations of the four "corners" of the cartridge. This needs a script.

4. Apply a perspective distortion to the output from (1).

5. Now the cartridge image is normalised, so the position of the oval is known, more or less. If you simply want a rectangle that includes the three lines, I expect the result will give this. If you need the exact location of the oval, further processing will be needed.
snibgo's IM pages: im.snibgo.com

allomorphy
Posts: 8
Joined: 2017-10-29T14:15:04-07:00
Authentication code: 1151

Re: Is it possible to crop two rectangles out of standard shapes

Post by allomorphy » 2018-04-16T14:31:24-07:00

Thanks very much Snibgo for this! Very thoughtful.

Post Reply