[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: color border in image, drop everything outside of it



On Sun, Nov 15, 2020 at 06:45:19AM -0500, The Wanderer wrote:

[...]

> Ah, right, I'd missed the "color that matches" part of the phrase. That
> does significantly change the meaning of the definition at that given
> link, to something which matches much better with what I'd have expected
> the term to mean in the first place.
> 
> In that case this seems like an even less suitable match for the request
> as I parse it.
> 
> >> What I understand the request to be is for a way to replace
> >> everything *outside of* a specified region with a specified color,
> >> where the region is *not* necessarily radius-based but may
> >> potentially be freeform.
> > 
> > Yes, but then, one person's "inside" is the other person's
> > "outside". You just have to choose judiciously your start point (aka
> > "seed"). And, of course, make sure the region thusly defined is
> > connected.
> 
> I either am badly confused, or don't understand how this relates to the
> request at all.
> 
> If I'm reading the original request correctly (which I'll admit is by no
> means guaranteed), the "single color" used to draw the "loop" - i.e.,
> in the example, black - is incidental to the goal; it just defines the
> boundary of the region which is to be left unchanged. It may or not even
> be important to retain the colored perimeter line in the result; I'd
> intuitively expect not, but there could be reasons to do so.

OK, now I get your objection: the colours "outside the loop" are
many and different. Perhaps the loop's colour is "different enough"
from every other, as determined by the fuzz metrics, then you are
lucky. Perhaps not. Then a more elaborate pipeline is necessary.

> What's outside (or, if you prefer, "on the other side of") the boundary
> may well be thousands of different colors, not necessarily in any
> pattern which the program can be expected to recognize with relation to
> another. The important part of the request is wanting to overwrite all
> of that region outside the boundary - regardless of its initial color -
> with a single, specified color (for values of "color" that include full
> transparency).
> 
> I don't see how "flood fill" can fulfill that at all. My best guess
> would involve the mention at your given reference link of a "-fuzz"
> option which lets the color matching be inexact; that could potentially
> make it possible to have a "define the boundary" color which is
> sufficiently distinct from every color in the external region that the
> fill will be able to match and replace all of the latter but will reject
> and neither replace nor propagate past the former, but it seems
> improbable that it would be practical to have this distinctness hold
> true for every image you might want to process this way.
> 
> (Then again it'd also seem impractical to guarantee that whatever color
> is chosen for defining the boundary will not occur outside of the
> boundary; I think that's why I've been mostly assuming that the literal
> drawing of the boundary with a color is just part of an attempt at a
> solution to the problem, not an essential part of the problem definition
> itself.)

Yep. That's one dificculty. Got it now. OTOH, if your loop isn't
"different enough" in colour from the rest of the image, how do
you differentiate it?

ImageMagick has many other more "intelligent" operations, but to
be able to pick one, the original problem has to be more specified,
I think.

[...]

> It comes in if the relative location of the specified region compared
> against the edges of the canvas is important.
> 
> If you stay with just one image (canvas), and replace everything outside
> the specified region with a specified color, then the relative position
> of the region remains unchanged.

[...]

> Similarly for the size of the original canvas vs. that of the new,
> previously-empty image.

I must have missed the cropping part in the OP's request. But then,
once the main problem is solved, ImageMagick has tools for that.

Another, more precise possibility is to use the "connected component
labeling" [1]: you have to then "know", by image coordinates, for
example, which is the one you want to keep. Basically, a multi-level
flood fill :-)

Cheers

[1] https://imagemagick.org/script/connected-components.php

 - t

Attachment: signature.asc
Description: Digital signature


Reply to: