// 1、先连上opencv包
System.setProperty("java.awt.headless", "false");
System.out.println(System.getProperty("java.library.path"));
URL url = ClassLoader.getSystemResource("lib/opencv/opencv_java4100.dll");
System.load(url.getPath());
// 2、计算倾斜的角度
String filePath = "需要处理的文件路径";
Mat src = Imgcodecs.imread(filePath);
Mat gary = new Mat();
Imgproc.cvtColor(src, gary, Imgproc.COLOR_RGB2GRAY);
Imgproc.GaussianBlur(gary, gary, new Size(3, 3), 0);
Imgproc.GaussianBlur(gary, gary, new Size(3, 3), 0);
Mat edge = new Mat();
Imgproc.Canny(gary, edge, 30, 50);
Mat lines = new Mat();
Imgproc.HoughLinesP(edge, lines, 1, Math.PI/180, 100, 50, 10);
double angle = 0.0;
for (int i = 0; i < lines.cols(); i++) {
double rho = lines.get(0, i)[0];
double theta = lines.get(0, i)[1];
double cosTheta = Math.cos(theta);
double sinTheta = Math.sin(theta);
double x0 = rho * cosTheta;
double y0 = rho * sinTheta;
double x1 = x0 + 1000 * (-sinTheta);
double y1 = y0 + 1000 * (cosTheta);
double x2 = x0 - 1000 * (-sinTheta);
double y2 = y0 - 1000 * (cosTheta);
angle += Math.atan2(y2 - y1, x2 - x1) * 180.0 / Math.PI;
}
angle /= lines.cols();
System.out.println("angle = " + angle);
if (angle < 0){
angle = 180 - angle;
}
// 3、旋转图像并显示,其实opencv也可以旋转图像,但效果感觉不如Java
BufferedImage image = ImageIO.read(new File(filePath));
BufferedImage rotate = ImageUtils.Rotate(image, angle);
BufferedImage grayImage = new BufferedImage(rotate.getWidth(),rotate.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
ColorConvertOp op = new ColorConvertOp(null);
grayImage = op.filter(rotate, grayImage);
Mat src = Mat.zeros(new Size(rotate.getWidth(), rotate.getHeight()), CvType.CV_8UC3);
byte[] imageData = ((DataBufferByte) grayImage.getRaster().getDataBuffer()).getData();
src.put(0,0,imageData);
HighGui.namedWindow("dst", HighGui.WINDOW_NORMAL);
HighGui.resizeWindow("dst", 1600, 900);
HighGui.imshow("dst", dst);
HighGui.waitKey(0);
src.release();
gary.release();
edge.release();
lines.release();
HighGui.destroyAllWindows();
System.exit(0);
// 上面 ImageUtils.Rotate是一个自定义的旋转方法,具体代码如下:
//region 图片旋转
/**
* 对图片进行旋转
*
* @param src 被旋转图片
* @param angel 旋转角度
* @return 旋转后的图片
*/
public static BufferedImage Rotate(Image src, int angel) {
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
// 计算旋转后图片的尺寸
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
src_width, src_height)), angel);
BufferedImage res = null;
res = new BufferedImage(rect_des.width, rect_des.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();
// 进行转换
g2.translate((rect_des.width - src_width) / 2,
(rect_des.height - src_height) / 2);
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
g2.drawImage(src, null, null);
return res;
}
/**
* 计算旋转后的图片
*
* @param src 被旋转的图片
* @param angel 旋转角度
* @return 旋转后的图片
*/
public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
// 如果旋转的角度大于90度做相应的转换
if (angel >= 90) {
if (angel / 90 % 2 == 1) {
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new Rectangle(new Dimension(des_width, des_height));
}
//endregion
标签:src,angel,多个,width,double,height,opencv,物品,Math From: https://www.cnblogs.com/Lee597/p/18370955