Woking with colfit in matlab - matlab

I wanted to calculate the histogram n local histogram equalisation of an input image, using colfit. But when i run the code i get the following error.
??? Error using ==> ge
Matrix dimensions must agree.
Error in ==> colfilt at 135
if all(block>=size(a)), % Process the whole matrix at once.
Error in ==> localhist at 10
z=colfilt(f,[w w],'sliding',#std);
Please provide some insights.

I haven't seen it written on the documentation (neither on help colfilt nor on docs colfilt), but I think you can only use colfilt, as nlfilter, with monochannel images. So that if you try to run the example code provided on help colfilt on a 3-channel image, say:
I = imread('peppers.png'); % 'peppers.png' is just a demo color image usually provided with matblab
figure, imshow(I)
I2 = uint8(colfilt(I,[5 5],'sliding',#mean));
figure, imshow(I2)
You get the kind of error that you posted:
Error using >=
Matrix dimensions must agree.
Error in colfilt (line 135)
if all(block>=size(a)), % Process the whole matrix at once.
If you thry this instead, which only takes the first channel (or any other combination of the channels) it will just work
% which is one of the example images usually provided with matlab
J = imread('peppers.png');
I = J(:,:,1);
figure, imshow(I)
I2 = uint8(colfilt(I,[5 5],'sliding',#mean));
figure, imshow(I2)
I hope this helps

Related

how to do image segmentation using built in fcm function in matlab?

I am new to matlab. Actually I have to do retinal blood vessels segmentation. I have used kmeans clustering for segmentation, but result is not satisfactory. Now I want to try out fuzzy c means clustering technique. However I am not able to find out how to use matlab built in function for this purpose. Please guide me about this. I have gone through the following page, but I am not able to understand how to apply all this to my image.
https://cn.mathworks.com/help/fuzzy/fcm.html
Thanks
A minimal working example:
% some sample rgb image
MyImage = imread('autumn.tif');
% display it
figure; imshow(MyImage)
% size of the image
sz = size(MyImage);
% reshape the image to column format (each color band into one column). I guess you
%also did this for the k-means. If not that's why you did get poor results.
ImageInColumnFormat = reshape(MyImage,[],sz(3));
% number of clusters you want
NumberOfClusters = 4;
% U shows how likely each pixel belongs to each cluster.
% double() is only necessary because the sample image is uint8 and fcm has trouble with that format. You may not have to do that.
[~,U] = fcm(double(ImageInColumnFormat),NumberOfClusters);
% Get for each pixel the most likely cluster
[~,Labels] = max(U,[],1);
% reshape it back into the image format
LabelsInImageFormat = reshape(Labels,sz(1),sz(2));
% show result
figure; imagesc(LabelsInImageFormat)

Error in removing the fisheye lens distortion of an image in Matlab [duplicate]

This question already has answers here:
correcting fisheye distortion programmatically
(7 answers)
Closed 7 years ago.
I have the following image:
I want to remove the fisheye lens distortion from this image, so I used the following code:
[X,map] = imread('Foam_Image.jpg'); % Read the indexed image
options = [size(X,1) size(X,2) 1]; % An array containing the columns, rows and exponent
tf = maketform('custom',2,2,[],... % Make the transformation structure
#fisheye_inverse,options);
newImage = imtransform(X,tf);
imshow(newImage); % show image
But I get the following error:
Error using imtransform>parse_inputs (line 438)
XData and YData could not be automatically determined. Try specifying XData and YData explicitly in the call to
IMTRANSFORM.
Error in imtransform (line 265)
args = parse_inputs(varargin{:});
I also used imwarp instead of imtransform, but I still get an error. Anyone has any idea why do I get this error and how to fix it?
As the message says, you need to manually specify the XData and YData properties during the call to imtransform with the Name-Property arguments syntax.
According to the docs, XData for example is:
A two-element, real vector that, when combined with 'YData', specifies
the spatial location of the output image B in the 2-D output space
X-Y. The two elements of 'XData' give the x-coordinates (horizontal)
of the first and last columns of B, respectively.
and likewise for YData. Therefore, you could modify your call to imtransform like so:
newImage = imtransform(X,tf,'XData',[1 col],'YData',[1 row]);
where col and row are the output of the size function you calculated earlier.
Hope that helps!

not enough input arguments fminsearch

I'm trying to write a script in MATLAB that graphs a function in three dimensions using the mesh function and then finds the maximum of the surface. This is my code so far:
%% Aquifer, 3D maximum search
figure(2)
[X,Y] = meshgrid(-10:.5:10,-10:.5:10);
h = #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y));
mesh(h(X,Y)) %graph aquifer surface
aquamax = fminsearch(h,[-5;-5])
When I run the code I get this error:
Error using #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y))
Not enough input arguments.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
I've read up on the fminsearch function but I'm not that familiar with it (still a bit of a noob at Matlab). Do I need to rework the code or is it just how I've input things into fminsearch?
Your h function requires 2 scalar inputs, but fminsearch only does one input, possibly a vector. Change h to h = #(x)-(1./(1+(x(1)-.25).^2+(x(2)-.5).^2+x(1)+x(1).*x(2))); and see if that works.

Image deblurring using cepstrum analysis in MATLAB

I'm trying to deblur an image I've blurred using gaussian filter, but using cepstrum analysis in MATLAB. So far I tried using "cceps" but there's a problem:
Error using -
Matrix dimensions must agree.
Error in cceps>rcunwrap (line 149)
y(:) = y(:)' - pi*nd*(0:(n-1))/nh;
Error in cceps (line 80)
[ah,nd] = rcunwrap(angle(h));
My image is 512x512 double. I only saw array usage in cepstrum analysis. I'm trying to "cceps(y)-cceps(h)" (y: blurred image, h: gaussian filter) then "icceps" to get the deblurred (original) image.
Is there any way to use cceps and icceps for images?
Also I tried applying the cceps algorithm;
Y=fftshift(fft2(y)); % Compute Fourier transform
CY=fftshift(ifft2(log(abs(Y)))); % Compute blurred image cepstrum
H=fftshift(fft2(h));
CH=fftshift(ifft2(log(abs(H)))); % Compute filter cepstrum
CX=CY-CH;
after this line I don't know how I can take the inverse cepstrum. There are some problems with unwrapping also, but I can't think of any way.

Convolution in frequency domain matlab

I just stumbled upon this question and am trying to know about its effects by practically testing it.
Consider a (nxn) Gaussian kernel. Choose the appropriate variance for
the same. Perform linear and circular convolution in the frequency
domain with this kernel on an image. Can you say anything about the
results?
I tried to implement the above in Matlab , with the following code.
clc;
close all;
clear all;
I = imread('my_face.jpg');
Irez = imresize(I,[512 512]); %resize image
figure(1);
imshow(Irez);
Irez = rgb2gray(Irez);
M = 2*size(I,1)+1;
N = 2*size(I,2)+1;
Ifreq = fft2(I,M,N);
gaus = fspecial('gaussian',5,0.7);
Igaus = conv2(gaus,abs(Ifreq));
Iface = real(ifft2(Igaus));
Iout = Iface(1:size(I,1),1:size(I,2));
figure(2)
imshow(Iout);
My questions are :
Am I on the right track? Am I doing what the problem is expecting me to? Or should I take or consider the fft of the gaussian kernel or have a similar Gaussian kernel in frequency domain? Please do tell me if you guys found the right way to achieve this.
Linear convolution's equivalent is multiplication. What is the equivalent of circular convolution?
Moreover, the above code is giving me the following error :
Undefined function or method 'conv2' for input arguments of type
'double' and attributes 'full 3d real'
It is obvious that both have to be double for input of conv2. Can you please help me to practically implement the problem?
I seems that you are providing right arguments in fspecial function. You have to specify the number of columns and rows in this function. I copied the below lines from matlab help:
h = fspecial('average', hsize) returns an averaging filter h of size hsize. The argument hsize can be a vector specifying the number of rows and columns in h, or it can be a scalar, in which case h is a square matrix. The default value for hsize is [3 3].
gaus = fspecial('gaussian', [M N],5,0.7);
You can find more about it here: http://www.mathworks.se/help/images/ref/fspecial.html