日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android studio证件照代码,Android 修图(换证件照背景,污点修复)

發(fā)布時間:2024/9/27 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android studio证件照代码,Android 修图(换证件照背景,污点修复) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

背景

前段時間的一個周末,一個女生讓我?guī)退龘Q一下他的證件照背景,我又沒帶電腦。我又不好意思拒接,怎么辦呢?應(yīng)用商店下載一個證件照換背景的APP,瞬間換完,我正準(zhǔn)備保存時,跳出來一個支付框,如果你要保存,支付2元錢,出于面子,我只好掏了2塊錢,保存了。于是我就想,這種技術(shù)活,還給別人付錢,自己來擼吧.我是一個專職Android開發(fā),那么就用Android來擼吧.

先來了解一下Android里原生API對圖片操作,一般有兩種方式,

一種是利用好Canvas繪制圖片,

一種是利用Bitmap的原生API,獲取像素進(jìn)行操作

這兩種操作我都寫了對應(yīng)的文章,可以快速查看

image

image

今天的主題是在Android里使用OpenCv來操作圖片,并實(shí)現(xiàn)兩個不同的效果,換證件照背景和污點(diǎn)修復(fù).

代碼已經(jīng)托管在Github上,和上兩篇文章代碼地址一樣,分支with-photo-changecolor

Github ,如果你喜歡,歡迎star 謝謝

Android OpenCv 快速入門

環(huán)境搭建

原生的API對圖片的操作有限,并且一些顏色空間轉(zhuǎn)化麻煩,效率低,那我們使用一個專業(yè)的圖片操作庫來操作圖片,會變得容易些.

OpenCv有很多語言版本,當(dāng)然底層是c/c++,他支持Android/IOS,Windows,Mac等,我們直接選擇Android版本. 那么來搭建一下環(huán)境,有兩部

下載OpenCv SDK 地址,將SDK 打包成aar,集成到項(xiàng)目中,快速獲取aar,可以直接到我打好的包里獲取 Github中獲取. 打aar包很簡單,用Android Studio打開下載好的SDK,然后到其目錄下,執(zhí)行./gradlew assembleRelease 或者用側(cè)邊的輔助工具

image

集成到你要使用OpenCv的項(xiàng)目,如下

image

圖像灰度測試

集成完成后,進(jìn)行OpenCV SDK接入成功測試

private void initLoaderOpenCV() {

boolean success = OpenCVLoader.initDebug();

if (!success) {

Log.d(TAG, "初始化失敗");

}

}

public void gray(View view) {

Mat src = new Mat();

Mat dst = new Mat();

Utils.bitmapToMat(bitmap, src);

Imgproc.cvtColor(src, dst, Imgproc.COLOR_BGRA2GRAY);

Bitmap resultBitmap = getResultBitmap();

Utils.matToBitmap(dst, resultBitmap);

src.release();

dst.release();

showCompare(resultBitmap);

}

如果接入沒問題,就可以愉快的使用OpenCV了,是不是很簡單.

image

換證件照背景 (從藍(lán)色到紅色)

換證件照算法,直接使用了一個c++ 版本算法的,翻譯為Android的. c++文章地址

主要步驟:

把RGB圖像轉(zhuǎn)換到HSV空間

取背景的一小塊20*20,計(jì)算藍(lán)色背景的平均色調(diào)和飽和度

設(shè)置閾值,取出藍(lán)色背景替換為紅色背景

把HSV圖像轉(zhuǎn)換會RGB空間

濾波器去除邊緣效應(yīng)

Android 代碼如下:

private void startDetail() {

Mat image = new Mat();

Utils.bitmapToMat(bitmap, image);

Mat hsvImg = new Mat();

Imgproc.cvtColor(image, hsvImg, Imgproc.COLOR_BGR2HSV);

List list = new ArrayList<>();

Core.split(hsvImg, list);

Mat roiH = list.get(0).submat(new Rect(0, 0, 20, 20));

Mat roiS = list.get(1).submat(new Rect(0, 0, 20, 20));

Log.i(TAG,"start sum bg");

int SumH = 0;

int SumS = 0;

byte[] h = new byte[1];

byte[] s = new byte[1];

//取一塊藍(lán)色背景,計(jì)算出它的平均色調(diào)和平均飽和度

for (int i = 0; i < 20; i++) {

for (int j = 0; j < 20; j++) {

roiH.get(j, i, h);

roiS.get(j, i, s);

SumH = h[0] + SumH;

SumS = s[0] + SumS;

}

}

int avgH, avgS;//藍(lán)底的平均色調(diào)和平均飽和度

avgH = SumH / 400;

avgS = SumS / 400;

Log.i(TAG,"depth="+list.get(0).depth());

Log.i(TAG,"start sum detail all photo");

//遍歷整個圖像

int nl = hsvImg.height();

int nc = hsvImg.width();

// byte[] changeColor = new byte[]{127};

byte[] hArray = new byte[nl * nc];

byte[] sArray = new byte[nl * nc];

byte[] vArray = new byte[nl * nc];

list.get(0).get(0,0,hArray);

list.get(1).get(0,0,sArray);

// list.get(2).get(0,0,vArray);

int row,index;

for (int j = 0; j < nl; j++) {

row = j * nc;

for (int i = 0; i < nc; i++) {

index = row + i;

if(hArray[index] <= (avgH + 20) && hArray[index] >= (avgH - 20)

&& sArray[index] <= (avgS + 150)

&& sArray[index] >= (avgS -150)

){

hArray[index] = 127;

// sArray[index] = 0;

// vArray[index] = (byte) 255;

}

}

}

list.get(0).put(0,0,hArray);

list.get(1).put(0,0,sArray);

// list.get(2).put(0,0,vArray);

Log.i(TAG,"merge photo");

Core.merge(list,hsvImg);

Imgproc.cvtColor(hsvImg,image, Imgproc.COLOR_HSV2BGR);

Bitmap resultBitmap = getResultBitmap();

Utils.matToBitmap(image,resultBitmap);

Message obtain = Message.obtain();

obtain.obj = resultBitmap;

handler.sendMessage(obtain);

}

