PS 图像调整算法——自动色阶 (Auto Levels)

PS 给出的定义: 

Enhance Per Channel Contrast:Maximizes the tonal range in each channel to produce a more dramatic correction.Because each channel is adjusted individually, Enhance Per Channel Contrast may remove or introduce color casts.The Auto Levels command uses this algorithm.

简单来说,就是对R,G,B三个通道的动态范围分别进行拉伸。这个算法还要考虑一个 clipping percentage, 默认值是 0.1%。根据这个定义,可以先编写一个拉伸动态范围的函数:

(函数中默认图像的动态范围是 0-1).

if x<x_min:  y=0;

if x>x_max:  y=1;

if x_min < x< x_max:   y=(x-x_min)/(x_max-x_min); 


function I_out=F_color(I, percent)

%%% the tonal range of the input image is 0-1.
[row, col]=size(I);
I_sort=sort(I(:));
I_out=I;

%%% based on the clipping percentage, 
%%% compute the upper and lower boundaries 

if (percent==0)
    I_min=min(I_sort)
    I_max=max(I_sort)
else
    I_min=I_sort(floor(row*col*percent))
    I_max=I_sort(floor(row*col*(1-percent)))
end

for i=1:row
    for j=1:col
            if(I(i,j)<I_min)
                I_out(i,j)=I_min;
            elseif(I(i,j)>I_max)
                I_out(i,j)=1;
            else
                I_out(i,j)=(I(i,j)-I_min)*(1-I_min)/(I_max-I_min)+I_min;
            end
    end 
end


对三个通道分别进行拉伸,可以得到最终调整后的图像。

clc;
clear all;
Image=imread('8.jpg');
Image=double(Image)/255;
imshow(Image);
R=Image(:,:,1);
G=Image(:,:,2);
B=Image(:,:,3);
percent=0.001;
Image_out=Image;
Image_out(:,:,1)=F_color(R, percent);
Image_out(:,:,2)=F_color(G, percent);
Image_out(:,:,3)=F_color(B, percent);
figure, imshow(Image_out);


原图:



调整后的图:



转载于:https://www.cnblogs.com/muyuge/p/6152399.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分水岭算法(watershed algorithm)是一种用于图像分割的算法,可以自动图像分割成不同的区域。OpenCV提供了分水岭算法的实现,可以通过调用cv2.watershed()函数来进行图像分割。 使用分水岭算法进行图像分割的基本步骤如下: 1. 读取图像并将其转换为灰度图像。 2. 对灰度图像进行二值化处理,得到前景(foreground)和背景(background)。 3. 对图像进行距离变换,得到每个像素到最近的背景像素的距离。 4. 对距离变换的结果进行阈值处理,得到一张标记(markers)图像。 5. 对标记图像进行分水岭算法处理,得到分割结果。 下面是一个简单的示例代码,演示了如何使用分水岭算法图像进行分割: ```python import cv2 import numpy as np # 读取图像并转换为灰度图像 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 对灰度图像进行二值化处理 ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) # 进行距离变换 dist_transform = cv2.distanceTransform(thresh, cv2.DIST_L2, 5) ret, markers = cv2.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0) # 对标记图像进行分水岭算法处理 markers = cv2.watershed(img, markers) img[markers == -1] = [0,255,0] # 显示分割结果 cv2.imshow('Segmented Image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上面的代码中,我们首先读取一张名为"image.jpg"的图像,并将其转换为灰度图像。然后利用cv2.threshold()函数对灰度图像进行二值化处理,得到前景和背景。接下来,我们使用cv2.distanceTransform()函数进行距离变换,得到每个像素到最近的背景像素的距离。然后对距离变换的结果进行阈值处理,得到一张标记图像。最后,我们利用cv2.watershed()函数对标记图像进行分水岭算法处理,得到分割结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值