java+opencv 目標影像調整
java+opencv 進行影像匹配,FeatureDetector ,DescriptorExtractor
DescriptorMatcher 配合使用。分別進行關鍵點檢測,提取描述向量,特徵匹配。
廢話少說直接上程式碼。有問題可留言!!!opencv+java 論壇
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.calib3d.*;
public class testFeatureDetector
{
/**
*
* @param scene 參考圖
* @param object 目標需要調整圖
*/
static void tiaozheng2(Mat scene,Mat object)
{
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
// DETECTION
// first image
Mat descriptors_scene= new Mat();
MatOfKeyPoint keypoints_scene= new MatOfKeyPoint();
detector.detect(scene, keypoints_scene);
descriptor.compute(scene, keypoints_scene, descriptors_scene);
// second image
Mat descriptors_object= new Mat();
MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
detector.detect(object, keypoints_object);
descriptor.compute(object, keypoints_object,descriptors_object);
// MATCHING
// match these two keypoints sets
List<MatOfDMatch> matches = new ArrayList<MatOfDMatch>();
matcher.knnMatch(descriptors_object, descriptors_scene,matches, 5);
/////////////////////////////////////////////////////////////
// ratio test
LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
for (Iterator<MatOfDMatch> iterator = matches.iterator(); iterator.hasNext();) {
MatOfDMatch matOfDMatch = (MatOfDMatch) iterator.next();
if (matOfDMatch.toArray()[0].distance / matOfDMatch.toArray()[1].distance < 0.9) {
good_matches.add(matOfDMatch.toArray()[0]);
}
}
// get keypoint coordinates of good matches to find homography and remove outliers using ransac
List<Point> pts_object = new ArrayList<Point>();
List<Point> pts_scene = new ArrayList<Point>();
for(int i = 0; i<good_matches.size(); i++){
pts_object.add(keypoints_object.toList().get(good_matches.get(i).queryIdx).pt);
pts_scene.add(keypoints_scene.toList().get(good_matches.get(i).trainIdx).pt);
}
// convertion of data types - there is maybe a more beautiful way
Mat outputMask = new Mat();
MatOfPoint2f pts_objectMat = new MatOfPoint2f();
pts_objectMat.fromList(pts_object);
MatOfPoint2f pts_sceneMat = new MatOfPoint2f();
pts_sceneMat.fromList(pts_scene);
Mat Homog = Calib3d.findHomography(pts_objectMat, pts_sceneMat, Calib3d.RANSAC, 10, outputMask);
Mat resultMat=new Mat(new Size(object.cols(),object.rows()),object.type());
Imgproc.warpPerspective(object,object, Homog, resultMat.size());
// // outputMask contains zeros and ones indicating which matches are filtered
LinkedList<DMatch> better_matches = new LinkedList<DMatch>();
for (int i = 0; i < good_matches.size(); i++)
{
// System.out.println(outputMask.get(i, 0)[0]);
if (outputMask.get(i, 0)[0] != 0.0) {
better_matches.add(good_matches.get(i));
}
}
// System.out.println(better_matches.toString());
/////////////////////////////////////////////////////////////
// // DRAWING OUTPUT
Mat outputImg = new Mat();
// this will draw all matches, works fine
MatOfDMatch better_matches_mat = new MatOfDMatch();
better_matches_mat.fromList(better_matches);
// System.out.println(better_matches_mat.toString());
Features2d.drawMatches( object, keypoints_object, scene, keypoints_scene,better_matches_mat, outputImg);
Mat outputImg2 = new Mat();
Features2d.drawMatches2( object, keypoints_object, scene, keypoints_scene,matches, outputImg2);
Highgui.imwrite("result.jpg", outputImg);
Highgui.imwrite("result2.jpg", outputImg2);
}
/**
*
* @param scene 參考圖
* @param object 目標需要調整圖
*/
static void tiaozheng(Mat scene,Mat object)
{
FeatureDetector detector =FeatureDetector.create(FeatureDetector.SURF);
MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene = new MatOfKeyPoint();
detector.detect(object, keypoints_object);
detector.detect(scene, keypoints_scene);
Mat descriptors_object=new Mat();
Mat descriptors_scene=new Mat();
DescriptorExtractor extractor=DescriptorExtractor.create(DescriptorExtractor.ORB);
extractor.compute( object, keypoints_object, descriptors_object );
extractor.compute( scene, keypoints_scene, descriptors_scene );
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors_object, descriptors_scene, matches);
DMatch[] tmmpDMatchs=matches.toArray();
double max_dist = 0; double min_dist =400;
for( int i = 0; i < descriptors_object.rows(); i++ )
{
double dist =tmmpDMatchs[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
for (int i = 0; i < descriptors_object.rows();++i)
{
double dist =tmmpDMatchs[i].distance;
if (dist < 3*min_dist )
{
good_matches.add(tmmpDMatchs[i]);
}
}
List<Point> pts_scene = new ArrayList<Point>();
List<Point> pts_object = new ArrayList<Point>();//
for(int i = 0; i<good_matches.size(); i++)
{
pts_object.add(keypoints_object.toList().get(good_matches.get(i).queryIdx).pt);
pts_scene.add(keypoints_scene.toList().get(good_matches.get(i).trainIdx).pt);
}
Mat outputMask = new Mat();
MatOfPoint2f pts_sceneMat = new MatOfPoint2f();
pts_sceneMat.fromList(pts_scene);
MatOfPoint2f pts_objectMat = new MatOfPoint2f();
pts_objectMat.fromList(pts_object);
Mat Homog = Calib3d.findHomography(pts_objectMat, pts_sceneMat, Calib3d.RANSAC, 10, outputMask);
Mat resultMat=new Mat(new Size(scene.cols(),scene.rows()),scene.type());
Imgproc.warpPerspective(object,object, Homog, resultMat.size());
}
public static void main(String args[])
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat scene=Highgui.imread("00.jpg");//參考
Mat object=Highgui.imread("11.jpg");//調整目標
object.convertTo(object, CvType.CV_8UC3);// 目標圖
scene.convertTo(scene, CvType.CV_8UC3);// 參考圖
Mat img2=object.submat(new Rect(0,0,object.width(),object.height()*9/10));
Mat img1=scene.submat(new Rect(0,0,object.width(),object.height()*9/10));
Highgui.imwrite("scene.jpg",img1);//調整目標
Highgui.imwrite("object.jpg",img2);//調整目標
// tiaozheng(img1,img2);
tiaozheng2(img1,img2);
Highgui.imwrite("object2.jpg",img2);
}
}
“`
相關文章
- Iridient Developer for macRAW影像調整軟體DeveloperMac
- 新增的影像批次調整背景或顯示方法
- 如何調整畫布內容並獲得調整大小(壓縮)的影像Base64資料?
- 深度學習之影像目標檢測速覽深度學習
- python語言之影像處理:亮度調整演算法Python演算法
- 如何使用Automator服務在Mac上快速調整影像大小Mac
- 04-OpenCvSharp4調整影像亮度和對比度OpenCV
- Mac 使用心得,調整MacOS的游標大小Mac
- iOS UIView分類調整控制元件座標iOSUIView控制元件
- 筆記: Oracle 11g效能調整(11.2)目錄筆記Oracle
- Gamma調整GAM
- 雲伺服器centos6.5調整home根目錄大小伺服器CentOS
- win10系統如何使用自帶Photos應用調整影像大小Win10
- 目標檢測和影像分類及其相關計算機視覺的影像分佈計算機視覺
- Oracle效能調整之--DML語句效能調整Oracle
- 目標跟蹤:KCF--調通C++程式碼C++
- 分散式 | 動態調整 DBLE 內執行緒池的數目分散式執行緒
- Oracle效能最佳化調整--調整重做機制Oracle
- 網路調整——效能調整手冊和參考
- Nginx調整(一)Nginx
- oracle 效能調整Oracle
- 調整策略工具
- Win10電腦怎麼調整游標閃爍速度Win10
- 亞馬遜目標價遭五名分析師下調亞馬遜
- 從單幅影像到雙目立體視覺的3D目標檢測演算法視覺3D演算法
- echarts調整圖表和標題的距離,以及設定高度Echarts
- 天貓淘寶合併四天後,花旗下調阿里目標價阿里
- oracle 線上調整redoOracle
- 字串的調整II字串
- MySQL引數調整MySql
- 資料塊調整
- AIX 調整時區AI
- oracle效能調整(1)Oracle
- oracle效能調整(2)Oracle
- ORACLE效能調整--1Oracle
- ORACLE效能調整---2Oracle
- Oracle 效能調整for HWOracle
- (zt)Oracle效能調整Oracle