Need to map pixels: how to speed up txt: output ?

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?".
Post Reply
homer314
Posts: 13
Joined: 2011-07-01T08:52:55-07:00
Authentication code: 8675308

Need to map pixels: how to speed up txt: output ?

Post by homer314 »

Hi all

i need to create an edges pixel map using some BIG images and the txt: output takes a lot of time to write the file.

What i'm doing now is to convert the image with

Code: Select all

convert in.png -alpha extract -edge 1 out.png
and then

Code: Select all

convert out.png txt: | ... 
with the varius grep and sed arguments to extract white pixels coordinates, but commands must to read hundreds of input lines every time, causing slow elaboration.

So is there a way for a FAST edges-only pixel mapping on BIG images ?

Really thank's
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Need to map pixels: how to speed up txt: output ?

Post by fmw42 »

what is your exact command using grep and sed, etc.?

You might be able to convert much of that into using AWK, which may be faster.
homer314
Posts: 13
Joined: 2011-07-01T08:52:55-07:00
Authentication code: 8675308

Re: Need to map pixels: how to speed up txt: output ?

Post by homer314 »

fmw42 wrote:what is your exact command using grep and sed, etc.?

You might be able to convert much of that into using AWK, which may be faster.
Thank's for answer, i use

Code: Select all

convert $f txt: | sed 's/ *,/,/g' | grep "white" | cut -d: -f1 
to extract white pixels coordinates.

There's no way to tell to IM txt output to work only on white pixel directly, without creating a very large number of line input for grep, sed, or awk?

Thank's
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Need to map pixels: how to speed up txt: output ?

Post by fmw42 »

You may be able to speed it up a bit by putting the grep "white" first. You can also speed it up by combining the sed and cut commands into an sed search and replace.

try this

convert rose: txt: | grep "white" | sed -n 's/^\(.*\):.*$/\1/p'
homer314
Posts: 13
Joined: 2011-07-01T08:52:55-07:00
Authentication code: 8675308

Re: Need to map pixels: how to speed up txt: output ?

Post by homer314 »

Thank's, just another little question:

i use "-alpha extract -edge 1" (tried also higher edge values) to create the black and white png to use to extact pixel coordinates and all is working fine expect in a case.

If i've a circle selection, edges ar cutted out on borders, like in the example


Image

It is possibile to prevent this ?

Thank's
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Need to map pixels: how to speed up txt: output ?

Post by fmw42 »

try padding the image with whatever is the background color

find the background color before edges are extracted, I presume black for now.


convert in.png -alpha extract -gravity center -bordercolor black -border 2 -edge 1 out.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Need to map pixels: how to speed up txt: output ?

Post by anthony »

The edge dector will generall get either the inside edge, or the outside edge, depending on which is lighter or darker. Looks like you detected the outside of the edge.

If you negate the image before doing the edge effect the edge will go in the oppisite direction.

See IM Examples, transforms, Edge
http://www.imagemagick.org/Usage/transform/#edge

Alternative, just draw the circle using -fill none -stroke {color}

If you need it absolutely balck and white (bitmap), then you need to threshold the image to remove 'grays'.

Give the command you are using and we can help further!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
homer314
Posts: 13
Joined: 2011-07-01T08:52:55-07:00
Authentication code: 8675308

Re: Need to map pixels: how to speed up txt: output ?

Post by homer314 »

ok, these are my steps:

starting from this image for example (imageshack upload remove my alpha channel but you consider it)

Image

i need to find the edge's pixels coordinates so:

Code: Select all

convert $im_in -resize "$weight_dpi"x"$height_dpi" \ #I RESIZE THE IMAGE TO 96DPI TO SPEED UP THE TXT OUTPUT EXTRACTION
        -alpha extract -edge 1 \ #THIS CREATES THE IMAGE IN THE FIRST  POST
        -crop 1x"$lines"@ +repage +adjoin tmp_%d.png; #I DON'T NEED ALL EDGES PIXELS, BUT JUST THE MAX AND MIN "X" FOR EVERY OF THESE SLICES

...then i use txt: output on every slice
The code above works fine with all the tests images i tried, except when there is an image with "non closing edges"

Really sorry for my bad english, and really realy thank's for help
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Need to map pixels: how to speed up txt: output ?

Post by anthony »

You could just take the image then using -trim.
By reading the resulting virtual canvas information you will get you X and Y 'bounds'.

Code: Select all

convert image.png -trim -format -format 'MinY = %X  MinY = %Y MaxX = %X+%w-1 MaxY = %Y+%h-1 ' info:
You can use %[fx:...] formulas to do the calculation too. Or just output the 'crop' that -trim applied which is simply...

Code: Select all

convert image.png -trim -format -format '%wx%h%O' info:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
homer314
Posts: 13
Joined: 2011-07-01T08:52:55-07:00
Authentication code: 8675308

Re: Need to map pixels: how to speed up txt: output ?

Post by homer314 »

Really really thanks for all of these helpful tips :)
Post Reply