crop + distort perspective + append creates a gap

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
rob

crop + distort perspective + append creates a gap

Post by rob »

Hello all,

I'm trying to split an image into two and then add a distortion effect to both sides and then reappend the images:

Code: Select all

convert test.png -crop 50%x100%+0+0 -alpha on -virtual-pixel transparent -distort perspective ' 0,0 0,0 0,634 0,634 422,634 422,634 422,0 422,0 ' \( test.png -gravity east -crop 50%x100%+0+0 -alpha on -virtual-pixel transparent -distort perspective ' 0,0 0,0 0,634 0,634 422,634 422,634 422,0 422,0 ' \) +append test.png
When ever I do this there is a small gap (maybe 1 pixel) between the two appended images. The code is part of a video compositing script, so that the values for the perspective distortion will change for each new image being processed. I need virtual pixels to be transparent because as the image is distorted it reveals a background image. The original image is 844x634, but it doesn't matter which size image I take. My question is if I'm doing anything wrong that's causing the gap? Or if there's any way to get rid of it?

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

Re: crop + distort perspective + append creates a gap

Post by fmw42 »

see http://www.imagemagick.org/Usage/distor ... rt_options and http://www.imagemagick.org/Usage/distor ... oordinates

also try using half pixel offsets of your coordinates to specify the center of the pixel

there were some recent changes to -distort to correct for half pixel problems. are you using the most current version of IM

Anthony can explain in more detail
rob

Re: crop + distort perspective + append creates a gap

Post by rob »

Thanks for your reply.
I've got ImageMagick 6.5.2-4 installed. I probably should install a newer version if this problem has already been fixed. I'm not sure I quite understand the image coordinate explanation, but using half pixels certainly did the trick:

Code: Select all

convert test.png -crop 50%x100%+0+0 -alpha on -virtual-pixel transparent -distort perspective ' 0,0 0,0 0,634 0,634 421.5,634 422,634 421.5,0 422,0 ' \( test.png -gravity east -crop 50%x100%+0+0 -alpha on -virtual-pixel transparent -distort perspective ' 0.5,0 0,0 0.5,634 0,634 422,634 422,634 422,0 422,0 ' \) +append test.png
The gap is gone. Should I be using half pixels at all positions both old and new in the distortion?

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

Re: crop + distort perspective + append creates a gap

Post by fmw42 »

The gap is gone. Should I be using half pixels at all positions both old and new in the distortion?

I believe so. But Anthony can explain that better or point you to some further documentation on his changes.

Search the archives also as I believe he has gone into explaining it for others recently.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: crop + distort perspective + append creates a gap

Post by anthony »

The 0.5 addition is to specify the center of a pixel.

The recent changes mentioned was not to do with distortion but for center effects with arc and polar distortions. The only other effect was in documentation about using 0.5


The problem is that distortion needs to merge colors. But on the edge it merges with transparency, and not with the correct color needed to join two distortions edge-to-edge. Having a slight overlap by alligning the center of the pixels removes any edge effects caused by the transparency.

See 3D Box Example which contains the full problem, and other alternative methods of fixing the problem. The best and simplest however is still using a slight overlap, or even using images at larger scales.

Reading the whole page, including the theory and techniques can also be helpful if you are serious about distorting images. I programmed IM to produce the best posible, and most accurate distortion, but reducing the accuracy could improve speed and results (especially when you plan to reduce the final size of the image). For example using -filter point, with a modification of -interpolation, can produce better edge effects but only if the distortion is not too sever (often isn't).

See also the reference to the forum discussion with the DVD cover example, in Perspective Layering, for even more detail about doing things like this.
viewtopic.php?t=11726

And please continue to provide details of your attempts, failures and successes here.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
rob

Re: crop + distort perspective + append creates a gap

Post by rob »

Hello,

using interpolate nearestneighbor also worked for me:

Code: Select all

convert test.png -crop 50%x100%+0+0 -alpha on -virtual-pixel transparent -interpolate nearestneighbor -filter point -distort perspective ' 0,0 0,0 0,634 0,634 422,634 423,634 422,0 423,0 ' \( test.png -gravity east -crop 50%x100%+0+0 -alpha on -virtual-pixel transparent -interpolate nearestneighbor -filter point -distort perspective ' 0,0 0,0 0,634 0,634 422,634 422,634 422,0 422,0 ' \) +append +repage test.png
However the images I'm aiming to use this on are very big and require a lot of distortion, so I think I'll stick with the code from my last post.

Anyway, thanks for the tip though; now I finally understand why the gap is there.

keep you posted, rob
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: crop + distort perspective + append creates a gap

Post by anthony »

Both -filter and -interpolate are global settings, they only need to be specified once, or if you want to change the effect later. WARNING: -filter also effects resize operators like -resize -thumbnail -resample you will probably want to use +filter to set it back to default if you also want to do those operations.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply