How to speed up an advanced threshold filter? ( -lat + median)

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
bluedxca93
Posts: 3
Joined: 2015-04-03T02:29:23-07:00
Authentication code: 6789

How to speed up an advanced threshold filter? ( -lat + median)

Post by bluedxca93 »

Hi,
found an image filter that does nearly the thing i want to do:

convert input.jpg -colorspace Gray -lat 35x35-1.8% -median 3x3 -morphology Dilate Plus:2 output.jpg

That command is already relatively fast and can output interesting images.

As you might notice its not exactly an image detector or trace contour filter its soemthing different.
Canny is interesting but failing on some images, and i don`t need exact 1 px thin lines.

But how to make it run faster?

B.t.w:
what would happen if i extract h s v channel of an image seperatly put them in a folder and then run:

convert *.jpg -evaluate-sequence median output.jpg ?

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

Re: How to speed up an advanced threshold filter? ( -lat + median)

Post by fmw42 »

Use Q8 compiled IM to save memory
Use OpenMP with multiple threads if you have multicore processors
Reduce the 35x35 to the minimum that works acceptably.

Not much else you can do! Is your image very large? Are you running out of RAM and thus thrashing to disk? You can set up the configuration for large image processing. See http://www.imagemagick.org/Usage/files/#massive and viewtopic.php?f=4&t=26801
bluedxca93
Posts: 3
Joined: 2015-04-03T02:29:23-07:00
Authentication code: 6789

Re: How to speed up an advanced threshold filter? ( -lat + median)

Post by bluedxca93 »

Hi,
thx for the tips. it seems to be indeed working faster with:
20x20-1.5
on smaller pictures.

Example picture : 50% of original size:
Image

Next is not blurred in original:
Image .

As you might see the lines are not 1 px like in most edge detectors, but it looks more natural than most other solutions i did see.

b.t.w.
before i tried lua 5.1 with imlib2 ( linux or cygwin):

Code: Select all

#!/usr/bin/lua5.1
-- simple edge detector 
-- by bluedxca93
-- convert input2.png -selective-blur 0x1+10% input.png
-- convert input2.jpg -adaptive-blur 3x1 input.png
require("imlib2")
local im =  imlib2.image.load("input.png")
local xlength = im:get_width()
local xheight = im:get_height()
for x=0, xlength-1 do
  for y=0, xheight-1 do
     c1 = im:get_pixel(x, y)
     c2 = im:get_pixel(x+1, y)
     c3 = im:get_pixel(x, y+1)
     c4 = im:get_pixel(x+1, y+1)
          c1r = (c1.red)
          c1g = (c1.green)
          c1b = (c1.blue)
          c2r = (c2.red)
          c2g = (c2.green)
          c2b = (c2.blue)
          c3r = (c3.red)
          c3g = (c3.green)
          c3b = (c3.blue)
          c4r = (c4.red)
          c4g = (c4.green)
          c4b = (c4.blue)
          level = 6   
          if c1r > c2r then c1v2r = c1r - c2r else c1v2r = c2r - c1r end
          if c1r > c3r then c1v3r = c1r - c3r else c1v3r = c3r - c1r end
          if level > c1v2r and level > c1v3r  then c1vdr = 0 else  c1vdr = 255 end
          if c1g > c2g then c1v2g = c1g - c2g else c1v2g = c2g - c1g end
          if c1g > c3g then c1v3g = c1g - c3g else c1v3g = c3g - c1g end
          if level > c1v2g and level > c1v3g  then c1vdg = 0 else  c1vdg = 255 end
          if c1b > c2b then c1v2b = c1b - c2b else c1v2b = c2b - c1b end
          if c1b > c3b then c1v3b = c1b - c3b else c1v3b = c3b - c1b end
          if level > c1v2b and level > c1v3b  then c1vdb = 0 else  c1vdb = 255 end
          if c1vdr == 255 and c1vdg == 255 and c1vdb == 255 then cf =  imlib2.color.new( 255, 255, 255) else cf =  imlib2.color.new( 0, 0, 0) end   
    im:draw_pixel(x, y, cf)
  end
end

im:save("output.png")
what it does it if color diiference to 3 "important" neighbour pixels ( x+1,y ; x, y+1; x+1;y+1) is greater than level (=6) pixel is white otherwise its black.
That filter was much slower, and salt/pepper noise is a problem. Would be great if there is an imagemagick equivalent.

output ex ( reduced 25 % of original size:)

Image

regards bluedxca93
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to speed up an advanced threshold filter? ( -lat + median)

Post by snibgo »

Your lua code doesn't use "c4 = im:get_pixel(x+1, y+1)".

It could be done in IM by using "-fx". Or (probably much faster) by morphology, or by taking the difference between an image and its clone displaced.
snibgo's IM pages: im.snibgo.com
bluedxca93
Posts: 3
Joined: 2015-04-03T02:29:23-07:00
Authentication code: 6789

Re: How to speed up an advanced threshold filter? ( -lat + median)

Post by bluedxca93 »

Hi,
closest equivalent of the lua script in imagemagick:

convert -page +1+1 test.png -background none -flatten test2.png ;
convert test.png test2.png -compose difference -composite -colorspace RGB -threshold 254 difference_gray.png

About four times faster than lua script above!
No fx functions necessary.

regards bluedxca93
Post Reply