OpenCV如何配置在IDEA中自行百度
环境:win10+IDEA2021.2.3+jdk11.0.1+OpenCV-460.jar
一、首先我们得了解OpenCV中的Mat类,详情见上一篇文章
二、算法流程
1.利用OpenCV识别图像的三通道像素值,并存储在二维数组中
2.因为后面需要得到累计频率为%2和98%所对应的像素值,所以需要将二维数组转换为一维数组方便排序
3.在一维数组中统计每个像素所出现的次数,利用HashMap结构的键值对存储像素和像素存在的次数,key存放像素值,value存放像素所出现的次数,然后累加除以像素总数获得累计频率为%2和98%所对应的像素值
4.然后对三通道的二维数组像素值进行%2的线性拉伸
线性拉伸概念:线性拉伸为[a,b]→[c,d],在%2线性拉伸中,a=累计频率为%2的像素值,b=累计频率为98%的像素值,c=0,d=255,原像素值小于a的赋予0,大于b的赋予255,处于[a,b]之间的进行线性变换:新像素值=(d-c)/(b-a)*(原像素值-a)+c
5.对拉伸后的三通道二维数组合成图像并存储
三、不说了,先上结果
输入图像:
经过 2%线性拉伸后的图像:
可知亮度提高了很多,拉伸效果较好
四、实现过程
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import java.util.*;
/**
* @Author: jue_chen
* @Date: 2022/10/25/ 16:04
* @Attention: 转载, 引用请注明出处
*/
public class TwoPercentLinear {
//加载本地动态链接库
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
//图像的行数和列数
int rows;
int cols;
//分离图像的三通道并用集合+数组存储
public ArrayList<int[][]> splitThreeChannels(String path) {
//使用Mat类存储图像信息
Mat mat = Imgcodecs.imread(path);
//获得图像的行数和列数
rows = mat.rows();
cols = mat.cols();
//将三个通道进行分离
ArrayList<Mat> channels_mat = new ArrayList<>();
//split函数是分离三个通道channel的值,例如将[1,2,4,1,2,4]分离得到[1,1],[2,2],[4,4]
Core.split(mat, channels_mat);
Mat mat1 = channels_mat.get(0);
Mat mat2 = channels_mat.get(1);
Mat mat3 = channels_mat.get(2);
//定义三个二维数组存储三个通道的像素值
int[][] channel1_arr = new int[mat1.rows()][mat1.cols()];
int[][] channel2_arr = new int[mat2.rows()][mat2.cols()];
int[][] channel3_arr = new int[mat3.rows()][mat3.cols()];
//获得通道1的数组channel1
for (int i = 0; i < channel1_arr.length; i++) {
for (int j = 0; j < channel1_arr[0].length; j++) {
channel1_arr[i][j] = (int) mat1.get(i, j)[0];
}
}
//获得通道2的数组channel2
for (int i = 0; i < channel2_arr.length; i++) {
for (int j = 0; j < channel2_arr[0].length; j++) {
channel2_arr[i][j] = (int) mat2.get(i, j)[0];
}
}
//获得通道3的数组channel3
for (int i = 0; i < channel3_arr.length; i++) {
for (int j = 0; j < channel3_arr[0].length; j++) {
channel3_arr[i][j] = (int) mat3.get(i, j)[0];
}
}
//将二维数组存放在集合中
ArrayList<int[][]> channel_arr = new ArrayList<>();
channel_arr.add(channel1_arr);
channel_arr.add(channel2_arr);
channel_arr.add(channel3_arr);
return channel_arr;
}
//将二维数组变一维数组,便于将灰度值排序
public ArrayList<int[]> erWeiToYiWei(ArrayList<int[][]> arrErWei) {
//从集合中得到二维数组
int[][] arr1_ErWei = arrErWei.get(0);
int[][] arr2_ErWei = arrErWei.get(1);
int[][] arr3_ErWei = arrErWei.get(2);
//定义一维数组
int[] arr1_YiWei = new int[arr1_ErWei.length * arr1_ErWei[0].length];
int[] arr2_YiWei = new int[arr2_ErWei.length * arr2_ErWei[0].length];
int[] arr3_YiWei = new int[arr3_ErWei.length * arr3_ErWei[0].length];
//得到通道1的一维数组1
for (int i = 0; i < arr1_ErWei.length; i++) {
for (int j = 0; j < arr1_ErWei[0].length; j++) {
arr1_YiWei[arr1_ErWei[0].length * i + j] = arr1_ErWei[i][j];
}
}
//得到通道2的一维数组2
for (int i = 0; i < arr2_ErWei.length; i++) {
for (int j = 0; j < arr2_ErWei[0].length; j++) {
arr2_YiWei[arr2_ErWei[0].length * i + j] = arr2_ErWei[i][j];
}
}
//得到通道3的一维数组3
for (int i = 0; i < arr3_ErWei.length; i++) {
for (int j = 0; j < arr3_ErWei[0].length; j++) {
arr3_YiWei[arr3_ErWei[0].length * i + j] = arr3_ErWei[i][j];
}
}
//将一维数组进行排序,便于获得每个像素值的累计数量
Arrays.sort(arr1_YiWei);
Arrays.sort(arr2_YiWei);
Arrays.sort(arr3_YiWei);
//将一维数组存放在集合中
ArrayList<int[]> arrYiWei = new ArrayList<>();
arrYiWei.add(arr1_YiWei);
arrYiWei.add(arr2_YiWei);
arrYiWei.add(arr3_YiWei);
return arrYiWei;
}
//获得累计频率为%2和%98所对应的像素值
public int[] getMinMaxPixel(int[] arr) {
//构建一个HashMap,key为数组元素,value为数组元素对应的个数
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < arr.length; i++) {
//arr[i]不存在HashMap的key中,将其作为key存入map中,value初始化计数为1
if (!map.containsKey(arr[i])) {
map.put(arr[i], 1);
}
//arr[i]存放在HashMap的key中,原有的value值+1,即每个像素存在的数量在原基础上加一
else {
map.put(arr[i], map.get(arr[i]) + 1);
}
}
//定义累计频率为%2和98%所对应的像素值
int minPixel = 0;
int maxPixel = 0;
//存放累加获得的像素值
int num = 0;
//计算累计频率
for (int i = 0; i < 255; i++) {
//图像的像素值范围为0-255,当该图像存在此大小的像素值时,获取它的格式
if (map.get(i) != null) {
num += map.get(i);
//累计频率为2%时的像素值
if ((double) num / (rows * cols) <= 0.02) {
minPixel = i;
}
//累计频率为98%时的像素值
if ((double) num / (rows * cols) <= 0.98) {
maxPixel = i;
}
}
}
//将此图像累计频率为%2,98%的像素值存入数组
int[] minMaxPixel = new int[2];
minMaxPixel[0] = minPixel;
minMaxPixel[1] = maxPixel;
return minMaxPixel;
}
//线性拉伸[a,b]到[c,d]
//进行%2线性拉伸,a=min,b=max,c=0,d=255
//像素值小于min的赋予0,像素值大于max的赋予255
//像素值处于[min,max]之间的进行线性拉伸:新像素值=(d-c)/(b-a)*(原像素值-min)+c
public int[][] twoPercentLiner(int min, int max, int[][] arr) {
//定义拉伸后的二维数组
int[][] arr_linear = new int[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if (arr[i][j] < min) {
arr_linear[i][j] = 0;
} else if (arr[i][j] > max) {
arr_linear[i][j] = 255;
} else {
arr_linear[i][j] = 255 / (max - min) * (arr[i][j] - min);
}
}
}
return arr_linear;
}
//合成显示
public Mat compositeImage(int[][] arr1, int[][] arr2, int[][] arr3) {
//定义存放拉伸后三通道像素的二维数组
int[][] mat_arr = new int[arr1.length][arr1[0].length * 3];
for (int i = 0; i < arr1.length; i++) {
//拉伸后通道1像素值放入二维数组
for (int j = 0; j < arr1[0].length; j++) {
mat_arr[i][3 * j] = arr1[i][j];
}
//拉伸后通道2像素值放入二维数组
for (int j = 0; j < arr2[0].length; j++) {
mat_arr[i][3 * j + 1] = arr2[i][j];
}
//拉伸后通道2像素值放入二维数组
for (int j = 0; j < arr3[0].length; j++) {
mat_arr[i][3 * j + 2] = arr3[i][j];
}
}
//定义新图像存放拉伸后的图像
Mat mat = new Mat(arr1.length, arr1[0].length, CvType.CV_32SC3);
//将像素放入图像
for (int i = 0; i < mat_arr.length; i++) {
//一次放入一行三通道像素值
mat.put(i, 0, mat_arr[i]);
}
return mat;
}
public static void main(String[] args) {
TwoPercentLinear tpl = new TwoPercentLinear();
//获得三通道的二维数组
ArrayList<int[][]> channel_arrErWei = tpl.splitThreeChannels("D:\\Project\\IDEA_Project\\RS01\\src\\rs01\\img\\10.png");
int[][] channel1_arrErwWei = channel_arrErWei.get(0);
int[][] channel2_arrErwWei = channel_arrErWei.get(1);
int[][] channel3_arrErwWei = channel_arrErWei.get(2);
//获得三通道的一维数组
ArrayList<int[]> channel_arrYiWei = tpl.erWeiToYiWei(channel_arrErWei);
//获得通道1的累计频率为%2,98%像素
int[] channel1_arr = channel_arrYiWei.get(0);
int[] channel1_minMaxPixel = tpl.getMinMaxPixel(channel1_arr);
int channel1_min = channel1_minMaxPixel[0];
int channel1_max = channel1_minMaxPixel[1];
//获得通道2的累计频率为%2,98%像素
int[] channel2_arr = channel_arrYiWei.get(1);
int[] channel2_minMaxPixel = tpl.getMinMaxPixel(channel2_arr);
int channel2_min = channel2_minMaxPixel[0];
int channel2_max = channel2_minMaxPixel[1];
//获得通道3的累计频率为%2,98%像素
int[] channel3_arr = channel_arrYiWei.get(2);
int[] channel3_minMaxPixel = tpl.getMinMaxPixel(channel3_arr);
int channel3_min = channel3_minMaxPixel[0];
int channel3_max = channel3_minMaxPixel[1];
//三通道进行%2线性拉伸后的数组
int[][] channel1_arrLinear = tpl.twoPercentLiner(channel1_min, channel1_max, channel1_arrErwWei);
int[][] channel2_arrLinear = tpl.twoPercentLiner(channel2_min, channel2_max, channel2_arrErwWei);
int[][] channel3_arrLinear = tpl.twoPercentLiner(channel3_min, channel3_max, channel3_arrErwWei);
//将%2拉伸后三通道合成新图像
Mat mat = tpl.compositeImage(channel1_arrLinear, channel2_arrLinear, channel3_arrLinear);
//将新图像写入文件
Imgcodecs.imwrite("D:\\Project\\IDEA_Project\\RS01\\src\\rs01\\img\\10_linear.png", mat);
}
}