by 今天不飞了
闲着没事,又来写代码了。今天相中这个,彩色图像找不同。写了个简易的版本,不适用于手机拍的图
核心代码
function [coorList,num] = SpotTheDifferences(im1,im2)
% 平滑
H = fspecial('gaussian',7,3);
smooth1 = imfilter(im1,H,'same');
smooth2 = imfilter(im2,H,'same');
% 差
ecolor = uint8(abs(double(smooth1)-double(smooth2)));
egray = rgb2gray(ecolor);
% 二值
bw = imbinarize(egray);
% 去小噪
se = strel('disk',5);
obj1 = imopen(bw,se);
% 合大体
se = strel('disk',11);
obj2 = imclose(obj1,se);
% 目标
[label,num] = bwlabel(obj2);
coorList = zeros(num,4);
for n = 1:num
[y,x] = find(label==n);
y1 = min(y); y2 = max(y);
x1 = min(x); x2 = max(x);
coorList(n,:) = [x1,x2,y1,y2];
end
end
测试
你自己找两张图像吧
clear; close all; clc
%% {读取图像}
im1 = imread('1.png');
im2 = imread('2.png');
[R,C,D] = size(im1);
im2 = cat(3,imresize(im2(:,:,1),[R,C]),...
imresize(im2(:,:,2),[R,C]),...
imresize(im2(:,:,3),[R,C]));
%% {找不同}
[coorList,num] = SpotTheDifferences(im1,im2);
%% {绘制}
subplot(121),imshow(im1)
hold on
for n = 1:num
drawbox(coorList(n,:))
end
subplot(122),imshow(im2)
hold on
for n = 1:num
drawbox(coorList(n,:))
end
% ----function----
function drawbox(coor)
x1 = coor(1); x2 = coor(2);
y1 = coor(3); y2 = coor(4);
x = [x1,x1,x2,x2,x1];
y = [y1,y2,y2,y1,y1];
plot(x,y,'r-','LineWidth',2)
end
两个测试
其他
欢迎提供有意思的题目 哔哩哔哩讲解视频:【MATLAB】大家来找茬——彩色图像找不同简易代码
|