How to set a Threshold for Imgproc.matchTemplate opencv android? - android

int match_method = Imgproc.TM_CCOEFF;
int result_cols = labReport.cols() - textMat.cols() + 1;
int result_rows = labReport.rows() - textMat.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
Imgproc.matchTemplate(labReport, textMat, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF
|| match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
return matchLoc;
I'm using this code. I am trying to find a template in my image, however if I use a template that I know is not in the image, Imgproc.matchTemplate, it will return me a point as a result.
I need something like threshold for this. I need similarity more than 80% and if it's not, then return me some value like null.

Related

OpenCV haarCascade whit Optical flow tracking

i'm trying to track objects with Optical flow in android after using a Haar Cascade detection like in the code below and i have this error can anyone help me with this
E/cv::error(): OpenCV(3.4.12) Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray), file /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp, line 1259
E/org.opencv.video: video::calcOpticalFlowPyrLK_15() caught cv::Exception: OpenCV(3.4.12) /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp:1259: error: (-215:Assertion failed) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function 'virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray)'
E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: opencv.org, PID: 31380
CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.12) /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp:1259: error: (-215:Assertion failed) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function 'virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray)'
]
enter code hereMatOfRect cars = new MatOfRect();
if (mDetectorType == JAVA_DETECTOR) {
if (mJavaDetector != null) {
mJavaDetector.detectMultiScale(matGray, cars,1.1, 2, 2,
// TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size(mAbsoluteCarSize, mAbsoluteCarSize), new Size());
}
}
else {
Log.e(TAG, "Detection method is not selected!");
}
Rect[] carsArray = cars.toArray();
ArrayList<Point> CurrentCars = new ArrayList<>();
for (int i = 0; i < carsArray.length; i++)
{
Imgproc.rectangle(matRGB, carsArray[i].tl(), carsArray[i].br(),
CAR_RECT_COLOR, 3);
xCenter = (carsArray[i].x + carsArray[i].width + carsArray[i].x) / 2;
yCenter = (carsArray[i].y + carsArray[i].y + carsArray[i].height) / 2;
Point center = new Point(xCenter, yCenter);
CurrentCars.add(center);
Imgproc.putText(matRGB, "[" + center.x + "," + center.y + "]",
new Point(center.x + 20, center.y + 20),
Core.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255,
255));
}
if (previousPoints.empty()){
previousPoints.fromList(CurrentCars);
matGray.copyTo(matPrevGray);
}
currentPoints = new MatOfPoint2f();
MatOfByte status = new MatOfByte();
MatOfFloat err = new MatOfFloat();
TermCriteria criteria = new TermCriteria(TermCriteria.COUNT + TermCriteria.EPS,10,0.03);
Video.calcOpticalFlowPyrLK(matPrevGray,matGray,previousPoints,currentPoints,status,err);
byte StatusArr[] = status.toArray();
Point p0Arr[] = previousPoints.toArray();
Point p1Arr[] = currentPoints.toArray();
ArrayList<Point> good_new = new ArrayList<>();
for (int i = 0; i<StatusArr.length ; i++ ) {
if (StatusArr[i] == 1) {
good_new.add(p1Arr[i]);
Imgproc.line(matRGB, p1Arr[i], p0Arr[i], new Scalar(255,255,0,0),2);
Imgproc.circle(matRGB, p1Arr[i],5, new Scalar(255,255,0,0),-1);
}
}
currentPoints.copyTo(previousPoints);
matGray.copyTo(matPrevGray);
matPrevGray is empty. that's what it's saying.

Perspective Transform OpenCV