Mat 為OpenCV中圖像的保存,很類似Android里的Bitmap,他和Bitmap轉(zhuǎn)化需要借助OpenCv的Utils進(jìn)行,OpenCV的核心API可以查看官網(wǎng),此處主要使用了Imgproc

image

效果

image

污點(diǎn)修復(fù)

修復(fù)原理

先來說一下污點(diǎn)修復(fù)的算法,一篇論文提到的 《An ImageInpainting Technique Based On the Fast Marching Method》

image

可以簡單理解為p點(diǎn)為待修復(fù)區(qū)域,ε為修復(fù)半徑,把ε的值區(qū)域的值計(jì)算出來,用于修復(fù)P點(diǎn),直到修復(fù)整個Ω區(qū)域.

詳細(xì)可以查看論文:論文地址

實(shí)際修復(fù)

OpenCV 里面已經(jīng)實(shí)現(xiàn)了此算法,具體方法如下:

//OpenCV Photo.java

/**

* Restores the selected region in an image using the region neighborhood.

*

* @param src Input 8-bit, 16-bit unsigned or 32-bit float 1-channel or 8-bit 3-channel image.

* @param inpaintMask Inpainting mask, 8-bit 1-channel image. Non-zero pixels indicate the area that

* needs to be inpainted.

* @param dst Output image with the same size and type as src .

* @param inpaintRadius Radius of a circular neighborhood of each point inpainted that is considered

* by the algorithm.

* @param flags Inpainting method that could be cv::INPAINT_NS or cv::INPAINT_TELEA

*

* The function reconstructs the selected image area from the pixel near the area boundary. The

* function may be used to remove dust and scratches from a scanned photo, or to remove undesirable

* objects from still images or video. See <http://en.wikipedia.org/wiki/Inpainting> for more details.

*

* Note:

*

*

* An example using the inpainting technique can be found at

* opencv_source_code/samples/cpp/inpaint.cpp

*

*

* (Python) An example using the inpainting technique can be found at

* opencv_source_code/samples/python/inpaint.py

*

*

*/

public static void inpaint(Mat src, Mat inpaintMask, Mat dst, double inpaintRadius, int flags) {

inpaint_0(src.nativeObj, inpaintMask.nativeObj, dst.nativeObj, inpaintRadius, flags);

}

其中上面提到的原理算法為,INPAINT_TELEA.

來一張實(shí)際的圖操作修復(fù)一下,如下:

private void startInpaint() {

bitmap = BitmapUtils.getBitmapByAssetsNameRGB(this,"test.png");

Mat desc = new Mat(bitmap.getHeight(),bitmap.getWidth(),CvType.CV_8UC3);

//轉(zhuǎn)化為mat對象

Utils.bitmapToMat(bitmap, desc,true);

//轉(zhuǎn)化為3通道圖像

Mat src = new Mat();

Imgproc.cvtColor(desc,src,Imgproc.COLOR_RGBA2RGB);

//灰度圖像

Mat srcGray = new Mat();

Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_RGB2GRAY);

//中值濾波去燥

Imgproc.medianBlur(srcGray,srcGray,3);

//獲取污點(diǎn)的二值化圖像

Mat srcThresh = new Mat();

Imgproc.threshold(srcGray,srcThresh,242,255,Imgproc.THRESH_BINARY);

Log.i("test","srcThresh channels:"+srcThresh.channels() + ",type:"+ CvType.typeToString(CvType.depth(srcThresh.type())));

Log.i("test","src channels:"+src.channels() + ",type:"+ CvType.typeToString(CvType.depth(src.type())));

// Bitmap resultBitmap = getResultBitmap();

// Utils.matToBitmap(srcThresh, resultBitmap);

//修復(fù)圖像

Mat inpaintResult = new Mat();

Photo.inpaint(src,srcThresh,inpaintResult,3,Photo.INPAINT_TELEA);

//把結(jié)果轉(zhuǎn)化為bitmap 用于顯示

Bitmap resultBitmap = getResultBitmap();

Utils.matToBitmap(inpaintResult, resultBitmap);

Message obtain = Message.obtain();

obtain.obj = resultBitmap;

handler.sendMessage(obtain);

}

效果

image

總結(jié)

本篇文章,主要介紹了OpenCV怎么快速使用,并結(jié)合了兩個實(shí)際的例子,來進(jìn)一步說明借助OpenCV里的API,可以實(shí)現(xiàn)很多不錯的效果.

文中圖片來源網(wǎng)絡(luò),若又侵權(quán),請聯(lián)系作者,立刻刪除!

本篇文章的兩個例子代碼地址:github ,如果你喜歡迎star,后續(xù)關(guān)于圖片的操作,都會在此庫里更新.

推薦閱讀

總結(jié)

以上是生活随笔為你收集整理的android studio证件照代码,Android 修图(换证件照背景,污点修复)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。