Page 1 of 1

mask for a distorted image

Posted: 2011-11-11T05:53:38-07:00
by terebentina
Hi,

I am trying to create a 3d preview of an wall art from a 2d image...in 1 command. I'm almost there but I just can't figure out how to apply a mask to a distorted image.
I was hoping someone could help...I am using IM 6.6.2-10 Q16 on windows now, once everything is ready, it'll run on linux.

The 2d "artwork" image:
Image
The canvas behind the artwork (only its edges will be visible in the final image):
Image
Highlights:
Image
Mask:
Image

Desired result:
Image

What I got so far:

Code: Select all

convert canvas_l_bw.png \
 \( preview2d.png -bordercolor white -border 0 -alpha set -filter point -virtual-pixel transparent +distort perspective "0,0,50,130  974,0,985,36  0,734,50,903  974,734,981,719" -write tmp.png \) \
 -layers merge +repage \
 canvas_highlights.png -compose HardLight -composite \
 result.png
..which gives me this:
Image

Now I have to apply the mask just to the distorted image, before merging it with the canvas and nothing I tried works...

Help, please?

Re: mask for a distorted image

Posted: 2011-11-11T10:29:10-07:00
by fmw42
I am not sure what you want as your output result looks good to me? Can you explain further what you are trying to do that is different from what you got? What do you want the output to look like that is different?

Re: mask for a distorted image

Posted: 2011-11-11T10:40:42-07:00
by terebentina
The artwork image is slightly bigger than the canvas. You can see this especially in the bottom left corner of my result image but it's the same everywhere. The right edge of the artwork goes a bit beyond the width of the canvas.

The purpose of the mask here is to cut the extra bits off the artwork so that it would (fake) look like the artwork goes slightly around the edges of the canvas.

You can compare the 'desired' and 'result' images - again, the bottom left corner shows it best - how it is with the mask applied and without.

In words, what I want to achieve is this:
1. distort the 2d artwork
2. apply the mask just to it
3. add the canvas behind the distorted artwork
4. add the highlights above

With the script I listed above I managed to do #1, #3 and #4.

Re: mask for a distorted image

Posted: 2011-11-11T10:48:05-07:00
by fmw42
I understand what you are saying and now see the issue. But it seems to me that you should just compute your control points so that the corners of your input image just fit the appropriate corners of the canvas and it should work fine to fit. Perhaps I am missing something.

Re: mask for a distorted image

Posted: 2011-11-11T10:53:18-07:00
by fmw42
Looking as carefully as I can, it appears that your canvas shape is not a perfect rectangular box. The lower left corner appears to me to be curved so that the white front is bigger than the back. Am I correct on this? So it appears that you need to correct the canvas and that should fix your issue with properly picked control points.

Re: mask for a distorted image

Posted: 2011-11-11T10:57:43-07:00
by terebentina
Perfect control points was my first approach. And it would've worked if there were only square/straight corners. Problem is that the canvas corners aren't really straight, they're rounded and have some small imperfections (imperfections on the bottom left corner in 'desired', slightly rounded top left corner, etc). The canvas image comes from a real photo of a real canvas.
I'd like to keep these imperfections as they give this 'authenticity' look, that's why I created the mask...but even more than this, after I spent so much time fighting this, I'd like to find out the solution - how to apply a mask to that distorted image. It's personal now :)

Re: mask for a distorted image

Posted: 2011-11-11T12:09:00-07:00
by fmw42
can you upload all your png images as they have been converted to jpgs

NEVER MIND, I downloaded the low res displayed images rather than the full res ones.

Re: mask for a distorted image

Posted: 2011-11-11T12:47:08-07:00
by terebentina
I ziped the files and put them here

Re: mask for a distorted image

Posted: 2011-11-11T12:48:33-07:00
by fmw42
Something seems wrong in the following simple test (IM 6.7.3.5 Q16). The alpha channel in the result (1tmp2.png) should not have any black extending outside the mask area. I don't know if this is a bug or not. What happens on your systems?



