color_test
clear
close all
fname = '2012_0605_111841AA.JPG';
warning('off','images:initSize:adjustingMag');
rgb = imread(fname);
imshow(rgb);
rgbd = im2double(rgb);
g = rgb2gray(rgbd);
imshow(g);
imhist(g);
cdiff = max(rgbd,[], 3) - min(rgbd, [], 3);
mask = cdiff<0.15;
imshow(mask);
bthresh = 0.1;
black = g<bthresh;
imshow(black&mask);
wthresh = 0.3;
white = g>wthresh;
imshow(white&mask);
se1 = strel('square',7);
b1 = imdilate(black,se1);
w1 = imdilate(white,se1);
bw1 = w1&b1;
bw2 = bwareaopen(bw1,500);
imshow(bw2);
CC = bwconncomp(bw2);
disp(CC);
L = labelmatrix(CC);
imshow(label2rgb(L));
Connectivity: 8
ImageSize: [1536 2048]
NumObjects: 3
PixelIdxList: {[23483x1 double] [175032x1 double] [5542x1 double]}
s = regionprops(CC,'Image','FilledImage','Area','EulerNumber','BoundingBox');
euler = [s.EulerNumber]';
idx = find(abs(euler)>20);
M = length(idx);
for k=1:M
subplot(1,M,k);
imshow(s(idx(k)).Image);
end
s(idx)
bb = s(idx).BoundingBox;
img = imcrop(rgb,floor(bb));
subplot(1,2,1);
imshow(img);
subplot(1,2,2);
imshow(s(idx).Image);
ans =
Area: 175032
BoundingBox: [510.5000 212.5000 1110 1108]
Image: [1108x1110 logical]
FilledImage: [1108x1110 logical]
EulerNumber: -180
close all;
sz = size(s(idx).Image);
small_area = ceil(.003*(sz(1)*sz(2)));
fprintf('small_area %g\n',small_area);
small_area = 500;
fprintf('small_area (overide) %g\n',small_area);
bw3 = s(idx).FilledImage&~s(idx).Image;
bw4 = bwareaopen(bw3,small_area);
CC2 = bwconncomp(bw4);
L2 = labelmatrix(CC2);
subplot(2,2,1);
imshow(img);
subplot(2,2,2);
rgbm = label2rgb(L2,'jet','k');
imshow(rgbm);
subplot(2,2,3);
mask = sum(rgbm,3)==0;
imshow(mask);
subplot(2,2,4);
gc = imcrop(g,bb);
if (size(gc)~=size(mask))
diff = size(gc)-size(mask);
rect = bb - [0 0 diff(2) diff(1)];
gc = imcrop(g,rect);
end
gm = gc.*mask;
rgbn = cat(3,gm,gm,gm)+im2double(rgbm);
imshow(rgbn);
small_area 3690
small_area (overide) 500
s2 = regionprops(CC2,'Area','Eccentricity','Centroid');
area = cat(1,s2.Area);
maxarea = max(area);
fprintf('maximum area %g\n',maxarea);
kdx = find(area>0.5*maxarea);
fprintf('number of squares %d\n',length(area));
fprintf('number of fullsize squares %d\n',length(kdx));
fprintf('Euler %d\n',-s(idx).EulerNumber);
maximum area 78373
number of squares 181
number of fullsize squares 1
Euler 180
close all
area = [s2.Area]'/maxarea;
ecen = [s2.Eccentricity]';
[ecen_sorted ndx] = sort(ecen);
plot(area,ecen,'ko');
axis([0 1 0 1]);
grid;
xlabel('relative area');
ylabel('eccentricity');
ecen_threshold = 0.6;
ecen_threshold = 1.01;
ndx = find(ecen<ecen_threshold);
pts = reshape([s2.Centroid],2,length(s2));
imshow(label2rgb(L2));
hold on
plot(pts(1,ndx),pts(2,ndx),'ko','LineWidth',2,'MarkerSize',6,'MarkerFaceColor','white');
hold off
close all
rgbm = label2rgb(L,'jet','k');
mask = sum(rgbm,3)==0;
gm = g.*mask;
rgbn = cat(3,gm,gm,gm)+im2double(rgbm);
imshow(rgbn);
warning('on');