我是android studio开发人员的新手,我希望将图像从RGB颜色空间转换为YIQ。我的应用程序包括将图像作为输入,然后将图像传输到YIQ,因此我使用Core.split从图像中获取红色,绿色和蓝色通道,然后应用方程式,但无法应用浮点数的矩阵,所以我使用标量类型并且仍然卡住。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "3:qinQctivity";
Button button;
ImageView imageView;
ArrayList<Mat> RGB = new ArrayList<Mat>(3);
ArrayList<Mat> YIQ = new ArrayList<Mat>(3);
Mat newImage;
Mat Blue,Green,Red,I,Y,Q,B,X,D,W;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
imageView = findViewById(R.id.image);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, getApplicationContext(), baseLoaderCallback);
} else {
baseLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
});
}
BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
super.onManagerConnected(status);
if (status == LoaderCallbackInterface.SUCCESS) {
try {
Y = new Mat();
Q = new Mat();
I = new Mat();
X = new Mat();
newImage = Utils.loadResource(getApplicationContext(), R.drawable.retinalimage, CvType.CV_32FC3);
Core.split(newImage, RGB);
Blue = RGB.get(0);
Red = RGB.get(1);
Green = RGB.get(2);
B = new Mat(); // result
D = new Mat(); // result
W = new Mat(); // result
/*working on Y channel*/
Scalar alpha_Y = new Scalar(0.299); // the factor
Log.i(TAG, "onManagerConnected: "+ alpha_Y.toString());
Scalar alpha1_Y = new Scalar(0.587); // the factor
Scalar alpha2_Y = new Scalar(0.114); // the factor
Core.multiply(Red,alpha_Y, B, CV_32F);
Log.i(TAG, "onManagerConnected: " + B);
Core.multiply(Green,alpha1_Y,D, CV_32F);
Core.multiply(Blue,alpha2_Y,W, CV_32F);
Core.add(B,D,Y);
Log.i(TAG, "onManagerConnected: "+ Y.toString());
Core.add(Y,W,Y);
/*working on I channel*/
/*I = 0.211 * Red - 0.523 * Green + 0.312 * Blue;*/
Mat Z = new Mat(); // result
Mat P = new Mat(); // result
Mat O = new Mat(); // result
Scalar alpha_I = new Scalar(0.211); // the factor
Scalar alpha1_I = new Scalar(0.523); // the factor
Scalar alpha2_I = new Scalar(0.312); // the factor
Core.multiply(Red,alpha_I,Z);
Core.multiply(Green,alpha1_I,P);
Core.multiply(Blue,alpha2_I,O);
Core.add(Z,P,I);
Core.add(I,O,I);
/*working on Q channel*/
/*Q = 0.596 * Red - 0.274 * Green - 0.322 * Blue;*/
Mat V = new Mat();
Mat W = new Mat();
Mat N = new Mat();
Scalar alpha_Q = new Scalar(0.596); // the factor
Scalar alpha1_Q = new Scalar(0.274); // the factor
Scalar alpha2_Q = new Scalar(0.322); // the factor
Core.multiply(Red,alpha_Q,V);
Core.multiply(Green,alpha1_Q,W);
Core.multiply(Blue,alpha2_Q,N);
Core.subtract(V,W,Q);
Core.subtract(Q,N,Y);
YIQ.set(0, Y);
YIQ.set(1, I);
YIQ.set(2, Q);
Log.i(TAG, "onManagerConnected: "+YIQ.toString());
Core.merge(YIQ,X);
Log.i(TAG, "onManagerConnected: "+X.toString());
showImage(X);
//
} catch(IOException e){
e.printStackTrace();
}
}
}
} ;
void showImage (Mat y){
Bitmap bm = Bitmap.createBitmap(y.width(), y.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(y, bm);
imageView.setImageBitmap(bm);
}
}
答案 0 :(得分:1)
更改这部分代码
YIQ.set(0, Y);
YIQ.set(1, I);
YIQ.set(2, Q);
像这样
YIQ.add(0, Y);
YIQ.add(1, I);
YIQ.add(2, Q);
这也可能与尝试优化代码的内存问题有关