Non-linear image scaling - imagemagick

Starting Situation:
I have a distorted image, for example a geographic map with a grid. The distortion is of that kind: At the top of the map 10° geographic latitude correspond to 30 px, but at the bottom of the image 10° latitude correspond to only 10 px.
Aim:
Now I want to scale the image, that 10° latitude correspond to let's say 20 px all over the image.
Let me illustrate the problem a litte more:
Starting situaiton: Image with an overall height of 100 px:
Area 1 - 30 px height
Area 2 - 25 px height
Area 3 - 20 px height
Area 4 - 15 px height
Area 5 - 10 px height
Aim: Image overall height stays constant (100 px), but now the areas should be of equal height:
Area 1 - 20 px height
Area 2 - 20 px height
Area 3 - 20 px height
Area 4 - 20 px height
Area 5 - 20 px height
If this can be achieved with other software than imagemagick, hints are welcome, too.
Thanks for your replies in advance.

Now I found out the tranformation function between my starting map and the map I desire:
f(y_px) = 200 (-0.5 + 0.01 * sqrt(150 + y_px))
This can be done by any fitting software.
Now I succesfully transformed the map using the "-fx" parameter and a 'custom matrix':
convert -monitor -size 100x100 xc: map-starting.png \
-virtual-pixel Black -interpolate NearestNeighbor \
-fx "xx = i ;
yy = 200 (-0.5 + 0.01 * sqrt(150+ j));
v.p{xx,yy}" \
map-aim.png
map-aim.png is what I wanted ;)

It's not possible to correctly rescale and reproject map images with just a raster image processing tool like ImageMagick. You need a tool that is capable of computing a proper image transformation taking into account the change of map projection. You will also need projection metadata about your image, either in the image itself (if it is some format like GeoTIFF) or with associated metadata header files if it is a raw raster format like PNG or JPEG.
I suggest you look at the Geospatial Data Abstraction Library GDAL. It is a library that contains command line tools to allow you to properly reproject and transform many kinds of raster map images.

Related

What size in cells is A4?

I'm trying to create a Google Sheets template that is exactly A4. The reason for that is I need to insert a picture in there that is absolutely placed and sized.
So, I'm figuring that since A4 is 210x297mm and Google Sheets default resolution is 96dpi, I need 793.59x1122.363 pixels.
Since a cell is 100x21 pixels, I need 7 cells horizontally + an additional cell that is 93 pixels large and 53 cells vertically + a cell that is 9 pixels high.
Which prints too big (A4, portrait, Scale Normal 100%, Margins 0,0,0,0):
too big
If I manually adjust the size of the cells to get a complete grey area without white lines on a single page, like this:
perfect size
I need 7 cells + one cell that is 85 px large x 53 cells + one cell that is 8 px high.
Note that (53*21+8)/785 = 1.428, which is not sqrt(2), the standard A4 ratio.
Why ? Thanks!

Calculate logical pixels from millimeters

