天天看點

Androidk開發之圖像局部放大算法Androidk開發之圖像局部放大算法

Androidk開發之圖像局部放大算法

前段時間有需求要做一個類似美圖秀秀的軟體,結果卡在增大眼睛這個功能了,到處搜尋,最多隻能見到各種論文,最氣的是有個家夥聲稱自己已經實作了該方法,卻不願意貢獻出自己的源碼,還說他那裡不歡迎隻會伸手的人,這種沒有開源精神的人,遲早會被淘汰!檢視了很多論文,終于寫出了該方法。

直接貼出代碼,如下

public static Bitmap bigEyes(Bitmap bitmap, int PointX, int PointY,
            int Radius, int Strength) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        // int[] pixNew = new int[width * height];
        // int[] pixOld = new int[width * height];
        int Left = PointX - Radius <  ?  : PointX - Radius; // 計算邊界值
        int Top = PointY - Radius <  ?  : PointY - Radius;
        int Bottom = PointY + Radius >= height ? height -  : PointY + Radius;
        int Right = PointX + Radius >= width ? width -  : PointX + Radius;
        Bitmap newBitmap = bitmap.copy(Bitmap.Config.RGB_565, true);
        // bitmap.getPixels(pixNew, Top * width + Left, width, Left, Top,
        // Radius * 2, Radius * 2);
        // bitmap.getPixels(pixOld, Top * width + Left, width, Left, Top,
        // Radius * 2, Radius * 2);
        int PowRadius = Radius * Radius;

        for (int Y = Top; Y <= Bottom; Y++) {
            int offSetY = Y - PointY;
            for (int X = Left; X <= Right; X++) {
                int offSetX = X - PointX;
                double XY = offSetX * offSetX + offSetY * offSetY;
                if (XY <= PowRadius) {
                    double ScaleFactor =  - XY / PowRadius;
                    ScaleFactor =  - (double) Strength /  * ScaleFactor; // 按照這種關系計算取樣點的位置
                    int PosX = (int) (offSetX * ScaleFactor + PointX);
                    int PosY = (int) (offSetY * ScaleFactor + PointY);
                    if (PosX < ) { // 放置越界
                        PosX = ;
                    } else if (PosX >= width) {
                        PosX = width - ;
                    }
                    if (PosY < ) {
                        PosY = ;
                    } else if (PosY >= height) {
                        PosY = height - ;
                    }
                    // int Speed = Y * width + X;
                    // int Index = PosY * width + PosX;
                    newBitmap.setPixel(X, Y, bitmap.getPixel(PosX, PosY));
                    // pixNew[Speed] = pixOld[Index];
                }
            }
        }
        // bitmap.setPixels(pixNew, Top * width + Left, width, Left, Top,
        // Radius * 2, Radius * 2);
        return newBitmap;
    }
           

應該很明了了,就不做注釋了,這還要注釋就是真正的伸手黨了。

繼續閱讀