% playing with max/min filtering % playing with convolution and masks addpath /research/src/DNAdesign addpath /research/src/AFMutils % rizal's beautiful image % [im,in] = showafm('~winfree/Research/DNA/AFM_old/data.rizal/02-07-20/QBOX.097',0); im = flipud(im); figure(1); imagesc(im); truesize colormap gray title 'raw image' % scale so that all elements lie in [0,1] low = min(min(im)); high = max(max(im)); im = im - low; im = im / high; % % goal: to learn to use min and max filtering % % improve image quality % % % % slow version maximum filtering % im_max = nlfilter(im, [5,5], 'max(x(:))'); % figure(2) % imagesc(im_max) % truesize % colormap gray % title 'maxima filter' % % % slow version minimum filtering % im_min = nlfilter(im, [3,3], 'min(x(:))'); % figure(3) % imagesc(im_min) % truesize % title 'minima filter' % % These are unreasonably slow. % % % min/max filtering is too responsive to noise? % h = fspecial('average', [3,3]); % im_ = imfilter(im, h); % figure(2) % imagesc(im_ave) % truesize % colormap gray % title 'averaging filter' % % blah % % % would it help to apply a gaussian filter first? % h = fspecial('gaussian', [5,5],1); % im_ = imfilter(im, h); % figure(2) % imagesc(im_ave) % truesize % colormap gray % title 'gaussian filter' % % blah % % % faster version % im_min = colfilt(im_, [3,3], 'sliding', @min); % im_max = colfilt(im_min, [3,3], 'sliding', @max); % im_new = im_max; % figure(3) % imagesc(im_new) % truesize % colormap gray % title 'min max 3,3' % % these perform infinitely better than nlfilter :) % % % will thresholding help at all? % min_ = min(min(im_new)); % max_ = max(max(im_new)); % threshold = min_ + (max_-min_)*.75 % figure(4) % imagesc(im_new>threshold) % truesize % colormap gray % title 'threshold image' % % not really, image is too segmented into blocks. tiles are cut in half by % % thresholding. % % % trying to emphasize maximums and deemphasize minimums % im_min = colfilt(im_, [5,5], 'sliding', @min); % move = max(max(im_min)); % im_min = im_min - move; % % im_max = colfilt(im_min, [5,5], 'sliding', @max); % move = min(min(im_max)); % im_max = im_max - move; % % im_new = im_min + im_max; % figure (2) % imagesc(im_new) % truesize % colormap gray % title 'worth trying' % % % thresholding again % up = max(max(im_new)); % down = min(min(im_new)); % range = up - down; % threshold = down + .7 * range; % figure (3) % imagesc(im_new > threshold ) % truesize % colormap gray % title 'threshold it' % % looks pretty nice to me... % % let's try a convolution on this image now % miserable failure. % % convolution appears to work better than this max and min filtering does % in locating and emphasizing image features /tiles. % % something completely unrelated that i wanted to play with. % selection = roipoly(im); % figure (2) % imagesc(selection); % truesize % colormap gray % title 'selected region' % % add feedback loop, user accepts selection or wants to restart? % % % make a mask using the user's selected area. % % mask = selection .* im; % mask = im; % % mask constrained to recentagle. % [r,c] = size(mask); % % if a column is empty, remove it % for i = c:-1:1 % temp = sum( selection(:,i) ); % will == 0 if empty % if (temp == 0) % mask(:,i) = []; % selection(:,i) = []; % end % end % % % if a row is empty, remove it. % for i = r:-1:1 % temp = sum( selection(i,:) ); % if (temp == 0) % mask(i,:) = []; % selection(i,:) = []; % end % end % % do not assume mask is symetric in any way. % % filter mask -- too noisy! % h=fspecial('gaussian'); % mask = imfilter(mask,h,'same'); % figure (2) % imagesc(mask); % truesize % colormap gray % title 'selected mask' % % mask is still too messy, cannot get good results. % % make up a fake mask, see how it works. % mask = ones([20,10]); % mask(1,:) = 0; % mask(20,:) = 0; % mask(:,1) = 0; % mask(:,10) = 0; % % works better than random mask.... blech. mask = [zeros(20,6), ones(20,6)] imc = conv2(im,mask,'same'); figure(3); imagesc(imc); colormap gray; truesize title 'mask applied to raw image' pause; close all;