I have a design with widths, heights, paddings... in millimeters. I'm now trying to figure out how to convert those numbers to the logical pixel system Flutter uses.
I found the docs for the device pixel ratio but I'm not sure how to interpret that number and I quote:
The Flutter framework operates in logical pixels, so it is rarely necessary to directly deal with this property.
So I am not sure if this is the way to go.
My question comes down to this: Is there an easy way to calculate from millimeter to logical pixels that works for both Android and iOS?
Flutter's unit is DP or dip (aka density independent pixels).
Like it's name is implying, it's independent from the screen pixel ratio.
What's the difference with millimeters ? None really important.
Converting mm>dp or dp>mm is similar to mm>inch/inch>mm.
The relationship between them is fairly simple :
1 inch = 25.4 mm = 160.0 DP
Which means 1 mm = 6.299 DP
I would say the current accepted answer is not really accurate.
You can print the number of logical pixels for real phones, like this:
maxWidth = MediaQuery.of(context).size.width;
maxHeight = MediaQuery.of(context).size.height;
print("maxWidth = $maxWidth / maxHeight = $maxHeight");
I would appreciate if someone with real iPhones and iPads could run this code and tell me the resulting maxWidth and maxHeights. So far, with Android phones, I've compiled the results comparing with the real phone sizes. What I got is this:
Galaxy S6 ➜ 5.668537826 dp/mm for height, and 5.668537826 dp/mm for width.
Galaxy S7 ➜ 5.668537826 dp/mm for height, and 5.668537826 dp/mm for width.
Galaxy S9 ➜ 5.223614747 dp/mm for height, and 5.585946405 dp/mm for width.
Pixel XL ➜ 5.612956709 dp/mm for height, and 6.007177748 dp/mm for width.
See my spreadsheet here: https://docs.google.com/spreadsheets/d/1zmGyeKSf4w4B-bX4HSY4oSuh9RkIIkFwYSd3P9C7eX8/edit?usp=sharing
Update:
Android docs (https://developer.android.com/training/multiscreen/screendensities#TaskUseDP) say "One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density)". That's obviously not true in practice for Samsung and Google phones. Or it is, if you pay attention to the word "roughly".
Flutter docs (https://api.flutter.dev/flutter/dart-ui/FlutterView/devicePixelRatio.html) say it's 3.8 logical pixels per mm, which is obviously very false.
Logical pixel is the ratio of dots per unit distance(mm), so you have to change your question to How many dots per mm represents 1 logical pixel?
As it is mentioned here
Flutter follows a simple density-based format like iOS. Assets might
be 1.0x, 2.0x, 3.0x, or any other multiplier. Flutter doesn’t have dps
but there are logical pixels, which are basically the same as
device-independent pixels. The so-called devicePixelRatio expresses
the ratio of physical pixels in a single logical pixel.
And as mentioned 1.0x logical pixel ratio represents mdpi in Android density qualifiers. And according to this, mdpi ≃ 160DPI and as dpi represents the number of individual dots that can be placed in a line within the span of 1 inch (2.54 cm) so:
160dpi = 160 dots per inch = 6.299 dots per mm
And as mdpi ≃ 160DPI and 1 logical pixel represents mdpi so:
1.0x logical pixel ratio ≃ 6.299 dots per mm
To display a widget at a real size.
double millimeterToSize(double millimeter) => millimeter * 160 * 0.03937;

Carrierwave difference between resizing options

I am trying to understand the difference between resize_to_fill, resize_to_fit, and resize_to_limit.
Say I have two images. Image A is 500x300 and image B is 300x100
resize_to_fill 300 x 300
A - will just slice off 200 pixels of width?
B - will blow it up to 900x300 and then slice off 600px of width?
resize_to_fit 300 x 300
A - will shrink it down to 300x180?
B - will not change anything?
resize_to_limit -- not sure what the difference between this one and resize_to_fit is..
Can someone explain in a way that's easy to understand?

SpriteKit SKLabelNode font size to pixels

What's a clean way to use SKLabelNode's fontSize to map to pixels? I'm trying to place text inside a box that fits in a fixed box proportionately.
According to apple's documentation, 1 font point is equal to 2 pixels.
For example, a 90 pixel font is equal to mytext.fontSize = 90 / 2

imcrop matlab does not return right size

I want to use imcrop to crop images in matlab. But, sometimes it results the images which has one size more than what I want. Basically, my question is same as this.
http://www.mathworks.se/matlabcentral/answers/46737-how-do-i-make-the-imcrop-function-accurate-to-the-pixel
But, even this is not solved. Please help!
imcrop does return the "right size", i.e. the size specified in its documentation. A rectangle r1 = [20 20 40 30] is always not sometimes 21 by 11 pixels wide.
If your expectation of what the right size should be is different, you could index the rectangle differently. If you want a rectangle with pixel (20,20) as its upper left edge and 20 pixels tall and 10 pixels wide, you can specify r1 = [20, 20, 39, 29].
imcrop works like this because it operates on image data consisting of pixels. Pixels are being indexed, not points in space. Mathematically, a point has no width and no height, but a pixel has the width and height of one pixel.
To provide an extreme example, r2 = [5, 6, 8, 9] includes the pixels from row 5 to 6 and column 8 to 9, and is thus 2 by 2 pixels wide.
I could not really resolve the problem. But, I applied a simple work around. I used imcrop to crop with the desired sizes. And then resize the cropped image to desired size again.

Resources