[Solved] Demosaicing with superpixels

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
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

[Solved] Demosaicing with superpixels

Post by jmac698 »

Hello,
I want to composite an image with a checkerboard, letting only the set pixels "shine through" from the image. The rest of the image should be black. I've almost got it, except the pixels are coming out white. I can think of ways to fix this up after, but I'd like to learn how to do it properly. This is what I have so far:

Code: Select all

magick orange.jpg -size %wx%h pattern:gray50 ( +clone -alpha copy ) -delete 1 -composite orange-G.png
Basically what I'm hoping for is a black image, with checkerboard opaque/transparent pixels in the alpha, which I can composite with a real image.
Thanks!
Last edited by jmac698 on 2016-08-15T20:22:38-07:00, edited 5 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite with transparent checkerboard

Post by fmw42 »

try this

Image

Window:

Code: Select all

magick lena.jpg ^
( -clone 0 -tile pattern:checkerboard -draw "color 0,0 reset" -auto-level ) ^
-alpha off -compose copy_opacity -composite lena_checks.png
Unix:

Code: Select all

magick lena.jpg \
\( -clone 0 -tile pattern:checkerboard -draw "color 0,0 reset" -auto-level \) \
-alpha off -compose copy_opacity -composite lena_checks.png
Image


Works also for IM 6 with convert rather than magick.

See
http://www.imagemagick.org/Usage/canvas/#tile
http://www.imagemagick.org/Usage/compose/#copyopacity

EDIT: Alternately, expressly for IM 7

Unix:

Code: Select all

magick lena.jpg -set option:dim "%wx%h" \
\( -size "%[dim]" tile:pattern:checkerboard -auto-level -transparent black \) \
-compose copy_opacity -composite lena_checks.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite with transparent checkerboard

Post by snibgo »

This does what I think you want:

Code: Select all

magick toes.png -size %wx%h ( pattern:gray50 -transparent White ) -composite x.png
Or perhaps Fred's version is what you want.
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Composite with transparent checkerboard

Post by jmac698 »

Thanks so much! It was the second answer which I wanted, but thanks for quick response and input as well, @fmw42

I am using this to mask and average Bayer channels, hopefully all goes well, I may have questions later so.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [Solved] Composite with transparent checkerboard

Post by snibgo »

If you haven't already seen it, my "Demosaicing" page may be of interest.

