Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to get contours edge values in android opencv project
    primarykey
    data
    text
    <p>i am working on android project in opencv and my task is to crop the image where i touch that after finding contours i want to crop that only part.</p> <p>the problem is that i already found the contours of image and i want to get coordinates of every contour of image that i can crop that particular part. </p> <p>So, code is this:-</p> <pre><code>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 -&gt; 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 -&gt; so most of the image is black Core.bitwise_not(tmp, tmp); // 4) Dilate -&gt; 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&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;(); 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; } </code></pre> <p>So, please help me out to get the coordinates of image after finding contours.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload