[Closed] (IM7) Find same pixel in n images

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

[Closed] (IM7) Find same pixel in n images

Post by jmac698 »

I found this way to find the same pixels in two images:

Code: Select all

magick black_ISO6400_1.tiff black_ISO6400_2 -fx "(u==v)?p:0" comp.bmp
and I found a lot. Now I have to do something much more complicated, for n images, find any set of 3 pictures where some pixels are the same.

Example
pic pixel
1 (0,0)=10
2 (0,0)=10
3 (0,0)=10
output (0,0)=10; rest are 255. Even better, make a mask in alpha for only pixels that are the same in all then set the value to the value, and 0 elsewhere.

Any luck?
ps I wish there was an Equal in -evaluate-sequence
Last edited by jmac698 on 2016-09-28T17:31:55-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: (IM7) Find same pixel in n images

Post by snibgo »

Rule of thumb: unless the images are very small, "-fx" is horribly slow. "-compose Difference -composite" is black where pixels are identical. Follow that with "-fill +opaque black" to make other pixels white, if you want. (Then, if you want, you can use that as a mask on one of the inputs to do anything you want.)

If you do that on every pair of images in your set, take any pair of these mask results, "-compose Lighten -composite". Any black pixels in that result have equal pixels in all the inputs to the masks.

Which masks should be compared with each other? Only the pairs that have one source input in common.

That's the image processing. The rest is just script work.

The method can be extended to find any subset with 3, 4, 5, ... images that have pixels in common.
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: (IM7) Find same pixel in n images

Post by jmac698 »

I think it's

Code: Select all

magick IMG_2021.tiff IMG_2022.tiff -compose difference -composite -fill white +opaque black comp.bmp
Thanks, I'll play with it
Last edited by jmac698 on 2016-08-24T19:04:14-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: (IM7) Find same pixel in n images

Post by snibgo »

Yeah, sorry, "-fill White +opaque black "
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: (IM7) Find same pixel in n images

Post by jmac698 »

So like this?

Code: Select all

magick IMG_2021.tiff IMG_2022.tiff -compose difference -composite -fill white +opaque black comp1.png
magick IMG_2021.tiff IMG_2023.tiff -compose difference -composite -fill white +opaque black comp2.png
magick comp1.png comp2.png -compose lighten -composite comp3.png
Also, is there a way to generate filenames like maybe

Code: Select all

-define n=2021 IMG_%n IMG_%n+1
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: (IM7) Find same pixel in n images

Post by snibgo »

Well, I would name the mask file after both the inputs. If the inputs are IMG_2021.tiff and IMG_2022.tiff, the mask for that pair might be mask_IMG_2021_IMG_2022.png.

Perhaps you can do this purely in IM. But you need a script anyhow, so personally I would handle the filenames in the script.
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: (IM7) Find same pixel in n images

Post by jmac698 »

I'm trying to work this out myself and understand how transparency works, it should be easy but a simple experiment isn't working.

Code: Select all

magick -size 10x10 canvas:white -opaque white PNG32:out1.png
magick out1.png -channel a -separate alpha1.png

Code: Select all

magick -size 10x10 canvas:white -transparent white PNG32:out2.png
magick out2.png -channel a -separate alpha2.png
out1 appears black and opaque
out2 appears transparent (but possibly has black pixels that aren't visible)
alpha1 and alpha2 are both black. I was expecting them to be set to different values, like black and white, where white=opaque and black=transparent

I found some tips here:
https://www.imagemagick.org/discourse-s ... hp?t=25816

the thumbnail shows up in the windows file explorer as black, if the image is transparent, no matter what the colour. If you open it, in the preview application, it shows as blank, indicating transparent.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: (IM7) Find same pixel in n images

Post by fmw42 »

-opaque white requires -fill some color

Code: Select all

convert image -fill red -opaque white result
will convert any white in the image to red. -opaque does not work like -transparent. See http://www.imagemagick.org/Usage/color_basics/#replace

The equivalent of -transparent white using -opaque would be

Code: Select all

convert image -fill none -opaque white
or

Code: Select all

convert image -fill transparent -opaque white
This will replace white with none=transparent

If the images do not have pure white, then you will need to add -fuzz XX% to allow it to fill near-white colors. The nearness is expressed as the XX%, so 0% would need and exact white to turn to transparent.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: (IM7) Find same pixel in n images

Post by fmw42 »

P.S. After -separate add +channel, especially for IM 6
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: (IM7) Find same pixel in n images

Post by snibgo »

jmac698 wrote:magick -size 10x10 canvas:white -opaque white PNG32:out1.png
This creates a white image then changes all the white pixels to the current fill colour. But you haven't set "-fill". Before "+opaque" or "-opaque", you should always set "-fill".
jmac698 wrote:magick out1.png -channel a -separate alpha1.png
When you have "-channel", this should almost always be followed by "+channel" at some time. Otherwise, only that channel is active.

V6 images, after they have been read in, always have channels RGBA or CMYKA. This is not true of v7.out1.png has no alpha channel, so "-channel a" doesn't do what you want. You need "-alpha set" after reading the input.

[EDIT: Fred got in first.]
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: (IM7) Find same pixel in n images

Post by jmac698 »

Thanks!

I get it now. My understanding was that -opaque white was "marking" white image pixels as opaque in the alpha channel. Now my understanding is that it's "converting" white pixels to opaque, leaving behind ... nothing, which is why you need a -fill. To make it work as I thought, you just -fill white, that is, the same colour that you converted to opaque.

Code: Select all

magick -size 10x10 canvas:white -fill white -opaque white PNG32:out1.png
magick -size 10x10 canvas:white -opaque white PNG32:out1.png
magick out1.png -channel a -separate alpha1.png
magick out2.png -channel a -separate alpha2.png
out1 is opaque white
out2 is transparent (invisible colour is black, possibly)
alpha1 is white, showing that opaque is encoded as alpha=255
alpha2 is black, showing that alpha=0 means transparent
which is what I expected.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: (IM7) Find same pixel in n images

Post by fmw42 »

Your first line really does nothing. The image is white and you are telling to convert white to white. Just so you understand. -fill is the color you desire to have and -opaque white is the color you want to replace with the fill color. You can also do

Code: Select all

convert image -fill white +opaque black result
That will convert all non-black pixels to white.

Likewise, there is a +transparent also.

Code: Select all

convert image -fill none +transparent black result
will convert everything not black to transparent.
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: (IM7) Find same pixel in n images

Post by jmac698 »

so you're saying

Code: Select all

magick out1.png -set alpha -channel a -separate +channel alpha1.png

Code: Select all

magick: unable to open image 'a'
strangely, when I do

Code: Select all

magick identify -verbose out1.png
I can see that the out?.png have alpha channels, and even that the opaque out1.png shows gray=255 and alpha=255
and out2, the opaque one shows gray=255 and alpha=0
even though I did it wrong.
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: (IM7) Find same pixel in n images

Post by jmac698 »

Ugh. Sorry to be so confused, but now you're presenting me with yet a 3rd model of what it's doing.

I made a typo above, I meant

Code: Select all

magick -size 10x10 canvas:white -transparent white PNG32:out2.png
Put it this way, I want a
white image1, with opaque alpha, and
white image2, with transparent alpha.
To prove to myself I've made those correctly, I need to 'see' the alpha, so I need to make the alpha into an image to verify.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: (IM7) Find same pixel in n images

Post by fmw42 »

try

Code: Select all

magick out1.png -channel a -separate +channel alpha1.png
-set is not needed and does other things. You need a suffix on your input image out1.xxx
Post Reply