(And I'm always interested in what other people are doing. Feel free to share interesting results.)
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: [Solved] Composite with transparent checkerboard

Post by jmac698 »

I have in fact looked at your demosaicing page! I came up with my own way to do it, simplified even.

Using
http://img.photographyblog.com/reviews/ ... _hs_08.cr2

1. Extract mosaic (let dcraw apply camera white balance, autoleveling, and gamma correction)

Code: Select all

dcraw64.exe -6 -d -w -T canon_powershot_sx50_hs_08.cr2
2. Demoasic via 'superpixels'

Code: Select all

magick canon_powershot_sx50_hs_08.tiff ^
( -clone 0 -define sample:offset=25,75 -sample 50%x50% ) ^
( -clone 0 -define sample:offset=25,25 -sample 50%x50% ) ^
( -clone 0 -define sample:offset=75,75 -sample 50%x50% ) ^
( -clone 2-3 -evaluate-sequence mean ) ^
-delete 2-3 ^
( -clone 0 -define sample:offset=75,25 -sample 50%x50% ) ^
-delete 0 ^
-combine ^
-modulate 100,180,100 ^
binned.jpg
I'm using a different set of clone operations than you do, and skip the need to use batch files by letting dcraw do some work.

What I *actually* want to do is use -sample instead of -evaluate-sequence mean, so that I can bin 4x4 superpixels.

It would work like this:
-As per the previous message, let each Bayer colour shine through, with the rest black. This creates 3 images.
-Multiply the greyscale image which mask the green pixels by 2. Multiply the others by 4. This is a trick to account for the black pixels which are about to get averaged in; I pre-multiply to counter-act.
-Sample to 50%
-combine
Now I can do it again but for 25% size, and multipliers of 8/16.
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Demosaicing with superpixels

Post by jmac698 »

I'm stuck at the moment, this creates my own checkerboard pattern:

Code: Select all

magick -size 2x2 xc:black -fill white -draw "point 0,0 point 1,1" -write mpr:tile +delete -size 256x256 tile:mpr:tile out.png
But following your example, this doesn't let my pixel shine through:

Code: Select all

magick orange.jpg ^
-size 2x2 xc:black -fill white -draw "point 0,0 point 1,1" -write mpr:tile +delete ^
-size %wx%h tile:mpr:tile -transparent White ^
-composite orange-G.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Demosaicing with superpixels

Post by fmw42 »

try this (Note that without parenthesis, your -transparent is working on both your tile and input image. But when using parentheses, your %wx%h is not being set.

Code: Select all

magick orange.jpg -set option:dim "%wx%h" ^
( -size 2x2 xc:black -fill white -draw "point 0,0 point 1,1" -write mpr:tile +delete ) ^
( -size "%[dim]" tile:mpr:tile -transparent White ) ^
-composite orange-G.png
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Demosaicing with superpixels

Post by jmac698 »

Good catch! It got me over the hump. I should be able to complete my 'big picture' operation now. Thanks again @fmw42
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [On Hold] Demosaicing with superpixels

Post by fmw42 »

I get a 403 Forbidden error messsage when trying to access your link above.
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: [On Hold] Demosaicing with superpixels

Post by jmac698 »

I have it working now, basically. It's slower than my first approach; no matter, it's necessary to do it this way to avoid writing out a very long command.

1.http://www.photographyblog.com/reviews/ ... le_images/ (under "Sample RAW Images", "1/320s · f/4 · ISO 6400")

Code: Select all

dcraw64.exe -6 -d -w -T canon_powershot_sx50_hs_08.cr2
2.

Code: Select all

magick canon_powershot_sx50_hs_08.tiff -set option:dim "%wx%h" ^
( -size 2x2 xc:black -fill white -draw "point 0,0 point 1,1" -write mpr:g +delete ) ^
( -size 2x2 xc:black -fill white -draw "point 1,0" -write mpr:b +delete ) ^
( -size 2x2 xc:black -fill white -draw "point 0,1" -write mpr:r +delete ) ^
( -clone 0 -size "%[dim]" tile:mpr:r -transparent White -composite ) ^
( -clone 0 -size "%[dim]" tile:mpr:g -transparent White -composite ) ^
( -clone 0 -size "%[dim]" tile:mpr:b -transparent White -composite ) ^
-delete 0 ^
-resize 50%x50% ^
-combine -color-matrix "4 0 0 0 2 0 0 0 4" ^
There's still a problem: some odd coloured pixels, I think it's from arithmetic overflow somewhere. Also, I wanted to use sample not resize, because I want the exact average of every 2x2 set of pixels, but that wasn't working.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [Updated] Demosaicing with superpixels

Post by fmw42 »

To get an average, use -scale, not -sample. Scale does an average of all pixels in a block, sample just select one pixel from the block.

Youi might do this in non-HDRI mode to see if that cures your odd colored pixels. IM 7 installs by default with HDRI on.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [Updated] Demosaicing with superpixels

Post by snibgo »

jmac698 wrote:-clone 0 -size "%[dim]" tile:mpr:r -transparent White -composite
As Fred has mentioned, this applies transparency to both the clone and the new image from the tiles. You need an extra pair of parentheses to apply the transparency only to the tiled image:

Code: Select all

-clone 0 -size "%[dim]" ( tile:mpr:r -transparent White ) -composite
Likewise for the other lines.
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: [Updated] Demosaicing with superpixels

Post by jmac698 »

Perfect! The problem pixels are gone now. I like this method as I can bin as many pixels as I want.

Because some people at Magic Lantern, CHDK were asking about binning, I made this demo to see how it works in only software. It's usually done in hardware by summing n pixels to a capacitor then reading it as one pixel with the adc. I can't see how it's any different in software, really. This basically reduces noise (the standard deviation is divided by sqrt(n)). Also see https://chdk.setepontos.com/index.php?t ... #msg129552 for more context.

Note: for bayer pattern GB/RG such as Canon. Change the "point x,y" commands for your sensor, for example "point 1,0 point 0,1"

Code: Select all

magick canon_powershot_sx50_hs_08.tiff -set option:dim "%wx%h" ^
( -size 2x2 xc:black -fill white -draw "point 0,0 point 1,1" -write mpr:g +delete ) ^
( -size 2x2 xc:black -fill white -draw "point 1,0" -write mpr:b +delete ) ^
( -size 2x2 xc:black -fill white -draw "point 0,1" -write mpr:r +delete ) ^
( -clone 0 -size "%[dim]" ( tile:mpr:r -transparent White ) -composite ) ^
( -clone 0 -size "%[dim]" ( tile:mpr:g -transparent White ) -composite ) ^
( -clone 0 -size "%[dim]" ( tile:mpr:b -transparent White ) -composite ) ^
-delete 0 ^
-scale 25%x25% ^
-combine -color-matrix "4 0 0 0 2 0 0 0 4" ^
binned4.jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [Solved] Demosaicing with superpixels

Post by fmw42 »

I probably would have put parentheses slightly differently and convert the input to mpr. Not sure if it is more efficient. This is untested. If you try it, let us know if it works.

Code: Select all

magick canon_powershot_sx50_hs_08.tiff -set option:dim "%wx%h" -write mpr:img ^
( -size 2x2 xc:black -fill white -draw "point 0,0 point 1,1" -write mpr:g +delete ) ^
( -size 2x2 xc:black -fill white -draw "point 1,0" -write mpr:b +delete ) ^
( -size 2x2 xc:black -fill white -draw "point 0,1" -write mpr:r +delete ) ^
( mpr:img ( -size "%[dim]" tile:mpr:r -transparent White ) -composite ) ^
( mpr:img ( -size "%[dim]" tile:mpr:g -transparent White ) -composite ) ^
( mpr:img ( -size "%[dim]" tile:mpr:b -transparent White ) -composite ) ^
-delete 0 ^
-scale 25%x25% ^
-combine -color-matrix "4 0 0 0 2 0 0 0 4" ^
binned4.jpg
Post Reply