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

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



On Sun 22 Nov 2020 at 03:11:21 (+0100), Emanuel Berg wrote:
> tomas wrote:
> 
> >> Hello, is there a tool/command/script anywhere that can
> >> overwrite with a single color (or make transparent) every pixel
> >> to the left, above, right, and below a certain other color?
> >> 
> >> e.g., if one draws a loop of black, tell the program black is
> >> the border, the result would be only what is within the loop?
> >
> > Your request is a bit... unspecific.
> 
> I don't know what this is called. Let's say it is called 'cac' for
> 'cut-around-color'.
> 
> so invocation looks like this:
> 
>   $ cac gray reaper.png
> 
> (we imagine the reaper being dressed in gray, and the sky around
> him in dark blue colors)
> 
> so what happens is:
> 
> start at (0, 0) - this is the top-left pixel, as you know - is
> this color gray? answer: no. go to (1, 0) etc until you get to
> a grey pixel. at that point, discard all pixels until then.
> and don't look any further to the right on that row!
> 
> do this for all rows from left to right, top to bottom.
> 
> after that, do the same but search top down instead, so after (0,
> 0), go to (0, 1). discard pixels vertically upon gray. and stop.
> 
> do the same from right to left and bottom to top!
> 
> Encirclement!

How does that work with figures like Ω or ω?

Don't misunderstand me; the figure is drawn as if with a
broad brush, and the closed loop is the entire perimeter
of the brushed area. There are good examples from the
omega wiki page drawn on:
https://en.wikipedia.org/wiki/File:Omega_uc_lc.svg

If the loop is composed of continuous pixels, then you can
scan entire lines, using a semaphore to keep track of
crossing into/out of the loop, as long as their are no
singularities (like there would be, were the Ω serifs
to be drawn as thin as one pixel).

If the loop is a polygon, then you should be calculating
whether each point is inside or outside by calculating
the angles swept out from the point to all the vertices
in turn (finishing back at the first vertex), and seeing
whether you swept out a rotation of 360° (point is
inside) or 0° (outside).

Cheers,
David.


Reply to: