package test;
import org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_highgui.imshow;
import static org.bytedeco.javacpp.opencv_highgui.waitKey;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
import static org.bytedeco.javacpp.opencv_imgproc.*;
public class PictureDiff {
public static void main(String[] args) {
Mat m1 = imread(TestCvEnvironment.pwd() + "/res/image/simple1.jpg");
Mat m2 = imread(TestCvEnvironment.pwd() + "/res/image/simple2.jpg");
// diff
Mat diff = new Mat();
absdiff(m1, m2, diff);
/* imshow("diff", diff);
waitKey(0);*/
// bi-color
Mat biColor = new Mat();
cvtColor(diff, biColor, CV_RGB2GRAY);
/*imshow("bi-color", biColor);
waitKey(0);*/
// threshold
Mat threshold = new Mat();
threshold(biColor, threshold, 15, 255, THRESH_BINARY);
imshow("threshold", threshold);
waitKey(0);
// dilate
Mat dilate = new Mat();
dilate(threshold, dilate, getStructuringElement(MORPH_RECT, new Size(15, 15)));
imshow("dilate", dilate);
waitKey(0);
// findContours
MatVector mv = new MatVector();
Mat hierarchy = new Mat();
findContours(dilate, mv, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
System.out.println("findContours : " + mv.size());
for (int i = 0; i < mv.size(); i++) {
drawContours(m1, mv, i, new Scalar(0, 0, 0, 0));
// minAreaRect
RotatedRect rRect = minAreaRect(mv.get(i));
Point2f rRectPoint = new Point2f(4);
rRect.points(rRectPoint);
for (int j = 0; j < 4; j++) {
line(m1, new Point((int) rRectPoint.position(j).x(), (int) rRectPoint.position(j).y()),
new Point((int) rRectPoint.position((j + 1) % 4).x(), (int) rRectPoint.position((j + 1) % 4).y()),
new Scalar(255, 0, 255, 255), 3, 1, 0);
}
rectangle(m1, rRect.boundingRect(), new Scalar(255, 0, 0, 255));
}
imshow("minAreaRect", m1);
imshow("src", m2);
waitKey(0);
mv.clear();
dilate.release();
threshold.release();
biColor.release();
diff.release();
m1.release();
m2.release();
}
}
图片找茬
猜你喜欢
转载自blog.csdn.net/wang805447391/article/details/79318946
今日推荐
周排行