圖像保持點位置的比例分辨率 (Scale resolution of image keeping point locations)


問題描述

圖像保持點位置的比例分辨率 (Scale resolution of image keeping point locations)

考慮 1920, 1017 像素圖像上坐標 821, 435 處的點,該點落在它的中間某處。現在,將圖像縮小到 1243, 633 像素大。顯然,我們的點不再在那些坐標上,而是隨著圖像一起縮小。我將如何計算新點的坐標?

對於以下假設 originalX = 1920 & newX = 1243 並且我們現在正在求解第一個值 x(最終為 y 進行縮放)

我已經完成了 newX/originalX 以獲得 0.64739583 我可以用它來做 originalX * 0.64739583,得到1243,說明newXoriginalX64.739583%。現在,回到我們之前點的 x,821。新點的 x 應該是 482,除了 originalX * 0.64739583 = 531482 不是 531,因此這些點與 64.7

的縮放百分比不符合預期

什麼是正確的計算(如果這些描述的計算甚至是錯誤的並且圖片的某些部分縮放錯誤)才能獲得縮放點的新坐標?


參考解法

方法 1:

If such positions really take place, then scaling is not linear like

NewX = X * NewWidth / OldWidth

Instead I see the next: There is no scaling but shifting of image while keeping central position

middle of 1920 = 960
960 ‑ 821 = 139
middle of 1243 = 621
621 ‑ 482 = 139 !

So transformation law looks like (I cannot claim it for single example)

NewX = NewWidth/2 + (OldX ‑ OldWidth/2)

方法 2:

Your math is wrong. You say, "The new point's x is expected to be 482, except that originalX * 0.64739583 = 531." The new point's x is, in fact, 532 (rounded). You don't give information for why newPointX is expected to be 482, and originalX * 0.64739583 = 1243. As far as how you would, "calculate what the new point's coordinates," are, see the code below.

int originalX = 1920;
int originalY = 1017;

int newX = 1243;
int newY = 633;

double scaleX = (double)newX/originalX;
double scaleY = (double)newY/originalY;

Point originalPoint = new Point(821, 435);

Method:

Point calculateNewPoint(Point originalPoint, double pScaleX, double pScaleY) {

    return new Point((int)Math.round(pScaleX*originalPoint.x), (int)Math.round(pScaleY*originalPoint.y));
}

Which, taken from your first paragraph results in a returned point of (532, 271).

方法 3:

I had to solve a similar problem. I had a picture of 1920x1080 pixeles, and I want to reduce that picture an 1.3, then I want to know a point in the reduced picture, which position correspond that point in the original picture. I show a picture that I want to do.

Problem

Then, I got a reduced picture of 1477x831, since (1920/1.3, 1080/1.3). If a have a point in the original picture in the position (250,300), and I reduce the picture, the new point in the reduced picture, it will be in the position (192,231), since (250/1.3 , 300/1.3) is (192,231). Then, If I reduce the picture and I get the point (192,231) and I want to get the position in the original picture, I have only to do (192 x 1.3 ,231 x 1.3) is (250,300). We can use the factor of scaled that we want prefer, I have use 1.3, but we can use 2, to reduce the picture to half.

I share the code.

private double factorScale = 1.3;

private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
            int x = evt.getX(); // x = 192
            int y = evt.getY(); // y = 231
            int xWithOutScaled = (int) (factorScale  * x); // xWithOutScaled = 250
            int yWithOutScaled = (int) (factorScale * y); //  yWithOutScaled = 300
        }

    private void printImage(int positionImage) {
            java.awt.image.BufferedImage image = null;
            File file = new File[pathImage];
            try{
                image = ImageIO.read(file); 
            }catch(IOException ie){
                javax.swing.JOptionPane.showMessageDialog(this,"Error reading image file","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
            }
            int widthScaled = (int) (width / factorScale); // widthScaled= 1477 ,width = 1920
            int heightScaled = (int) (height / factorScale); // heightScaled=831 ,height = 1080
            Image img = image.getScaledInstance(widthScaled,heightScaled,java.awt.Image.SCALE_SMOOTH);
            ImageIcon newIcon = new ImageIcon(img);
            this.image.setIcon(newIcon);                                        
        }                                 

In your case, you have to calculate the factor scaled, that it is (1920/1243) = 1.54 and (1017/633)=1.60, In your case, the proportion is no the same in x an y because the size of the reduced image chosen, but if we only do in the x. The point in the original picture in the position 821, it is in the position 533 (821/1.54) in the reduced picture. In the same way, we can calculate the position in the original picture (533* x 1.54) is 821.

(by ScarszMBoJavaDonIgnacio Marín Reyes)

參考文件

  1. Scale resolution of image keeping point locations (CC BY‑SA 2.5/3.0/4.0)

#java #scale #scaling #calc #math






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論