利用opencv去除图片上下左右黑边。
代码:
类:ImageUtils.java
/**
* 去除图片黑边,若无黑边,则原图返回。默认“全黑”阈值为 {@code BLACK_VALUE}
*
* @param srcMat 预去除黑边的Mat
* @return 去除黑边之后的Mat
*/
public static Mat removeBlackEdge(Mat srcMat) {
return removeBlackEdge(srcMat, BLACK_VALUE);
}
/**
* 去除图片黑边,若无黑边,则原图返回。
*
* @param blackValue 一般低于5的已经是很黑的颜色了
* @param srcMat 源Mat对象
* @return Mat对象
*/
public static Mat removeBlackEdge(Mat srcMat, int blackValue) {
// 预截取,默认播放条等情况的处理
Mat smallMat = cut(srcMat, (int) (srcMat.width() * 0.02), (int) (srcMat.height() * 0.02));
// 灰度
Mat grayMat = gray(smallMat);
int topRow = 0;
int leftCol = 0;
int rightCol = grayMat.width() - 1;
int bottomRow = grayMat.height() - 1;
// 上方黑边判断
for (int row = 0; row < grayMat.height(); row++) {
// 判断当前行是否基本“全黑”,阈值自定义;
if (sum(grayMat.row(row)) / grayMat.width() < blackValue) {
// 更新截取条件
topRow = row;
} else {
break;
}
}
// 左边黑边判断
for (int col = 0; col < grayMat.width(); col++) {
// 判断当前列是否基本“全黑”,阈值自定义;
if (sum(grayMat.col(col)) / grayMat.height() < blackValue) {
// 更新截取条件
leftCol = col;
} else {
break;
}
}
// 右边黑边判断
for (int col = grayMat.width() - 1; col > 0; col--) {
// 判断当前列是否基本“全黑”,阈值自定义;
if (sum(grayMat.col(col)) / grayMat.height() < blackValue) {
// 更新截取条件
rightCol = col;
} else {
break;
}
}
// 下方黑边判断
for (int row = grayMat.height() - 1; row > 0; row--) {
// 判断当前行是否基本“全黑”,阈值自定义;
if (sum(grayMat.row(row)) / grayMat.width() < blackValue) {
// 更新截取条件
bottomRow = row;
} else {
break;
}
}
int x = leftCol;
int y = topRow;
int width = rightCol - leftCol;
int height = bottomRow - topRow;
if (leftCol == 0 && rightCol == grayMat.width() - 1 && topRow == 0 && bottomRow == grayMat.height() - 1) {
return srcMat;
}
return cut(smallMat, x, y, width, height);
}
/**
* 求和
*
* @param mat mat
* @return sum
*/
private static int sum(Mat mat) {
int sum = 0;
for (int row = 0; row < mat.height(); row++) {
for (int col = 0; col < mat.width(); col++) {
sum += mat.get(row, col)[0];
}
}
return sum;
}
测试代码:
类:ImageUtilsTest.java
@Test
public void testRemoveBlackEdge() {
Mat mat = Imgcodecs.imread("/tmp/999.jpg");
Mat result = ImageUtils.removeBlackEdge(mat);
showImg(result);
}
@Test
public void testRemoveBlackEdge2() {
Mat mat = Imgcodecs.imread("/tmp/999.jpg");
Mat result = ImageUtils.removeBlackEdge(mat, 5);
showImg(result);
}
结果:
原图:
testRemoveBlackEdge
testRemoveBlackEdge2
maven依赖,各区所需吧
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.edu.zua.javacv</groupId>
<artifactId>javacv</artifactId>
<version>1.0-SNAPSHOT</version>
<name>javacv</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<description>测试javacv</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<javacpp.platform.dependencies>windows-x86_64</javacpp.platform.dependencies>
<!--util-->
<util.mytool-core>1.0</util.mytool-core>
<!--下面的平台只是用来参考配置 javacpp.platform.dependencies-->
<javacpp.platform.android-arm>android-arm</javacpp.platform.android-arm>
<javacpp.platform.android-arm64>android-arm64</javacpp.platform.android-arm64>
<javacpp.platform.android-x86>android-x86</javacpp.platform.android-x86>
<javacpp.platform.android-x86_64>android-x86_64</javacpp.platform.android-x86_64>
<javacpp.platform.ios-arm>ios-arm</javacpp.platform.ios-arm>
<javacpp.platform.ios-arm64>ios-arm64</javacpp.platform.ios-arm64>
<javacpp.platform.ios-x86>ios-x86</javacpp.platform.ios-x86>
<javacpp.platform.ios-x86_64>ios-x86_64</javacpp.platform.ios-x86_64>
<javacpp.platform.linux-armhf>linux-armhf</javacpp.platform.linux-armhf>
<javacpp.platform.linux-arm64>linux-arm64</javacpp.platform.linux-arm64>
<javacpp.platform.linux-ppc64le>linux-ppc64le</javacpp.platform.linux-ppc64le>
<javacpp.platform.linux-x86>linux-x86</javacpp.platform.linux-x86>
<javacpp.platform.linux-x86_64>linux-x86_64</javacpp.platform.linux-x86_64>
<javacpp.platform.macosx-x86_64>macosx-x86_64</javacpp.platform.macosx-x86_64>
<javacpp.platform.windows-x86>windows-x86</javacpp.platform.windows-x86>
<javacpp.platform.windows-x86_64>windows-x86_64</javacpp.platform.windows-x86_64>
</properties>
<dependencies>
<dependency>
<groupId>cn.edu.zua.mytool</groupId>
<artifactId>mytool-core</artifactId>
<version>${util.mytool-core}</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.3</version>
<classifier>${javacpp.platform.dependencies}</classifier>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.jpg</include>
</includes>
</resource>
</resources>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2020/08/18 更新
好多人评论说方法没有贴全,具体的东西我忘记的差不多了,大家去github找找吧。
https://github.com/HiAscend/javacv/blob/master/src/main/java/cn/edu/zua/javacv/util/ImageUtils.java
看这里,看这里
文章总目录:博客导航
参考文章:https://blog.csdn.net/u_ascend/article/details/85064795