Odd results while using SRT

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
marckyL
Posts: 7
Joined: 2016-12-15T16:25:12-07:00
Authentication code: 1151

Odd results while using SRT

Post by marckyL »

Admittedly, I am new when it comes to ImageMagick - so this could be user error.
In essence, I will be making a script that will take a large still image, and make a series out 1080p output images to make it look like a pan.
First, I wanted to understand what settings i need to have the viewport all the way to the right (the opposite of the default). The source image is 5184x3456. So I figured for the image to be all the way to the right, I take the image width, and subtract the width of the output frame... and I got this:

Code: Select all

convert img001.jpg -virtual-pixel Black -unsharp 0x2 -set option:distort:viewport 1920x1080-0-0 -quality 100 -write mpr:tmp +delete mpr:tmp -distort SRT "3264,0 1 0 0,0" "out_1.jpg" 
The above is with a scale of 1. So, next I tried it with a scale of .5 like this:

Code: Select all

convert img001.jpg -virtual-pixel Black -unsharp 0x2 -set option:distort:viewport 1920x1080-0-0 -quality 100 -write mpr:tmp +delete mpr:tmp -distort SRT "3264,0 .5 0 0,0" "out_1.jpg" 
This returned an image that still needed to be shifted 960 frames to right. So I adjusted it accordingly:

Code: Select all

convert img001.jpg -virtual-pixel Black -unsharp 0x2 -set option:distort:viewport 1920x1080-0-0 -quality 100 -write mpr:tmp +delete mpr:tmp -distort SRT "4224,0 .5 0 0,0" "out_1.jpg" 
Now the image went the other direction!

So my question is this:
When invoking scaling, how does that effect the x/y offset? What multiplier do I have to add to make the image reach the edge of the frame?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Odd results while using SRT

Post by snibgo »

SRT effectively does this: grabs the first pair of coords you give, and moves the image so that coord moves to (0,0), ie top-left. Then it scales and rotates. Then it moves what is at (0,0) to the final pair of coords you give.

So you move the image to the left, not right.

I suggest you first play without viewport. When you understand it, then add the viewport setting.
snibgo's IM pages: im.snibgo.com
marckyL
Posts: 7
Joined: 2016-12-15T16:25:12-07:00
Authentication code: 1151

Re: Odd results while using SRT

Post by marckyL »

This this situation can I do a 2d transform with a larger image, rendering out 1920x1080, without using viewport?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Odd results while using SRT

Post by snibgo »

"-distort" makes the output the same size as the input, but "+distort" doesn't. If the output from your SRT isn't in a 1920:1080 ratio, you need to think about whether you want to crop, or pad, or change the aspect ratio.
snibgo's IM pages: im.snibgo.com
marckyL
Posts: 7
Joined: 2016-12-15T16:25:12-07:00
Authentication code: 1151

Re: Odd results while using SRT

Post by marckyL »

AH. Ok. I think I solved my problem. It seems it's much easier to use SRT for the scale, and move the viewport accordingly for the 'panning'. Is there anything inherently wrong with this approach?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Odd results while using SRT

Post by anthony »

marckyL wrote:AH. Ok. I think I solved my problem. It seems it's much easier to use SRT for the scale, and move the viewport accordingly for the 'panning'. Is there anything inherently wrong with this approach?
That means you do two operations on the image. Though as long as the panning involves only whole numbers (not sub-pixel moves - and yes SRT can move images a 'part of a pixel') and you select a filter that does NOT blur the image, than that should be no problem.

Remember "distort" uses a 2 Dimentional filter, which due to diagonals, are not perfect pixel preservation in a NO-OP distortion
See No-Op Distortions
http://www.imagemagick.org/Usage/distorts/#distort_noop

Better still do the 'panning' using viewport to avoid a second distort filter of the image. OR do it as a single step.

Essentially a viewport is like a '-crop' or '-extent', but does it by defining what pixels it needs to distort from the destination mage into the source images (which is just the way distort works!).

SRT distortion rotates and scales the image around the first coordinate given. That coordinate then gets moved to the final coordinate (if given).

NOTE -distort basically defines the viewport to be the same is the input image (except in polar distorts which uses a +distort viewport anyway)
+disport tries to work out what the best 'viewport' to capyure the whole distorted image, if possible. If it can't work it out (EG shepards Distortion) it falls back to using -distort (eg the original image as the viewport). Setting "distort:viewport" simply overrides the -/+distort viewport selection, in which case it does not matter which form of the operator you use.

The viewport defines how much and what part of the image you want to see. Especially if you JUNK the virtual canvas at the end which in practical terms just pans the image so the top left corner of the viewport becomes 0,0. In other terms. If you are providing a viewport, AND moving the center of rotation and scaling, and junking the virtual canvas. you may as well defind a viewport located at +0+0 of the size you want, and SRT position the center of roatation where you want on that viewport.

The major difference between using SRT (actually a Affine Transform) for panning VS crop/viewport/virtual canvas offset is that SRT can as part of its process do sub-pixel moves of the image. The others are limited to whole pixel moves.
It is the same for scaling using SRT rather than resize/scale. Resize limits the final size to whole pixels, while SRT lets you scale using any scale factor you want. But it is still better to use SRT to do both operations together... scale and panning (sub-pixel positioning)

SRT, Affine (2 or 3 point), and Affine Projection (matrix) are all the same operation. scale, rotate, panning...
The later distortions (3 point or projection) also adds shear and aspect ratio components to the distortions.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Odd results while using SRT

Post by snibgo »

marckyl wrote:AH. Ok. I think I solved my problem. It seems it's much easier to use SRT for the scale, and move the viewport accordingly for the 'panning'. Is there anything inherently wrong with this approach?
No, nothing wrong. Personally, that's not how I do it. I would have a constant viewport 1920x1080+0+0, and vary the SRT parameters. See my "Animation with SRT" page.
snibgo's IM pages: im.snibgo.com
Post Reply