日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 字符串相似度对比,Android中的OpenCV图像比较和相似度

發(fā)布時間:2025/3/20 Android 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 字符串相似度对比,Android中的OpenCV图像比较和相似度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Andrii Omelc..

7

但是我們可以看到兩個圖像都具有相同的視覺元素(in).

因此,我們應該比較不是整個圖像,而是"相同的視覺元素".Match如果不比較"模板"和"相機"圖像本身,您可以更多地提高價值,但處理方式相同(例如轉(zhuǎn)換為二進制黑/白)"模板"和"相機"圖像.例如,嘗試在兩個("模板"和"相機")圖像上找到藍色(模板徽標的背景)正方形,并比較該正方形(感興趣的區(qū)域).代碼可能是這樣的:

Bitmap bmImageTemplate = ;

Bitmap bmTemplate = findLogo(bmImageTemplate); // process template image

Bitmap bmImage = ;

Bitmap bmLogo = findLogo(bmImage); // process camera image same way

compareBitmaps(bmTemplate, bmLogo);

哪里

private Bitmap findLogo(Bitmap sourceBitmap) {

Bitmap roiBitmap = null;

Mat sourceMat = new Mat(sourceBitmap.getWidth(), sourceBitmap.getHeight(), CvType.CV_8UC3);

Utils.bitmapToMat(sourceBitmap, sourceMat);

Mat roiTmp = sourceMat.clone();

final Mat hsvMat = new Mat();

sourceMat.copyTo(hsvMat);

// convert mat to HSV format for Core.inRange()

Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV);

Scalar lowerb = new Scalar(85, 50, 40); // lower color border for BLUE

Scalar upperb = new Scalar(135, 255, 255); // upper color border for BLUE

Core.inRange(hsvMat, lowerb, upperb, roiTmp); // select only blue pixels

// find contours

List contours = new ArrayList<>();

List squares = new ArrayList<>();

Imgproc.findContours(roiTmp, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

// find appropriate bounding rectangles

for (MatOfPoint contour : contours) {

MatOfPoint2f areaPoints = new MatOfPoint2f(contour.toArray());

RotatedRect boundingRect = Imgproc.minAreaRect(areaPoints);

double rectangleArea = boundingRect.size.area();

// test min ROI area in pixels

if (rectangleArea > 400) {

Point rotated_rect_points[] = new Point[4];

boundingRect.points(rotated_rect_points);

Rect rect = Imgproc.boundingRect(new MatOfPoint(rotated_rect_points));

double aspectRatio = rect.width > rect.height ?

(double) rect.height / (double) rect.width : (double) rect.width / (double) rect.height;

if (aspectRatio >= 0.9) {

squares.add(rect);

}

}

}

Mat logoMat = extractSquareMat(roiTmp, getBiggestSquare(squares));

roiBitmap = Bitmap.createBitmap(logoMat.cols(), logoMat.rows(), Bitmap.Config.ARGB_8888);

Utils.matToBitmap(logoMat, roiBitmap);

return roiBitmap;

}

方法extractSquareMat()只從整個圖像中提取感興趣區(qū)域(徽標)

public static Mat extractSquareMat(Mat sourceMat, Rect rect) {

Mat squareMat = null;

int padding = 50;

if (rect != null) {

Rect truncatedRect = new Rect((int) rect.tl().x + padding, (int) rect.tl().y + padding,

rect.width - 2 * padding, rect.height - 2 * padding);

squareMat = new Mat(sourceMat, truncatedRect);

}

return squareMat ;

}

并compareBitmaps()為您的代碼包裝:

private void compareBitmaps(Bitmap bitmap1, Bitmap bitmap2) {

Mat mat1 = new Mat(bitmap1.getWidth(), bitmap1.getHeight(), CvType.CV_8UC3);

Utils.bitmapToMat(bitmap1, mat1);

Mat mat2 = new Mat(bitmap2.getWidth(), bitmap2.getHeight(), CvType.CV_8UC3);

Utils.bitmapToMat(bitmap2, mat2);

compareMats(mat1, mat2);

}

你的代碼作為方法:

private void compareMats(Mat img1, Mat img2) {

FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);

DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.BRIEF);

DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);

Mat descriptors1 = new Mat();

MatOfKeyPoint keypoints1 = new MatOfKeyPoint();

detector.detect(img1, keypoints1);

extractor.compute(img1, keypoints1, descriptors1);

//second image

// Mat img2 = Imgcodecs.imread(path2);

Mat descriptors2 = new Mat();

MatOfKeyPoint keypoints2 = new MatOfKeyPoint();

detector.detect(img2, keypoints2);

extractor.compute(img2, keypoints2, descriptors2);

//matcher image descriptors

MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptors1,descriptors2,matches);

// Filter matches by distance

MatOfDMatch filtered = filterMatchesByDistance(matches);

int total = (int) matches.size().height;

int Match= (int) filtered.size().height;

Log.d("LOG", "total:" + total + " Match:" + Match);

}

static MatOfDMatch filterMatchesByDistance(MatOfDMatch matches){

List matches_original = matches.toList();

List matches_filtered = new ArrayList();

int DIST_LIMIT = 30;

// Check all the matches distance and if it passes add to list of filtered matches

Log.d("DISTFILTER", "ORG SIZE:" + matches_original.size() + "");

for (int i = 0; i < matches_original.size(); i++) {

DMatch d = matches_original.get(i);

if (Math.abs(d.distance) <= DIST_LIMIT) {

matches_filtered.add(d);

}

}

Log.d("DISTFILTER", "FIL SIZE:" + matches_filtered.size() + "");

MatOfDMatch mat = new MatOfDMatch();

mat.fromList(matches_filtered);

return mat;

}

調(diào)整大小(縮放為50%)的結果是從您的問題結果中保存的圖像是:

D/DISTFILTER: ORG SIZE:237

D/DISTFILTER: FIL SIZE:230

D/LOG: total:237 Match:230

NB!這是一個快速而骯臟的例子,僅用于演示給定模板的方法.

PS getBiggestSquare()可以是那樣的(基于面積比較):

public static Rect getBiggestSquare(List squares) {

Rect biggestSquare = null;

if (squares != null && squares.size() >= 1) {

Rect square;

double maxArea;

int ixMaxArea = 0;

square = squares.get(ixMaxArea);

maxArea = square.area();

for (int ix = 1; ix < squares.size(); ix++) {

square = squares.get(ix);

if (square.area() > maxArea) {

maxArea = square.area();

ixMaxArea = ix;

}

}

biggestSquare = squares.get(ixMaxArea);

}

return biggestSquare;

}

總結

以上是生活随笔為你收集整理的android 字符串相似度对比,Android中的OpenCV图像比较和相似度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。