Get inner rectangle of wonky image on transparent background

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
mviereck
Posts: 13
Joined: 2018-12-13T10:56:29-07:00
Authentication code: 1152

Re: Get inner rectangle of wonky image on transparent background

Post by mviereck »

fmw42 wrote: 2018-12-24T16:47:10-07:00 You can filter the CCL output to export only the coords of the largest regions.
What do you mean with "filtering CCL output"? I can get a list of islands with:

Code: Select all

convert image -define connected-components:verbose=true -connected-components 4 info:
I can work on this output with grep, sort and other tools to find the greatest region.
Or is there a better IM way to filter the output?

Edit: On first test runs I get an error on some images:

Code: Select all

convert: too many objects `align_PTO.min1.tif' @ error/vision.c/ConnectedComponentsImage/438.
I would need a syntax that gives me only a few big results and drops the rest without an error.
It is important that it does not miss empty rows or columns as might happen with "-define connected-components:area-threshold=410" if I understand correctly.

My current reference is https://imagemagick.org/script/connected-components.php . Is another helpful explantation elsewhere?
You do not need to find islands on each iteration, if I understand.
Unfortunately the island check has to be done on each iteration; otherwise I would have to check for empty rows/columns on each iteration again.
If you look at the third animated gif here you'll see some "jumps" at the end of the animation where empty columns were found. If that case is not catched, the script fails.
Last edited by mviereck on 2018-12-26T04:33:48-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get inner rectangle of wonky image on transparent background

Post by fmw42 »

Try this with CCL to get the largest yellow region. It is super fast.

Image

Code: Select all

id=`convert yellow.png \
-define connected-components:verbose=true \
-connected-components 4 \
null: | grep "srgb(255,255,0)" | head -n 1 | sed 's/^[ ]*//' | cut -d\: -f1`
echo $id
9

convert yellow.png \
-define connected-components:keep=$id \
-define connected-components:mean-color=true \
-connected-components 4 \
yellow_largest.png
Image

With this you can trim all the transparency, then use an outside inward or inside outward approach to get the inner rectangle.

This won't solve all your complex situations, but I think it works fine to do most of the basic cases that IM users might want.

With lots of small regions and various colors you will have to reduce colors or convert to binary and also use the area-threshold to get rid of noise so that you do not exceed the quantum range number of regions.
mviereck
Posts: 13
Joined: 2018-12-13T10:56:29-07:00
Authentication code: 1152

Re: Get inner rectangle of wonky image on transparent background

Post by mviereck »

Thanks for your example. I'll give it a try to create a script based on connected-components.
I am fighting with several details, however, the concept is clear:

Target is the greatest possible inner rectangle without border color.
- Find the greatest island with connected-components.
- Remove side with greatest mean.
- Repeat.
- Ready if mean of all 4 sides is zero.

For the majority of possible source images a single initial island check is enough.
A repeated check for an island makes the script foolproof for some edge cases.

A failing case for border checks without island detection appears if two opposite sides have a zero mean and an empty row/column lies between them. That case can be seen in the last animated seconds here (needs a single island detection) and here (solved with repeated island detection).

This might be provided as a proposal to the IM developers.
Last edited by mviereck on 2018-12-26T15:45:48-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get inner rectangle of wonky image on transparent background

Post by fmw42 »

I do not know about your black outline case, but if you have to iterate on various objects, you can simple use CCL to get a list off all objects in order of largest regions first. You can grep for whatever color you want to filter or all regions not of background color. CCL automatically lists them in order of largest size first.
mviereck
Posts: 13
Joined: 2018-12-13T10:56:29-07:00
Authentication code: 1152

Re: Get inner rectangle of wonky image on transparent background

Post by mviereck »

I've decided not to develop a new script. My own use case is already covered with border check only in trim_hard2 script and I want to spend more time on the microscopic capture processing project where this inner rectangle detection is a part of.

I believe an ImageMagick option as outlined in my previous post would be quite useful for many users. It would be nice if the IM developers would consider it.

-----------------

As a final eye catcher an image created from 108 microscopic captures from same motive with different focus, preprocessed with ImageMagick, aligned with Hugin tools, combined to several overall sharp images with enfuse, post-processed with ImageMagick ("-evaluate-sequence median" on all enfuse results), and with a ruler drawn by ImageMagick. It is the top of a hair of a hemp flower:

Image
mviereck
Posts: 13
Joined: 2018-12-13T10:56:29-07:00
Authentication code: 1152

Re: Get inner rectangle of wonky image on transparent background

Post by mviereck »

I found a case where the approach outlined above does not give the greatest local maximum:
Image

I see two possible attempts:

1.) Use the resulting geometry as a seed geometry for an inside-outwards approach.

2.)
- Grep the splitting column.
- Return to last detected island.
- Split island at detected column.
- Start again here with outside-inwards approach.
Last edited by mviereck on 2018-12-30T05:41:49-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get inner rectangle of wonky image on transparent background

Post by fmw42 »

I suspect there will always be some special case where this kind general iterative approach will fail.
Post Reply