Tix
Tix

Reputation: 449

OpenCV, Android: Detecting an object from an image instead of real-time detection

I am making an android application that can detect an object from an image frame captured from a video.

The sample applications in openCV only have examples on real-time detection.

Additional Info: -I'm using Haar classifier

As of now I'm storing the frames captured in an array of ImageView, how can i use OpenCV to detect the object and draw a rectangle around it?

for(int i=0 ;i <6; i++)
        {
            ImageView imageView = (ImageView)findViewById(ids_of_images[i]);

            imageView.setImageBitmap(retriever.getFrameAtTime(looper,MediaMetadataRetriever.OPTION_CLOSEST_SYNC));
            Log.e("MicroSeconds: ", ""+looper);
            looper +=10000;
        }

Upvotes: 0

Views: 2335

Answers (1)

ImLearning
ImLearning

Reputation: 357

i hope you have integrated opencv 4 android library in your project . Now, you can convert image to Mat using opencv function

Mat srcMat = new Mat();
Utils.bitmapToMat(yourbitmap,srcMat);

Once, you have mat you can apply opencv functions to find rectangle objects from image. Now , follow the code to detect rectangle :

Mat mGray = new Mat();
cvtColor(mRgba, mGray, Imgproc.COLOR_BGR2GRAY, 1);
Imgproc.GaussianBlur(mGray, mGray, new Size(3, 3), 5, 10, BORDER_DEFAULT);
Canny(mGray, mGray, otsu_thresold, otsu_thresold * 0.5, 3, true); // edge detection using canny edge detection algorithm
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(mGray,contours,hierarchy,Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

Now , you have contours from image . So,you can get the max contour from it and draw it using drawContour() method :

for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++){
Imgproc.drawContours(src, contours, contourIdx, new Scalar(0, 0, 255)-1);
}

and you're done !! you can refer this link : Android using drawContours to fill region

hope it will help !!

Upvotes: 3

Related Questions