MATLAB - Restore central sub-section of an image - matlab

So, I have a 512x512 distorted image, but what I'm trying to do is restore only a 400x400 centrally-positioned subsection of the image while it is still distorted outside of it. How do I go about implementing something like that?
I was thinking to have a for loop within a for loop like
for row = 57:457
for col = 57:457
%some filter in here
end
end
But I'm not quite sure what to do next...

As a general rule, you can do a lot of things in MATLAB without loops using vectorization instead. As discussed in the comments below your question, there are filtering functions included with MATLAB such as medfilt2, wiener2 or imfilter which all work on two-dimensional images directly without the need for any loops.
To restore only the center part of your image, you apply the filter to the full image, store the result in a temporary variable and then copy over the part that you want into your distored image:
tmpimage = medfilt2(distortedimage);
finalimage = distortedimage;
finalimage(57:456,57:456)=tmpimage(57:456,57:456);
Of course if you don't care about edge effects during the reconstruction, you can just call the reconstruction for the part that interests you and avoid the tmpimage:
finalimage = distortedimage;
finalimage(57:456,57:456)=medfilt2(distortedimage(57:456,57:456));
Note how the sizes in an assignment need to match: you can't assign finalimage(57:456,57:456)=medfilt2(distortedimage) since the right-hand-size produces a 512-by-512 matrix which doesn't fit into the 400-by-400 center of finalimage.

Related

Dilate an image based on a distribution

I have an image that show some random filled circles (e.g. see here). I want to change these circles to make some irregular shapes. In other words, I want to define a distribution by which I can expand the circles. Clearly, the resulted new objects will not be circles anymore, because the generated objects are expanded based on a distribution which is variable; see this new deformed circle.
I was wondering if there is any method that can do this? In my first try, I tried to use image dilation in Matlab, but I have no idea on how the dilation "distribution" should be used.
IM2 = imdilate(IM,SE)
If you want to do it using dilation, a solution could be:
Let say Im is your original image
ImResult = Same(Im)
ImClone = Clone(Im)
Randomly delete pixels in ImClone. The number of pixels to delete may be a percentage, or whatever you prefer
ImDilate = Dilate(ImClone), with the structuring element of size N
Result = Maximum(Result, ImDilate)
If you want different size of deformations, then you iterate from step 3 to 6, with different structuring element sizes.
But what you want is more an elastic deformation. You should take a look to the free form deformation (FFD).

DCT filter image in Matlab

I used the below function to filter an image. Basically it sets coefficients of DCT to 0 except for top-left 8x8 elements, which means it filter out all high frequency part and only left the low frequency part.
function I_out = em_DCT_filter(I_in,N)
I_trim = double(I_in)-128;
MYDCT=dctmtx(N);
dct = #(block_struct)MYDCT*block_struct.data*MYDCT';
B=blockproc(I_trim,[N,N],dct);
mask = zeros(N,N);
mask(1:N/4,1:N/4)= 1;
AnselmMask = #(block_struct)block_struct.data.*mask;
BMask=blockproc(B,[N N],AnselmMask);
InverseDct = #(block_struct)MYDCT'*block_struct.data*MYDCT;
BReversedl = blockproc(BMask,[N N],InverseDct);
I_out= uint8(BReversedl+128);
After processing, an image looks like this:
I need the function removes the details in the image (e.g. patterns on the sweater, shadow on the pants), which it seems working fine. However, the function also makes the image very fuzzy. How can I remove the details, as well as keeping the region structure clear? For example, the sweater/pants region will be more uniform coloured region than before.
You basically applied "Local Low Pass Filter".
No wonder "Fuzzy" look is the result, you removed data in the High Frequency we usually interpret as details and "Sharpness".
What you really should do is remove High Frequency details yet keep large edges in tact.
A good way to do is use something like Anisotropic Diffusion.
By using the optimized parameters you'll be able to achieve the look you're after.
In general those methods are called image abstractions.
Here's a great Open Source code for advanced Anisotropic Diffusion:
https://github.com/RoyiAvital/Fast-Anisotropic-Curvature-Preserving-Smoothing
Work with, if you can contribute, it would be amazing.

Calculating unique transformation for multiple images in folder

