Page 1 of 1

Crop Image Based on Edge Detection

Posted: 2020-01-21T16:10:40-07:00
by awinhold
Hi Folks,

I have a set of images I've taken where I placed a canvas sheet over a white lightbox. The sheet has a rectagle cut out of it and I image the light coming through this cutout to use for some image correction and pixel saturation work. I need to detect the illuminated rectangle region, crop and rotate it. So far I haven't found answers online.

So far I've successfully performed the edge detection:

Code: Select all

$ convert edge.png -edge 1 -negate edge-1.png
Since this rectangle is back illuminated and the orientation isn't perfect with the camera, the rectangle does not have perfectly straight lines :/
Is there a best next step to try and take this edge detection result and crop it? I've tinkered with -fuzz, hough-line detector, and the windings example found here: https://imagemagick.org/Usage/transform/#canny

Re: Crop Image Based on Edge Detection

Posted: 2020-01-21T16:20:49-07:00
by snibgo
What version of IM, on what platform?

It would help if you showed example images.

If you have an edge image that shows the rough rectangle, do you want to trim around that rectangle or within it?

"-format %@" will be the outer rectangle. If you want the inner rectangle, precede this with "-define trim:percent-background=0%". See viewtopic.php?f=4&t=35579&sid=72010b969 ... e901578425

Re: Crop Image Based on Edge Detection

Posted: 2020-01-21T18:35:05-07:00
by fmw42
It would help if you post your original image. Also identify your IM version and platform. If on a Unix-like system, I have a script called trim2detail that does just that. See my link below.

Re: Crop Image Based on Edge Detection

Posted: 2020-01-21T20:19:01-07:00
by awinhold
Thanks, folks.
It doesn't look like I have the ability to post images here, yet, and I don't have a public location to place them :/

I am running:
Windows 10
Version: ImageMagick 6.9.10-11 Q16 x86_64 2018-09-08 https://www.imagemagick.org
on Cygwin.
snibgo wrote:
2020-01-21T16:20:49-07:00
If you have an edge image that shows the rough rectangle, do you want to trim around that rectangle or within it?
Ideally I think inside the rectangle would be best, so the edge and dark area is not considered.

Re: Crop Image Based on Edge Detection

Posted: 2020-01-21T20:48:09-07:00
by snibgo
Images can't be posted to the forum. You can upload to anywhere on the internet, and paste the URLs here.

Re: Crop Image Based on Edge Detection

Posted: 2020-01-21T21:11:55-07:00
by awinhold
snibgo: Ok, I've tried to host them on Imgur:

Original image: The rectangular light source taken at 1/250 sec shutter speed with Canon D60 (no 60D). Was CRW originally but converted to png in python. Link: https://imgur.com/oRrsMsH

Edge Detection Result: using the code I originally posted I get what seems to be a pretty decent edge. I think the -negate causes the color inversion to mostly white. Link: https://imgur.com/IZXbMGs

Result using fmw42's trim2detail: This gives a pretty good result when I pull the image up in Gimp and adjust the exposure to exagerate the lit area.
Link: https://imgur.com/3urBWEN

the trim2detail result, exposure exagerated in gimp: https://imgur.com/GBWNxSl

Re: Crop Image Based on Edge Detection

Posted: 2020-01-21T21:33:07-07:00
by fmw42
Your images do not show up properly for me. They have been converted to JPG and it shows only black (possibly loss of transparency?). Try dropbox or some other hosting service that won't change your images.

Re: Crop Image Based on Edge Detection

Posted: 2020-01-22T08:57:05-07:00
by awinhold
fmw42,
A few of them are going to look very dark and is expected. There should be two that do not look dark though. The edge outline (black outline on white background) and the trim2detail result which should have a white rectangle with a black border and yellow crop outline (from gimp).
If you're still only seeing black for all of them, I can try another hosting service.

Re: Crop Image Based on Edge Detection

Posted: 2020-01-22T11:08:54-07:00
by fmw42
OK, thanks. Your first image looked totally black, so I did not download it. The second, a square outline, was unexpected. I thought your images were of real-world content. So I thought there was a problem. I downloaded the first file and applied auto-level to it and can now see your square.

Re: Crop Image Based on Edge Detection

Posted: 2020-01-22T19:13:07-07:00
by awinhold
Yes it's an interesting feature (the almost dark image). It is a picture of a light box, covered with canvas, except for a rectangle cut out in the middle (the white rectangle you vaguely see). I'm imaging in Canon RAW (.CRW) which some how returns this very dark image. I think it has to do with the raw data not being interpolated/de-bayered to a 'normal' colored image. When I mouse over the pixels in gimp it goes from ~150 for pixel value to ~1200 for pixel value in the rectangle area. The color difference just doesn't seem to be an intuitive representation of that difference.

Thank you both, again, for your help on this effort. The trimming works very nicely!