I'm new to OpenCV on Android and try to do Perspective Transform but I don't know how to use getperspectivetransform() and warpperspective() functions.I could detect rectangle from an image, but don't know how to warp.
Here is the detect rectangle function:
Mat tempMat = new Mat();
Mat src = new Mat();
Utils.bitmapToMat(image, tempMat);
Imgproc.cvtColor(tempMat, src, Imgproc.COLOR_BGR2RGB);
Mat blurred = src.clone();
Imgproc.medianBlur(src, blurred, 9);
Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
List<Mat> blurredChannel = new ArrayList<Mat>();
blurredChannel.add(blurred);
List<Mat> gray0Channel = new ArrayList<Mat>();
gray0Channel.add(gray0);
MatOfPoint2f approxCurve = new MatOfPoint2f();
double maxArea = 0;
int maxId = -1;
for (int c = 0; c < 3; c++) {
int ch[] = { c, 0 };
Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));
int thresholdLevel = 1;
for (int t = 0; t < thresholdLevel; t++) {
if (t == 0) {
Imgproc.Canny(gray0, gray, 50, 50, 3, true); // true ?
Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
// ?
} else {
Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
Imgproc.THRESH_BINARY,
(src.width() + src.height()) / 200, t);
}
Imgproc.findContours(gray, contours, new Mat(),
Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
for (MatOfPoint contour : contours) {
MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());
double area = Imgproc.contourArea(contour);
approxCurve = new MatOfPoint2f();
Imgproc.approxPolyDP(temp, approxCurve,
Imgproc.arcLength(temp, true) * 0.02, true);
if (approxCurve.total() == 4 && area >= maxArea) {
double maxCosine = 0;
List<Point> curves = approxCurve.toList();
for (int j = 2; j < 5; j++) {
double cosine = Math.abs(angle(curves.get(j % 4),
curves.get(j - 2), curves.get(j - 1)));
maxCosine = Math.max(maxCosine, cosine);
}
if (maxCosine < 0.45) {
maxArea = area;
maxId = contours.indexOf(contour);
}
}
}
}
}
I draw rectangle with this statement.
if (maxId >= 0) {
Rect rect = Imgproc.boundingRect(contours.get(maxId));
Imgproc.rectangle(src, rect.tl(), rect.br(), new Scalar(255, 0, 0,
.8), 4);
}
After that I convert mat to bitmap and show on an imageview.
Here is the screenshot
So my problem is warpping, How can I warp the rectangle and rotate it?
and If it is possible, how can I improve detecting rectangle? Any hints?
(OpenCV Android SDK Ver: 3.41, Android Studio Ver: 3.01)
If you are looking to warp the detected contour into rectangle,
Get the contours of the rectangle
find convex hull of the contour
Using approxPolyDP reduce the convex hull points into 4 points
fit line to consecutive points (ex, if pts is the array, lines are fit as follows l1 = line Between(pts[0], pts[1]), l2 = line Between(pts[1], pts[2]), l3 = line Between(pts[2], pts[3]), l4 = lineBetween(pts[3], pts[0])
find the intersection between these lines, you'll end up with four points
Order the points in clockwise order (inputCorners = TopLeft, TopRight, BottomRight, BottomLeft)
create an output image with needed resolution and make the corner points in the same clockwise order ((0,0), (0, cols), (rows, cols), (rows, 0))
find homography using the function
Mat homography = Calib3d.findHomography(inputCorners, imageCorners, Calib3d.RANSAC, 10);
using the output homography matrix, warp the input image using the function
Imgproc.warpPerspective(image, outputMat, homography, new Size(image.cols(), image.rows()));
you can refer to the following link
This is my kotlin extensin version you can use it in your projects.
fun Bitmap.perspectiveTransform(srcPoints: List<org.opencv.core.Point>) :
Bitmap{
val dstWidth = max(
srcPoints[0].distanceFrom(srcPoints[1]),
srcPoints[2].distanceFrom(srcPoints[3])
)
val dstHeight = max(
srcPoints[0].distanceFrom(srcPoints[2]),
srcPoints[1].distanceFrom(srcPoints[3])
)
val dstPoints: List<org.opencv.core.Point> = listOf(
org.opencv.core.Point(0.0, 0.0),
org.opencv.core.Point(dstWidth, 0.0),
org.opencv.core.Point(0.0, dstHeight),
org.opencv.core.Point(dstWidth, dstHeight)
)
return try {
val srcMat = Converters.vector_Point2d_to_Mat(srcPoints)
val dstMat = Converters.vector_Point2d_to_Mat(dstPoints)
val perspectiveTransformation =
Imgproc.getPerspectiveTransform(srcMat, dstMat)
val inputMat = Mat(this.height, this.width, CvType.CV_8UC1)
Utils.bitmapToMat(this, inputMat)
val outPutMat = Mat(dstHeight.toInt(), dstWidth.toInt(), CvType.CV_8UC1)
Imgproc.warpPerspective(
inputMat,
outPutMat,
perspectiveTransformation,
Size(dstWidth, dstHeight)
)
val outPut = Bitmap.createBitmap(
dstWidth.toInt(),
dstHeight.toInt(), Bitmap.Config.RGB_565
)
//Imgproc.cvtColor(outPutMat , outPutMat , Imgproc.COLOR_GRAY2BGR)
Utils.matToBitmap(outPutMat , outPut)
outPut
}
catch ( e : Exception){
e.printStackTrace()
this
}
}
To use distance from I write another extension function
fun org.opencv.core.Point.distanceFrom(srcPoint: org.opencv.core.Point):
Double {
val w1 = this.x - srcPoint.x
val h1 = this.y - srcPoint.y
val distance = w1.pow(2) + h1.pow(2)
return sqrt(distance)
}
Also in this answer the correct src Points indices are :
0 : topleft
1 : topRight
2 : bottomLeft
3 : bottomRight
Good luck

Image Template matching in android using openCV

I am trying to make an automation android applicaton which finds an image(template/subimage) inside another image(main/bigger image).
Menu image is from onePlus 3T.
Whatsapp icon image is from motoG 3.
I have tried to find whatsapp image from oneplus 3T in its menu image and it was successfully found.
But, when i am trying to find some subimage from different device of different screensize, it is not working.
Can someone please help. Below is the code I am using.
class MatchingDemo {
public Mat run(Mat img, Mat templ, String outFile, int match_method) {
System.out.println("\nRunning Template Matching");
// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
System.out.println("matchloc.x "+ matchLoc.x);
System.out.println("templ.cols "+ templ.cols());
System.out.println("matchloc.y "+ matchLoc.y);
System.out.println("templ.rows "+ templ.rows());
// / Show me what you got
Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0), 20);
// Save the visualized detection.
System.out.println("Writing "+ outFile);
Imgcodecs.imwrite(outFile, img);
return img;
}
}
I cropped the template image from your given snapshot and everything worked fine:
New template Image:
Code:
import cv2
import numpy as np
img_1 = cv2.imread("path/to/snapshot", 0)
img_rgb = cv2.imread("path/to/snapshot")
template_img = cv2.imread("path/to/template", 0)
h, w = template_img.shape
res = cv2.matchTemplate(img_1, template_img, cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
cv2.rectangle(img_rgb, max_loc, (max_loc[0]+w, max_loc[1]+h), np.array([0, 0, 255]), 3)
cv2.imwrite("./debug.png", img_rgb)
Output:
Note: matchTemplate is a very basic implementation, to get more scale invariant results you may try with SIFT features

Error Converting a CtvColor on Android and OpenCV on Eclipse

i'm developing to Android using openCV in Eclipse. I'm trying do template matching Frame a Frame. I can't convert the template and doing the match template on it. I'm using that function:
public void initialize(){
if (src.empty())
return;
if(template == null){
Mat templ = Highgui.imread(getFileAbsPath("1.png",
Highgui.CV_LOAD_IMAGE_UNCHANGED);
template = new Mat(templ.size(), CvType.CV_32F);
Imgproc.cvtColor(templ, (Mat) template, Imgproc.COLOR_BGR2RGBA);
}
}
private String getFileAbsPath(String fileName) {
File f = new File(cacheDir, fileName);
return f.getAbsolutePath();
}
I get a error on:
Imgproc.cvtColor(templ, (Mat) template, Imgproc.COLOR_BGR2RGBA);
My image is that (it's number 1):
https://drive.google.com/file/d/0B5tH_Qo3-GvhaV9QSTUteXFiQmM/view?usp=sharing
Next, I've my method:
#Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
src = inputFrame.rgba();
initialize();
int match_method = Imgproc.TM_SQDIFF;
// Create the result matrix
int result_cols = src.cols() - ((Mat) template).cols() + 1;
int result_rows = src.rows() - ((Mat) template).rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);
// Do the Matching and Normalize
Imgproc.matchTemplate(src, (Mat) template, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
Rect roi = new Rect((int) matchLoc.x, (int) matchLoc.y, ((Mat) template).cols(), ((Mat) template).rows());
Core.rectangle(src, new Point(roi.x, roi.y), new Point(roi.width - 2, roi.height - 2), new Scalar(255, 0, 0, 255), 2);
return src;
}
I get a error on that line:
Imgproc.matchTemplate(src, (Mat) template, result, match_method);
I can't do the match, don't know why ... Cans someone help me ?

Template Matching in Android using openCV

I'm trying to match an image with the camera input in Android using template matching. When i try this with static 2 images like in here: OpenCV Template Matching example in Android, everything works just fine. But when I try to use the captured images from the camera, I do not get the correct result. Following is the code that I have written:
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
Mat img = Highgui.imread(baseDir + "/mediaAppPhotos/img2.png");
Mat templ = Highgui.imread(baseDir+ "/mediaAppPhotos/chars.png");
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_cols, result_rows, CvType.CV_32FC1);
// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, Imgproc.TM_CCOEFF);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1,
new Mat());
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (Imgproc.TM_CCOEFF == Imgproc.TM_SQDIFF
|| Imgproc.TM_CCOEFF == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
// / Show me what you got
Core.rectangle(
img,
matchLoc,
new Point(matchLoc.x + templ.cols(), matchLoc.y
+ templ.rows()), new Scalar(0, 255, 0));
// Save the visualized detection.
System.out.println("Writing " + baseDir+ "/mediaAppPhotos/result.png");
Highgui.imwrite(baseDir + "/mediaAppPhotos/result.png", img);
I want to this template matching to work when the image is captured from the camera as well. Any help is greatly appreciated!
maybe is like this:
https://play.google.com/store/apps/details?id=in.mustafaak.imagematcher&hl=es_419
code available in github

Categories

Resources