我正在opencv中的android项目上工作,我的任务是裁剪我触摸到的图像,在找到轮廓后我想裁剪那个部分。
问题是我已经找到了图像的轮廓,并且我想获取可以裁剪该特定部分的图像的每个轮廓的坐标。
所以,代码是这样的: -
ImageView imageView1, imageView2, imageView3;
Bitmap bmInImg, bmOutImg, bmImg;
private static final String TAG = "OCVSample::Activity";
private static final int PIC_CROP = 1;
private boolean mIsColorSelected = false;
private Mat mRgba, tmp,tmp1;
private Scalar mBlobColorRgba;
private Scalar mBlobColorHsv;
private ColorBlobDetector mDetector;
private Mat mSpectrum;
private Size SPECTRUM_SIZE;
private Scalar CONTOUR_COLOR;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "Trying to load OpenCV library");
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this,
mLoaderCallback)) {
Log.e(TAG, "Cannot connect to OpenCV Manager");
}
imageView1 = (ImageView) this.findViewById(R.id.imageView1);
imageView1.setOnTouchListener(this);
imageView2 = (ImageView) this.findViewById(R.id.imageView2);
imageView3 = (ImageView) this.findViewById(R.id.imageView3);
InputStream is;
is = this.getResources().openRawResource(R.drawable.mask);
bmInImg = BitmapFactory.decodeStream(is);
imageView1.setImageBitmap(bmInImg);
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
Log.i(TAG, "OpenCV loaded successfully");
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
public native boolean CannyJNI(int width, int height, int[] mPhotoIntArray,
int[] mCannyOutArray);
static {
System.loadLibrary("first-opencvjni");
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
tmp1 = new Mat(bmInImg.getWidth(), bmInImg.getHeight(),
CvType.CV_8UC1);
Utils.bitmapToMat(bmInImg, tmp1);
tmp= new Mat();
// 1) Apply gaussian blur to remove noise
Imgproc.GaussianBlur(tmp1, tmp, new Size(1, 1), 3);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(tmp, tmp, 10, 100);
bmOutImg = Bitmap.createBitmap(tmp.cols(), tmp.rows(),
Bitmap.Config.ARGB_8888);
Utils.matToBitmap(tmp, bmOutImg);
imageView3.setImageBitmap(bmOutImg);
// Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2RGBA);
// 2) AdaptiveThreshold -> classify as either black or white
Imgproc.adaptiveThreshold(tmp, tmp, 255,
Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, 1);
// 3) Invert the image -> so most of the image is black
Core.bitwise_not(tmp, tmp);
// 4) Dilate -> fill the image using the MORPH_DILATE
Imgproc.dilate(tmp, tmp, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, 1)));
//Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(tmp, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);
hierarchy.release();
// Imgproc.drawContours(tmp, contours, -1, new Scalar(Math.random()*255, Math.random()*255, Math.random()*255));
tmp1.copyTo(tmp);
Imgproc.drawContours(tmp, contours, -1, new Scalar(0,255,0), 1);
bmImg = Bitmap.createBitmap(tmp.cols(), tmp.rows(),
Bitmap.Config.ARGB_8888);
Utils.matToBitmap(tmp, bmImg);
imageView2.setImageBitmap(bmImg);
int positionX = (int) event.getX();
int positionY = (int) event.getY();
Log.i("ON TOUCH COORDINATES", "x: " + positionX + " y: "
+ positionY);
int color_selected = bmImg.getPixel(positionX, positionY);
int redValue = Color.red(color_selected);
int blueValue = Color.blue(color_selected);
int greenValue = Color.green(color_selected);
Log.i("COLOR SELECTED", " " + color_selected);
Log.i("redValue", " " + redValue);
Log.i("blueValue", " " + blueValue);
Log.i("greenValue", " " + greenValue);
}
return true;
}
private Scalar converScalarHsv2Rgba(Scalar mBlobColorHsv2) {
// TODO Auto-generated method stub
Mat pointMatRgba = new Mat();
Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, mBlobColorHsv2);
Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL,
4);
return new Scalar(pointMatRgba.get(0, 0));
}
void OutputBitmapToFile(Bitmap InBm, String Filename) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
InBm.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File f = new File(Filename);
try {
f.createNewFile();
// write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
所以,请在找到轮廓后帮我获取图像的坐标。
谢谢。