使用matlab编写的一个程序可以将所需的图片分别进行以下处理:
1)用图像相减的方法检测某个固定场景是否有人出现;
2)选用一幅图像,实现图像旋转;
3)对所给定的图像,采用合适的图像增强算法提高图像质量,并对至少三种增强算法进行效果对比;
4)选用一幅图像,采用大津法分割该图像;
5)选用一幅合适的图像,采用基于自适应阈值的图像分割方法完成图像分割。
代码:
classdef zy1 < handle
properties
figure
image_path
original_image
upload_btn
image_label1
subtract_btn
rotate_btn
enhance_btn
segmentation1_btn
segmentation2_btn
end
methods
function obj = zy1()
obj.figure = figure('Name', '图像处理');
obj.upload_btn = uicontrol('Style', 'pushbutton', 'String', '上传图片', 'Position', [20 20 100 30], 'Callback', @obj.upload_image);
obj.image_label1 = axes('Units', 'pixels', 'Position', [150 150 300 300]);
obj.subtract_btn = uicontrol('Style', 'pushbutton', 'String', '图像相减', 'Position', [20 60 100 30], 'Callback', @obj.subtract_images);
obj.rotate_btn = uicontrol('Style', 'pushbutton', 'String', '图像旋转', 'Position', [20 100 100 30], 'Callback', @obj.rotate_image);
obj.enhance_btn = uicontrol('Style', 'pushbutton', 'String', '图像增强对比', 'Position', [20 140 100 30], 'Callback', @obj.enhance_image);
obj.segmentation1_btn = uicontrol('Style', 'pushbutton', 'String', '大津法分割', 'Position', [20 180 100 30], 'Callback', @obj.segmentation_otsu);
obj.segmentation2_btn = uicontrol('Style', 'pushbutton', 'String', '自适应阈值分割', 'Position', [20 220 100 30], 'Callback', @obj.segmentation_adaptive);
end
function upload_image(obj, ~, ~)
[file_name, file_path] = uigetfile({'*.png;*.jpg;*.jpeg', 'Image Files'});
if file_name
obj.image_path = fullfile(file_path, file_name);
obj.original_image = imread(obj.image_path);
axes(obj.image_label1);
imshow(obj.original_image);
end
end
function subtract_images(obj, ~, ~)
[file_name, file_path] = uigetfile({'*.png;*.jpg;*.jpeg', 'Image Files'});
if file_name
image1 = imread(fullfile(file_path, file_name));
image2 = imread(obj.image_path);
image1 = imresize(image1, [size(obj.original_image, 1), size(obj.original_image, 2)]);
image2 = imresize(image2, [size(obj.original_image, 1), size(obj.original_image, 2)]);
subtracted_image = imabsdiff(rgb2gray(image1), rgb2gray(image2));
subtracted_image = mat2gray(subtracted_image);
figure('Name', '图像相减结果');
imshow(subtracted_image);
end
end
function rotate_image(obj, ~, ~)
if ~isempty(obj.image_path)
rotated_image = imrotate(obj.original_image, 45, 'bilinear', 'crop');
figure('Name', '图像旋转结果');
imshow(rotated_image);
end
end
function enhance_image(obj, ~, ~)
if ~isempty(obj.image_path)
enhanced_image1 = histeq(rgb2gray(obj.original_image));
enhanced_image2 = adapthisteq(rgb2gray(obj.original_image));
enhanced_image3 = imadjust(rgb2gray(obj.original_image));
figure('Name', '图像增强对比结果');
subplot(1, 3, 1);
imshow(enhanced_image1);
title('直方图均衡化');
subplot(1, 3, 2);
imshow(enhanced_image2);
title('自适应直方图均衡化');
subplot(1, 3, 3);
imshow(enhanced_image3);
title('局部对比度增强');
end
end
function segmentation_otsu(obj, ~, ~)
if ~isempty(obj.image_path)
segmented_image = imbinarize(rgb2gray(obj.original_image), graythresh(rgb2gray(obj.original_image)));
figure('Name', '大津法分割结果');
imshow(segmented_image);
end
end
function segmentation_adaptive(obj, ~, ~)
if ~isempty(obj.image_path)
segmented_image = imbinarize(rgb2gray(obj.original_image), adaptthresh(rgb2gray(obj.original_image), 'NeighborhoodSize', [11 11]));
figure('Name', '自适应阈值分割结果');
imshow(segmented_image);
end
end
end
end