本文介绍matlab代码实现图像处理中常见的点运算。
点运算
主程序:
f = imread('outman.jpg');
gray_f = rgb2gray(f);
figure();
imshow(gray_f),title('原图');
thresholding(gray_f, 0.35);
uptruncation(gray_f, 100);
downtruncation(gray_f, 100);
multi_thresholding(gray_f, 50, 150, 250, 100,150,200);
updowntruncation(gray_f, 200,100);
offset(gray_f, 50);
square(gray_f);
squareroot(gray_f);
inverse(gray_f);
scaling(gray_f);
gammaCorrection(gray_f, 1, 1);
gammaCorrection(gray_f, 1, 0.5);
gammaCorrection(gray_f, 1, 2);
子函数:
甄别
function thresholding(image, threshold)
threshold_number = threshold;
thresholding = imbinarize(image,threshold_number);
figure();
imshow(thresholding),title('甄别');
end
上截断
function uptruncation(image, floorholding)
[heighth, width] = size(image);
for i=1:heighth
for j=1:width
if (image(i,j)>floorholding)
image(i,j)=floorholding;
end
end
end
figure();
imshow(image),title('上截断');
end
下截断
function xiajieduan(image, floorholding)
[heighth, width] = size(image);
for i=1:heighth
for j=1:width
if (image(i,j)<floorholding)
image(i,j)=floorholding;
end
end
end
figure();
imshow(image),title('下截断');
end
上下截断
function updowntruncation(image, upholding, lowholding)
[heighth, width] = size(image);
for i=1:heighth
for j=1:width
if (image(i,j)>upholding)
image(i,j)=upholding;
end
if (image(i,j)<lowholding)
image(i,j)=lowholding;
end
end
end
figure();
imshow(image),title('上下截断');
end
多值甄别
function multi_thresholding(image, l1, l2, l3, t1, t2, t3)
[heighth, width] = size(image);
for i=1:heighth
for j=1:width
if (image(i,j)<t1)
image(i,j)=l1;
end
if (image(i,j)>t1 && image(i,j)<t2)
image(i,j)=l2;
end
if (image(i,j)>t2 && image(i,j)<t3)
image(i,j)=l3;
end
if (image(i,j)>t3)
image(i,j)=255;
end
end
end
figure();
imshow(image),title('多值甄别');
end
补偿
function offset(image, offsetvalue)
offsetim = image + offsetvalue;
figure();
imshow(offsetim),title('补偿');
end
平方
function square(image)
squareim = image.*image;
figure();
imshow(squareim),title('平方');
end
方根
function squareroot(image)
image = im2double(image);
squareroot = image.^(1/2);
figure();
imshow(squareroot),title('方根');
end
求反
function inverse(image)
[heighth, width] = size(image);
for i=1:heighth
for j=1:width
image(i,j)=255-image(i,j);
end
end
figure();
imshow(image),title('求反');
end
放缩
function scaling(image)
scaling = image.*2;
figure();
imshow(scaling),title('放缩');
end
伽马变换
function gammaCorrection(name, a, gamma)
r = name;
r=im2double(r);
s = a * (r .^ gamma);
figure();
imshow(s), title(sprintf('Gamma: %0.1f',gamma));
end