主文件:
clear close all; %%读取图片,并显示 fileName = 'test14.png'; img = imread(fileName); % figure();imshow(img);title('原图'); %获取图片信息 [image_h,image_w,image_c] = size(img); image_w2 = ceil(image_w/2); %获取灰度图像 img_gray = rgb2gray(img); %%对源图片对半竖直分割 img_M1 = zeros(image_h,image_w2,3); img_M2 = zeros(image_h,image_w2,3); img_M1 = imcrop(img,[0,0,image_w2,image_h]); img_M1 = uint8(img_M1); img_M1_gray = rgb2gray(img_M1); figure(); imshow(img_M1);title('原左图'); img_M2 = imcrop(img,[image_w2+1,0,image_w2,image_h]); img_M2 = uint8(img_M2); img_M2_gray = rgb2gray(img_M2); figure(); imshow(img_M2);title('原右图'); %%判断是否需要一键找茬 % yijian_flag = 0; % char = input('是否一键找茬,y/n:','s'); % if char=='y' % yijian_flag = 1; % end % num_fine = 0; % %输入偏移量 % if yijian_flag == 0 % while(~num_fine) % pic = input('请确定要偏移的图,l/r:','s'); % pic_dx = input('请输入竖直偏移量(0~8):'); % pic_dy = input('请输入竖直偏移量(0~8):'); % % if( ((pic=='l')| (pic=='r'))&(pic_dx>=0)& (pic_dx<=8)&(pic_dy>=0)& (pic_dy<=8) ) % num_fine = 1; % else % fprintf('输入错误,请重新输入!!!\n'); % end % end % else % pic = 'l'; % pic_dx = 0; % pic_dy = 0; % end %针对test01.jpg默认值 pic = 'l'; pic_dx = 0; pic_dy = 0; %%进行图片偏移 img_M1_O = zeros(image_h+pic_dx,image_w2+pic_dy,3); img_M2_O = zeros(image_h+pic_dx,image_w2+pic_dy,3); img_M1_O = uint8(img_M1_O); img_M2_O = uint8(img_M2_O); img_M1_O(:,:,:) = 255; img_M2_O(:,:,:) = 255; if pic==1 for i = 1:image_h for j = 1:image_w2 img_M1_O(i+pic_dx,j+pic_dy,:) = img_M1(i,j,:); end end for i = 1:image_h for j = 1:image_w2 img_M2_O(i,j,:) = img_M2(i,j,:); end end else for i = 1:image_h for j = 1:image_w2 img_M2_O(i+pic_dx,j+pic_dy,:) = img_M2(i,j,:); end end for i = 1:image_h for j = 1:image_w2 img_M1_O(i,j,:) = img_M1(i,j,:); end end end figure();imshow(img_M1_O);title('左图偏移后'); figure();imshow(img_M2_O);title('右图偏移后'); %%计算出左右图片偏差值 img_M1_gray = rgb2gray(img_M1_O); img_M2_gray = rgb2gray(img_M2_O); % 根据图片中心锁定图片中心 image_h41 = ceil(image_h/4); image_w41 = ceil(image_w2/4); image_h43 = image_h41*3; image_w43 = image_w41*3; differ = 255*200*200; differ = uint32(differ); dx_min = 0; dy_min = 0; tmp = 0; count = 0; count = uint32(count); qiuhe = zeros(600); qiuhe = uint32(qiuhe); z = 1; for dx=-(10+pic_dx):10+pic_dx for dy=-(10+pic_dy):10+pic_dy for i=image_h41:image_h43 for j=image_w41:image_w43 tmp = uint32(abs(img_M1_gray(i,j)-img_M2_gray(i+dx,j+dy))); count = count + tmp; end end qiuhe(z) = count; z = z+1; if count<differ differ = count dx_min = dx dy_min = dy end count = 0; %count计数清零 end end % %针对test01.jpg默认值 % dx_min = 0 % dy_min = 1 %%根据偏移量裁剪左右图 image_Nh = image_h-abs(dx_min); image_Nw = image_w2-abs(dy_min); img_N1 = zeros(image_Nh,image_Nw); img_N2 = img_N1; %判断裁剪所需值 if dx_min<=0 dx1 = -dx_min; dx2 = 0; else dx2 = dx_min; dx1 = 0; end if dy_min<=0 dy1 = -dy_min; dy2 = 0; else dy2 = dy_min; dy1 = 0; end img_N1 = zeros(image_Nh,image_Nw,3); img_N2 = img_N1; for i=1:image_Nh for j=1:image_Nw img_N1(i,j,:) = img_M1_O(i+dx1,j+dy1,:); end end img_N1 = uint8(img_N1); figure(); imshow(img_N1);title('裁剪好后的左图'); for i=1:image_Nh for j=1:image_Nw img_N2(i,j,:) = img_M2_O(i+dx2,j+dy2,:); end end img_N2 = uint8(img_N2); figure(); imshow(img_N2);title('裁剪好后的右图'); %%通过三个通道分别找茬后再综合 img_N2_R = img_N2(:,:,1); img_N2_G = img_N2(:,:,2); img_N2_B = img_N2(:,:,3); % figure(); imshow(img_N2_R); title('红色通道'); img_N1_R = img_N1(:,:,1); img_N1_G = img_N1(:,:,2); img_N1_B = img_N1(:,:,3); % figure(); imshow(img_N2_R); title('红色通道'); %获取各个通道的不同区域 img_T_R = imabsdiff(img_N2_R,img_N1_R); T1 = graythresh(img_T_R); img_D_R = im2bw(img_T_R,T1);%Otus阈值进行分割 img_T_G = imabsdiff(img_N2_G,img_N1_G); T2 = graythresh(img_T_G); img_D_G = im2bw(img_T_G,T2); img_T_B = imabsdiff(img_N2_B,img_N1_B); T3 = graythresh(img_T_B); img_D_B = im2bw(img_T_B,T3); img_D = img_D_R + img_D_G +img_D_B; se1=strel('disk',1); se2=strel('disk',3);%disk其实就是一个八边形 img_N5=imclose(img_D,se2);%经过闭运算 img_N5=imopen(img_D,se1);%经过开运算 K=imclose(img_N5,se2); %获取到茬所在区域 figure(); imshow(K);title('获取茬区域块'); %%绘制标定茬的对比图 [L, n]=bwlabel(K,8); %获取茬连通区域 img_reg = regionprops(L,'area','boundingbox'); areas = [img_reg.Area]; rects = cat(1,img_reg.BoundingBox); rects = round(rects); img_L1_T = img_N1; img_L2_T = img_N2; %输出判定图框,并在图框上绘制茬的矩形 %标定左图茬 for i=1:size(rects,1); [state_L1,img_L1_T] = draw_rect(img_L1_T,[rects(i,2),rects(i,1)],[rects(i,3),rects(i,4)]); end img_L1 = img_L1_T; for i=1:size(rects,1); [state_L2,img_L2_T] = draw_rect(img_L2_T,[rects(i,2),rects(i,1)],[rects(i,3),rects(i,4)]); end img_L2 = img_L2_T; figure(); imshow(img_L1); figure(); imshow(img_L2); % figure('visible','off'); imshow(img_L1_T); % for i=1:size(rects,1); % rectangle('position',rects(i,:),'EdgeColor','r','linewidth',2); % end % frame1=getframe(gcf); % img_L1=frame2im(frame1); % imwrite(img_L1,'L1.png','png');%可以修改保存的格式 % % % figure('visible','off'); imshow(img_L2_T); % for i=1:size(rects,1); % rectangle('position',rects(i,:),'EdgeColor','r','linewidth',2); % end % frame2=getframe(gcf); % img_L2=frame2im(frame2); % imwrite(img_L2,'L2.png','png');%可以修改保存的格式 % figure(); imshow(img_L1); % figure(); imshow(img_L2); if n>0 str = sprintf('有茬,茬个数:%d',n); title(str, 'Color', 'r'); else str = sprintf('没有茬'); title(str, 'Color', 'g'); end