I'm currently working on an alignment script which aligns two images very well. Usually, I get a data set which contains over 50 images of cells. I normally calculate a transformation matrix (T) based on fluorescent beads. However, this T-matrix gave rise to polarization in unpolarized cells, indicating that the transformation is not optimal. Therefore I switched to another script, which calculates a T-matrix based on cells and not beads. This new T-matrix aligns almost perfectly for a fraction of the cells, but there is always a portion of the images which aligns not so good.
I would like to continue with the alignment on cells, because this script works much better than the alignment on beads. In order to have optimal T-matrix for each image, I would like to calculate unique T-matrices for each image couple. I'm not very skilled in Matlab so the solution I could think of did not work.
Below you can find the current script. It functions by creating variables of the images I want to align and assign them to im1 and im2 in the script:
function [T] = alim(im1, im2, Tstart)
%ALIM Determines the transformation between the cameras.
im3=im2;
if (nargin>2)
im2=imwarp(im2, Tstart,'OutputView',imref2d(size(im1)));
end
optimizer = registration.optimizer.RegularStepGradientDescent;
optimizer.MaximumIterations=500;
metric = registration.metric.MattesMutualInformation;
T = imregtform(im2, im1, 'affine', optimizer, metric);
if (nargin>2)
T.T=Tstart.T*T.T;
end
figure;
imshowpair(im1,imwarp(im3,T,'OutputView',imref2d(size(im1))));
end
I tried to incorporate a loop which imports all images from the folder sequentially and assign these to im1 and im2. However, the problems that arises is that the type of data changes from uint16 into cell, which can't be used for this type of transformation. One defines in the script the location of the folders 'CAM1' and 'CAM2' and the number of images in these folders ('imnum')
for i:imnum
x{i}=imread(strcat(link,'CAM1\',num2str(i),'.tif'));
y{i}=imread(strcat(link,'CAM2\',num2str(i),'.tif'));
I would like to have your view on this problem and hopefully you can make some suggestions on how I can import the images in a folder in one go and keep the data type uint16. I'm always open for suggestions so if you have other ideas on how to solve my problem, I would love it if you shared them with me. If anything is unclear, please contact me with questions!
With kind regards,
Reinier
x is a cell array, where each element x{i} is a uint16 array. Cell arrays can hold any other datatype, including more cell arrays, and are a great way to wrap collections of objects, especially when their sizes and/or types may differ.
In your case, just call your function like this:
T = alim(x{i}, y{i}, tstart);
Or, even better, put the output matrix into a similar cell:
T{i} = alim(x{i}, y{i}, tstart);

Line thickening image filter for preprocessing of scanned digits

For a school project I've built a scanner and connected it to matlab. The scanner scans images (16-by-16 pixels) of handwritten digits from 0 to 9. I'm using a principal component analysis in order to classify the scans. Due to the low accuracy of the scanner, I need to preprocess the scans first, before I can actually send them through the recognition machine.
One of these preprocessing-steps is to thicken the lines. So far, I've used a pretty simple averageing filter for this: H = ones(3, 3) ./ 9. This bears the problem, that the circular gap of the digits 8 and 9 is likely to be "closed". I enclose a picture of all my preprocessing-steps, where the problem is visible: the image with the caption "threshholded" still shows the gap, but it disappeared after the thickening step.
My question is: Do you know a better filter for this "thickening"-step, which would not erase the gap? Or do you have an idea for a filter which could be applied after the thickening to produced the desired result? Any other suggestions or hints are also greatly appreciated.
I=imread('numberreco.png');
subplot(1,2,1),imshow(I)
I=rgb2gray(I);
BW=~im2bw(I,graythresh(I));
BW2 = bwmorph(BW,'thin');
I1=double(I).*BW2;
subplot(1,2,2),imshow(uint8(I1))
The gap is kept, and you can start from here...
Not a very general answer, but if you have the Image Processing Toolbox, and your system doesn't depend on having multiple grey levels, then converting to binary images and using the 'thicken' operation from bwmorph() should do exactly what you want.
Thinking a bit harder, you could also use a suitably thickened binary image as a mask to restore holes - either just elementwise multiply it with the blurred greyscale image or, for more flexibility:
invert it to form a background/holes mask
remove the background with imclearborder() to leave just the holes
optionally dilate the mask
use as a logical index to clear the 'hole' areas of the blurred/brightened greyscale image.
Even without the morphological steps you can use a mask to artificially reintroduce the original holes later, e.g.:
bgmask = (thresholdedimage == 0); % assuming 0 == background
holes = imclearborder(bgmask);
... % other processing steps
brightenedimage(holes) = 0; % punch holes in updated image

How do I detect an instance of an object in an image?

I have an image containing several specific objects. I would like to detect the positions of those objects in this image. To do that I have some model images containing the objects I would like to detect. These images are well cropped around the object instance I want to detect.
Here is an example:
In this big image,
I would like to detect the object represented in this model image:
Since you originally posted this as a 'gimme-da-codez' question, showing absolutely no effort, I'm not going to give you the code. I will describe the approach in general terms, with hints along the way and it's up to you to figure out the exact code to do it.
Firstly, if you have a template, a larger image, and you want to find instances of that template in the image, always think of cross-correlation. The theory is the same whether you're processing 1D signals (called a matched filter in signal processing) or 2D images.
Cross-correlating an image with a known template gives you a peak wherever the template is an exact match. Look up the function normxcorr2 and understand the example in the documentation.
Once you find the peak, you'll have to account for the offset from the actual location in the original image. The offset is related to the fact that cross-correlating an N point signal with an M point signal results in an N + M -1 point output. This should be clear once you read up on cross-correlation, but you should also look at the example in the doc I mentioned above to get an idea.
Once you do these two, then the rest is trivial and just involves cosmetic dressing up of your result. Here's my result after detecting the object following the above.
Here's a few code hints to get you going. Fill in the rest wherever I have ...
%#read & convert the image
imgCol = imread('http://i.stack.imgur.com/tbnV9.jpg');
imgGray = rgb2gray(img);
obj = rgb2gray(imread('http://i.stack.imgur.com/GkYii.jpg'));
%# cross-correlate and find the offset
corr = normxcorr2(...);
[~,indx] = max(abs(corr(:))); %# Modify for multiple instances (generalize)
[yPeak, xPeak] = ind2sub(...);
corrOffset = [yPeak - ..., xPeak - ...];
%# create a mask
mask = zeros(size(...));
mask(...) = 1;
mask = imdilate(mask,ones(size(...)));
%# plot the above result
h1 = imshow(imgGray);
set(h1,'AlphaData',0.4)
hold on
h2 = imshow(imgCol);
set(h2,'AlphaData',mask)
Here is the answer that I was about to post when the question was closed. I guess it's similar to yoda's answer.
You can try to use normalized cross corelation:
im=rgb2gray(imread('di-5Y01.jpg'));
imObj=rgb2gray(imread('di-FNMJ.jpg'));
score = normxcorr2(imObj,im);
imagesc(score)
The result is: (As you can see, the whitest point corresponds to the position of your object.)
The Mathworks has a classic demo of image registration using the same technique as in #yoda's answer:
Registering an Image Using Normalized Cross-Correlation