Page 1 of 1

Is it possible to crop two rectangles out of standard shapes

Posted: 2018-04-15T20:42:19+00:00
by allomorphy

I have two cartridges that are photographed adjacent to one another as shown in: ... 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: ... 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!


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

Posted: 2018-04-15T23:55:46+00:00
by snibgo
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.

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

Posted: 2018-04-16T08:34:08+00:00
by allomorphy
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: ... 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: ... 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.

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

Posted: 2018-04-16T20:21:04+00:00
by snibgo
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.

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

Posted: 2018-04-16T21:31:24+00:00
by allomorphy
Thanks very much Snibgo for this! Very thoughtful.