import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class HsyOpencvUtils {
static {
// 解决awt报错问题
System.setProperty("java.awt.headless", "false");
System.out.println("java.library.path = " + System.getProperty("java.library.path"));
// 加载动态库
// URL url = ClassLoader.getSystemResource("opencv_java341.dll");
// URL url = HsyOpencvTest.class.getResource("opencv_java320.dll");
// String path = url.toString();
// System.out.println( "path = " + path );
System.load( "E:\\git\\study\\opencvHandleImg-master\\out\\production\\Opencv\\opencv_java320.dll" );
}
/**
* 油画特效
* @param imagePath
* @param outputPath
*/
public static void oilPaintingOperation( String imagePath,String outputPath ){
long t = System.currentTimeMillis();
Mat mat_src = Imgcodecs.imread(imagePath);
Mat mat_gray = HsyOpencvUtils.getGrayMat(mat_src);
Mat mat_dst = new Mat();
mat_gray.copyTo( mat_dst );
int height = mat_gray.rows();
int width = mat_gray.cols();
int[] array = new int[ 8 ];
for( int row=4;row<( height - 4 );row++ ){
for( int col=4;col< ( width - 4 );col++ ){
// int[] array = new int[ 8 ];
array[ 0 ] = 0;
array[ 1 ] = 0;
array[ 2 ] = 0;
array[ 3 ] = 0;
array[ 4 ] = 0;
array[ 5 ] = 0;
array[ 6 ] = 0;
array[ 7 ] = 0;
for( int m =-4;m<=4;m++ ){
int row1 = row + m;
for( int n=-4;n<=4;n++ ){
// 0 ~ 7
Double index = mat_gray.get(row1, col + n)[0] / 32;
array[ index.intValue() ]++;
}
}
// 求 array 的最大值以及对应角标
int value_max = array[0];
int index_max = 0;
for( int index=1;index<=7;index++ ){
int value_curr = array[index];
if( value_max < value_curr ){
value_max = value_curr;
index_max = index;
}
}
double[] bgr = null;
int v1 = index_max * 32;
int v2 = ( index_max + 1 ) * 32;
for( int m=-4;m<=4;m++ ){
int row1 = row + m;
for( int n=-4;n<=4;n++ ){
int col1 = col + n;
double v = mat_gray.get( row1, col1 )[ 0 ];
if( v >= v1 && v <= v2 ){
bgr = mat_src.get( row1, col1 );
}
}
}
mat_dst.put( row,col,bgr );
}
}
Imgcodecs.imwrite( outputPath,mat_dst );
System.out.println( "耗时:" + ( System.currentTimeMillis() - t ) + "毫秒" );
}
}
import java.io.File;
public class HsyOpencvTest {
public static void main(String[] args) {
String imagePath = "E:\\sucai\\video\\liuyifei2.jpeg";
String outputPath = "E:\\sucai\\video\\liuyifei2_oilPainting.jpeg";
HsyOpencvUtils.oilPaintingOperation( imagePath,outputPath );
}
}