您的位置 首页 > 数码极客

「android如何挂在opencv」如何提高android版本!

今天我们将制作一个Android应用程序,在OpenCV的帮助下将图像转换为铅笔素描。在我们开始之前:我假设您的android项目中已经安装了Open CV。

步骤1:打开activity_main。添加一个ImageView和3个按钮(第一个按钮用于从手机图库中选择图像,另两个按钮用于将图像转换为素描和彩色素描。

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" android:layout_above="@id/btn_contain" android:layout_marginBottom="10dp"/> <LinearLayout android:id="@+id/btn_contain" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="40dp"> <Button android:id="@+id/select_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select Image "/> <Button android:id="@+id/sketch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sketch"/> <Button android:id="@+id/colored_sketch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Colored Sketch"/> </LinearLayout> </RelativeLayout>

步骤2:打开MainAc,再onCreate中添加如下java代码。

private ImageView image; private Button select, sketch, colored @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView); O(); image = findViewById(R.id.image); select = findViewById); sketch = findViewById); colored = findViewById); }

步骤3:创建一个ImageChooser函数,从图库中选择图像并显示在ImageView中。

为图像选择器声明几个变量。

private final int SELECT_PHOTO = 1; private bitmap originBitmap = null; private File tempFile = new File("/sdcard;); private Bitmap processImg;

显示图像选择器的函数

private void showImageChooser() { Intent photoPickerIntent = new Inten); ("image/*"); startActivityForResult(photoPickerIntent, SELECT_PHOTO); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { (requestCode, resultCode, data); if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null != data) { Uri selectedImage = da(); InputStream imageStream; try { imageStream = getContentResolver().openInputStream(selectedImage); originBitmap = Bi(imageStream); } catch (FileNotFoundException e) { e.printStackTrace(); } if (originBitmap != null) { (); this.image.setImageBitmap(originBitmap); } } }

将OnClickListener设置为图像选择按钮

(new View.OnClickListener() { @Override public void onClick(View view) { showImageChooser(); } });

步骤4:让我们将效果应用到选定的图像。

为了将图像转换为素描,我们需要制作3个不同的图像层

  1. 灰度(第1层)
  2. 反转灰度图像(第2层)
  3. 将高斯模糊应用于inverted image(第3层)
  4. 用于铅笔素描的颜色减淡和混合(第3层,第1层),用于彩色素描的颜色减淡(原始图像,第1层)。

素描函数的java代码如下:

public void Sketch(Bitmap bitmap, String type){ bitmap=bi, true); Mat src = new Ma(), bi(), CvTy); Mat dest = new Ma(), bi(), CvTy); Mat grey = new Ma(), bi(), CvTy); Mat invert = new Ma(), bi(), CvTy); Bitmap inv, gray; U(bitmap, src); Img(src, grey, Img); Img(grey, grey, Img, 4); Core.bitwise_not( grey, invert); Img(invert,invert, new Size(11,11),0); inv = Bi(),invert.rows(),Bi); gray = Bi(),invert.rows(),Bi); U(invert, inv); U(grey, gray); Bitmap b = null; if ("normal")) { b = ColorDodgeBlend(inv, gray); }else if("colored")){ b = ColorDodgeBlend(inv, bitmap); } processImg = Bi(),de(),Bi); U(dest, processImg); image.setImageBitmap(b); }

颜色减淡和混合的java代码:

private static int colordodge(int in1, int in2) { float image = (float)in2; float mask = (float)in1; return ((int) ((image == 255) ? image:Math.min(255, (((long)mask << 8 ) / (255 - image))))); } public static Bitmap ColorDodgeBlend(Bitmap source, Bitmap layer) { Bitmap base = , true); Bitmap blend = layer.copy, false); IntBuffer buffBase = In() * ba()); ba(buffBase); bu(); IntBuffer buffBlend = In() * blend.getHeight()); blend.copyPixelsToBuffer(buffBlend); bu(); IntBuffer buffOut = In() * ba()); bu(); while () < bu()) { int filterInt = bu(); int srcInt = bu(); int redValueFilter = Color.red(filterInt); int greenValueFilter = Color.green(filterInt); int blueValueFilter = Color.blue(filterInt); int redValueSrc = Color.red(srcInt); int greenValueSrc = Color.green(srcInt); int blueValueSrc = Color.blue(srcInt); int redValueFinal = colordodge(redValueFilter, redValueSrc); int greenValueFinal = colordodge(greenValueFilter, greenValueSrc); int blueValueFinal = colordodge(blueValueFilter, blueValueSrc); int pixel = Color.argb(255, redValueFinal, greenValueFinal, blueValueFinal); bu(pixel); } bu(); ba(buffOut); blend.recycle(); return base; }

将OnClickListener添加到按钮

(new View.OnClickListener() { @Override public void onClick(View view) { if (originBitmap != null) { Sketch(originBitmap, "normal"); } } }); colored.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (originBitmap != null) { Sketch(originBitmap, "colored"); } } });

步骤5:在模拟器中构建和测试应用程序。

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“android如何挂在opencv,如何提高android版本,android如何root,android如何升级,android如何刷机”边界阅读