OpenCV-图像对比度-Java

public static Mat Contrast(Mat src, int percent) {
    float alpha = percent / 100.0f;
    alpha = Math.max(-1f, Math.min(1f, alpha));
    Mat temp = src.clone();
    int row = src.rows();
    int col = src.cols();
    int thresh = 127;
    for (int i = 0; i < row; i++) {
        byte[] t = new byte[col * 3];
        byte[] s = new byte[col * 3];
        src.get(i, 0, s);
        temp.get(i, 0, t);
        for (int j = 0; j < col; j++) {
            int b = s[j * 3] & 0xff;
            int g = s[j * 3 + 1] & 0xff;
            int r = s[j * 3 + 2] & 0xff;
            int newb, newg, newr;
            if (alpha == 1) {
                t[j * 3 + 2] = (byte) (r > thresh ? 255 : 0);
                t[j * 3 + 1] = (byte) (g > thresh ? 255 : 0);
                t[j * 3] = (byte) (b > thresh ? 255 : 0);
                continue;
            } else if (alpha >= 0) {
                newr = (int) (thresh + (r - thresh) / (1 - alpha));
                newg = (int) (thresh + (g - thresh) / (1 - alpha));
                newb = (int) (thresh + (b - thresh) / (1 - alpha));
            } else {
                newr = (int) (thresh + (r - thresh) * (1 + alpha));
                newg = (int) (thresh + (g - thresh) * (1 + alpha));
                newb = (int) (thresh + (b - thresh) * (1 + alpha));
            }
            newr = Math.max(0, Math.min(255, newr));
            newg = Math.max(0, Math.min(255, newg));
            newb = Math.max(0, Math.min(255, newb));
            t[j * 3 + 2] = (byte) newr;
            t[j * 3 + 1] = (byte) newg;
            t[j * 3] = (byte) newb;
        }
        temp.put(i, 0, t);
    }
    return temp;
}
 

以上代码改编自c++代码:(7条消息) OpenCV-图像对比度_opencv 对比度_翟天保Steven的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值