convert 1preview2d.png +repage -alpha set \
-filter point -virtual-pixel transparent +distort perspective \
"0,0,50,130 974,0,985,36 0,734,50,903 974,734,981,719" -alpha off +repage -write 1tmp0.png \
\( 1canvasmask.png +repage -threshold 50% -alpha off -write 1tmp1.png \) \
-alpha off -compose copy_opacity -composite 1tmp2.png



http://www.fmwconcepts.com/misc_tests/m ... /1tmp0.png


http://www.fmwconcepts.com/misc_tests/m ... /1tmp1.png


http://www.fmwconcepts.com/misc_tests/m ... /1tmp2.png

Re: mask for a distorted image

Posted: 2011-11-11T13:04:16-07:00
by terebentina
Same...

Re: mask for a distorted image

Posted: 2011-11-11T13:09:37-07:00
by fmw42
I will send a note to Anthony, but he may not answer for a few days as this is already his weekend. It fails for me also with gif or tif, so it is not a png bug.

Re: mask for a distorted image

Posted: 2011-11-11T13:24:59-07:00
by terebentina
Thanks, I'll keep trying and looking through other examples. If I can find a solution I'll let you know.

Re: mask for a distorted image

Posted: 2011-11-13T22:14:35-07:00
by anthony
Downloading the low res versions from image shack is not recommended. It is a horrible image cloud service.

I downloaded the full images and checked the mask against the background canvas...
(just a disolve of the mask overlayed on the canvas...

convert canvaslbw.png canvasmask.png -define compose:args=50% -compose dissolve -composite show:

Looks like a good fit.

WARNING: when using +distort, you generate a layer image with image offsets! That is your problem!
Remember +distort is bestfit, with image offsets. Lower level composition does not use image offsets, only user input geometry offsets are used, not layer offsets with is a higher level operation.

This makes masking the distorted image trickier! Especially as -layers merge is not designed with masking in mind!

Easiest solution is set your own 'viewport' (output image size) for the distort to the size needed for the "canvasmask.png" (zero offset). That removes the offset problem, though does make teh distort a little longer while it processes the extra pixels.

With everything aligned you then can use lower level composition to handle the masking.
And then add canvas and highlights as you need. (High lighting does not currently preserve transparency!)
So an extra masking step is also added (this could use some improvement)

Code: Select all

identify canvasmask.png
canvasmask.png PNG 1000x917 1000x917+0+0 8-bit DirectClass 13.3KB 0.000u 0:00.000

convert preview2d.png -alpha set -virtual-pixel transparent \
            -define distort:viewport=1000x917+0+0  -distort perspective \
                 "0,0,50,130 974,0,985,36 0,734,50,903 974,734,981,719" \
            \( canvasmask.png -alpha shape \) -compose DstIn -composite \
           canvaslbw.png +swap -compose over -composite \
           canvashighlights.png -compose hardlight -composite \
           canvaslbw.png -compose DstIn -composite \
           result.png
NOTE: a check of the distorted image shows a white line at the bottom, that comes from the original "preview2.png" input image. Adjustment of the source distort coordinates (which is image coordinates NOT pixel coordinates) may be needed in order to remove that line! See http://www.imagemagick.org/Usage/distor ... oordinates

Re: mask for a distorted image

Posted: 2011-11-14T02:09:39-07:00
by terebentina
Ohhh, so it was that viewport define that I wasn't aware of. What I was trying just now was to change my approach: start with a black 1000x917, add the +distorted then flatten/repage so I could get rid of the image offsets, apply the mask and then add the rest. But the viewport define is better.

Thanks for the help guys, you've been great.

Re: mask for a distorted image

Posted: 2011-11-14T20:21:45-07:00
by anthony
Repage would have just junked the offsets. -flatten would have been another alternative solution to the offset (+repage is not required) though -mosaic would probably have been better to ensure canvas size was right! (it should have been).


Another is to simply use -distort instead of +distort, and make sure you distorted image does not overflow the original images bounds. -distort just used the original image to define the output 'viewport'. +distort calculates a 'best fit' viewport.

The viewport define is just a way to specifically define the size, and more importunately the offset, of the resulting image (overriding 'best fit' flag). In this case to match your masking image. It does not have to match the mask image, but you don't want a offset interfering with the compositions that does not understand offsets.