我想创建一个带有红色通道的项目,当我按下按钮时,它将进行图像处理(通道提取-红色通道),并且输出图像是灰度图像,但是当我合并时存在问题红色通道和灰度脚本。当我按下“灰色”按钮时,该应用程序已停止(无错误消息)。如果您可以解决我的问题,或者有另一种方法,请提供帮助,您所做的一切都是我对您的关心。
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Uri imageUri;
Bitmap grayBitmap, imageBitmap;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
OpenCVLoader.initDebug();
}
public void openGallery (View v){
Intent myIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(myIntent,100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK && data !=null)
{
imageUri = data.getData();
try
{
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (IOException e)
{
e.printStackTrace();
}
imageView.setImageBitmap(imageBitmap);
}
}
public void convertToGray(View v)
{
Mat grayMat = new Mat();
Mat redC = new Mat();
BitmapFactory.Options o = new BitmapFactory.Options();
o.inDither=false;
o.inSampleSize=4;
int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
grayBitmap= Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
//red channel
for (int x = 0; x < imageBitmap.getWidth(); x++)
{
for (int y = 0; y < imageBitmap.getHeight(); y++)
{
redChannel.setPixel(x, y, imageBitmap.getPixel(x, y) & 0xFFFF0000);
}
}
//bitmap to MAT
Utils.bitmapToMat(imageBitmap,redC);
Imgproc.cvtColor(redC, grayMat, Imgproc.COLOR_RGB2GRAY);
Utils.matToBitmap(grayMat, grayBitmap);
imageView.setImageBitmap(grayBitmap); }}
这是我的activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openGallery"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:text="@string/gallery" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="34dp"
android:layout_toEndOf="@+id/button"
android:onClick="convertToGray"
android:text="@string/gray" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:srcCompat="@mipmap/ic_launcher" />
如果您不理解或需要更多解释,请发表评论,谢谢